-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcassandra-rolling-restart.sh
172 lines (137 loc) · 4.11 KB
/
cassandra-rolling-restart.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
# exit on any error
set -e
function usage()
{
cat << EOF
Usage: $0 cassandra-install-directory [jmx_port] [additional-delay]
This script will do a rolling-restart of a cassandra cluster. A rolling restart
is defined as performing the following sequence on each cassandra node:
nodetool disablegossip
nodetool disablethrift
nodetool disablebinary
nodetool drain
service cassandra restart
wait for node to come up
wait any additional time
See the below note on how this script remotely restarts the cassandra service.
ARGUMENTS:
cassandra-install-directory full or relative path of install directory
nodetool should be located in a subdirectory
called "bin", yaml configuration should be
in conf/cassandra.yaml
additional-delay additional delay after a node goes up before
shutting down the next node.
NOTE: Remote Cassandra service restart
This script uses the following comand to restart a remote cassandra node:
ssh -n {node} /sbin/service cassandra restart.
This reqires that your environment be set up properly with remote ssh keys
or you may be required to type a password. You can set an environment variable
to utilize a different command
CASSANDRA_RESTART_COMMAND='ssh -n ${node} /sbin/service cassandra restart'
Example:
$0 ./cassandra-2.0.19 7199 30s
EOF
}
if [[ -z "$1" ]]; then
usage
exit 1
fi
DIRECTORY=$1
shift
JMX_PORT=7199
if [[ ! -z "$1" ]]; then
JMX_PORT=$1
shift
fi
DELAY=30s
if [[ ! -z "$1" ]]; then
DELAY=$1
shift
fi
# If last parameter is --ccm we can use CCM to start and stop nodes
# this is mostly for testing.
if [[ "$1" == "--ccm" ]]; then
CCM=1
ccm_index=1
shift
fi
if [[ -z "$CASSANDRA_RESTART_COMMAND" ]]; then
CASSANDRA_RESTART_COMMAND='ssh -n ${node} /sbin/service cassandra restart'
fi
CASSANDRA_NODETOOL_COMMAND='${DIRECTORY}/bin/nodetool -h ${node} -p ${JMX_PORT} '
if [[ $CCM == "1" ]]; then
CASSANDRA_RESTART_COMMAND='ccm node${ccm_index} stop && ccm node${ccm_index} start'
CASSANDRA_NODETOOL_COMMAND='${DIRECTORY}/bin/nodetool -h 127.0.0.1 -p ${JMX_PORT} '
fi
function is_up()
{
local node=$1
local output
local nodestat
output=$(eval "$CASSANDRA_NODETOOL_COMMAND status" 2>&1)
if [[ "$?" == "1" ]]; then
return 1
else
nodestat="$(echo "${output}" | grep "${node}" | awk '{print $1}')"
if [[ "${nodestat}" != "UN" ]]; then
return 1
else
return 0
fi
fi
}
function wait_up()
{
local node=$1
local output
echo "Waiting for ${node} to rejoin the ring..."
until is_up ${node}; do
sleep 5s
done
}
function check_cluster_up()
{
local cluster_status
local abnormal_count
cluster_status=$(eval "$CASSANDRA_NODETOOL_COMMAND status")
abnormal_count=$(echo "$cluster_status" | grep -E "^.L|^.J|^.M|^DN" | wc -l)
if [[ "$abnormal_count" != "0" ]]; then
echo "Aborting! All cluster nodes are not up!"
echo
echo "$cluster_status"
exit 1
fi
}
echo "Getting listen_address for this node..."
listen_address=$(grep "^listen_address:" ${DIRECTORY}/conf/cassandra.yaml | cut -d ':' -f2)
node=$listen_address
echo "This node is listening on ${listen_address}"
echo
echo "Getting list of nodes in the cluster..."
check_cluster_up
nodes=$(eval "$CASSANDRA_NODETOOL_COMMAND status" | grep "^UN" | awk '{print $2}')
echo "This script will perform a rolling restart on the following nodes:"
echo "$nodes"
echo ""
while IFS= read -r node; do
echo "Disable gossip on ${node}..."
eval "$CASSANDRA_NODETOOL_COMMAND disablegossip"
echo "Disable thrift on ${node}..."
eval "$CASSANDRA_NODETOOL_COMMAND disablethrift"
echo "Disable binary(cql) on ${node}..."
eval "$CASSANDRA_NODETOOL_COMMAND disablebinary"
echo "Draining ${node}..."
eval "$CASSANDRA_NODETOOL_COMMAND drain"
echo "Restarting cassandra service on ${node}..."
eval $CASSANDRA_RESTART_COMMAND
wait_up ${node}
echo "Sleeping for an addition ${DELAY}..."
sleep ${DELAY}
echo ""
if [[ "$CCM" == "1" ]]; then
JMX_PORT=$((JMX_PORT+100))
ccm_index=$((ccm_index+1))
fi
done <<<"${nodes}"
echo "Done with Cassandra cluster rolling restart!"