forked from StarRocks/starrocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfe_entrypoint.sh
More file actions
executable file
·294 lines (259 loc) · 8.44 KB
/
Copy pathfe_entrypoint.sh
File metadata and controls
executable file
·294 lines (259 loc) · 8.44 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
# $prog <fe-svc-name>
# Editlog port: default 9010
EDIT_LOG_PORT=9010
# Query port: default 9030
QUERY_PORT=9030
# host_type, default "IP"
HOST_TYPE=${HOST_TYPE:-"IP"}
# FE leader
FE_LEADER=
# probe interval: 2 seconds
PROBE_INTERVAL=2
# timeout for probe leader: 120 seconds
PROBE_LEADER_POD0_TIMEOUT=30 # at most 15 attempts, no less than the times needed for an election
PROBE_LEADER_PODX_TIMEOUT=120 # at most 60 attempts
# myself as IP or FQDN
MYSELF=
STARROCKS_ROOT=${STARROCKS_ROOT:-"/opt/starrocks"}
STARROCKS_HOME=${STARROCKS_ROOT}/fe
FE_CONFFILE=$STARROCKS_HOME/conf/fe.conf
META_DIR=$STARROCKS_HOME/meta
EXIT_IN_PROGRESS=false
IS_FE_OBSERVER=${IS_FE_OBSERVER:-"false"}
case "$IS_FE_OBSERVER" in
1|true|True) IS_FE_OBSERVER="true" ;;
*) IS_FE_OBSERVER="false" ;;
esac
log_stderr()
{
echo "[`date`] $@" >&2
}
parse_confval_from_fe_conf()
{
# a naive script to grep given confkey from fe conf file
# assume conf format: ^\s*<key>\s*=\s*<value>\s*$
local confkey=$1
local confvalue=`grep "\<$confkey\>" $FE_CONFFILE | grep -v '^\s*#' | sed 's|^\s*'$confkey'\s*=\s*\(.*\)\s*$|\1|g'`
echo "$confvalue"
}
collect_env_info()
{
# set POD_IP, POD_FQDN, POD_INDEX, EDIT_LOG_PORT, QUERY_PORT
if [[ "x$POD_IP" == "x" ]] ; then
POD_IP=`hostname -i | awk '{print $1}'`
fi
if [[ "x$POD_FQDN" == "x" ]] ; then
POD_FQDN=`hostname -f`
fi
if [[ "x$HOST_TYPE" == "xFQDN" ]] ; then
MYSELF=$POD_FQDN
else
MYSELF=$POD_IP # default type if not specified
fi
# example: fe-sr-deploy-1.fe-svc.kc-sr.svc.cluster.local
POD_INDEX=`echo $POD_FQDN | awk -F'.' '{print $1}' | awk -F'-' '{print $NF}'`
# edit_log_port from conf file
local edit_log_port=`parse_confval_from_fe_conf "edit_log_port"`
if [[ "x$edit_log_port" != "x" ]] ; then
EDIT_LOG_PORT=$edit_log_port
fi
# query_port from conf file
local query_port=`parse_confval_from_fe_conf "query_port"`
if [[ "x$query_port" != "x" ]] ; then
QUERY_PORT=$query_port
fi
}
show_frontends()
{
local svc=$1
# ensure `mysql` command can be ended with 15 seconds
# "show frontends" query will hang when there is no leader yet in the cluster
timeout 15 mysql --connect-timeout 2 -h $svc -P $QUERY_PORT -u root --skip-column-names --batch -e 'show frontends;'
}
probe_leader_for_pod0()
{
# possible to have no result at all, because myself is the first FE instance in the cluster
local svc=$1
local start=`date +%s`
local has_member=false
local memlist=
while true
do
NC="nc -z -w 2"
if $NC $svc $QUERY_PORT ; then
log_stderr "FE service is alive, check if has leader ..."
memlist=`show_frontends $svc`
local leader=`echo "$memlist" | grep '\<LEADER\>' | awk '{print $3}'`
if [[ "x$leader" != "x" ]] ; then
# has leader, done
log_stderr "Find leader: $leader!"
FE_LEADER=$leader
return 0
fi
if [[ "x$memlist" != "x" ]] ; then
# FE service does not have leader yet, but has members.
has_member=true
fi
log_stderr "No leader yet, has_member: $has_member ..."
else
log_stderr "FE service $svc:$QUERY_PORT is not alive yet!"
fi
# no leader yet, check if needs timeout and quit
local timeout=$PROBE_LEADER_POD0_TIMEOUT
if $has_member ; then
# set timeout to the same as PODX since there are other members
timeout=$PROBE_LEADER_PODX_TIMEOUT
fi
local now=`date +%s`
let "expire=start+timeout"
if [[ $expire -le $now ]] ; then
if $has_member ; then
log_stderr "Timed out, abort!"
exit 1
else
log_stderr "Timed out, no members detected ever, assume myself is the first node .."
# empty FE_LEADER
FE_LEADER=""
return 0
fi
fi
sleep $PROBE_INTERVAL
done
}
probe_leader_for_podX()
{
# wait until find a leader or timeout
local svc=$1
local start=`date +%s`
while true
do
NC="nc -z -w 2"
if $NC $svc $QUERY_PORT ; then
log_stderr "FE service is alive, check if has leader ..."
local leader=`show_frontends $svc | grep '\<LEADER\>' | awk '{print $3}'`
if [[ "x$leader" != "x" ]] ; then
# has leader, done
log_stderr "Find leader: $leader!"
FE_LEADER=$leader
return 0
fi
# no leader yet, check if needs timeout and quit
log_stderr "No leader yet ..."
else
log_stderr "FE service $svc:$QUERY_PORT is not alive yet!"
fi
local now=`date +%s`
let "expire=start+PROBE_LEADER_PODX_TIMEOUT"
if [[ $expire -le $now ]] ; then
log_stderr "Timed out, abort!"
exit 1
fi
sleep $PROBE_INTERVAL
done
}
probe_leader()
{
local svc=$1
# find leader under current service and set to FE_LEADER
if [[ "$POD_INDEX" -eq 0 ]] && [[ "$IS_FE_OBSERVER" == "false" ]]; then
probe_leader_for_pod0 $svc
else
probe_leader_for_podX $svc
fi
}
update_conf_from_configmap()
{
if [[ "x$CONFIGMAP_MOUNT_PATH" == "x" ]] ; then
log_stderr 'Empty $CONFIGMAP_MOUNT_PATH env var, skip it!'
return 0
fi
if ! test -d $CONFIGMAP_MOUNT_PATH ; then
log_stderr "$CONFIGMAP_MOUNT_PATH not exist or not a directory, ignore ..."
return 0
fi
local tgtconfdir=$STARROCKS_HOME/conf
for conffile in `ls $CONFIGMAP_MOUNT_PATH`
do
log_stderr "Process conf file $conffile ..."
local tgt=$tgtconfdir/$conffile
if test -e $tgt ; then
# make a backup
mv -f $tgt ${tgt}.bak
fi
ln -sfT $CONFIGMAP_MOUNT_PATH/$conffile $tgt
done
}
start_fe_no_meta()
{
# apply --host_type and --helper option
local svc=$1
local opts=""
if [[ "x$HOST_TYPE" != "x" ]] ; then
opts+=" --host_type $HOST_TYPE"
fi
if [[ "x$FE_LEADER" != "x" ]] ; then
opts+=" --helper $FE_LEADER:$EDIT_LOG_PORT"
local start=`date +%s`
while true
do
if [[ "$IS_FE_OBSERVER" != "false" ]]; then
log_stderr "Add myself($MYSELF:$EDIT_LOG_PORT) to leader as observer ..."
mysql --connect-timeout 2 -h $FE_LEADER -P $QUERY_PORT -u root --skip-column-names --batch -e "ALTER SYSTEM ADD OBSERVER \"$MYSELF:$EDIT_LOG_PORT\";"
else
log_stderr "Add myself($MYSELF:$EDIT_LOG_PORT) to leader as follower ..."
mysql --connect-timeout 2 -h $FE_LEADER -P $QUERY_PORT -u root --skip-column-names --batch -e "ALTER SYSTEM ADD FOLLOWER \"$MYSELF:$EDIT_LOG_PORT\";"
fi
# check if added successful
if show_frontends $svc | grep -q -w "$MYSELF" &>/dev/null ; then
break;
fi
local now=`date +%s`
let "expire=start+30" # 30s timeout
if [[ $expire -le $now ]] ; then
log_stderr "Timed out, abort!"
exit 1
fi
log_stderr "Sleep a while and retry adding ..."
sleep $PROBE_INTERVAL
done
fi
if [[ "x$LOG_CONSOLE" == "x1" ]] ; then
opts+=" --logconsole"
fi
log_stderr "first start with no meta run start_fe.sh with additional options: '$opts'"
$STARROCKS_HOME/bin/start_fe.sh $opts
}
start_fe_with_meta()
{
local opts=""
if [[ "x$HOST_TYPE" != "x" ]] ; then
opts+=" --host_type $HOST_TYPE"
fi
if [[ "x$LOG_CONSOLE" == "x1" ]] ; then
opts+=" --logconsole"
fi
log_stderr "start with meta run start_fe.sh with additional options: '$opts'"
$STARROCKS_HOME/bin/start_fe.sh $opts
}
svc_name=$1
if [[ "x$svc_name" == "x" ]] ; then
echo "Need a required parameter!"
echo " Example: $0 <fe_service_name>"
exit 1
fi
update_conf_from_configmap
# meta_dir from conf file
meta_dir=`parse_confval_from_fe_conf "meta_dir"`
if [[ "x$meta_dir" != "x" ]] ; then
META_DIR=$meta_dir
fi
if [[ -f "$META_DIR/image/ROLE" ]];then
log_stderr "start fe with exist meta."
start_fe_with_meta
else
log_stderr "first start fe with meta not exist."
collect_env_info
probe_leader $svc_name
start_fe_no_meta $svc_name
fi