Skip to content

Instantly share code, notes, and snippets.

@mathewmoon
Created March 15, 2019 19:22
Show Gist options
  • Save mathewmoon/29bfa71a8e5327a71d3422c756b6a008 to your computer and use it in GitHub Desktop.
Save mathewmoon/29bfa71a8e5327a71d3422c756b6a008 to your computer and use it in GitHub Desktop.
#!/bin/bash
JAVA=$(which java)
[ -z "${BOOKIE_MIN_HEAP}" ] && export BOOKIE_MIN_HEAP=1000
[ -z "${BOOKIE_MAX_HEAP}" ] && export BOOKIE_MAX_HEAP=1000
[ -z "${BOOKIE_MAX_DIRECT_MEMORY}" ] && BOOKIE_MAX_DIRECT_MEMORY=2000
[ -z "${CLUSTER_NAME}" ] && CLUSTER_NAME=bookkeeper
[ -z "${GC_OPTS}" ] && GC_OPTS=" -XX:+UseG1GC \
-XX:MaxGCPauseMillis=10 \
-XX:+ParallelRefProcEnabled \
-XX:+UnlockExperimentalVMOptions \
-XX:+AggressiveOpts \
-XX:+DoEscapeAnalysis \
-XX:ParallelGCThreads=32 \
-XX:ConcGCThreads=32 \
-XX:G1NewSizePercent=50 \
-XX:+DisableExplicitGC \
-XX:-ResizePLAB "
[ -z "${GC_LOGGING}"] && GC_LOGGING=" -XX:+PrintGCDetails \
-XX:+PrintGCApplicationStoppedTime \
-XX:+UseGCLogFileRotation \
-XX:NumberOfGCLogFiles=5 \
-XX:GCLogFileSize=64m \
-Xloggc:/logs/gc_%p.log "
MEM_OPTS=" -Xms${BOOKIE_MIN_HEAP}m -Xmx${BOOKIE_MAX_HEAP}m -XX:MaxDirectMemorySize=${BOOKIE_MAX_DIRECT_MEMORY}m "
# Configure the classpath
if grep -q StreamStorageLifecycleComponent /conf/bookkeeper.conf; then
BOOKIE_SERVER_MODULE=$(find /bookkeeper/lib -type f | egrep '(org.apache.bookkeeper-)?bookkeeper-server')
else
BOOKIE_SERVER_MODULE=$(find /bookkeeper/lib -type f | egrep '(org.apache.bookkeeper-)?stream-storage-server')
fi
CLASSPATH="$(find /bookkeeper/lib -name '*.jar' |tr '\n' ':'|sed 's/:$//'):/logs:${BOOKIE_SERVER_MODULE}"
[ -z "${NETTY_LEAK_DETECTION_LEVEL}" ] && NETTY_LEAK_DETECTION_LEVEL="disabled"
[ -z "${NETTY_RECYCLER_MAXCAPACITY}" ] && NETTY_RECYCLER_MAXCAPACITY="1000"
[ -z "${NETTY_RECYCLER_LINKCAPACITY}"] && NETTY_RECYCLER_LINKCAPACITY="1024"
NETTY_OPTS=" -Dio.netty.leakDetectionLevel=${NETTY_LEAK_DETECTION_LEVEL} \
-Dio.netty.recycler.maxCapacity.default=${NETTY_RECYCLER_MAXCAPACITY} \
-Dio.netty.recycler.linkCapacity=${NETTY_RECYCLER_LINKCAPACITY} "
LOG_OPTS="-Dlog4j.configuration=file:/conf/log4j.properties "
CMD=$(cat<<EOF
${JAVA}
-Djava.net.preferIPv4Stack=true
${LOG_OPTS}
${MEM_OPTS}
${GC_OPTS}
${GC_LOGGING}
${NETTY_OPTS}
-cp ${CLASSPATH}
${JVM_OPTS}
EOF
)
[ -z "${STREAM_STORAGE_ROOT_PATH}" ] && export STREAM_STORAGE_ROOT_PATH="/${CLUSTER_NAME}/stream"
# Where Zookeeper is
METADATA_URI=$(grep metadataServiceUri /conf/bookkeeper.conf | cut -d'=' -f2 | tr -d ' ')
#echo "Metadata URI is ${METADATA_URI}"
# Basically the same as METADATA_URI, just without the protocol. FYI we don't support zkServers list since it is deprecated
ZK_HOST=$(echo "${METADATA_URI}" | sed -r 's#.*//(.*:[0-9]+).*#\1#')
echo "Zookeeper host endpoint is $ZK_HOST"
# The longest time in seconds to wait on a peer to init the cluster before giving up
[ -z "${MAX_INIT_WAIT}" ] && MAX_INIT_WAIT=100
# Init the cluster if we havent
if ${CMD} org.apache.zookeeper.ZooKeeperMain -server ${ZK_HOST} stat /${CLUSTER_NAME}/ledgers 2>&1 | grep -q "Node does not exist:"; then
echo "${CLUSTER_NAME}/ledgers does not exist in Zookeeper ${ZK_HOST}. We will try to initialize...."
# If we can create this ephemeral node then we know that another node hasn't and we are the first to try and do an init
if ! ${CMD} org.apache.zookeeper.ZooKeeperMain -server ${ZK_HOST} create -e /${CLUSTER_NAME}-bkInitLock locked 2>&1 | grep -q 'Node already exists:'; then
echo "Got a lock. Running init now........"
if ${CMD} org.apache.bookkeeper.tools.cli.BKCtl --conf /conf/bookkeeper.conf cluster init -x ${CLUSTER_NAME} zk+hierarchical://${ZK_HOST}; then
echo "Init finished..."
else
echo "Init failed. Exiting..."
exit 2
fi
else
# If we couldn't write to that node then there must be a peer that did. Lets wait on them to finish initializing
echo "Could not get a lock on bkInitLock. Waiting on peer to initialize cluster"
N=1
while ${CMD} org.apache.zookeeper.ZooKeeperMain -server ${ZK_HOST} stat ${STREAM_STORAGE_ROOT_PATH} 2>&1 | grep -q "Node does not exist:"; do
echo "Waiting on peer to init cluster......"
sleep 5
((N++))
if [ "$N" -gt "${MAX_INIT_WAIT}" ]; then
echo "Gave up on initializing after ${MAX_INIT_WAIT} seconds"
exit 2
fi
done
fi
else
echo "Cluster is already initialized. Going on with our lives"
fi
if [ -z "${AUTO_RECOVERY}" ]; then
cat<<EOF
##############################################################################################
# #
# Starting Bookie #
# #
##############################################################################################
EOF
${CMD} org.apache.bookkeeper.server.Main --conf /conf/bookkeeper.conf
else
cat<<EOF
##############################################################################################
# #
# Starting Autorecovery daemon #
# #
##############################################################################################
EOF
${CMD} org.apache.bookkeeper.replication.AutoRecoveryMain --conf /conf/bookkeeper.conf
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment