forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_fe.sh
More file actions
425 lines (364 loc) · 12.2 KB
/
Copy pathinit_fe.sh
File metadata and controls
425 lines (364 loc) · 12.2 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
set -eo pipefail
shopt -s nullglob
# Constant Definition
readonly DORIS_HOME="/opt/apache-doris"
readonly MAX_RETRY_TIMES=60
readonly RETRY_INTERVAL=1
readonly MYSQL_PORT=9030
readonly FE_CONFIG_FILE="${DORIS_HOME}/fe/conf/fe.conf"
# Log Function
log_message() {
local level="$1"
shift
local message="$*"
if [ "$#" -eq 0 ]; then
message="$(cat)"
fi
local timestamp="$(date -Iseconds)"
printf '%s [%s] [Entrypoint]: %s\n' "${timestamp}" "${level}" "${message}"
}
log_info() {
log_message "INFO" "$@"
}
log_warn() {
log_message "WARN" "$@" >&2
}
log_error() {
log_message "ERROR" "$@" >&2
exit 1
}
# Check if the parameter is empty
check_required_param() {
local param_name="$1"
local param_value="$2"
if [ -z "$param_value" ]; then
log_error "${param_name} is required but not set"
fi
}
# Check whether it is a source file call
is_sourced() {
[ "${#FUNCNAME[@]}" -ge 2 ] &&
[ "${FUNCNAME[0]}" = 'is_sourced' ] &&
[ "${FUNCNAME[1]}" = 'source' ]
}
# Initialize environment variables
init_environment() {
declare -g database_exists
if [ -d "${DORIS_HOME}/fe/doris-meta/image" ]; then
database_exists='true'
fi
}
# Verify IP address format
validate_ip_address() {
local ip="$1"
local ipv4_regex="^[1-2]?[0-9]?[0-9](\.[1-2]?[0-9]?[0-9]){3}$"
local ipv6_regex="^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)$"
if [[ $ip =~ $ipv4_regex ]] || [[ $ip =~ $ipv6_regex ]]; then
return 0
fi
return 1
}
# Verify port number
validate_port() {
local port="$1"
if [[ $port =~ ^[1-6]?[0-9]{1,4}$ ]] && [ "$port" -le 65535 ]; then
return 0
fi
return 1
}
# Verify the necessary environment variables
validate_environment() {
declare -g run_mode
# RECOVERY Mode Verification
if [ -n "$RECOVERY" ]; then
if [[ $RECOVERY =~ ^([tT][rR][uU][eE]|1)$ ]]; then
run_mode="RECOVERY"
log_info "Running in Recovery mode"
return
fi
fi
# K8S mode verification
if [ -n "$BUILD_TYPE" ]; then
if [[ $BUILD_TYPE =~ ^([kK]8[sS])$ ]]; then
run_mode="K8S"
log_info "Running in K8S mode"
return
fi
log_error "Invalid BUILD_TYPE. Expected: k8s"
fi
# Election Mode Verification
if [[ -n "$FE_SERVERS" && -n "$FE_ID" ]]; then
validate_election_mode
return
fi
# Specifying a schema for validation
if [[ -n "$FE_MASTER_IP" && -n "$FE_MASTER_PORT" &&
-n "$FE_CURRENT_IP" && -n "$FE_CURRENT_PORT" ]]; then
validate_assign_mode
return
fi
log_error "Missing required parameters. Please check documentation."
}
# Verify election mode configuration
validate_election_mode() {
run_mode="ELECTION"
# Verify FE_SERVERS format
local fe_servers_regex="^.+:[1-2]{0,1}[0-9]{0,1}[0-9]{1}(\.[1-2]{0,1}[0-9]{0,1}[0-9]{1}){3}:[1-6]{0,1}[0-9]{1,4}(,.+:[1-2]{0,1}[0-9]{0,1}[0-9]{1}(\.[1-2]{0,1}[0-9]{0,1}[0-9]{1}){3}:[1-6]{0,1}[0-9]{1,4})*$"
if ! [[ $FE_SERVERS =~ $fe_servers_regex ]]; then
log_error "Invalid FE_SERVERS format. Expected: name:ip:port[,name:ip:port]..."
fi
# Verify FE_ID
if ! [[ $FE_ID =~ ^[1-9]{1}$ ]]; then
log_error "Invalid FE_ID. Must be a single digit between 1-9"
fi
log_info "Running in Election mode"
}
# Verify the specified mode configuration
validate_assign_mode() {
run_mode="ASSIGN"
# Verify IP Address
if ! validate_ip_address "$FE_MASTER_IP"; then
log_error "Invalid FE_MASTER_IP format"
fi
if ! validate_ip_address "$FE_CURRENT_IP"; then
log_error "Invalid FE_CURRENT_IP format"
fi
# Verify port
if ! validate_port "$FE_MASTER_PORT"; then
log_error "Invalid FE_MASTER_PORT"
fi
if ! validate_port "$FE_CURRENT_PORT"; then
log_error "Invalid FE_CURRENT_PORT"
fi
log_info "Running in Assign mode"
}
# Parse a colon-delimited string
parse_colon_separated() {
local input="$1"
local -n arr=$2 # 使用nameref来存储结果
local IFS=':'
read -r -a arr <<< "$input"
}
# Parsing a comma-delimited string
parse_comma_separated() {
local input="$1"
local -n arr=$2 # 使用nameref来存储结果
local IFS=','
read -r -a arr <<< "$input"
}
# Configuring the election mode
setup_election_mode() {
local fe_server_array
parse_comma_separated "$FE_SERVERS" fe_server_array
# Parsing master node information
local master_info
parse_colon_separated "${fe_server_array[0]}" master_info
master_fe_ip="${master_info[1]}"
master_fe_port="${master_info[2]}"
# Parse the current node information
local found=false
local node_info
for node in "${fe_server_array[@]}"; do
parse_colon_separated "$node" node_info
if [ "${node_info[0]}" = "fe${FE_ID}" ]; then
current_fe_ip="${node_info[1]}"
current_fe_port="${node_info[2]}"
found=true
break
fi
done
if [ "$found" = "false" ]; then
log_error "Could not find configuration for fe${FE_ID} in FE_SERVERS"
fi
is_master_fe=$([[ "$FE_ID" == "1" ]] && echo "true" || echo "false")
}
# Set up a preferred network
setup_priority_networks() {
local ip_parts
parse_colon_separated "$1" ip_parts
priority_networks="${ip_parts[0]}.${ip_parts[1]}.${ip_parts[2]}.0/24"
}
# Configure the specified mode
setup_assign_mode() {
master_fe_ip="$FE_MASTER_IP"
master_fe_port="$FE_MASTER_PORT"
current_fe_ip="$FE_CURRENT_IP"
current_fe_port="$FE_CURRENT_PORT"
is_master_fe=$([[ "$master_fe_ip" == "$current_fe_ip" ]] && echo "true" || echo "false")
}
# Add RECOVERY mode configuration function
setup_recovery_mode() {
# In recovery mode, we need to read the configuration from the metadata
local meta_dir="${DORIS_HOME}/fe/doris-meta"
if [ ! -d "$meta_dir" ] || [ -z "$(ls -A "$meta_dir")" ]; then
log_error "Cannot start in recovery mode: meta directory is empty or does not exist"
fi
log_info "Starting in recovery mode, using existing meta directory"
is_master_fe="true" # In recovery mode, it starts as the master node by default
}
# Configuring FE Nodes
setup_fe_node() {
declare -g master_fe_ip master_fe_port current_fe_ip current_fe_port
declare -g priority_networks is_master_fe
case $run_mode in
"ELECTION")
setup_election_mode
;;
"ASSIGN")
setup_assign_mode
;;
"RECOVERY")
setup_recovery_mode
;;
esac
# Set priority network (if not in recovery mode)
if [ "$run_mode" != "RECOVERY" ]; then
local ip_parts
IFS='.' read -r -a ip_parts <<< "$current_fe_ip"
priority_networks="${ip_parts[0]}.${ip_parts[1]}.${ip_parts[2]}.0/24"
fi
# Print key configuration information
log_info "==== FE Node Configuration ===="
log_info "Run Mode: ${run_mode}"
if [ "$run_mode" = "RECOVERY" ]; then
log_info "Recovery Mode: true"
log_info "Meta Directory: ${DORIS_HOME}/fe/doris-meta"
else
log_info "Is Master: ${is_master_fe}"
log_info "Master FE IP: ${master_fe_ip}"
log_info "Master FE Port: ${master_fe_port}"
log_info "Current FE IP: ${current_fe_ip}"
log_info "Current FE Port: ${current_fe_port}"
log_info "Priority Networks: ${priority_networks}"
if [ "$run_mode" = "ELECTION" ]; then
log_info "FE ID: ${FE_ID}"
log_info "FE Servers: ${FE_SERVERS}"
fi
fi
log_info "=========================="
}
# Start FE node
start_fe() {
if [ "$run_mode" = "RECOVERY" ]; then
log_info "Starting FE node in recovery mode"
${DORIS_HOME}/fe/bin/start_fe.sh --metadata_failure_recovery
return
fi
if [ "$is_master_fe" = "true" ]; then
log_info "Starting master FE node"
${DORIS_HOME}/fe/bin/start_fe.sh --console
else
log_info "Starting follower FE node"
${DORIS_HOME}/fe/bin/start_fe.sh --helper "${master_fe_ip}:${master_fe_port}" --console
fi
}
# Check whether the FE node is registered
check_fe_registered() {
local retry_count=0
while [ $retry_count -lt $MAX_RETRY_TIMES ]; do
# Query the existing FE node list
local query_result
query_result=$(mysql -uroot -P"${MYSQL_PORT}" -h"${master_fe_ip}" \
-N -e "SHOW FRONTENDS" 2>/dev/null | grep -w "${current_fe_ip}" | grep -w "${current_fe_port}" || true)
if [ -n "$query_result" ]; then
log_info "FE node ${current_fe_ip}:${current_fe_port} is already registered"
return 0
fi
retry_count=$((retry_count + 1))
if [ $((retry_count % 20)) -eq 1 ]; then
log_info "Waiting for master FE to be ready... ($retry_count/$MAX_RETRY_TIMES)"
fi
sleep "$RETRY_INTERVAL"
done
return 1
}
# Check the metadata directory
check_meta_dir() {
local meta_dir="${DORIS_HOME}/fe/doris-meta"
if [ -d "$meta_dir/image" ] && [ -n "$(ls -A "$meta_dir/image")" ]; then
log_info "Meta directory already exists and not empty"
return 0
fi
return 1
}
# Register FE Node
register_fe() {
if [ "$is_master_fe" = "true" ]; then
log_info "Master FE node does not need registration"
return
fi
# First check if the node is registered
if check_fe_registered; then
return
fi
local retry_count=0
while [ $retry_count -lt $MAX_RETRY_TIMES ]; do
if mysql -uroot -P"${MYSQL_PORT}" -h"${master_fe_ip}" \
-e "ALTER SYSTEM ADD FOLLOWER '${current_fe_ip}:${current_fe_port}'" 2>/dev/null; then
log_info "Successfully registered FE node"
return
fi
retry_count=$((retry_count + 1))
if [ $((retry_count % 20)) -eq 1 ]; then
log_warn "Failed to register FE node, retrying... ($retry_count/$MAX_RETRY_TIMES)"
fi
sleep "$RETRY_INTERVAL"
done
log_error "Failed to register FE node after ${MAX_RETRY_TIMES} attempts"
}
# Cleanup Function
cleanup() {
log_info "Stopping FE node"
${DORIS_HOME}/fe/bin/stop_fe.sh
}
# Main Function
main() {
validate_environment
trap cleanup SIGTERM SIGINT
if [ "$run_mode" = "K8S" ]; then
${DORIS_HOME}/fe/bin/start_fe.sh --console &
wait $!
elif [ "$run_mode" = "RECOVERY" ]; then
setup_fe_node
start_fe &
wait $!
else
init_environment
setup_fe_node
# Check the metadata directory
if check_meta_dir; then
log_info "Meta directory exists, starting FE directly"
start_fe &
wait $!
return
fi
# The metadata directory does not exist and needs to be initialized and registered.
log_info "Initializing meta directory"
if [ -z "$database_exists" ]; then
echo "priority_networks = ${priority_networks}" >> "$FE_CONFIG_FILE"
fi
register_fe
start_fe &
wait $!
fi
}
if ! is_sourced; then
main "$@"
fi