|
| 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 | +# 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 | +# |
| 19 | +# Derived from work Copyright 2017 The Kubernetes Authors. |
| 20 | +# See https://github.com/kow3ns/kubernetes-zookeeper/blob/master/docker/scripts/start-zookeeper for more details |
| 21 | +# and then https://github.com/cloudurable/kube-zookeeper-statefulsets/ |
| 22 | +# In the below we change the '--heap' argument to '--percentage' so |
| 23 | +# could set the server heap as a percentage of the container resource |
| 24 | +# limit rather than hard-code it. |
| 25 | +# Currently zookeeper.root.logger is CONSOLE only. We do not write |
| 26 | +# logs to files. Fix config. if you need it. |
| 27 | +# |
| 28 | +# |
| 29 | +# Usage: start-zookeeper [OPTIONS] |
| 30 | +# Starts a ZooKeeper server based on the supplied options. |
| 31 | +# --servers The number of servers in the ensemble. The default |
| 32 | +# value is 1. |
| 33 | +# --data_dir The directory where the ZooKeeper process will store its |
| 34 | +# snapshots. The default is /var/lib/zookeeper/data. |
| 35 | +# --data_log_dir The directory where the ZooKeeper process will store its |
| 36 | +# write ahead log. The default is |
| 37 | +# /var/lib/zookeeper/data/log. |
| 38 | +# --conf_dir The directory where the ZooKeeper process will store its |
| 39 | +# configuration. The default is /opt/zookeeper/conf. |
| 40 | +# --client_port The port on which the ZooKeeper process will listen for |
| 41 | +# client requests. The default is 2181. |
| 42 | + |
| 43 | +# --election_port The port on which the ZooKeeper process will perform |
| 44 | +# leader election. The default is 3888. |
| 45 | + |
| 46 | +# --server_port The port on which the ZooKeeper process will listen for |
| 47 | +# requests from other servers in the ensemble. The |
| 48 | +# default is 2888. |
| 49 | + |
| 50 | +# --tick_time The length of a ZooKeeper tick in ms. The default is |
| 51 | +# 2000. |
| 52 | + |
| 53 | +# --init_limit The number of Ticks that an ensemble member is allowed |
| 54 | +# to perform leader election. The default is 10. |
| 55 | + |
| 56 | +# --sync_limit The maximum session timeout that the ensemble will |
| 57 | +# allows a client to request. The default is 5. |
| 58 | + |
| 59 | +# --percentage The percentage of container memory to give to the JVM. |
| 60 | + |
| 61 | +# --max_client_cnxns The maximum number of client connections that the |
| 62 | +# ZooKeeper process will accept simultaneously. The |
| 63 | +# default is 60. |
| 64 | + |
| 65 | +# --snap_retain_count The maximum number of snapshots the ZooKeeper process |
| 66 | +# will retain if purge_interval is greater than 0. The |
| 67 | +# default is 3. |
| 68 | + |
| 69 | +# --purge_interval The number of hours the ZooKeeper process will wait |
| 70 | +# between purging its old snapshots. If set to 0 old |
| 71 | +# snapshots will never be purged. The default is 0. |
| 72 | + |
| 73 | +# --max_session_timeout The maximum time in milliseconds for a client session |
| 74 | +# timeout. The default value is 2 * tick time. |
| 75 | + |
| 76 | +# --min_session_timeout The minimum time in milliseconds for a client session |
| 77 | +# timeout. The default value is 20 * tick time. |
| 78 | + |
| 79 | +# --log_level The log level for the zookeeeper server. Either FATAL, |
| 80 | +# ERROR, WARN, INFO, DEBUG. The default is INFO. |
| 81 | + |
| 82 | +# --quorum_listen_on_all_ips |
| 83 | +# When set to true the ZooKeeper server will listen for |
| 84 | +# connections from its peers on all available IP addresses, |
| 85 | +# and not only the address configured in the server list of |
| 86 | +# the configuration file. It affects the connections handling |
| 87 | +# the ZAB protocol and the Fast Leader Election protocol. |
| 88 | +# Default value is false. |
| 89 | +set -x |
| 90 | + |
| 91 | +ZOOKEEPER_HOME="$( ls -d /apache-zookeeper* )" |
| 92 | +USER="$(whoami)" |
| 93 | +HOST="$(hostname -s)" |
| 94 | +DOMAIN="$(hostname -d)" |
| 95 | +LOG_LEVEL=INFO |
| 96 | +DATA_DIR="/var/lib/zookeeper/data" |
| 97 | +DATA_LOG_DIR="/var/lib/zookeeper/log" |
| 98 | +LOG_DIR="/var/log/zookeeper" |
| 99 | +CONF_DIR="/opt/zookeeper/conf" |
| 100 | +CLIENT_PORT=2181 |
| 101 | +SERVER_PORT=2888 |
| 102 | +ELECTION_PORT=3888 |
| 103 | +PROM_PORT=7001 |
| 104 | +TICK_TIME=2000 |
| 105 | +INIT_LIMIT=10 |
| 106 | +SYNC_LIMIT=5 |
| 107 | +JVM_HEAP_PERCENTAGE_OF_RESOURCE_LIMIT=50 |
| 108 | +MAX_CLIENT_CNXNS=1000 |
| 109 | +SNAP_RETAIN_COUNT=3 |
| 110 | +PURGE_INTERVAL=0 |
| 111 | +SERVERS=1 |
| 112 | +QUORUM_LISTEN_ON_ALL_IPS=false |
| 113 | + |
| 114 | +function print_usage() { |
| 115 | +echo "\ |
| 116 | +Usage: start-zookeeper [OPTIONS] |
| 117 | +Starts a ZooKeeper server based on the supplied options. |
| 118 | + --servers The number of servers in the ensemble. The default |
| 119 | + value is 1. |
| 120 | +
|
| 121 | + --data_dir The directory where the ZooKeeper process will store its |
| 122 | + snapshots. The default is /var/lib/zookeeper/data. |
| 123 | +
|
| 124 | + --data_log_dir The directory where the ZooKeeper process will store its |
| 125 | + write ahead log. The default is |
| 126 | + /var/lib/zookeeper/data/log. |
| 127 | +
|
| 128 | + --conf_dir The directoyr where the ZooKeeper process will store its |
| 129 | + configuration. The default is /opt/zookeeper/conf. |
| 130 | +
|
| 131 | + --client_port The port on which the ZooKeeper process will listen for |
| 132 | + client requests. The default is 2181. |
| 133 | +
|
| 134 | + --election_port The port on which the ZooKeeper process will perform |
| 135 | + leader election. The default is 3888. |
| 136 | +
|
| 137 | + --server_port The port on which the ZooKeeper process will listen for |
| 138 | + requests from other servers in the ensemble. The |
| 139 | + default is 2888. |
| 140 | +
|
| 141 | + --tick_time The length of a ZooKeeper tick in ms. The default is |
| 142 | + 2000. |
| 143 | +
|
| 144 | + --init_limit The number of Ticks that an ensemble member is allowed |
| 145 | + to perform leader election. The default is 10. |
| 146 | +
|
| 147 | + --sync_limit The maximum session timeout that the ensemble will |
| 148 | + allows a client to request. The default is 5. |
| 149 | +
|
| 150 | + --percentage The percentage of container memory to give to the JVM. |
| 151 | +
|
| 152 | + --max_client_cnxns The maximum number of client connections that the |
| 153 | + ZooKeeper process will accept simultaneously. The |
| 154 | + default is 60. |
| 155 | +
|
| 156 | + --snap_retain_count The maximum number of snapshots the ZooKeeper process |
| 157 | + will retain if purge_interval is greater than 0. The |
| 158 | + default is 3. |
| 159 | +
|
| 160 | + --purge_interval The number of hours the ZooKeeper process will wait |
| 161 | + between purging its old snapshots. If set to 0 old |
| 162 | + snapshots will never be purged. The default is 0. |
| 163 | +
|
| 164 | + --max_session_timeout The maximum time in milliseconds for a client session |
| 165 | + timeout. The default value is 2 * tick time. |
| 166 | +
|
| 167 | + --min_session_timeout The minimum time in milliseconds for a client session |
| 168 | + timeout. The default value is 20 * tick time. |
| 169 | +
|
| 170 | + --log_level The log level for the zookeeeper server. Either FATAL, |
| 171 | + ERROR, WARN, INFO, DEBUG. The default is INFO. |
| 172 | +" |
| 173 | +} |
| 174 | + |
| 175 | +function create_data_dirs() { |
| 176 | + if [ ! -d "$DATA_DIR" ]; then |
| 177 | + mkdir -p "$DATA_DIR" |
| 178 | + chown -R "$USER":"$USER" "$DATA_DIR" |
| 179 | + fi |
| 180 | + |
| 181 | + if [ ! -d "$DATA_LOG_DIR" ]; then |
| 182 | + mkdir -p "$DATA_LOG_DIR" |
| 183 | + chown -R "$USER":"$USER" "$DATA_LOG_DIR" |
| 184 | + fi |
| 185 | + |
| 186 | + if [ ! -d "$LOG_DIR" ]; then |
| 187 | + mkdir -p "$LOG_DIR" |
| 188 | + chown -R "$USER":"$USER" "$LOG_DIR" |
| 189 | + fi |
| 190 | + if [ ! -f "$ID_FILE" ] && [ "$SERVERS" -gt 1 ]; then |
| 191 | + echo "$MY_ID" >> "$ID_FILE" |
| 192 | + fi |
| 193 | +} |
| 194 | + |
| 195 | +function print_servers() { |
| 196 | + for (( i=1; i<=SERVERS; i++ )) |
| 197 | + do |
| 198 | + echo "server.$i=$NAME-$((i-1)).$DOMAIN:$SERVER_PORT:$ELECTION_PORT" |
| 199 | + done |
| 200 | +} |
| 201 | + |
| 202 | +function create_config() { |
| 203 | + rm -f "$CONFIG_FILE" |
| 204 | + { |
| 205 | + echo "#This file was autogenerated DO NOT EDIT" |
| 206 | + echo "clientPort=$CLIENT_PORT" |
| 207 | + echo "dataDir=$DATA_DIR" |
| 208 | + echo "dataLogDir=$DATA_LOG_DIR" |
| 209 | + echo "tickTime=$TICK_TIME" |
| 210 | + echo "initLimit=$INIT_LIMIT" |
| 211 | + echo "syncLimit=$SYNC_LIMIT" |
| 212 | + echo "maxClientCnxns=$MAX_CLIENT_CNXNS" |
| 213 | + echo "minSessionTimeout=$MIN_SESSION_TIMEOUT" |
| 214 | + echo "maxSessionTimeout=$MAX_SESSION_TIMEOUT" |
| 215 | + echo "autopurge.snapRetainCount=$SNAP_RETAIN_COUNT" |
| 216 | + echo "autopurge.purgeInteval=$PURGE_INTERVAL" |
| 217 | + echo "quorumListenOnAllIPs=$QUORUM_LISTEN_ON_ALL_IPS" |
| 218 | + # Allow running all zk commands. |
| 219 | + echo "4lw.commands.whitelist=*" |
| 220 | + if [ "$SERVERS" -gt 1 ]; then |
| 221 | + print_servers |
| 222 | + fi |
| 223 | + echo "metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider" |
| 224 | + echo "metricsProvider.httpPort=$PROM_PORT" |
| 225 | + } >> "$CONFIG_FILE" |
| 226 | + cat "$CONFIG_FILE" >&2 |
| 227 | +} |
| 228 | + |
| 229 | +function create_jvm_props() { |
| 230 | + rm -f "$JAVA_ENV_FILE" |
| 231 | + { |
| 232 | + echo "SERVER_JVMFLAGS=\"-XX:MaxRAMPercentage=${JVM_HEAP_PERCENTAGE_OF_RESOURCE_LIMIT} \ |
| 233 | + -XX:InitialRAMPercentage=${JVM_HEAP_PERCENTAGE_OF_RESOURCE_LIMIT}\"" |
| 234 | + echo "ZOO_LOG_DIR=$LOG_DIR" |
| 235 | + echo "JVMFLAGS=" |
| 236 | + } >> "$JAVA_ENV_FILE" |
| 237 | +} |
| 238 | + |
| 239 | +function create_log_props() { |
| 240 | + rm -f "$LOGGER_PROPS_FILE" |
| 241 | + echo "Creating ZooKeeper log4j configuration" |
| 242 | + { |
| 243 | + echo "zookeeper.root.logger=CONSOLE" |
| 244 | + echo "zookeeper.console.threshold=$LOG_LEVEL" |
| 245 | + echo "log4j.rootLogger=\${zookeeper.root.logger}" |
| 246 | + echo "log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender" |
| 247 | + echo "log4j.appender.CONSOLE.Threshold=\${zookeeper.console.threshold}" |
| 248 | + echo "log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout" |
| 249 | + echo "log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n" |
| 250 | + } >> "$LOGGER_PROPS_FILE" |
| 251 | +} |
| 252 | + |
| 253 | +optspec=":hv-:" |
| 254 | +while getopts "$optspec" optchar; do |
| 255 | + |
| 256 | + case "${optchar}" in |
| 257 | + -) |
| 258 | + case "${OPTARG}" in |
| 259 | + servers=*) |
| 260 | + SERVERS=${OPTARG##*=} |
| 261 | + ;; |
| 262 | + data_dir=*) |
| 263 | + DATA_DIR=${OPTARG##*=} |
| 264 | + ;; |
| 265 | + data_log_dir=*) |
| 266 | + DATA_LOG_DIR=${OPTARG##*=} |
| 267 | + ;; |
| 268 | + log_dir=*) |
| 269 | + LOG_DIR=${OPTARG##*=} |
| 270 | + ;; |
| 271 | + conf_dir=*) |
| 272 | + CONF_DIR=${OPTARG##*=} |
| 273 | + ;; |
| 274 | + client_port=*) |
| 275 | + CLIENT_PORT=${OPTARG##*=} |
| 276 | + ;; |
| 277 | + election_port=*) |
| 278 | + ELECTION_PORT=${OPTARG##*=} |
| 279 | + ;; |
| 280 | + server_port=*) |
| 281 | + SERVER_PORT=${OPTARG##*=} |
| 282 | + ;; |
| 283 | + tick_time=*) |
| 284 | + TICK_TIME=${OPTARG##*=} |
| 285 | + ;; |
| 286 | + init_limit=*) |
| 287 | + INIT_LIMIT=${OPTARG##*=} |
| 288 | + ;; |
| 289 | + sync_limit=*) |
| 290 | + SYNC_LIMIT=${OPTARG##*=} |
| 291 | + ;; |
| 292 | + percentage=*) |
| 293 | + JVM_HEAP_PERCENTAGE_OF_RESOURCE_LIMIT=${OPTARG##*=} |
| 294 | + ;; |
| 295 | + max_client_cnxns=*) |
| 296 | + MAX_CLIENT_CNXNS=${OPTARG##*=} |
| 297 | + ;; |
| 298 | + snap_retain_count=*) |
| 299 | + SNAP_RETAIN_COUNT=${OPTARG##*=} |
| 300 | + ;; |
| 301 | + purge_interval=*) |
| 302 | + PURGE_INTERVAL=${OPTARG##*=} |
| 303 | + ;; |
| 304 | + max_session_timeout=*) |
| 305 | + MAX_SESSION_TIMEOUT=${OPTARG##*=} |
| 306 | + ;; |
| 307 | + min_session_timeout=*) |
| 308 | + MIN_SESSION_TIMEOUT=${OPTARG##*=} |
| 309 | + ;; |
| 310 | + quorum_listen_on_all_ips=*) |
| 311 | + QUORUM_LISTEN_ON_ALL_IPS=${OPTARG##*=} |
| 312 | + ;; |
| 313 | + log_level=*) |
| 314 | + LOG_LEVEL=${OPTARG##*=} |
| 315 | + ;; |
| 316 | + *) |
| 317 | + echo "Unknown option --${OPTARG}" >&2 |
| 318 | + exit 1 |
| 319 | + ;; |
| 320 | + esac;; |
| 321 | + h) |
| 322 | + print_usage |
| 323 | + exit |
| 324 | + ;; |
| 325 | + v) |
| 326 | + echo "Parsing option: '-${optchar}'" >&2 |
| 327 | + ;; |
| 328 | + *) |
| 329 | + if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then |
| 330 | + echo "Non-option argument: '-${OPTARG}'" >&2 |
| 331 | + fi |
| 332 | + ;; |
| 333 | + esac |
| 334 | +done |
| 335 | + |
| 336 | +MIN_SESSION_TIMEOUT=${MIN_SESSION_TIMEOUT:- $((TICK_TIME*2))} |
| 337 | +MAX_SESSION_TIMEOUT=${MAX_SESSION_TIMEOUT:- $((TICK_TIME*20))} |
| 338 | +ID_FILE="$DATA_DIR/myid" |
| 339 | +if [ ! -d "$CONF_DIR" ]; then |
| 340 | + mkdir -p "$CONF_DIR" |
| 341 | + chown -R "$USER":"$USER" "$CONF_DIR" |
| 342 | +fi |
| 343 | +CONFIG_FILE="$CONF_DIR/zoo.cfg" |
| 344 | +LOGGER_PROPS_FILE="$CONF_DIR/log4j.properties" |
| 345 | +JAVA_ENV_FILE="$CONF_DIR/java.env" |
| 346 | + |
| 347 | +if [[ $HOST =~ (.*)-([0-9]+)$ ]]; then |
| 348 | + NAME=${BASH_REMATCH[1]} |
| 349 | + ORD=${BASH_REMATCH[2]} |
| 350 | +else |
| 351 | + echo "Failed to parse name and ordinal of Pod" |
| 352 | + exit 1 |
| 353 | +fi |
| 354 | + |
| 355 | +MY_ID=$((ORD+1)) |
| 356 | + |
| 357 | +export ZOOCFGDIR=${CONF_DIR} |
| 358 | +create_config && create_jvm_props && create_log_props && create_data_dirs && exec "${ZOOKEEPER_HOME}/bin/zkServer.sh" start-foreground |
0 commit comments