Skip to content

Instantly share code, notes, and snippets.

@premkumr
Created August 6, 2023 00:53
Show Gist options
  • Save premkumr/ade30973eea8cfdc1cf6742a0da61a07 to your computer and use it in GitHub Desktop.
Save premkumr/ade30973eea8cfdc1cf6742a0da61a07 to your computer and use it in GitHub Desktop.
create a 3-node yb cluster
#!/bin/bash
debug=1
# base directry where the data will be stored - ${HOME}/var/node1, ${HOME}/var/node2, ${HOME}/var/node3 ..
BASEDIR=${HOME}/var/node
# location of yb repo - eg yugabyte-2.13.2.0
VERSIONBASE=${HOME}/software
function bold() {
local WHITE="\e[1;33m"
local NC="\e[0m"
printf "${WHITE}$@${NC}"
}
function title() {
bold "${@}"
printf "\n"
}
# to echo the commands
function exe() {
if [[ -n $debug ]]; then
printf "Running >> $(bold ${@/eval/})\n"
fi
"$@" ;
}
function has_procs() {
ps x | grep -i "$@ " | grep -v " grep "
}
function available_versions() {
local cur_version=$(current_version)
find ${HOME}/software -type d -d 1 -name 'yugabyte-*' | awk -F- '{print $NF}' | grep -v ${cur_version}
}
function current_version() {
ls -al ${HOME}/software/yugabyte | awk -F- '{print $NF}'
}
function switch_to() {
local ver="$1"
if [ ! -d ${VERSIONBASE}/yugabyte-$ver ]
then
echo "unable to find yugabyte-${ver} in ${VERSIONBASE}"
title Available to switch Versions:
available_versions
exit 1
fi
(
cd ${VERSIONBASE}
rm -f yugabyte
ln -s yugabyte-${ver} yugabyte
)
echo "switched to ${ver}"
}
# -----------------------------------------------------------------
function versions () {
bold "Current Version: "; echo $(current_version)
if [[ $# -ne 0 ]]
then
switch_to $1
else
title Other Versions:
available_versions
fi
}
function procs() {
local count=0
if [[ -n $(has_procs yb-master) ]]; then
title "Masters >>>> "
ps x | grep -i yb-master | grep -v grep | awk '{n=split($5, a, "/"); print $1,a[n],$(NF-2),$(NF-1),$(NF) }'
((count++))
fi
if [[ -n $(has_procs yb-tserver) ]]; then
echo
title "T-Servers >>>> "
ps x | grep -i yb-tserver | grep -v grep | awk '{n=split($5, a, "/"); print $1,a[n],$(NF-2),$(NF-1),$(NF) }'
((count++))
fi
if [[ -n $(has_procs yugabyted) ]]; then
echo
title "Yugabyted >>>> "
ps x | grep -i "yugabyted " | grep -v grep | awk '{n=split($6, a, "/"); print $1,a[n],$(NF-2),$(NF-1),$(NF) }'
((count++))
fi
if [[ -n $(has_procs yb-admin) ]]; then
echo
title "Yb-Admin >>>> "
ps x | grep -i "yb-admin " | grep -v grep | awk '{n=split($5, a, "/"); print $1,a[n],$(NF-2),$(NF-1),$(NF) }'
((count++))
fi
if [[ $count == 0 ]]; then
echo "No yugabyte process running"
fi
}
function start() {
local node=""
num_re='^[0-9]+$'
if [[ $1 =~ ${num_re} ]] ; then
node=$1
shift
fi
local additional_flags=$1
# TEST_docdb_log_write_batches=true
local tflags="ysql_enable_packed_row=true,ysql_beta_features=true,yb_enable_read_committed_isolation=true"
local mflags="ysql_enable_packed_row=true"
local zone=(0 a b c)
#local additional_tflags="ysql_sequence_cache_method=server"
#local additional_mflags=""
if [[ ! -z ${additional_flags} ]]; then
if [[ -z ${tflags} ]] ; then
tflags="${additional_flags}"
else
tflags="${tflags},${additional_flags}"
fi
if [[ -z ${mflags} ]] ; then
mflags="${additional_flags}"
else
mflags="${mflags},${additional_flags}"
fi
fi
for num in 1 2 3
do
JOIN=
if [[ $num -gt 1 ]]; then
JOIN=--join=127.0.0.1
fi
#
if [[ -z $node ]] || [[ $node == $num ]]; then
exe yugabyted start ${JOIN} \
--advertise_address=127.0.0.${num} \
--cloud_location=aws.us-east-2.us-east-2${zone[$num]} \
--base_dir=${BASEDIR}${num} \
--master_flags="${mflags}" --tserver_flags="${tflags}"
fi
done
if [[ -z $node ]]; then
if ! (exe yugabyted configure data_placement --base_dir=${BASEDIR}1 --fault_tolerance=zone 2>/dev/null); then
exe yugabyted configure --base_dir=${BASEDIR}1 --fault_tolerance=zone
fi
fi
}
function killnode() {
if [[ $# == 0 ]]; then
echo "error : instance not specified."
return
fi
local node=$1
local pid=$(ps aux | grep "base_dir=${BASEDIR}${node}" | grep -v grep | awk '{print $2}')
# kill yugabyted
if [[ -n $pid ]]; then
echo "killing yugabyted : $pid"
exe kill -9 $pid
else
echo "unable to find the yugabyted instance [$node]"
fi
# kill t-server
pid=$(ps aux | grep "yb-tserver.*fs_data_dirs=${BASEDIR}${node}" | grep -v grep | awk '{print $2}')
if [[ -n $pid ]]; then
echo "killing t-server: $pid"
exe kill -9 $pid
else
echo "unable to find the t-server instance [$node]"
fi
# kill master
pid=$(ps aux | grep "yb-master.*fs_data_dirs=${BASEDIR}${node}" | grep -v grep | awk '{print $2}')
if [[ -n $pid ]]; then
echo "killing master: $pid"
exe kill -9 $pid
else
echo "unable to find the master instance [$node]"
fi
echo "cleaning pid files..."
exe rm ${BASEDIR}${node}/data/*.pid
}
function stop() {
local node=""
local num_re='^[0-9]+$'
if [[ $1 =~ ${num_re} ]] ; then
node=$1
shift
fi
for num in 3 2 1
do
if [[ -z $node ]] || [[ $node == $num ]]; then
exe yugabyted stop --base_dir=${BASEDIR}${num}
fi
done
}
function status() {
local node=1
local pids
for node in 1 2 3
do
exe yugabyted status --base_dir=${BASEDIR}${node} &
pids[${node}]=$!
done
# wait for all pids
for pid in ${pids[*]}; do
wait $pid
done
}
function destroy() {
for node in 3 2 1
do
exe yugabyted destroy --base_dir=${BASEDIR}${node}
done
}
# --- MAIN ---
CMD="${1:-status}"
shift
if [[ $CMD == "start" ]]; then
start $@
elif [[ $CMD == "stop" ]]; then
stop $@
elif [[ $CMD == "restart" ]]; then
stop $@
wait
start $@
elif [[ $CMD == "status" ]]; then
status $@
versions
elif [[ $CMD == "kill" ]]; then
killnode $@
elif [[ $CMD == "destroy" ]]; then
destroy $@
elif [[ $CMD =~ proc* ]]; then
procs $@
elif [[ $CMD =~ vers* ]]; then
versions $@
else
# print help
echo "3-node yb manager. on 127.0.0.[1-3]"
echo "USAGE: yb [command] [instance]"
echo
echo "command:"
echo " start - start the db instances"
echo " stop - stop the db instances"
echo " status - status of the instances"
echo " destroy - shutdown and clean the cluster"
echo " kill - kill the specificied instance"
echo " ---------- "
echo " procs - list running processes"
echo " version - list currrent version & switch"
echo
echo "instance: one of [ 1, 2, 3 ]"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment