forked from byzer-org/byzer-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-mlsql-cluster.sh
executable file
·361 lines (272 loc) · 9.69 KB
/
run-mlsql-cluster.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
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
#!/usr/bin/env bash
set -e
set -o pipefail
function exit_with_usage {
cat << EOF
=== Usage: run mlsql cluster in Aliyun ===
-- Aliyun configuration
SECURITY_GROUP - the security-group id of aliyun. Notice that by default, the master will allocate public ip so we can visit them, but there are no any protect except the
SECURITY_GROUP. So please create a proper SECURITY_GROUP in Aliyun before run this script.
AK - access key
AKS - access key secret
MASTER_INSTANCE_TYPE - default ecs.r5.large
SLAVE_INSTANCE_TYPE - default ecs.r5.large
-- MLSQL configuration
MLSQL_KEY_PARE_NAME - a ssh key which you can connect to the esc server.
if you do not have one, use pymlsql to create one:
pymlsql start --image-id m-bp13ubsorlrxdb9lmv2x --need-public-ip false --init-ssh-key true
then the ssh key file will be created in your directory ~/.ssh
MLSQL_INIT_SSH_KEY - Is need to init ssh key. default false
MLSQL_SPARK_VERSION - the spark version, 2.2/2.3/2.4 default 2.3
MLSQL_VERSION - the mlsql version, 1.1.6 default 1.1.6
MLSQL_SLAVE_NUM - the number of worker. default 1
MASTER_WITH_PUBLIC_IP - default true
PYMLSQL_VERSIOIN - 1.1.6.3
MLSQL_THIRD_PARTY_JARS - None
EOF
exit 0
}
if [[ $@ == *"help"* ]]; then
exit_with_usage
fi
for env in AK AKS MLSQL_KEY_PARE_NAME; do
if [[ -z "${!env}" ]]; then
echo "===$env must be set to run this script==="
exit 1
fi
done
pymlsql --help > /dev/null
if [[ "$?" != "0" ]];then
echo "=== please use pip install pymlsql first==="
exit 1
fi
#export MLSQL_KEY_PARE_NAME=mlsql-build-env-local
export MLSQL_SPARK_VERSION=${MLSQL_SPARK_VERSION:-2.3}
export MLSQL_VERSION=${MLSQL_VERSION:-1.1.6}
export SECURITY_GROUP=${SECURITY_GROUP:-sg-bp1hi23xfzybp0exjp8a}
export MASTER_INSTANCE_TYPE=${MASTER_INSTANCE_TYPE:-ecs.r5.large}
export SLAVE_INSTANCE_TYPE=${SLAVE_INSTANCE_TYPE:-ecs.r5.large}
export MASTER_WITH_PUBLIC_IP=${MASTER_WITH_PUBLIC_IP:-true}
export MLSQL_SLAVE_NUM=${MLSQL_SLAVE_NUM:-1}
export PYMLSQL_VERSIOIN=${PYMLSQL_VERSIOIN:-1.1.6.3}
export MLSQL_INIT_SSH_KEY=${MLSQL_INIT_SSH_KEY:-false}
export MLSQL_TAR="streamingpro-spark_${MLSQL_SPARK_VERSION}-${MLSQL_VERSION}.tar.gz"
export MLSQL_NAME="streamingpro-spark_${MLSQL_SPARK_VERSION}-${MLSQL_VERSION}"
export SCRIPT_FILE="/tmp/k.sh"
if [[ -z "${OSS_AK}" ]];then
export OSS_AK=${AK}
fi
if [[ -z "${OSS_AKS}" ]];then
export OSS_AKS=${AKS}
fi
echo "Create ECS instance for master"
start_output=$(pymlsql start --image-id m-bp13ubsorlrxdb9lmv2x --instance-type ${MASTER_INSTANCE_TYPE} --need-public-ip ${MASTER_WITH_PUBLIC_IP} --init-ssh-key ${MLSQL_INIT_SSH_KEY} --security-group ${SECURITY_GROUP})
echo ----"${start_output}"-----
export instance_id=$(echo "${start_output}"|grep '^instance_id:'|cut -d ':' -f2)
export public_ip=$(echo "${start_output}"|grep '^public_ip:'|cut -d ':' -f2)
export inter_ip=$(echo "${start_output}"|grep '^intern_ip:'|cut -d ':' -f2)
echo "${instance_id}" > mlsql.master
echo "Fetch master hostname"
cat << EOF > ${SCRIPT_FILE}
#!/usr/bin/env bash
echo hostname:\`hostname\`
EOF
host_output=$(pymlsql exec-shell --instance-id ${instance_id} --script-file ${SCRIPT_FILE} --execute-user root)
export master_hostname=$(echo "${host_output}"|grep '^hostname:'|cut -d ':' -f2)
cat << EOF
---------------------------------------
Master INFO:
master instance_id : ${instance_id}
master public_ip : ${public_ip}
master inter_ip : ${inter_ip}
master host_name: ${master_hostname}
---------------------------------------
EOF
echo "Start spark master"
cat << EOF > ${SCRIPT_FILE}
#!/usr/bin/env bash
source activate mlsql-3.5
cd /home/webuser/apps/spark-2.3
mkdir -p ~/.ssh
./sbin/start-master.sh -h ${inter_ip}
EOF
pymlsql exec-shell --instance-id ${instance_id} \
--script-file ${SCRIPT_FILE} \
--execute-user webuser
echo "copy ssh file and script to master, so we can create/start slave in master"
pymlsql copy-from-local --instance-id ${instance_id} --execute-user root \
--source ~/.ssh/${MLSQL_KEY_PARE_NAME} \
--target /home/webuser/.ssh/
for file in "start-slaves.sh" "copy-main-jar-to-slaves.sh";
do
pymlsql copy-from-local --instance-id ${instance_id} --execute-user root \
--source $file \
--target /home/webuser
done
echo "configure auth of the script"
cat << EOF > ${SCRIPT_FILE}
#!/usr/bin/env bash
mkdir -p /home/webuser/third-party-jars
chown -R webuser:webuser /home/webuser/third-party-jars
chown -R webuser:webuser /home/webuser/*.sh
chown -R webuser:webuser /home/webuser/.ssh/${MLSQL_KEY_PARE_NAME}
chmod 600 /home/webuser/.ssh/${MLSQL_KEY_PARE_NAME}
chmod u+x /home/webuser/*.sh
EOF
pymlsql exec-shell --instance-id ${instance_id} \
--script-file ${SCRIPT_FILE} \
--execute-user root
cat << EOF > ${SCRIPT_FILE}
#!/usr/bin/env bash
source activate mlsql-3.5
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
mkdir ~/.pip
echo -e "[global]\ntrusted-host = mirrors.aliyun.com\nindex-url = https://mirrors.aliyun.com/pypi/simple" > ~/.pip/pip.conf
if [[ -z "${PyMLSQL_PIP}" ]];then
git clone https://github.com/allwefantasy/PyMLSQL.git
cd PyMLSQL
rm -rf ./dist && pip uninstall -y pymlsql && python setup.py sdist bdist_wheel && cd ./dist/ && pip install pymlsql-${PYMLSQL_VERSIOIN}-py2.py3-none-any.whl && cd -
else
pip install pymlsql
fi
EOF
pymlsql exec-shell --instance-id ${instance_id} \
--script-file ${SCRIPT_FILE} \
--execute-user webuser
echo "run start slave script in master"
cat << EOF > ${SCRIPT_FILE}
#!/usr/bin/env bash
source activate mlsql-3.5
cd /home/webuser
export instance_id=${instance_id}
export public_ip=${public_ip}
export inter_ip=${inter_ip}
export master_hostname=${master_hostname}
export MLSQL_KEY_PARE_NAME=${MLSQL_KEY_PARE_NAME}
export AK=${AK}
export AKS=${AKS}
export SECURITY_GROUP=${SECURITY_GROUP}
export SLAVE_INSTANCE_TYPE=${SLAVE_INSTANCE_TYPE}
pids=""
for page in {1..${MLSQL_SLAVE_NUM}}
do
./start-slaves.sh &
pids[\${page}]=\$!
done
FAIL_NUM=0
for job in \${pids[*]};
do
echo \$job
wait \$job || let "FAIL_NUM+=1"
done
echo "total salves: ${MLSQL_SLAVE_NUM} FAILs: \${FAIL_NUM}"
EOF
pymlsql exec-shell --instance-id ${instance_id} \
--script-file ${SCRIPT_FILE} \
--execute-user webuser
echo "sleep 15 wait until the salves are up"
sleep 15
echo "copy mlsql.slaves from master"
pymlsql copy-to-local --instance-id ${instance_id} --execute-user root \
--source /home/webuser/mlsql.slaves \
--target .
echo "Download MLSQL to master"
cat << EOF > ${SCRIPT_FILE}
#!/usr/bin/env bash
cd /home/webuser
source activate mlsql-3.5
export AK=${OSS_AK}
export AKS=${OSS_AKS}
pymlsql oss-download --bucket-name mlsql-release-repo --source ${MLSQL_TAR} --target ${MLSQL_TAR}
tar xf ${MLSQL_TAR}
EOF
pymlsql exec-shell --instance-id ${instance_id} \
--script-file ${SCRIPT_FILE} \
--execute-user webuser
if [[ ! -z "${MLSQL_THIRD_PARTY_JARS}" ]];then
echo "copy ${MLSQL_THIRD_PARTY_JARS} to master"
pymlsql copy-from-local --instance-id ${instance_id} --execute-user root \
--source ${MLSQL_THIRD_PARTY_JARS} \
--target /home/webuser/third-party-jars
fi
echo "copy main-jar and third-party-jars to slave"
cat << EOF > ${SCRIPT_FILE}
#!/usr/bin/env bash
source activate mlsql-3.5
cd /home/webuser
export AK=${AK}
export AKS=${AKS}
export MLSQL_KEY_PARE_NAME=${MLSQL_KEY_PARE_NAME}
export MLSQL_JAR_PATH=/home/webuser/${MLSQL_NAME}
pids=""
while read LINE
do
if [[ -z "\$LINE" ]];then
echo "\$LINE is empty skip."
else
export slave_instance_id=\$LINE
./copy-main-jar-to-slaves.sh &
pids[\${page}]=\$!
let "count+=1"
fi
done <<< "\$(cat mlsql.slaves)"
FAIL_NUM=0
for job in \${pids[*]};
do
echo \$job
wait \$job || let "FAIL_NUM+=1"
done
echo "copy FAILs: \${FAIL_NUM}"
EOF
pymlsql exec-shell --instance-id ${instance_id} \
--script-file ${SCRIPT_FILE} \
--execute-user webuser
echo "submit MLSQL"
cat << EOF > ${SCRIPT_FILE}
#!/usr/bin/env bash
source activate mlsql-3.5
cd /home/webuser
cd ${MLSQL_NAME}
export SPARK_HOME=/home/webuser/apps/spark-2.3
export MLSQL_HOME=\`pwd\`
JARS=\$(echo \${MLSQL_HOME}/libs/*.jar | tr ' ' ',')
if [ -d "/home/webuser/third-party-jars" ]; then
JARS=\${JARS},\$(echo /home/webuser/third-party-jars/*.jar | tr ' ' ',')
fi
MAIN_JAR=\$(ls \${MLSQL_HOME}/libs|grep 'streamingpro-mlsql')
echo \$JARS
echo \${MAIN_JAR}
cd \$SPARK_HOME
nohup ./bin/spark-submit --class streaming.core.StreamingApp \
--jars \${JARS} \
--master spark://${inter_ip}:7077 \
--deploy-mode client \
--name mlsql \
--conf "spark.kryoserializer.buffer=256k" \
--conf "spark.kryoserializer.buffer.max=1024m" \
--conf "spark.serializer=org.apache.spark.serializer.KryoSerializer" \
--conf "spark.scheduler.mode=FAIR" \
--conf "spark.executor.extraClassPath=\${SPARK_HOME}/conf/:\${SPARK_HOME}/jars/*:/home/webuser/\${MAIN_JAR}" \
\${MLSQL_HOME}/libs/\${MAIN_JAR} \
-streaming.name mlsql \
-streaming.platform spark \
-streaming.ps.cluster.enable true \
-streaming.rest true \
-streaming.driver.port 9003 \
-streaming.spark.service true \
-streaming.thrift false \
-streaming.enableHiveSupport false > mlsql.log 2>&1 &
EOF
pymlsql exec-shell --instance-id ${instance_id} \
--script-file ${SCRIPT_FILE} \
--execute-user webuser
cat << EOF > cluster.info
#!/usr/bin/env bash
cluster ui: http://${public_ip}:8080
spark ui: http://${public_ip}:4040
mlsql ui/api: http://${public_ip}:9003
instance ids are stored in mlsql.master/mlsql.slaves
EOF
cat cluster.info