Skip to content

Commit 5a0594c

Browse files
committed
modify entrypoint
1 parent db23d42 commit 5a0594c

File tree

1 file changed

+285
-0
lines changed

1 file changed

+285
-0
lines changed
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
#!/bin/bash
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
19+
20+
# the fe query port for mysql.
21+
FE_QUERY_PORT=${FE_QUERY_PORT:-9030}
22+
# timeout for probe fe master.
23+
PROBE_TIMEOUT=60
24+
# interval time to probe fe.
25+
PROBE_INTERVAL=2
26+
# rpc port for fe communicate with be.
27+
HEARTBEAT_PORT=9050
28+
# fqdn or ip
29+
MY_SELF=
30+
MY_IP=`hostname -i`
31+
MY_HOSTNAME=`hostname -f`
32+
DORIS_ROOT=${DORIS_ROOT:-"/opt/apache-doris"}
33+
# if config secret for basic auth about operate node of doris, the path must be `/etc/basic_auth`. This is set by operator and the key of password must be `password`.
34+
AUTH_PATH="/etc/basic_auth"
35+
DORIS_HOME=${DORIS_ROOT}/be
36+
BE_CONFIG=$DORIS_HOME/conf/be.conf
37+
# represents self in fe meta or not.
38+
REGISTERED=false
39+
40+
DB_ADMIN_USER=${USER:-"root"}
41+
42+
DB_ADMIN_PASSWD=$PASSWD
43+
44+
ENABLE_WORKLOAD_GROUP=${ENABLE_WORKLOAD_GROUP:-false}
45+
WORKLOAD_GROUP_PATH="/sys/fs/cgroup/cpu/doris"
46+
47+
48+
log_stderr()
49+
{
50+
echo "[`date`] $@" >&2
51+
}
52+
53+
function add_cluster_info_to_conf()
54+
{
55+
if [[ "x$ENABLE_WORKLOAD_GROUP" == "xtrue" ]]; then
56+
echo "doris_cgroup_cpu_path=$WORKLOAD_GROUP_PATH" >> ${DORIS_HOME}/conf/be.conf
57+
fi
58+
}
59+
60+
update_conf_from_configmap()
61+
{
62+
add_cluster_info_to_conf
63+
if [[ "x$CONFIGMAP_MOUNT_PATH" == "x" ]] ; then
64+
log_stderr '[info] Empty $CONFIGMAP_MOUNT_PATH env var, skip it!'
65+
return 0
66+
fi
67+
if ! test -d $CONFIGMAP_MOUNT_PATH ; then
68+
log_stderr "[info] $CONFIGMAP_MOUNT_PATH not exist or not a directory, ignore ..."
69+
return 0
70+
fi
71+
local tgtconfdir=$DORIS_HOME/conf
72+
for conffile in `ls $CONFIGMAP_MOUNT_PATH`
73+
do
74+
log_stderr "[info] Process conf file $conffile ..."
75+
local tgt=$tgtconfdir/$conffile
76+
if test -e $tgt ; then
77+
# make a backup
78+
mv -f $tgt ${tgt}.bak
79+
fi
80+
if [[ "$conffile" == "be.conf" ]]; then
81+
cp $CONFIGMAP_MOUNT_PATH/$conffile $DORIS_HOME/conf/$file
82+
add_cluster_info_to_conf
83+
continue
84+
fi
85+
ln -sfT $CONFIGMAP_MOUNT_PATH/$conffile $tgt
86+
done
87+
}
88+
89+
# resolve password for root
90+
resolve_password_from_secret()
91+
{
92+
if [[ -f "$AUTH_PATH/password" ]]; then
93+
DB_ADMIN_PASSWD=`cat $AUTH_PATH/password`
94+
fi
95+
if [[ -f "$AUTH_PATH/username" ]]; then
96+
DB_ADMIN_USER=`cat $AUTH_PATH/username`
97+
fi
98+
}
99+
100+
# get all backends info to check self exist or not.
101+
show_backends(){
102+
local svc=$1
103+
backends=`timeout 15 mysql --connect-timeout 2 -h $svc -P $FE_QUERY_PORT -uroot --skip-column-names --batch -e 'SHOW BACKENDS;' 2>&1`
104+
log_stderr "[info] use root no password show backends result $backends ."
105+
if echo $backends | grep -w "1045" | grep -q -w "28000" &>/dev/null; then
106+
log_stderr "[info] use username and password that configured to show backends."
107+
backends=`timeout 15 mysql --connect-timeout 2 -h $svc -P $FE_QUERY_PORT -u$DB_ADMIN_USER -p$DB_ADMIN_PASSWD --skip-column-names --batch -e 'SHOW BACKENDS;'`
108+
fi
109+
110+
echo "$backends"
111+
}
112+
113+
# get all registered fe in cluster, for check the fe have `MASTER`.
114+
function show_frontends()
115+
{
116+
local addr=$1
117+
frontends=`timeout 15 mysql --connect-timeout 2 -h $addr -P $FE_QUERY_PORT -uroot --batch -e 'show frontends;' 2>&1`
118+
log_stderr "[info] use root no password show frontends result $frontends ."
119+
if echo $frontends | grep -w "1045" | grep -q -w "28000" &>/dev/null; then
120+
log_stderr "[info] use username and passwore that configured to show frontends."
121+
frontends=`timeout 15 mysql --connect-timeout 2 -h $addr -P $FE_QUERY_PORT -u$DB_ADMIN_USER -p$DB_ADMIN_PASSWD --batch -e 'show frontends;'`
122+
fi
123+
124+
echo "$frontends"
125+
}
126+
127+
#parse the `$BE_CONFIG` file, passing the key need resolve as parameter.
128+
parse_confval_from_conf()
129+
{
130+
# a naive script to grep given confkey from fe conf file
131+
# assume conf format: ^\s*<key>\s*=\s*<value>\s*$
132+
local confkey=$1
133+
local confvalue=`grep "\<$confkey\>" $BE_CONFIG | grep -v '^\s*#' | sed 's|^\s*'$confkey'\s*=\s*\(.*\)\s*$|\1|g'`
134+
echo "$confvalue"
135+
}
136+
137+
collect_env_info()
138+
{
139+
# heartbeat_port from conf file
140+
local heartbeat_port=`parse_confval_from_conf "heartbeat_service_port"`
141+
if [[ "x$heartbeat_port" != "x" ]] ; then
142+
HEARTBEAT_PORT=$heartbeat_port
143+
fi
144+
145+
if [[ "x$HOST_TYPE" == "xIP" ]] ; then
146+
MY_SELF=$MY_IP
147+
else
148+
MY_SELF=$MY_HOSTNAME
149+
fi
150+
}
151+
152+
add_self()
153+
{
154+
local svc=$1
155+
start=`date +%s`
156+
local timeout=$PROBE_TIMEOUT
157+
158+
while true
159+
do
160+
memlist=`show_backends $svc`
161+
if echo "$memlist" | grep -q -w "$MY_SELF" &>/dev/null ; then
162+
log_stderr "[info] Check myself ($MY_SELF:$HEARTBEAT_PORT) exist in FE, start be directly ..."
163+
break;
164+
fi
165+
166+
# check fe cluster have master, if fe have not master wait.
167+
fe_memlist=`show_frontends $svc`
168+
local pos=`echo "$fe_memlist" | grep '\<IsMaster\>' | awk -F '\t' '{for(i=1;i<NF;i++) {if ($i == "IsMaster") print i}}'`
169+
local leader=`echo "$fe_memlist" | grep '\<FOLLOWER\>' | awk -v p="$pos" -F '\t' '{if ($p=="true") print $2}'`
170+
log_stderr "'IsMaster' sequence in columns is $pos master=$leader ."
171+
172+
if [[ "x$leader" == "x" ]]; then
173+
log_stderr "[info] resolve the eighth column for finding master !"
174+
leader=`echo "$fe_memlist" | grep '\<FOLLOWER\>' | awk -F '\t' '{if ($8=="true") print $2}'`
175+
fi
176+
if [[ "x$leader" == "x" ]]; then
177+
# compatible 2.1.0
178+
log_stderr "[info] resoluve the ninth column for finding master!"
179+
leader=`echo "$fe_memlist" | grep '\<FOLLOWER\>' | awk -F '\t' '{if ($9=="true") print $2}'`
180+
fi
181+
182+
if [[ "x$leader" != "x" ]]; then
183+
create_account $leader
184+
log_stderr "[info] myself ($MY_SELF:$HEARTBEAT_PORT) not exist in FE and fe have leader register myself into fe."
185+
add_result=`timeout 15 mysql --connect-timeout 2 -h $svc -P $FE_QUERY_PORT -uroot --skip-column-names --batch -e "ALTER SYSTEM ADD BACKEND \"$MY_SELF:$HEARTBEAT_PORT\";" 2>&1`
186+
if echo $add_result | grep -w "1045" | grep -q -w "28000" &>/dev/null ; then
187+
timeout 15 mysql --connect-timeout 2 -h $svc -P $FE_QUERY_PORT -u$DB_ADMIN_USER -p$DB_ADMIN_PASSWD --skip-column-names --batch -e "ALTER SYSTEM ADD BACKEND \"$MY_SELF:$HEARTBEAT_PORT\";"
188+
fi
189+
190+
let "expire=start+timeout"
191+
now=`date +%s`
192+
if [[ $expire -le $now ]] ; then
193+
log_stderr "[error] exit probe master for probing timeout."
194+
return 0
195+
fi
196+
else
197+
log_stderr "[info] not have leader wait fe cluster elect a master, sleep 2s..."
198+
sleep $PROBE_INTERVAL
199+
fi
200+
done
201+
}
202+
203+
function create_account()
204+
{
205+
master=$1
206+
users=`mysql --connect-timeout 2 -h $master -P $FE_QUERY_PORT -uroot --skip-column-names --batch -e 'SHOW ALL GRANTS;' 2>&1`
207+
if echo $users | grep -w "1045" | grep -q -w "28000" &>/dev/null; then
208+
log_stderr "the 'root' account have set password! not need auto create management account."
209+
return 0
210+
fi
211+
if echo $users | grep -q -w "$DB_ADMIN_USER" &>/dev/null; then
212+
log_stderr "the $DB_ADMIN_USER have exist in doris."
213+
return 0
214+
fi
215+
mysql --connect-timeout 2 -h $master -P$FE_QUERY_PORT -uroot --skip-column-names --batch -e "CREATE USER '$DB_ADMIN_USER' IDENTIFIED BY '$DB_ADMIN_PASSWD';GRANT NODE_PRIV ON *.*.* TO $DB_ADMIN_USER;" 2>&1
216+
log_stderr "created new account and grant NODE_PRIV!"
217+
218+
}
219+
220+
# check be exist or not, if exist return 0, or register self in fe cluster. when all fe address failed exit script.
221+
# `xxx1:port,xxx2:port` as parameter to function.
222+
function check_and_register()
223+
{
224+
addrs=$1
225+
local addrArr=(${addrs//,/ })
226+
for addr in ${addrArr[@]}
227+
do
228+
add_self $addr
229+
230+
if [[ $REGISTERED ]]; then
231+
break;
232+
fi
233+
done
234+
235+
if [[ $REGISTERED ]]; then
236+
return 0
237+
else
238+
log_stderr "not find master in fe cluster, please use mysql connect to fe for verfing the master exist and verify domain connectivity with two pods in different node. "
239+
exit 1
240+
fi
241+
}
242+
243+
function work_load_group_for_cgroup_path() {
244+
output=$(cat /proc/filesystems | grep cgroup)
245+
if [ -z "$output" ]; then
246+
log_stderr "[error] The host machine does not have cgroup installed, so the workload group function will be limited."
247+
exit 1
248+
fi
249+
250+
mkdir -p /sys/fs/cgroup/cpu/doris
251+
chmod 770 /sys/fs/cgroup/cpu/doris
252+
chown -R root:root /sys/fs/cgroup/cpu/doris
253+
254+
if [[ -f "/sys/fs/cgroup/cgroup.controllers" ]]; then
255+
log_stderr "[info] The host machine cgroup version: v2."
256+
chmod a+w /sys/fs/cgroup/cgroup.procs
257+
else
258+
log_stderr "[info] The host machine cgroup version: v1."
259+
fi
260+
}
261+
262+
fe_addrs=$1
263+
if [[ "x$fe_addrs" == "x" ]]; then
264+
echo "need fe address as paramter!"
265+
echo " Example $0 <fe_addr>"
266+
exit 1
267+
fi
268+
269+
if [[ "x$ENABLE_WORKLOAD_GROUP" == "xtrue" ]]; then
270+
log_stderr '[info] Enable workload group !'
271+
work_load_group_for_cgroup_path
272+
fi
273+
274+
update_conf_from_configmap
275+
# resolve password for root to manage nodes in doris.
276+
resolve_password_from_secret
277+
collect_env_info
278+
#add_self $fe_addr || exit $?
279+
check_and_register $fe_addrs
280+
./doris-debug --component be
281+
log_stderr "run start_be.sh"
282+
# the server will start in the current terminal session, and the log output and console interaction will be printed to that terminal
283+
# befor doris 2.0.2 ,doris start with : start_xx.sh
284+
# sine doris 2.0.2 ,doris start with : start_xx.sh --console doc: https://doris.apache.org/docs/dev/install/standard-deployment/#version--202
285+
$DORIS_HOME/bin/start_be.sh --console

0 commit comments

Comments
 (0)