zkServer.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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|startRequireSASLAuth|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. CLASSPATH="$CLASSPATH:${zk_base}/zookeeper-server/target/classes"
  78. for i in "${zk_base}"/build/lib/*.jar
  79. do
  80. CLASSPATH="$CLASSPATH:$i"
  81. done
  82. for d in "${zk_base}"/zookeeper-server/target/lib/*.jar
  83. do
  84. CLASSPATH="$d:$CLASSPATH"
  85. done
  86. for i in "${zk_base}"/zookeeper-server/src/main/resource/lib/*.jar
  87. do
  88. CLASSPATH="$CLASSPATH:$i"
  89. done
  90. CLASSPATH="$CLASSPATH:${CLOVER_HOME}/lib/clover*.jar"
  91. if $cygwin
  92. then
  93. CLASSPATH=`cygpath -wp "$CLASSPATH"`
  94. fi
  95. PROPERTIES="-Dzookeeper.extendedTypesEnabled=true -Dznode.container.checkIntervalMs=100"
  96. case $1 in
  97. start|startClean)
  98. if [ "x${base_dir}" == "x" ]
  99. then
  100. mkdir -p /tmp/zkdata
  101. java -cp "$CLASSPATH" $PROPERTIES org.apache.zookeeper.server.ZooKeeperServerMain $ZOOPORT /tmp/zkdata 3000 $ZKMAXCNXNS &> /tmp/zk.log &
  102. pid=$!
  103. echo -n $! > /tmp/zk.pid
  104. else
  105. mkdir -p "${base_dir}/build/tmp/zkdata"
  106. java -cp "$CLASSPATH" $PROPERTIES org.apache.zookeeper.server.ZooKeeperServerMain $ZOOPORT "${base_dir}/build/tmp/zkdata" 3000 $ZKMAXCNXNS &> "${base_dir}/build/tmp/zk.log" &
  107. pid=$!
  108. echo -n $pid > "${base_dir}/build/tmp/zk.pid"
  109. fi
  110. # wait max 120 seconds for server to be ready to server clients
  111. # this handles testing on slow hosts
  112. success=false
  113. for i in {1..120}
  114. do
  115. if ps -p $pid > /dev/null
  116. then
  117. java -cp "$CLASSPATH" $PROPERTIES org.apache.zookeeper.ZooKeeperMain -server localhost:$ZOOPORT ls / > /dev/null 2>&1
  118. if [ $? -ne 0 ]
  119. then
  120. # server not up yet - wait
  121. sleep 1
  122. else
  123. # server is up and serving client connections
  124. success=true
  125. break
  126. fi
  127. else
  128. # server died - exit now
  129. echo -n " ZooKeeper server process failed"
  130. break
  131. fi
  132. done
  133. if $success
  134. then
  135. ## in case for debug, but generally don't use as it messes up the
  136. ## console test output
  137. echo -n " ZooKeeper server started"
  138. else
  139. echo -n " ZooKeeper server NOT started"
  140. fi
  141. ;;
  142. startReadOnly)
  143. if [ "x${base_dir}" == "x" ]
  144. then
  145. echo "this target is for unit tests only"
  146. exit 2
  147. else
  148. tmpdir="${base_dir}/build/tmp"
  149. mkdir -p "${tmpdir}/zkdata"
  150. rm -f "${tmpdir}/zkdata/myid" && echo 1 > "${tmpdir}/zkdata/myid"
  151. sed "s#TMPDIR#${tmpdir}#g" ${base_dir}/zookeeper-client/zookeeper-client-c/tests/quorum.cfg > "${tmpdir}/quorum.cfg"
  152. # force read-only mode
  153. PROPERTIES="$PROPERTIES -Dreadonlymode.enabled=true"
  154. java -cp "$CLASSPATH" $PROPERTIES org.apache.zookeeper.server.quorum.QuorumPeerMain ${tmpdir}/quorum.cfg &> "${tmpdir}/zk.log" &
  155. pid=$!
  156. echo -n $pid > "${base_dir}/build/tmp/zk.pid"
  157. sleep 3 # wait until read-only server is up
  158. fi
  159. ;;
  160. startRequireSASLAuth)
  161. if [ "x${base_dir}" == "x" ]
  162. then
  163. echo "this target is for unit tests only"
  164. exit 2
  165. else
  166. mkdir -p "${base_dir}/build/tmp/zkdata"
  167. java -cp "$CLASSPATH" -Dzookeeper.sessionRequireClientSASLAuth=true org.apache.zookeeper.server.ZooKeeperServerMain 23456 "${base_dir}/build/tmp/zkdata" 3000 $ZKMAXCNXNS &> "${base_dir}/build/tmp/zk.log" &
  168. pid=$!
  169. echo -n $pid > "${base_dir}/build/tmp/zk.pid"
  170. sleep 3 # wait until server is up.
  171. fi
  172. ;;
  173. stop)
  174. # Already killed above
  175. ;;
  176. *)
  177. echo "Unknown command " + $1
  178. exit 2
  179. esac