2011-02-02

Tokyo Tyrant ttexpire.lua

  1. function expire()  
  2.    local args = {}  
  3.    local cdate = string.format("%d", _time())  
  4.    table.insert(args, "addcond\0x\0NUMLE\0" .. cdate)  
  5.    table.insert(args, "out")  
  6.    local res = _misc("search", args)  
  7.    if not res then  
  8.       _log("expiration was failed", 2)  
  9.    end  
  10.    print("rnum=" .. _rnum() .. "  size=" .. _size())  
  11. end  

Tokyo Tyrant 起動スクリプト スレーブ

  1. #! /bin/sh  
  2. # chkconfig: 345 65 55  
  3. # description: Startup script for the server of Tokyo Tyrant For Session  
  4. # processname: tokyotyrant  
  5.   
  6. #----------------------------------------------------------------  
  7. # Startup script for the server of Tokyo Tyrant  
  8. #----------------------------------------------------------------  
  9.   
  10.   
  11. # configuration variables  
  12. prog="ttservctl"  
  13. cmd="ttserver"  
  14. basedir="/home/ttserver/data"  
  15. port="1978"  
  16. pidfile="$basedir/ttserver.pid"  
  17. logfile="$basedir/ttserver.log"  
  18. ulogdir="$basedir/ulog"  
  19. ulimsiz="256m"  
  20. sid=2  
  21. mhost="remotehost1"  
  22. mport="1978"  
  23. rtsfile="$basedir/ttserver.rts"  
  24. dbname="$basedir/session.tct#bnum=1000000#idx=x:dec#dfunit=8"  
  25. ext="/home/ttserver/ext/ttexpire.lua"  
  26. extpc="expire 1"  
  27. maxcon="65535"  
  28. retval=0  
  29.   
  30.   
  31. # setting environment variables  
  32. LANG=C  
  33. LC_ALL=C  
  34. PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin:/usr/local/tokyotyrant/bin"  
  35. export LANG LC_ALL PATH  
  36.   
  37.   
  38. # start the server  
  39. start(){  
  40.   printf 'Starting the server of Tokyo Tyrant\n'  
  41.   mkdir -p "$basedir"  
  42.   if [ -z "$basedir" ] || [ -z "$port" ] || [ -z "$pidfile" ] || [ -z "$dbname" ] ; then  
  43.     printf 'Invalid configuration\n'  
  44.     retval=1  
  45.   elif ! [ -d "$basedir" ] ; then  
  46.     printf 'No such directory: %s\n' "$basedir"  
  47.     retval=1  
  48.   elif [ -f "$pidfile" ] ; then  
  49.     pid=`cat "$pidfile"`  
  50.     printf 'Existing process: %d\n' "$pid"  
  51.     retval=1  
  52.   else  
  53.     if [ -n "$maxcon" ] ; then  
  54.       ulimit -n "$maxcon" >/dev/null 2>&1  
  55.     fi  
  56.     cmd="$cmd -port $port -dmn -pid $pidfile"  
  57.     if [ -n "$logfile" ] ; then  
  58.       cmd="$cmd -log $logfile"  
  59.     fi  
  60.     if [ -n "$ulogdir" ] ; then  
  61.       mkdir -p "$ulogdir"  
  62.       cmd="$cmd -ulog $ulogdir"  
  63.     fi  
  64.     if [ -n "$ulimsiz" ] ; then  
  65.       cmd="$cmd -ulim $ulimsiz"  
  66.     fi  
  67.     if [ -n "$sid" ] ; then  
  68.       cmd="$cmd -sid $sid"  
  69.     fi  
  70.     if [ -n "$mhost" ] ; then  
  71.       cmd="$cmd -mhost $mhost"  
  72.     fi  
  73.     if [ -n "$mport" ] ; then  
  74.       cmd="$cmd -mport $mport"  
  75.     fi  
  76.     if [ -n "$rtsfile" ] ; then  
  77.       cmd="$cmd -rts $rtsfile"  
  78.     fi  
  79.     if [ -n "$ext" ] ; then  
  80.       cmd="$cmd -ext $ext"  
  81.     fi  
  82.     if [ -n "$extpc" ] ; then  
  83.       cmd="$cmd -extpc $extpc"  
  84.     fi  
  85.     printf "Executing: %s\n" "$cmd"  
  86.     cmd="$cmd $dbname"  
  87.     $cmd  
  88.     if [ "$?" -eq 0 ] ; then  
  89.       printf 'Done\n'  
  90.     else  
  91.       printf 'The server could not started\n'  
  92.       retval=1  
  93.     fi  
  94.   fi  
  95. }  
  96.   
  97.   
  98. # stop the server  
  99. stop(){  
  100.   printf 'Stopping the server of Tokyo Tyrant\n'  
  101.   if [ -f "$pidfile" ] ; then  
  102.     pid=`cat "$pidfile"`  
  103.     printf "Sending the terminal signal to the process: %s\n" "$pid"  
  104.     kill -TERM "$pid"  
  105.     c=0  
  106.     while true ; do  
  107.       sleep 0.1  
  108.       if [ -f "$pidfile" ] ; then  
  109.         c=`expr $c + 1`  
  110.         if [ "$c" -ge 100 ] ; then  
  111.           printf 'Hanging process: %d\n' "$pid"  
  112.           retval=1  
  113.           break  
  114.         fi  
  115.       else  
  116.         printf 'Done\n'  
  117.         break  
  118.       fi  
  119.     done  
  120.   else  
  121.     printf 'No process found\n'  
  122.     retval=1  
  123.   fi  
  124. }  
  125.   
  126.   
  127. # send HUP to the server for log rotation  
  128. hup(){  
  129.   printf 'Sending HUP signal to the server of Tokyo Tyrant\n'  
  130.   if [ -f "$pidfile" ] ; then  
  131.     pid=`cat "$pidfile"`  
  132.     printf "Sending the hangup signal to the process: %s\n" "$pid"  
  133.     kill -HUP "$pid"  
  134.     printf 'Done\n'  
  135.   else  
  136.     printf 'No process found\n'  
  137.     retval=1  
  138.   fi  
  139. }  
  140.   
  141.   
  142. # check permission  
  143. if [ -d "$basedir" ] && ! touch "$basedir/$$" >/dev/null 2>&1  
  144. then  
  145.   printf 'Permission denied\n'  
  146.   exit 1  
  147. fi  
  148. rm -f "$basedir/$$"  
  149.   
  150.   
  151. # dispatch the command  
  152. case "$1" in  
  153. start)  
  154.   start  
  155.   ;;  
  156. stop)  
  157.   stop  
  158.   ;;  
  159. restart)  
  160.   stop  
  161.   start  
  162.   ;;  
  163. hup)  
  164.   hup  
  165.   ;;  
  166. *)  
  167.   printf 'Usage: %s {start|stop|restart|hup}\n' "$prog"  
  168.   exit 1  
  169.   ;;  
  170. esac  
  171.   
  172.   
  173. # exit  
  174. exit "$retval"  
  175.   
  176.   
  177.   
  178. # END OF FILE  

