-
Notifications
You must be signed in to change notification settings - Fork 2
/
cassandraRepairByTokenRange.sh
102 lines (102 loc) · 3.61 KB
/
cassandraRepairByTokenRange.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
#!/bin/bash
###############################################################
# Perform repair by local token range
# Local ranges are broken into sub range repairs
# Subrange repair:
# * IS NOT COMPATIBLE WITH INCREMENTAL REPAIRS
# * AVOIDS ANTICOMPACTION after data is streamed
#
# Assumptions:
# * Cassandra is running
# * JMX interface is available
# * CQLSH/NODETOOL credentials work before executing
# * This script is to be executed on a per node basis
#
USR=cassandra
PWD=cassandra
JMX_USR=""
JMX_PWD=""
###############################################################
# CQLSH invoke
###############################################################
TIMEOUT="300"
CQLSH="cqlsh -u ${USR} -p ${PWD} --request-timeout=${TIMEOUT}"
###############################################################
# Invoke Repair
###############################################################
function doRepair(){
KSP=$1
TBL=$2
STR=$3
ETR=$4
[ "${JMX_USR}" ] && [ "${JMX_USR}" ] && AUTH="-u ${JMX_USR} -pw ${JMX_PWD}"
nodetool ${AUTH} -pw ${JMX_PWD} repair -st ${STR} -et ${ETR} -- ${KSP} ${TBL}
}
###############################################################
# Get Local Tokens
###############################################################
function getTokens(){
for i in $($CQLSH -e "select tokens from system.local;" | awk -F, '/{/{print $0}' | tr -d '{' | tr -d '}' | tr -d ','); do
echo ${i//\'/}
done | sort -n
}
###############################################################
# Calculate subranges, If STP>1 for even lower footprint
# Otherwise repair the whole range
###############################################################
function repairByTokenRange(){
i=0
STP=256
KSP=1
TBL=2
tokens=($(getTokens))
while [ ${i} -lt ${#tokens[@]} ]; do
if [ "${tokens[i+1]}" ]; then
# If STP > 1
if [ "${STP}" -gt 1 ]; then
j=0
range=$(echo "(${tokens[i+1]} - ${tokens[i]})" | bc -l)
step=$(echo "scale=0; ${range} /${STP}" | bc -l)
if (( ${range} % ${STP} == 0 )); then
subTokens=($(seq ${tokens[i]} ${step} ${tokens[i+1]}))
else
subTokens=($(seq ${tokens[i]} ${step} ${tokens[i+1]}) ${tokens[i+1]})
fi
while [ ${j} -lt ${#subTokens[@]} ] && [ "${subTokens[j+1]}" ]; do
doRepair ${KSP} ${TBL} ${subTokens[j]} ${subTokens[j+1]}
((j++))
done
# Otherwise... STP=1
else
doRepair ${KSP} ${TBL} ${tokens[i]} ${tokens[i+1]}
fi
fi
((i++))
done
}
###############################################################
# Where all comes together
# You have the options to specify Keyspace (KSP)/Table (TBL)
# Otherwise the script will try running repair on all keyspaces/tables
# The logic validates the given parameters are valid
# Invalid entries will be quietly ignored
###############################################################
main(){
[ "$1" ] && KSP=$1
[ "$2" ] && TBL=$2
SCHEMA=$($CQLSH -e "DESCRIBE SCHEMA;")
echo "Begin processing ...
for KSP in $(echo "${SCHEMA}" | awk '/KEYSPACE '"${KSP}"'/ {print $3}'); do
for TBL in $(echo "${SCHEMA}" | sed -n '/'${KSP}'/,/CREATE KEYSPACE/p' | awk '/CREATE TABLE '"${TBL}"'/ {print $3}'); do
TBL=${TBL/*./}
[ "${KSP}" ] && [ "${TBL}" ] && repairByTokenRange ${KSP} ${TBL}
done
done
echo "End procesing"
}
###############################################################
# Execution
###############################################################
main $@
#
#EOF