zkServer.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #! /usr/bin/env 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. # https://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing,
  14. # software distributed under the License is distributed on an
  15. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. # KIND, either express or implied. See the License for the
  17. # specific language governing permissions and limitations
  18. # under the License.
  19. #
  20. # This is the port where zookeeper server runs on.
  21. ZOOPORT=${ZOOPORT:-"22181"}
  22. # Some tests are setting the maxClientConnections. When it is not set, we fallback to default 100
  23. ZKMAXCNXNS=${ZKMAXCNXNS:-"100"}
  24. EXTRA_JVM_ARGS=${EXTRA_JVM_ARGS:-""}
  25. if [[ -z $1 ]]; then
  26. echo "USAGE: $0 startClean|start|startCleanReadOnly|startRequireSASLAuth [jaasConf] [readOnly]|stop"
  27. exit 2
  28. fi
  29. # =====
  30. # ===== cleanup old executions
  31. # =====
  32. case "$(uname)" in
  33. CYGWIN* | MINGW*) cygwin=true ;;
  34. *) cygwin=false ;;
  35. esac
  36. if $cygwin; 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" ]]; then
  44. pid=$(cat /tmp/zk.pid)
  45. $KILL -9 "$pid"
  46. rm -f /tmp/zk.pid
  47. fi
  48. if [[ -r "$base_dir/build/tmp/zk.pid" ]]; then
  49. pid=$(cat "$base_dir/build/tmp/zk.pid")
  50. $KILL -9 "$pid"
  51. rm -f "$base_dir/build/tmp/zk.pid"
  52. fi
  53. # [ZOOKEEPER-820] If lsof command is present, look for a process listening
  54. # on ZOOPORT and kill it.
  55. if which lsof &>/dev/null; then
  56. pid=$(lsof -i ":$ZOOPORT" | grep LISTEN | awk '{print $2}')
  57. if [[ -n $pid ]]; then
  58. $KILL -9 "$pid"
  59. fi
  60. fi
  61. # =====
  62. # ===== build classpath
  63. # =====
  64. if [[ -z $base_dir ]]; then
  65. zk_base="../../../"
  66. else
  67. zk_base="$base_dir"
  68. fi
  69. CLASSPATH="$CLASSPATH:$zk_base/build/classes"
  70. CLASSPATH="$CLASSPATH:$zk_base/conf"
  71. CLASSPATH="$CLASSPATH:$zk_base/zookeeper-server/target/classes"
  72. for i in "$zk_base"/build/lib/*.jar; do
  73. CLASSPATH="$CLASSPATH:$i"
  74. done
  75. for d in "$zk_base"/zookeeper-server/target/lib/*.jar; do
  76. CLASSPATH="$d:$CLASSPATH"
  77. done
  78. for i in "$zk_base"/zookeeper-server/src/main/resource/lib/*.jar; do
  79. CLASSPATH="$CLASSPATH:$i"
  80. done
  81. CLASSPATH="$CLASSPATH:$CLOVER_HOME/lib/clover*.jar"
  82. if $cygwin; then
  83. CLASSPATH=$(cygpath -wp "$CLASSPATH")
  84. fi
  85. export CLASSPATH
  86. # =====
  87. # ===== initialize JVM arguments
  88. # =====
  89. read_only=
  90. # shellcheck disable=SC2206
  91. PROPERTIES=($EXTRA_JVM_ARGS "-Dzookeeper.extendedTypesEnabled=true" "-Dznode.container.checkIntervalMs=100")
  92. if [[ $1 == "xstartRequireSASLAuth" ]]; then
  93. PROPERTIES=("-Dzookeeper.sessionRequireClientSASLAuth=true" "${PROPERTIES[@]}"
  94. "-Dzookeeper.authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider")
  95. if [[ -n $2 ]]; then
  96. PROPERTIES=("${PROPERTIES[@]}" "-Djava.security.auth.login.config=$2")
  97. fi
  98. if [[ -n $3 ]]; then
  99. PROPERTIES=("-Dreadonlymode.enabled=true" "${PROPERTIES[@]}")
  100. read_only=true
  101. fi
  102. fi
  103. if [[ $1 == "startCleanReadOnly" ]]; then
  104. PROPERTIES=("-Dreadonlymode.enabled=true" "${PROPERTIES[@]}")
  105. read_only=true
  106. fi
  107. # =====
  108. # ===== initialize data and test directories
  109. # =====
  110. if [[ -z $base_dir ]]; then
  111. tmp_dir="/tmp"
  112. tests_dir="tests"
  113. else
  114. tmp_dir="$base_dir/build/tmp"
  115. tests_dir=$base_dir/zookeeper-client/zookeeper-client-c/tests
  116. fi
  117. # =====
  118. # ===== start the ZooKeeper server
  119. # =====
  120. case $1 in
  121. start | startClean | startRequireSASLAuth | startCleanReadOnly)
  122. if [[ $1 == "startClean" ]] || [[ $1 == "startCleanReadOnly" ]]; then
  123. rm -rf "$tmp_dir/zkdata"
  124. fi
  125. mkdir -p "$tmp_dir/zkdata"
  126. # ===== initialize certificates
  127. certs_dir="/tmp/certs"
  128. rm -rf "$certs_dir"
  129. mkdir -p "$certs_dir"
  130. cp "$tests_dir"/../ssl/gencerts.sh "$certs_dir/" >/dev/null
  131. (
  132. cd "$certs_dir" >/dev/null &&
  133. # GitHub is providing us hostnames with more than 64 characters now.
  134. # And there are no cppunit tests do hostname verification currently,
  135. # so we could set CN to arbitrary hostname for now.
  136. ./gencerts.sh tests.zookeeper.apache.org >./gencerts.stdout 2>./gencerts.stderr
  137. )
  138. # ===== prepare the configs
  139. 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"
  140. if [[ -n $read_only ]]; then
  141. # we can put the new server to read-only mode by starting only a single instance of a three node server
  142. {
  143. echo "server.1=localhost:22881:33881"
  144. echo "server.2=localhost:22882:33882"
  145. echo "server.3=localhost:22883:33883"
  146. } >>"$tmp_dir/zoo.cfg"
  147. echo "1" >"$tmp_dir/zkdata/myid"
  148. main_class="org.apache.zookeeper.server.quorum.QuorumPeerMain"
  149. else
  150. main_class="org.apache.zookeeper.server.ZooKeeperServerMain"
  151. fi
  152. # ===== start the server
  153. java "${PROPERTIES[@]}" "$main_class" "$tmp_dir/zoo.cfg" &>"$tmp_dir/zk.log" &
  154. pid=$!
  155. echo -n $! >/tmp/zk.pid
  156. # ===== wait for the server to start
  157. if [[ $1 == "startRequireSASLAuth" ]] || [[ $1 == "startCleanReadOnly" ]]; then
  158. # ===== in these cases we can not connect simply with the java client, so we are just waiting...
  159. sleep 4
  160. success=true
  161. else
  162. # ===== wait max 120 seconds for server to be ready to server clients (this handles testing on slow hosts)
  163. success=false
  164. for i in {1..120}; do
  165. if ps -p $pid >/dev/null; then
  166. if ! java "${PROPERTIES[@]}" org.apache.zookeeper.ZooKeeperMain -server "localhost:$ZOOPORT" ls / &>/dev/null; then
  167. # server not up yet - wait
  168. sleep 1
  169. else
  170. # server is up and serving client connections
  171. success=true
  172. break
  173. fi
  174. else
  175. # server died - exit now
  176. echo -n " ZooKeeper server process failed"
  177. break
  178. fi
  179. done
  180. fi
  181. if $success; then
  182. ## in case for debug, but generally don't use as it messes up the
  183. ## console test output
  184. echo -n " ZooKeeper server started"
  185. else
  186. echo -n " ZooKeeper server NOT started"
  187. fi
  188. ;;
  189. stop)
  190. # Already killed above
  191. ;;
  192. *)
  193. echo "Unknown command $1"
  194. exit 2
  195. ;;
  196. esac