Tokyo Tyrant 起動スクリプト マスタ

  1. #! /bin/sh  
  2. # chkconfig: 345 65 55  
  3. # description: Startup script for the server of Tokyo Tyrant For Session  
  4. # processname: tokyotyrant  
  5.   
  6. #----------------------------------------------------------------  
  7. # Startup script for the server of Tokyo Tyrant  
  8. #----------------------------------------------------------------  
  9.   
  10.   
  11. # configuration variables  
  12. prog="ttservctl"  
  13. cmd="ttserver"  
  14. basedir="/home/ttserver/data"  
  15. port="1978"  
  16. pidfile="$basedir/ttserver.pid"  
  17. logfile="$basedir/ttserver.log"  
  18. ulogdir="$basedir/ulog"  
  19. ulimsiz="256m"  
  20. sid=1  
  21. mhost="remotehost2"  
  22. mport="1978"  
  23. rtsfile="$basedir/ttserver.rts"  
  24. dbname="$basedir/session.tct#bnum=1000000#idx=x:dec#dfunit=8"  
  25. ext="/home/ttserver/ext/ttexpire.lua"  
  26. extpc="expire 1"  
  27. maxcon="65535"  
  28. retval=0  
  29.   
  30.   
  31. # setting environment variables  
  32. LANG=C  
  33. LC_ALL=C  
  34. PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin:/usr/local/tokyotyrant/bin"  
  35. export LANG LC_ALL PATH  
  36.   
  37.   
  38. # start the server  
  39. start(){  
  40.   printf 'Starting the server of Tokyo Tyrant\n'  
  41.   mkdir -p "$basedir"  
  42.   if [ -z "$basedir" ] || [ -z "$port" ] || [ -z "$pidfile" ] || [ -z "$dbname" ] ; then  
  43.     printf 'Invalid configuration\n'  
  44.     retval=1  
  45.   elif ! [ -d "$basedir" ] ; then  
  46.     printf 'No such directory: %s\n' "$basedir"  
  47.     retval=1  
  48.   elif [ -f "$pidfile" ] ; then  
  49.     pid=`cat "$pidfile"`  
  50.     printf 'Existing process: %d\n' "$pid"  
  51.     retval=1  
  52.   else  
  53.     if [ -n "$maxcon" ] ; then  
  54.       ulimit -n "$maxcon" >/dev/null 2>&1  
  55.     fi  
  56.     cmd="$cmd -port $port -dmn -pid $pidfile"  
  57.     if [ -n "$logfile" ] ; then  
  58.       cmd="$cmd -log $logfile"  
  59.     fi  
  60.     if [ -n "$ulogdir" ] ; then  
  61.       mkdir -p "$ulogdir"  
  62.       cmd="$cmd -ulog $ulogdir"  
  63.     fi  
  64.     if [ -n "$ulimsiz" ] ; then  
  65.       cmd="$cmd -ulim $ulimsiz"  
  66.     fi  
  67.     if [ -n "$sid" ] ; then  
  68.       cmd="$cmd -sid $sid"  
  69.     fi  
  70.     if [ -n "$mhost" ] ; then  
  71.       cmd="$cmd -mhost $mhost"  
  72.     fi  
  73.     if [ -n "$mport" ] ; then  
  74.       cmd="$cmd -mport $mport"  
  75.     fi  
  76.     if [ -n "$rtsfile" ] ; then  
  77.       cmd="$cmd -rts $rtsfile"  
  78.     fi  
  79.     if [ -n "$ext" ] ; then  
  80.       cmd="$cmd -ext $ext"  
  81.     fi  
  82.     if [ -n "$extpc" ] ; then  
  83.       cmd="$cmd -extpc $extpc"  
  84.     fi  
  85.     printf "Executing: %s\n" "$cmd"  
  86.     cmd="$cmd $dbname"  
  87.     $cmd  
  88.     if [ "$?" -eq 0 ] ; then  
  89.       printf 'Done\n'  
  90.     else  
  91.       printf 'The server could not started\n'  
  92.       retval=1  
  93.     fi  
  94.   fi  
  95. }  
  96.   
  97.   
  98. # stop the server  
  99. stop(){  
  100.   printf 'Stopping the server of Tokyo Tyrant\n'  
  101.   if [ -f "$pidfile" ] ; then  
  102.     pid=`cat "$pidfile"`  
  103.     printf "Sending the terminal signal to the process: %s\n" "$pid"  
  104.     kill -TERM "$pid"  
  105.     c=0  
  106.     while true ; do  
  107.       sleep 0.1  
  108.       if [ -f "$pidfile" ] ; then  
  109.         c=`expr $c + 1`  
  110.         if [ "$c" -ge 100 ] ; then  
  111.           printf 'Hanging process: %d\n' "$pid"  
  112.           retval=1  
  113.           break  
  114.         fi  
  115.       else  
  116.         printf 'Done\n'  
  117.         break  
  118.       fi  
  119.     done  
  120.   else  
  121.     printf 'No process found\n'  
  122.     retval=1  
  123.   fi  
  124. }  
  125.   
  126.   
  127. # send HUP to the server for log rotation  
  128. hup(){  
  129.   printf 'Sending HUP signal to the server of Tokyo Tyrant\n'  
  130.   if [ -f "$pidfile" ] ; then  
  131.     pid=`cat "$pidfile"`  
  132.     printf "Sending the hangup signal to the process: %s\n" "$pid"  
  133.     kill -HUP "$pid"  
  134.     printf 'Done\n'  
  135.   else  
  136.     printf 'No process found\n'  
  137.     retval=1  
  138.   fi  
  139. }  
  140.   
  141.   
  142. # check permission  
  143. if [ -d "$basedir" ] && ! touch "$basedir/$$" >/dev/null 2>&1  
  144. then  
  145.   printf 'Permission denied\n'  
  146.   exit 1  
  147. fi  
  148. rm -f "$basedir/$$"  
  149.   
  150.   
  151. # dispatch the command  
  152. case "$1" in  
  153. start)  
  154.   start  
  155.   ;;  
  156. stop)  
  157.   stop  
  158.   ;;  
  159. restart)  
  160.   stop  
  161.   start  
  162.   ;;  
  163. hup)  
  164.   hup  
  165.   ;;  
  166. *)  
  167.   printf 'Usage: %s {start|stop|restart|hup}\n' "$prog"  
  168.   exit 1  
  169.   ;;  
  170. esac  
  171.   
  172.   
  173. # exit  
  174. exit "$retval"  
  175.   
  176.   
  177.   
  178. # END OF FILE  

