zkServer.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. 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. case $1 in
  96. start|startClean)
  97. if [ "x${base_dir}" == "x" ]
  98. then
  99. mkdir -p /tmp/zkdata
  100. java -cp "$CLASSPATH" org.apache.zookeeper.server.ZooKeeperServerMain $ZOOPORT /tmp/zkdata 3000 $ZKMAXCNXNS &> /tmp/zk.log &
  101. pid=$!
  102. echo -n $! > /tmp/zk.pid
  103. else
  104. mkdir -p "${base_dir}/build/tmp/zkdata"
  105. java -cp "$CLASSPATH" org.apache.zookeeper.server.ZooKeeperServerMain $ZOOPORT "${base_dir}/build/tmp/zkdata" 3000 $ZKMAXCNXNS &> "${base_dir}/build/tmp/zk.log" &
  106. pid=$!
  107. echo -n $pid > "${base_dir}/build/tmp/zk.pid"
  108. fi
  109. # wait max 120 seconds for server to be ready to server clients
  110. # this handles testing on slow hosts
  111. success=false
  112. for i in {1..120}
  113. do
  114. if ps -p $pid > /dev/null
  115. then
  116. java -cp "$CLASSPATH" org.apache.zookeeper.ZooKeeperMain -server localhost:$ZOOPORT ls / > /dev/null 2>&1
  117. if [ $? -ne 0 ]
  118. then
  119. # server not up yet - wait
  120. sleep 1
  121. else
  122. # server is up and serving client connections
  123. success=true
  124. break
  125. fi
  126. else
  127. # server died - exit now
  128. echo -n " ZooKeeper server process failed"
  129. break
  130. fi
  131. done
  132. if $success
  133. then
  134. ## in case for debug, but generally don't use as it messes up the
  135. ## console test output
  136. echo -n " ZooKeeper server started"
  137. else
  138. echo -n " ZooKeeper server NOT started"
  139. fi
  140. ;;
  141. startReadOnly)
  142. if [ "x${base_dir}" == "x" ]
  143. then
  144. echo "this target is for unit tests only"
  145. exit 2
  146. else
  147. tmpdir="${base_dir}/build/tmp"
  148. mkdir -p "${tmpdir}/zkdata"
  149. rm -f "${tmpdir}/zkdata/myid" && echo 1 > "${tmpdir}/zkdata/myid"
  150. sed "s#TMPDIR#${tmpdir}#g" ${base_dir}/zookeeper-client/zookeeper-client-c/tests/quorum.cfg > "${tmpdir}/quorum.cfg"
  151. # force read-only mode
  152. java -cp "$CLASSPATH" -Dreadonlymode.enabled=true org.apache.zookeeper.server.quorum.QuorumPeerMain ${tmpdir}/quorum.cfg &> "${tmpdir}/zk.log" &
  153. pid=$!
  154. echo -n $pid > "${base_dir}/build/tmp/zk.pid"
  155. sleep 3 # wait until read-only server is up
  156. fi
  157. ;;
  158. stop)
  159. # Already killed above
  160. ;;
  161. *)
  162. echo "Unknown command " + $1
  163. exit 2
  164. esac