-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathreindexTimer.sh
executable file
·177 lines (147 loc) · 5.77 KB
/
reindexTimer.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
173
174
175
176
177
#!/bin/bash
#
# Time how long it takes to do a full reindex in k8s
#
####################################################################################################
# VARS
####################################################################################################
monitor_period_sec=5
base_url="${1}"
release_name=$(helm list | grep metacat | awk '{print $1}')
echo "release_name: $release_name"
secret_name=$(kubectl get secrets | egrep ".*\-metacat-secrets" | awk '{print $1}')
echo "secret_name: $secret_name"
rmq_pwd=$(kubectl get secret "$secret_name" \
-o jsonpath="{.data.rabbitmq-password}" | base64 -d)
####################################################################################################
# FUNCTIONS
####################################################################################################
function usage() {
echo
echo "USAGE:"
echo
echo "* Monitoring only; NO re-index:"
echo
echo " $ $0"
echo
echo "* Monitoring WITH a full re-index: (IMPORTANT: used 'export' when setting TOKEN!)"
echo
echo " $ export TOKEN=\$(cat /path/to/admin/token)"
echo " $ $0 <base url for metacat api>"
echo
echo " # EXAMPLES:"
echo
echo " # jwt token from filesystem:"
echo " $ export TOKEN=\$(cat urn_node_KNB_dev_from_DataONETestIntCA.jwt)"
echo " $ $0 https://knb-dev.test.dataone.org/metacat"
echo
echo " # jwt token from k8s Secret"
echo " # $ export TOKEN=\$( kubectl get secret MYRELEASE-indexer-token \\"
echo " -o jsonpath=\"{.data.DataONEauthToken}\" | base64 -d )"
echo " $ $0 https://knb-dev.test.dataone.org/metacat"
echo
}
function reindexWarningMsg() {
echo
echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
echo
echo " BEWARE! the FULL RE-INDEX option will trigger a FULL RE-INDEX of all datasets on:"
echo " $base_url"
echo " ...which cannot before stopped, and which may take a very long time (hours or days),"
echo " and will likely affect the responsiveness of your metacat instance during that time"
echo
echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
echo "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
echo
}
function portWarning() {
echo "ERROR: Ensure port forwarding is running before executing; e.g.:"
echo " $ kubectl port-forward service/$release_name-rabbitmq-headless 15672:15672"
exit 3
}
handle_sigint() {
echo -e "\nCaught Ctrl+C, exiting loop..."
do_loop=false
# shellcheck disable=SC2104
break
}
####################################################################################################
# INPUT & VALIDATION
####################################################################################################
if [[ $(nc -zv localhost 15672 2>&1 | grep -c "refused") -gt 0 ]]; then
portWarning
else
echo "RMQ port is open"
fi
reindexWarningMsg
reindex_command="re-index-all"
echo "Available Options:"
echo "* FULL RE-INDEX: type '$reindex_command' and hit 'Enter'."
echo "* ONLY MONITOR queue size & # indexer workers, every $monitor_period_sec sec: hit 'Enter'."
echo "* EXIT: ctrl-c"
action=""
read -p "Option: " action
####################################################################################################
# MAIN
####################################################################################################
start=$(date +%s)
echo "Starting at: $(date)"; echo
if [[ "$action" == "$reindex_command" ]]; then
if [ -z "$base_url" ]; then
echo "ERROR: Must provide base url!"
usage
exit 1
fi
if [ -z "$TOKEN" ]; then
echo "ERROR: Ensure env. variable \$TOKEN contains your indexer/admin jwt token"
usage
exit 2
fi
echo "calling $base_url/d1/mn/v2/index?all=true"
# Start reindex-all
curl -X PUT -H "Authorization: Bearer $TOKEN" "$base_url/d1/mn/v2/index?all=true"
# delay for reindex to begin populating queue, otherwise we exit too early
sleep 10
fi
max_queue_size=0
max_worker_count=0
do_loop=true
trap 'handle_sigint' SIGINT
echo -e "Minutes\t\tQueue size\tQueue max\t# Indexers\tIndexer max\tObjects/minute"
while [[ "$do_loop" == "true" ]]; do
queue_size=$(curl -u metacat-rmq-guest:"$rmq_pwd" \
http://localhost:15672/api/queues/%2f/index 2>/dev/null | \
jq -r '.messages')
if [ "$queue_size" -gt "$max_queue_size" ]; then
max_queue_size=$queue_size
fi
idx_worker_count=$(kubectl get pods | grep -c d1index)
if [ "$idx_worker_count" -gt "$max_worker_count" ]; then
max_worker_count=$idx_worker_count
fi
now=$(date +%s)
objpermin=0
time_min=$(((now - start)/60))
if [ $time_min -ne 0 ]; then
objpermin=$((max_queue_size / time_min))
fi
echo -ne "\033[K$time_min\t\t$queue_size\t\t$max_queue_size\t\t$idx_worker_count\t\t$max_worker_count\t\t$objpermin\n\n\r\033[1A\033[1A"
if [ "$queue_size" -eq 0 ] && [ "$action" == "$reindex_command" ]; then
do_loop=false
else
sleep $monitor_period_sec
fi
done
finish=$(date +%s)
tot_time_min=$(((finish - start)/60))
echo; echo; echo "Finished at: $(date)"
echo "Total objects indexed: $max_queue_size"
echo "Max index workers used: $max_worker_count"
echo "Total time for reindex: $time_min minutes"
if [ $tot_time_min -ne 0 ]; then
echo "objects per minute: $((max_queue_size / tot_time_min))"
fi
if [ $max_queue_size -ne 0 ]; then
echo "milliSec per object: $((1000 * (finish - start) / max_queue_size))"
fi