zkServer.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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=${ZOOPORT:-"22181"}
  20. # Some tests are setting the maxClientConnections. When it is not set, we fallback to default 100
  21. ZKMAXCNXNS=${ZKMAXCNXNS:-"100"}
  22. EXTRA_JVM_ARGS=${EXTRA_JVM_ARGS:-""}
  23. if [ "x$1" == "x" ]
  24. then
  25. echo "USAGE: $0 startClean|start|startCleanReadOnly|startRequireSASLAuth [jaasConf] [readOnly]|stop"
  26. exit 2
  27. fi
  28. # =====
  29. # ===== cleanup old executions
  30. # =====
  31. case "`uname`" in
  32. CYGWIN*) cygwin=true ;;
  33. *) cygwin=false ;;
  34. esac
  35. if $cygwin
  36. then
  37. # cygwin has a "kill" in the shell itself, gets confused
  38. KILL=/bin/kill
  39. else
  40. KILL=kill
  41. fi
  42. # Make sure nothing is left over from before
  43. if [ -r "/tmp/zk.pid" ]
  44. then
  45. pid=`cat /tmp/zk.pid`
  46. $KILL -9 $pid
  47. rm -f /tmp/zk.pid
  48. fi
  49. if [ -r "${base_dir}/build/tmp/zk.pid" ]
  50. then
  51. pid=`cat "${base_dir}/build/tmp/zk.pid"`
  52. $KILL -9 $pid
  53. rm -f "${base_dir}/build/tmp/zk.pid"
  54. fi
  55. # [ZOOKEEPER-820] If lsof command is present, look for a process listening
  56. # on ZOOPORT and kill it.
  57. which lsof &> /dev/null
  58. if [ $? -eq 0 ]
  59. then
  60. pid=`lsof -i :$ZOOPORT | grep LISTEN | awk '{print $2}'`
  61. if [ -n "$pid" ]
  62. then
  63. $KILL -9 $pid
  64. fi
  65. fi
  66. # =====
  67. # ===== build classpath
  68. # =====
  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. # =====
  96. # ===== initialize JVM arguments
  97. # =====
  98. read_only=
  99. PROPERTIES="$EXTRA_JVM_ARGS -Dzookeeper.extendedTypesEnabled=true -Dznode.container.checkIntervalMs=100"
  100. if [ "x$1" == "xstartRequireSASLAuth" ]
  101. then
  102. PROPERTIES="-Dzookeeper.sessionRequireClientSASLAuth=true $PROPERTIES"
  103. if [ "x$2" != "x" ]
  104. then
  105. PROPERTIES="$PROPERTIES -Dzookeeper.authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider"
  106. PROPERTIES="$PROPERTIES -Djava.security.auth.login.config=$2"
  107. fi
  108. if [ "x$3" != "x" ]
  109. then
  110. PROPERTIES="-Dreadonlymode.enabled=true $PROPERTIES"
  111. read_only=true
  112. fi
  113. fi
  114. if [ "x$1" == "xstartCleanReadOnly" ]
  115. then
  116. PROPERTIES="-Dreadonlymode.enabled=true $PROPERTIES"
  117. read_only=true
  118. fi
  119. # =====
  120. # ===== initialize data and test directories
  121. # =====
  122. if [ "x${base_dir}" == "x" ]
  123. then
  124. tmp_dir="/tmp"
  125. tests_dir="tests"
  126. else
  127. tmp_dir="${base_dir}/build/tmp"
  128. tests_dir=${base_dir}/zookeeper-client/zookeeper-client-c/tests
  129. fi
  130. # =====
  131. # ===== start the ZooKeeper server
  132. # =====
  133. case $1 in
  134. start|startClean|startRequireSASLAuth|startCleanReadOnly)
  135. if [ "x$1" == "xstartClean" ] || [ "x$1" == "xstartCleanReadOnly" ]
  136. then
  137. rm -rf "${tmp_dir}/zkdata"
  138. fi
  139. mkdir -p "${tmp_dir}/zkdata"
  140. # ===== initialize certificates
  141. certs_dir="/tmp/certs"
  142. rm -rf "${certs_dir}"
  143. mkdir -p "${certs_dir}"
  144. cp ${tests_dir}/../ssl/gencerts.sh "${certs_dir}/" > /dev/null
  145. cd ${certs_dir} > /dev/null
  146. ./gencerts.sh > ./gencerts.stdout 2> ./gencerts.stderr
  147. cd - > /dev/null
  148. # ===== prepare the configs
  149. sed "s#TMPDIR#${tmp_dir}#g;s#CERTDIR#${certs_dir}#g;s#MAXCLIENTCONNECTIONS#${ZKMAXCNXNS}#g;s#CLIENTPORT#${ZOOPORT}#g" ${tests_dir}/zoo.cfg > "${tmp_dir}/zoo.cfg"
  150. if [ "x$read_only" != "x" ]
  151. then
  152. # we can put the new server to read-only mode by starting only a single instance of a three node server
  153. echo "server.1=localhost:22881:33881" >> ${tmp_dir}/zoo.cfg
  154. echo "server.2=localhost:22882:33882" >> ${tmp_dir}/zoo.cfg
  155. echo "server.3=localhost:22883:33883" >> ${tmp_dir}/zoo.cfg
  156. echo "1" > ${tmp_dir}/zkdata/myid
  157. main_class="org.apache.zookeeper.server.quorum.QuorumPeerMain"
  158. else
  159. main_class="org.apache.zookeeper.server.ZooKeeperServerMain"
  160. fi
  161. # ===== start the server
  162. java -cp "$CLASSPATH" $PROPERTIES ${main_class} ${tmp_dir}/zoo.cfg &> "${tmp_dir}/zk.log" &
  163. pid=$!
  164. echo -n $! > /tmp/zk.pid
  165. # ===== wait for the server to start
  166. if [ "x$1" == "xstartRequireSASLAuth" ] || [ "x$1" == "xstartCleanReadOnly" ]
  167. then
  168. # ===== in these cases we can not connect simply with the java client, so we are just waiting...
  169. sleep 4
  170. success=true
  171. else
  172. # ===== wait max 120 seconds for server to be ready to server clients (this handles testing on slow hosts)
  173. success=false
  174. for i in {1..120}
  175. do
  176. if ps -p $pid > /dev/null
  177. then
  178. java -cp "$CLASSPATH" $PROPERTIES org.apache.zookeeper.ZooKeeperMain -server localhost:$ZOOPORT ls / > /dev/null 2>&1
  179. if [ $? -ne 0 ]
  180. then
  181. # server not up yet - wait
  182. sleep 1
  183. else
  184. # server is up and serving client connections
  185. success=true
  186. break
  187. fi
  188. else
  189. # server died - exit now
  190. echo -n " ZooKeeper server process failed"
  191. break
  192. fi
  193. done
  194. fi
  195. if $success
  196. then
  197. ## in case for debug, but generally don't use as it messes up the
  198. ## console test output
  199. echo -n " ZooKeeper server started"
  200. else
  201. echo -n " ZooKeeper server NOT started"
  202. fi
  203. ;;
  204. stop)
  205. # Already killed above
  206. ;;
  207. *)
  208. echo "Unknown command " + $1
  209. exit 2
  210. esac