验证中...
Languages: Shell
Categories: 数据库相关
Latest update 2019-03-19 10:42
tikv-deploy.sh
Raw Copy
#!/bin/bash
set -e
file=tidb-latest-linux-amd64
dir=/data/tidb-latest-linux-amd64
nodes=(10.16.148.72 10.16.148.73 10.16.148.74 10.16.148.75)
metrics_addr=10.110.27.6:9091
len=${#nodes[@]}
echo_with_color() {
echo -e "\n\033[34m$1\033[0m"
}
get_ip() {
echo $(ip a | grep -A 2 eth0: | grep inet | tr ' ' '\n' | egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" | head -1)
}
do_mount() {
if ! mount | grep '/dev/sdh1'; then
mkdir -p /data
parted -s -a optimal /dev/sdh mklabel gpt -- mkpart primary ext4 1 -1
mkfs.ext4 /dev/sdh1
# if immediately run the following, mkfs will say: Could not stat /dev/sdh1 --- No such file or directory
# seems that mkfs.ext4 returns before it finishes its job
# so we check here
while true; do
if [ -e /dev/sdh1 ]; then
break
fi
done
mount -o nodelalloc,noatime /dev/sdh1 /data
fi
}
start_pd() {
dir=$2
metrics_addr=$3
if ! pidof pd-server; then
cd $dir
# > /dev/null is needed by ssh. In some cases maybe nohup and 2>&1 are also needed
#-config conf/pd.toml
bin/pd-server --name=pd1 --data-dir=pd1 --client-urls="http://$1:2379" --peer-urls="http://$1:2380" --initial-cluster="pd1=http://$1:2380" --log-file=pd1.log -metrics-addr "$metrics_addr" > /dev/null &
else
echo "pd-server started !"
fi
}
start_tikv() {
dir=$2
metrics_addr=$3
pd=$4
if ! pidof tikv-server; then
cd $dir
#
bin/tikv-server --config conf/tikv.toml --pd-endpoints="$pd:2379" --addr="$1:20160" --data-dir=tikv1 --log-file=tikv1.log --metrics-addr "$metrics_addr" > /dev/null &
else
echo "tikv-server started !"
fi
}
# Order: mount -> copy -> start
case $1 in
mount)
for ((i=0; i<len; i++)) ; do
h=${nodes[$i]}
echo_with_color "mounting on $h..."
ssh root@$h "$(declare -f do_mount); do_mount"
done
;;
umount)
for ((i=0; i<len; i++)) ; do
h=${nodes[$i]}
echo_with_color "unmounting on $h"
ssh root@$h "umount /data"
done
;;
start)
for ((i=0; i<len; i++)) ; do
h=${nodes[$i]}
echo_with_color "starting $h"
if [ $i = 0 ]; then
pd=$h
ssh root@$h "$(declare -f start_pd); start_pd $h $dir $metrics_addr"
echo "sleep 5s before starting tikv ..."
sleep 5
else
ssh root@$h "$(declare -f start_tikv); start_tikv $h $dir $metrics_addr $pd"
fi
done
;;
stop)
for ((i=0; i<len; i++)) ; do
h=${nodes[$i]}
echo "stop $h"
if [ $i = 0 ]; then
ssh root@$h "killall pd-server || echo -n"
else
ssh root@$h "killall tikv-server || echo -n"
fi
done
;;
status|st)
for ((i=0; i<len; i++)) ; do
h=${nodes[$i]}
echo_with_color "status: $h ..."
if [ $i = 0 ]; then
pd=$h
ssh root@$h "ps aux | grep \[p\]d-server || echo -n"
ssh root@$h "netstat -tlunp | grep pd-serv || echo -n"
else
ssh root@$h "ps aux | grep \[t\]ikv-server || echo -n"
ssh root@$h "netstat -tlunp | grep tikv-s || echo -n"
fi
done
# why -t here ?
ssh -t root@$pd "$dir/bin/pd-ctl store -d -u http://$pd:2379"
;;
tail)
for ((i=0; i<len; i++)) ; do
h=${nodes[$i]}
echo_with_color "tailing $h ..."
if [ $i = 0 ]; then
ssh root@$h "tail $dir/pd1.log || echo -n"
else
ssh root@$h "tail $dir/tikv1.log"
fi
done
;;
copy)
for ((i=0; i<len; i++)) ; do
h=${nodes[$i]}
echo_with_color "copying binary to $h ..."
ssh root@$h "bash -c '[ -e $dir ]'"
if [ $? != 0 ]; then
scp -r /root/cs/$file root@$h:/data
fi
scp -r ~/cs/FlameGraph root@$h:/data
done
;;
perf)
if [ x$2 = x ]; then i=1; fi
if [ x$3 = x ]; then s=30; fi
h=${nodes[$i]}
echo_with_color "perfing for $s seconds on $h ..."
# -t and \$ are very important !!
ssh -t root@$h "cd /data/FlameGraph && perf record -F 199 -ag --call-graph dwarf -p \$(pidof tikv-server) -- sleep $s && ls -lh perf.data"
d=`date +%H%M%S`
f=tikv-$h-$d.svg
echo_with_color "generating FlameGraph to $h ..."
ssh -t root@$h "cd /data/FlameGraph && perf script | ./stackcollapse-perf.pl | ./flamegraph.pl > $f && mv $f /var/www/html"
echo "You can see the results from $h/$f"
ssh root@$h "yum install -y httpd && service httpd restart"
;;
probe)
if [ x$2 = x ]; then i=1; fi
if [ x$3 = x ]; then s=30; fi
h=${nodes[$i]}
echo_with_color "perf probing malloc for $s seconds on $h ..."
# -t and \$ are very important !!
ssh -t root@$h "cd /data/FlameGraph && perf probe -x $dir/bin/tikv-server -f --add malloc && perf record -e probe_tikv:malloc -F 199 -ag --call-graph dwarf -p \$(pidof tikv-server) -- sleep $s && ls -lh perf.data"
d=`date +%H%M%S`
f=tikv-malloc-$h-$d.svg
echo_with_color "generating malloc FlameGraph to $h ..."
ssh -t root@$h "cd /data/FlameGraph && perf script | ./stackcollapse-perf.pl | ./flamegraph.pl > $f && mv $f /var/www/html"
echo "You can see the results from $h/$f"
#ssh root@$h "yum install -y httpd && service httpd restart"
;;
clean)
# clean all data
for ((i=0; i<len; i++)) ; do
h=${nodes[$i]}
echo_with_color "cleaning $h"
if [ $i = 0 ]; then
ssh root@$h "rm -rf $dir/pd1* $dir/tikv1*"
else
ssh root@$h "rm -rf $dir/tikv1*"
fi
service httpd stop
done
;;
*)
echo "no such command"
break
;;
esac

Comment list( 0 )

You need to Sign in for post a comment

Help Search