zkServer.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/bin/bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. # This is the port where zookeeper server runs on.
  19. ZOOPORT=22181
  20. if [ "x$1" == "x" ]
  21. then
  22. echo "USAGE: $0 startClean|start|startReadOnly|stop hostPorts"
  23. exit 2
  24. fi
  25. case "`uname`" in
  26. CYGWIN*) cygwin=true ;;
  27. *) cygwin=false ;;
  28. esac
  29. if [ "x$1" == "xstartClean" ]
  30. then
  31. if [ "x${base_dir}" == "x" ]
  32. then
  33. rm -rf /tmp/zkdata
  34. else
  35. rm -rf "${base_dir}/build/tmp"
  36. fi
  37. fi
  38. if $cygwin
  39. then
  40. # cygwin has a "kill" in the shell itself, gets confused
  41. KILL=/bin/kill
  42. else
  43. KILL=kill
  44. fi
  45. # Make sure nothing is left over from before
  46. if [ -r "/tmp/zk.pid" ]
  47. then
  48. pid=`cat /tmp/zk.pid`
  49. $KILL -9 $pid
  50. rm -f /tmp/zk.pid
  51. fi
  52. if [ -r "${base_dir}/build/tmp/zk.pid" ]
  53. then
  54. pid=`cat "${base_dir}/build/tmp/zk.pid"`
  55. $KILL -9 $pid
  56. rm -f "${base_dir}/build/tmp/zk.pid"
  57. fi
  58. # [ZOOKEEPER-820] If lsof command is present, look for a process listening
  59. # on ZOOPORT and kill it.
  60. which lsof &> /dev/null
  61. if [ $? -eq 0 ]
  62. then
  63. pid=`lsof -i :$ZOOPORT | grep LISTEN | awk '{print $2}'`
  64. if [ -n "$pid" ]
  65. then
  66. $KILL -9 $pid
  67. fi
  68. fi
  69. if [ "x${base_dir}" == "x" ]
  70. then
  71. zk_base="../../../"
  72. else
  73. zk_base="${base_dir}"
  74. fi
  75. CLASSPATH="$CLASSPATH:${zk_base}/build/classes"
  76. CLASSPATH="$CLASSPATH:${zk_base}/conf"
  77. for i in "${zk_base}"/build/lib/*.jar
  78. do
  79. CLASSPATH="$CLASSPATH:$i"
  80. done
  81. for i in "${zk_base}"/zookeeper-server/src/main/resource/lib/*.jar
  82. do
  83. CLASSPATH="$CLASSPATH:$i"
  84. done
  85. CLASSPATH="$CLASSPATH:${CLOVER_HOME}/lib/clover.jar"
  86. if $cygwin
  87. then
  88. CLASSPATH=`cygpath -wp "$CLASSPATH"`
  89. fi
  90. case $1 in
  91. start|startClean)
  92. if [ "x${base_dir}" == "x" ]
  93. then
  94. mkdir -p /tmp/zkdata
  95. java -cp "$CLASSPATH" org.apache.zookeeper.server.ZooKeeperServerMain $ZOOPORT /tmp/zkdata 3000 $ZKMAXCNXNS &> /tmp/zk.log &
  96. pid=$!
  97. echo -n $! > /tmp/zk.pid
  98. else
  99. mkdir -p "${base_dir}/build/tmp/zkdata"
  100. java -cp "$CLASSPATH" org.apache.zookeeper.server.ZooKeeperServerMain $ZOOPORT "${base_dir}/build/tmp/zkdata" 3000 $ZKMAXCNXNS &> "${base_dir}/build/tmp/zk.log" &
  101. pid=$!
  102. echo -n $pid > "${base_dir}/build/tmp/zk.pid"
  103. fi
  104. # wait max 120 seconds for server to be ready to server clients
  105. # this handles testing on slow hosts
  106. success=false
  107. for i in {1..120}
  108. do
  109. if ps -p $pid > /dev/null
  110. then
  111. java -cp "$CLASSPATH" org.apache.zookeeper.ZooKeeperMain -server localhost:$ZOOPORT ls / > /dev/null 2>&1
  112. if [ $? -ne 0 ]
  113. then
  114. # server not up yet - wait
  115. sleep 1
  116. else
  117. # server is up and serving client connections
  118. success=true
  119. break
  120. fi
  121. else
  122. # server died - exit now
  123. echo -n " ZooKeeper server process failed"
  124. break
  125. fi
  126. done
  127. if $success
  128. then
  129. ## in case for debug, but generally don't use as it messes up the
  130. ## console test output
  131. echo -n " ZooKeeper server started"
  132. else
  133. echo -n " ZooKeeper server NOT started"
  134. fi
  135. ;;
  136. startReadOnly)
  137. if [ "x${base_dir}" == "x" ]
  138. then
  139. echo "this target is for unit tests only"
  140. exit 2
  141. else
  142. tmpdir="${base_dir}/build/tmp"
  143. mkdir -p "${tmpdir}/zkdata"
  144. rm -f "${tmpdir}/zkdata/myid" && echo 1 > "${tmpdir}/zkdata/myid"
  145. sed "s#TMPDIR#${tmpdir}#g" ${base_dir}/zookeeper-client/zookeeper-client-c/tests/quorum.cfg > "${tmpdir}/quorum.cfg"
  146. # force read-only mode
  147. java -cp "$CLASSPATH" -Dreadonlymode.enabled=true org.apache.zookeeper.server.quorum.QuorumPeerMain ${tmpdir}/quorum.cfg &> "${tmpdir}/zk.log" &
  148. pid=$!
  149. echo -n $pid > "${base_dir}/build/tmp/zk.pid"
  150. sleep 3 # wait until read-only server is up
  151. fi
  152. ;;
  153. stop)
  154. # Already killed above
  155. ;;
  156. *)
  157. echo "Unknown command " + $1
  158. exit 2
  159. esac