Tokyo Tyrant インストール

ソースコード取得。
  1. # cd /usr/local/src  
  2. # wget http://1978th.net/tokyocabinet/tokyocabinet-1.4.9.tar.gz  
  3. # wget http://1978th.net/tokyotyrant/tokyotyrant-1.1.40.tar.gz  
TokyoCabinetインストール。
  1. # tar zxvf ./tokyocabinet-1.4.9.tar.gz  
  2. # cd tokyocabinet-1.4.9  
  3. # ./configure  
  4. # make  
  5. # make install  
TokyoTyrantインストール。
  1. # tar zxvf tokyotyrant-1.1.40.tar.gz  
  2. # cd tokyotyrant-1.1.40  
  3. # ./configure  
  4. # make  
  5. # make install  
Tokyo Tyrantの起動スクリプトに以下を追加。
  1. # vi /usr/local/tokyotyrant/sbin/ttservctl  
  2.   
  3. # chkconfig: 345 65 55  
  4. # description: Startup script for the server of Tokyo Tyrant  
  5. # processname: tokyotyrant  
/etc/init.dにリンクを作ってchkconfigに登録&確認。
  1. # ln -s /usr/local/tokyotyrant/sbin/ttservctl /etc/init.d/  
  2. # chkconfig --add ttservctl  
  3. # chkconfig --list ttservctl  
  4. ttservctl       0:off   1:off   2:off   3:on    4:on    5:on    6:off  

logrotateに登録。
/home/ttserver/data/ttserver.logにTokyo Tyrantのログが書き出されますが、放置しておくと肥大化します。
そのため、ログをローテーションする仕組みに任せてます。
Tokyo Tyrantの起動停止スクリプトがHUPをフォローしてくれているので、これだけでOK。
ここまでやっておけば、永続化機能のついた、OSごと再起動が掛かっても勝手に復活するようになります。
  1. # vi /etc/logrotate.d/tokyotyrant  
  2.     
  3. /home/ttserver/data/ttserver.log {  
  4.     monthly  
  5.     missingok  
  6.     rotate 12  
  7.     compress  
  8.     ifempty  
  9.     create  
  10.     sharedscripts  
  11.     postrotate  
  12.         /sbin/service ttservctl hup > /dev/null 2>/dev/null || true  
  13.     endscript  
  14. }  
参考サイト