-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
1917 lines (1819 loc) · 58.5 KB
/
run.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/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 -e
PID=$$
ROOT=`pwd`
LOCAL_HOSTNAME=`hostname -f`
export REPORT_DIR="$ROOT/test_report"
export DSN_ROOT=$ROOT/DSN_ROOT
export THIRDPARTY_ROOT=$ROOT/thirdparty
export LD_LIBRARY_PATH=$DSN_ROOT/lib:$THIRDPARTY_ROOT/output/lib:$LD_LIBRARY_PATH
function usage()
{
echo "usage: run.sh <command> [<args>]"
echo
echo "Command list:"
echo " help print the help info"
echo " build build the system"
echo
echo " start_zk start local single zookeeper server"
echo " stop_zk stop local zookeeper server"
echo " clear_zk stop local zookeeper server and clear data"
echo
echo " start_onebox start pegasus onebox"
echo " stop_onebox stop pegasus onebox"
echo " list_onebox list pegasus onebox"
echo " clear_onebox clear pegasus onebox"
echo
echo " start_onebox_instance start pegasus onebox instance"
echo " stop_onebox_instance stop pegasus onebox instance"
echo " restart_onebox_instance restart pegasus onebox instance"
echo
echo " start_kill_test start pegasus kill test"
echo " stop_kill_test stop pegasus kill test"
echo " list_kill_test list pegasus kill test"
echo " clear_kill_test clear pegasus kill test"
echo
echo " bench run benchmark test"
echo " shell run pegasus shell"
echo " migrate_node migrate primary replicas out of specified node"
echo " downgrade_node downgrade replicas to inactive on specified node"
echo
echo " test run unit test"
echo
echo " pack_server generate pegasus server package for deploy with minos"
echo " pack_client generate pegasus client package"
echo " pack_tools generate pegasus tools package for shell and benchmark test"
echo
echo " bump_version change the version of the project"
echo "Command 'run.sh <command> -h' will print help for subcommands."
}
#####################
## build
#####################
function usage_build()
{
echo "Options for subcommand 'build':"
echo " -h|--help print the help info"
echo " -t|--type build type: debug|release, default is release"
echo " -c|--clear clear pegasus before building, not clear thirdparty"
echo " --clear_thirdparty clear thirdparty/pegasus before building"
echo " --compiler specify c and cxx compiler, sperated by ','"
echo " e.g., \"gcc,g++\" or \"clang-3.9,clang++-3.9\""
echo " default is \"gcc,g++\""
echo " -j|--jobs <num> the number of jobs to run simultaneously, default 8"
echo " --enable_gcov generate gcov code coverage report, default no"
echo " -v|--verbose build in verbose mode, default no"
echo " --disable_gperf build without gperftools, this flag is mainly used"
echo " to enable valgrind memcheck, default no"
echo " --use_jemalloc build with jemalloc"
echo " --sanitizer <type> build with sanitizer to check potential problems,
type: address|leak|thread|undefined"
echo " --skip_thirdparty whether to skip building thirdparties, default no"
echo " --enable_rocksdb_portable build a portable rocksdb binary"
echo " --test whether to build test binaries"
}
function exit_if_fail() {
if [ $1 != 0 ]; then
exit $1
fi
}
function run_build()
{
# Note(jiashuo1): No "memory" check mode, because MemorySanitizer is only available in Clang for Linux x86_64 targets
# # https://www.jetbrains.com/help/clion/google-sanitizers.html
SANITIZERS=("address" "leak" "thread" "undefined")
C_COMPILER="gcc"
CXX_COMPILER="g++"
BUILD_TYPE="release"
CLEAR=NO
CLEAR_THIRDPARTY=NO
JOB_NUM=8
ENABLE_GCOV=NO
RUN_VERBOSE=NO
ENABLE_GPERF=ON
SKIP_THIRDPARTY=NO
SANITIZER=""
ROCKSDB_PORTABLE=OFF
USE_JEMALLOC=OFF
BUILD_TEST=OFF
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_build
exit 0
;;
-t|--type)
BUILD_TYPE="$2"
shift
;;
-c|--clear)
CLEAR=YES
;;
--clear_thirdparty)
CLEAR_THIRDPARTY=YES
;;
--compiler)
C_COMPILER=`echo $2 | awk -F',' '{print $1}'`
CXX_COMPILER=`echo $2 | awk -F',' '{print $2}'`
if [ "x"$C_COMPILER == "x" -o "x"$CXX_COMPILER == "x" ]; then
echo "ERROR: invalid compiler option: $2"
echo
usage_build
exit 1
fi
shift
;;
-j|--jobs)
JOB_NUM="$2"
shift
;;
--enable_gcov)
ENABLE_GCOV=YES
;;
--sanitizer)
IS_SANITIZERS=`echo ${SANITIZERS[@]} | grep -w $2`
if [[ -z ${IS_SANITIZERS} ]]; then
echo "ERROR: unknown sanitizer type \"$2\""
usage_build
exit 1
fi
SANITIZER="$2"
shift
;;
-v|--verbose)
RUN_VERBOSE=YES
;;
--disable_gperf)
ENABLE_GPERF=OFF
;;
--skip_thirdparty)
SKIP_THIRDPARTY=YES
;;
--enable_rocksdb_portable)
ROCKSDB_PORTABLE=ON
;;
--use_jemalloc)
ENABLE_GPERF=OFF
USE_JEMALLOC=ON
;;
--test)
BUILD_TEST=ON
;;
*)
echo "ERROR: unknown option \"$key\""
echo
usage_build
exit 1
;;
esac
shift
done
if [ "$BUILD_TYPE" != "debug" -a "$BUILD_TYPE" != "release" ]; then
echo "ERROR: invalid build type \"$BUILD_TYPE\""
echo
usage_build
exit 1
fi
CMAKE_OPTIONS="-DCMAKE_C_COMPILER=${C_COMPILER}
-DCMAKE_CXX_COMPILER=${CXX_COMPILER}
-DUSE_JEMALLOC=${USE_JEMALLOC}
-DCMAKE_BUILD_TYPE=${BUILD_TYPE}
-DENABLE_GCOV=${ENABLE_GCOV}
-DENABLE_GPERF=${ENABLE_GPERF}
-DBoost_NO_BOOST_CMAKE=ON
-DBOOST_ROOT=${THIRDPARTY_ROOT}/output
-DBoost_NO_SYSTEM_PATHS=ON"
if [ "$(uname)" == "Darwin" ]; then
CMAKE_OPTIONS="${CMAKE_OPTIONS} -DMACOS_OPENSSL_ROOT_DIR=/usr/local/opt/openssl"
fi
if [ ! -z "${SANITIZER}" ]; then
CMAKE_OPTIONS="${CMAKE_OPTIONS} -DSANITIZER=${SANITIZER}"
fi
MAKE_OPTIONS="-j$JOB_NUM"
if [ "$RUN_VERBOSE" == "YES" ]; then
MAKE_OPTIONS="${MAKE_OPTIONS} VERBOSE=1"
fi
echo "Build start time: `date`"
start_time=`date +%s`
if [[ ${SKIP_THIRDPARTY} == "YES" ]]; then
echo "Skip building third-parties..."
else
cd ${THIRDPARTY_ROOT}
if [[ "$CLEAR_THIRDPARTY" == "YES" ]]; then
echo "Clear third-parties..."
rm -rf build
rm -rf output
fi
echo "Start building third-parties..."
mkdir -p build
pushd build
CMAKE_OPTIONS="${CMAKE_OPTIONS} -DROCKSDB_PORTABLE=${ROCKSDB_PORTABLE}"
cmake .. ${CMAKE_OPTIONS}
make -j$JOB_NUM
exit_if_fail $?
popd
cd ..
fi
echo "INFO: start build Pegasus..."
BUILD_DIR="${ROOT}/src/${BUILD_TYPE}_${SANITIZER}_builder"
if [ "$CLEAR" == "YES" ]; then
echo "Clear $BUILD_DIR ..."
rm -rf $BUILD_DIR
fi
pushd ${ROOT}
echo "Gen thrift"
# TODO(yingchun): should be optimized
python3 $ROOT/scripts/compile_thrift.py
sh ${ROOT}/scripts/recompile_thrift.sh
mkdir -p ${DSN_ROOT}
if [ ! -d "$BUILD_DIR" ]; then
mkdir -p $BUILD_DIR
echo "Running cmake Pegasus..."
pushd $BUILD_DIR
CMAKE_OPTIONS="${CMAKE_OPTIONS} -DBUILD_TEST=${BUILD_TEST}"
cmake ../.. -DCMAKE_INSTALL_PREFIX=$BUILD_DIR/output $CMAKE_OPTIONS
exit_if_fail $?
fi
echo "Gen git_commit.h ..."
pushd "$ROOT/src"
PEGASUS_GIT_COMMIT="non-git-repo"
if git rev-parse HEAD; then # this is a git repo
PEGASUS_GIT_COMMIT=$(git rev-parse HEAD)
fi
echo "PEGASUS_GIT_COMMIT=${PEGASUS_GIT_COMMIT}"
GIT_COMMIT_FILE=$ROOT/src/include/pegasus/git_commit.h
echo "Generating $GIT_COMMIT_FILE..."
echo "#pragma once" >$GIT_COMMIT_FILE
echo "#define PEGASUS_GIT_COMMIT \"$PEGASUS_GIT_COMMIT\"" >>$GIT_COMMIT_FILE
# rebuild link
rm -f ${ROOT}/src/builder
ln -s ${BUILD_DIR} ${ROOT}/src/builder
echo "[$(date)] Building Pegasus ..."
pushd $BUILD_DIR
make install $MAKE_OPTIONS
exit_if_fail $?
echo "Build finish time: `date`"
finish_time=`date +%s`
used_time=$((finish_time-start_time))
echo "Build elapsed time: $((used_time/60))m $((used_time%60))s"
}
#####################
## test
#####################
function usage_test()
{
echo "Options for subcommand 'test':"
echo " -h|--help print the help info"
echo " -m|--modules specify modules to test, split by ',',"
echo " e.g., \"pegasus_unit_test,dsn_runtime_tests,dsn_meta_state_tests\","
echo " if not set, then run all tests"
echo " -k|--keep_onebox whether keep the onebox after the test[default false]"
}
function run_test()
{
local test_modules=""
local clear_flags="1"
local enable_gcov="no"
local all_tests=(
backup_restore_test
base_api_test
base_test
bulk_load_test
detect_hotspot_test
dsn_aio_test
dsn_block_service_test
dsn.failure_detector.tests
dsn_http_test
dsn_meta_state_tests
dsn.meta.test
dsn_nfs_test
dsn_perf_counter_test
dsn_replica_backup_test
dsn_replica_bulk_load_test
dsn_replica_dup_test
dsn_replica_split_test
dsn.replica.test
dsn_replication_common_test
dsn.replication.simple_kv
dsn.rep_tests.simple_kv
dsn_runtime_tests
dsn_utils_tests
dsn.zookeeper.tests
partition_split_test
pegasus_geo_test
pegasus_rproxy_test
pegasus_unit_test
recovery_test
restore_test
throttle_test
)
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_test
exit 0
;;
-m|--modules)
test_modules=$2
shift
;;
-k|--keep_onebox)
clear_flags=""
;;
--enable_gcov)
enable_gcov="yes"
;;
*)
echo "Error: unknown option \"$key\""
echo
usage_test
exit 1
;;
esac
shift
done
echo "Test start time: `date`"
start_time=`date +%s`
if [ ! -d "$REPORT_DIR" ]; then
mkdir -p $REPORT_DIR
fi
BUILD_DIR=$ROOT/src/builder
if [ "$test_modules" == "" ]; then
test_modules=$(IFS=,; echo "${all_tests[*]}")
fi
echo "test_modules=$test_modules"
# download bulk load test data
if [[ "$test_modules" =~ "bulk_load_test" && ! -d "$ROOT/src/test/function_test/bulk_load_test/pegasus-bulk-load-function-test-files" ]]; then
echo "Start to download files used for bulk load function test"
wget "https://github.com/XiaoMi/pegasus-common/releases/download/deps/pegasus-bulk-load-function-test-files.zip"
unzip "pegasus-bulk-load-function-test-files.zip" -d "$ROOT/src/test/function_test/bulk_load_test"
rm "pegasus-bulk-load-function-test-files.zip"
echo "Prepare files used for bulk load function test succeed"
fi
for module in `echo $test_modules | sed 's/,/ /g'`; do
echo "====================== run $module =========================="
# restart onebox when test pegasus
local need_onebox_tests=(
backup_restore_test
base_api_test
bulk_load_test
detect_hotspot_test
partition_split_test
pegasus_geo_test
pegasus_rproxy_test
recovery_test
restore_test
throttle_test
)
if [[ "${need_onebox_tests[@]}" =~ "${test_modules}" ]]; then
run_clear_onebox
m_count=3
if [ "${test_modules}" == "recovery_test" ]; then
m_count=1
opts="meta_state_service_type=meta_state_service_simple,distributed_lock_service_type=distributed_lock_service_simple"
fi
if [ "${test_modules}" == "backup_restore_test" ]; then
opts="cold_backup_disabled=false,cold_backup_checkpoint_reserve_minutes=0,cold_backup_root=onebox"
fi
if [ "${test_modules}" == "restore_test" ]; then
opts="cold_backup_disabled=false,cold_backup_checkpoint_reserve_minutes=0,cold_backup_root=mycluster"
fi
if ! run_start_onebox -m ${m_count} -w -c --opts ${opts}; then
echo "ERROR: unable to continue on testing because starting onebox failed"
exit 1
fi
# TODO(yingchun): remove it?
sed -i "s/@LOCAL_HOSTNAME@/${LOCAL_HOSTNAME}/g" $ROOT/src/builder/src/server/test/config.ini
else
run_stop_zk
run_start_zk
fi
pushd $ROOT/src/builder/bin/$module
REPORT_DIR=$REPORT_DIR TEST_BIN=$module ./run.sh
if [ $? != 0 ]; then
echo "run test \"$module\" in `pwd` failed"
exit 1
fi
# clear onebox if needed
if [[ "${need_onebox_tests[@]}" =~ "${test_modules}" ]]; then
if [ "$clear_flags" == "1" ]; then
run_clear_onebox
fi
fi
popd
done
echo "Test finish time: `date`"
finish_time=`date +%s`
used_time=$((finish_time-start_time))
echo "Test elapsed time: $((used_time/60))m $((used_time%60))s"
if [ "$enable_gcov" == "yes" ]; then
echo "Generating gcov report..."
cd $ROOT
mkdir -p "$ROOT/gcov_report"
echo "Running gcovr to produce HTML code coverage report."
$BUILD_DIR
gcovr --html --html-details -r $ROOT --object-directory=$BUILD_DIR \
-o $GCOV_DIR/index.html
if [ $? -ne 0 ]; then
exit 1
fi
fi
}
#####################
## start_zk
#####################
function usage_start_zk()
{
echo "Options for subcommand 'start_zk':"
echo " -h|--help print the help info"
echo " -d|--install_dir <dir>"
echo " zookeeper install directory,"
echo " if not set, then default is './.zk_install'"
echo " -p|--port <port> listen port of zookeeper, default is 22181"
}
function run_start_zk()
{
# first we check the environment that zk need: java and nc command
# check java
type java >/dev/null 2>&1 || { echo >&2 "start zk failed, need install jre..."; exit 1;}
# check nc command
type nc >/dev/null 2>&1 || { echo >&2 "start zk failed, need install netcat command..."; exit 1;}
INSTALL_DIR=`pwd`/.zk_install
PORT=22181
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_start_zk
exit 0
;;
-d|--install_dir)
INSTALL_DIR=$2
shift
;;
-p|--port)
PORT=$2
shift
;;
*)
echo "ERROR: unknown option \"$key\""
echo
usage_start_zk
exit 1
;;
esac
shift
done
INSTALL_DIR="$INSTALL_DIR" PORT="$PORT" $ROOT/scripts/start_zk.sh
}
#####################
## stop_zk
#####################
function usage_stop_zk()
{
echo "Options for subcommand 'stop_zk':"
echo " -h|--help print the help info"
echo " -d|--install_dir <dir>"
echo " zookeeper install directory,"
echo " if not set, then default is './.zk_install'"
}
function run_stop_zk()
{
INSTALL_DIR=`pwd`/.zk_install
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_stop_zk
exit 0
;;
-d|--install_dir)
INSTALL_DIR=$2
shift
;;
*)
echo "ERROR: unknown option \"$key\""
echo
usage_stop_zk
exit 1
;;
esac
shift
done
INSTALL_DIR="$INSTALL_DIR" $ROOT/scripts/stop_zk.sh
}
#####################
## clear_zk
#####################
function usage_clear_zk()
{
echo "Options for subcommand 'clear_zk':"
echo " -h|--help print the help info"
echo " -d|--install_dir <dir>"
echo " zookeeper install directory,"
echo " if not set, then default is './.zk_install'"
}
function run_clear_zk()
{
INSTALL_DIR=`pwd`/.zk_install
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_clear_zk
exit 0
;;
-d|--install_dir)
INSTALL_DIR=$2
shift
;;
*)
echo "ERROR: unknown option \"$key\""
echo
usage_clear__zk
exit 1
;;
esac
shift
done
INSTALL_DIR="$INSTALL_DIR" $ROOT/scripts/clear_zk.sh
}
#####################
## start_onebox
#####################
function usage_start_onebox()
{
echo "Options for subcommand 'start_onebox':"
echo " -h|--help print the help info"
echo " -m|--meta_count <num>"
echo " meta server count, default is 3"
echo " -r|--replica_count <num>"
echo " replica server count, default is 3"
echo " -c|--collector"
echo " start the collector server, default not start"
echo " -a|--app_name <str>"
echo " default app name, default is temp"
echo " -p|--partition_count <num>"
echo " default app partition count, default is 8"
echo " -w|--wait_healthy"
echo " wait cluster to become healthy, default not wait"
echo " -s|--server_path <str>"
echo " server binary path, default is ${DSN_ROOT}/bin/pegasus_server"
echo " --config_path"
echo " specify the config template path, default is ./src/server/config.min.ini in non-production env"
echo " ./src/server/config.ini in production env"
echo " --use_product_config"
echo " use the product config template"
echo " --opts"
echo " update configs before start onebox, the configs are in the form of 'key1=value1,key2=value2'"
}
function run_start_onebox()
{
META_COUNT=3
REPLICA_COUNT=3
COLLECTOR_COUNT=0
APP_NAME=temp
PARTITION_COUNT=8
WAIT_HEALTHY=false
SERVER_PATH=${DSN_ROOT}/bin/pegasus_server
CONFIG_FILE=""
USE_PRODUCT_CONFIG=false
OPTS=""
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_start_onebox
exit 0
;;
-m|--meta_count)
META_COUNT="$2"
shift
;;
-r|--replica_count)
REPLICA_COUNT="$2"
shift
;;
-c|--collector)
COLLECTOR_COUNT=1
;;
-a|--app_name)
APP_NAME="$2"
shift
;;
-p|--partition_count)
PARTITION_COUNT="$2"
shift
;;
-w|--wait_healthy)
WAIT_HEALTHY=true
;;
-s|--server_path)
SERVER_PATH="$2"
shift
;;
--config_path)
CONFIG_FILE="$2"
shift
;;
--use_product_config)
USE_PRODUCT_CONFIG=true
;;
--opts)
OPTS="$2"
shift
;;
*)
echo "ERROR: unknown option \"$key\""
echo
usage_start_onebox
exit 1
;;
esac
shift
done
if [ ! -f ${SERVER_PATH}/pegasus_server ]; then
echo "ERROR: file ${SERVER_PATH}/pegasus_server not exist"
exit 1
fi
if ps -ef | grep '/pegasus_server config.ini' | grep -E -q 'app_list meta|app_list replica|app_list collector'; then
echo "ERROR: some onebox processes are running, start failed"
exit 1
fi
ln -s -f "${SERVER_PATH}/pegasus_server" "${ROOT}"
if ! run_start_zk; then
echo "ERROR: unable to setup onebox because zookeeper can not be started"
exit 1
fi
if [ $USE_PRODUCT_CONFIG == "true" ]; then
[ -z "${CONFIG_FILE}" ] && CONFIG_FILE=${ROOT}/src/server/config.ini
[ ! -f "${CONFIG_FILE}" ] && { echo "${CONFIG_FILE} is not exist"; exit 1; }
cp -f ${CONFIG_FILE} ${ROOT}/config-server.ini
sed -i 's/\<34601\>/@META_PORT@/' ${ROOT}/config-server.ini
sed -i 's/\<34801\>/@REPLICA_PORT@/' ${ROOT}/config-server.ini
sed -i 's/%{cluster.name}/onebox/g' ${ROOT}/config-server.ini
sed -i 's/%{network.interface}/eth0/g' ${ROOT}/config-server.ini
sed -i 's/%{email.address}//g' ${ROOT}/config-server.ini
sed -i 's/%{app.dir}/.\/data/g' ${ROOT}/config-server.ini
sed -i 's/%{slog.dir}//g' ${ROOT}/config-server.ini
sed -i 's/%{data.dirs}//g' ${ROOT}/config-server.ini
sed -i 's@%{home.dir}@'"$HOME"'@g' ${ROOT}/config-server.ini
for i in $(seq ${META_COUNT})
do
meta_port=$((34600+i))
if [ $i -eq 1 ]; then
meta_list="${LOCAL_HOSTNAME}:$meta_port"
else
meta_list="$meta_list,${LOCAL_HOSTNAME}:$meta_port"
fi
done
sed -i 's/%{meta.server.list}/'"$meta_list"'/g' ${ROOT}/config-server.ini
sed -i 's/%{zk.server.list}/'"${LOCAL_HOSTNAME}"':22181/g' ${ROOT}/config-server.ini
sed -i 's/app_name = .*$/app_name = '"$APP_NAME"'/' ${ROOT}/config-server.ini
sed -i 's/partition_count = .*$/partition_count = '"$PARTITION_COUNT"'/' ${ROOT}/config-server.ini
else
[ -z "${CONFIG_FILE}" ] && CONFIG_FILE=${ROOT}/src/server/config.min.ini
[ ! -f "${CONFIG_FILE}" ] && { echo "${CONFIG_FILE} is not exist"; exit 1; }
sed "s/@LOCAL_HOSTNAME@/${LOCAL_HOSTNAME}/g;s/@APP_NAME@/${APP_NAME}/g;s/@PARTITION_COUNT@/${PARTITION_COUNT}/g" \
${CONFIG_FILE} >${ROOT}/config-server.ini
fi
OPTS=`echo $OPTS | xargs`
config_kvs=(${OPTS//,/ })
for config_kv in ${config_kvs[@]}; do
config_kv=`echo $config_kv | xargs`
kv=(${config_kv//=/ })
if [ ! ${#kv[*]} -eq 2 ]; then
echo "Invalid --opts arguments!"
exit 1
fi
sed -i '/^\s*'"${kv[0]}"'/c '"${kv[0]}"' = '"${kv[1]}" ${ROOT}/config-server.ini
done
echo "starting server"
ld_library_path=${SERVER_PATH}:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$ld_library_path
mkdir -p onebox
cd onebox
for i in $(seq ${META_COUNT})
do
meta_port=$((34600+i))
prometheus_port=$((9091+i))
mkdir -p meta$i;
cd meta$i
ln -s -f ${SERVER_PATH}/pegasus_server pegasus_server
sed "s/@META_PORT@/$meta_port/;s/@REPLICA_PORT@/34800/;s/@PROMETHEUS_PORT@/$prometheus_port/" ${ROOT}/config-server.ini >config.ini
$PWD/pegasus_server config.ini -app_list meta &>result &
PID=$!
ps -ef | grep '/pegasus_server config.ini' | grep "\<$PID\>"
cd ..
done
for j in $(seq ${REPLICA_COUNT})
do
prometheus_port=$((9091+${META_COUNT}+j))
replica_port=$((34800+j))
mkdir -p replica$j
cd replica$j
ln -s -f ${SERVER_PATH}/pegasus_server pegasus_server
sed "s/@META_PORT@/34600/;s/@REPLICA_PORT@/$replica_port/;s/@PROMETHEUS_PORT@/$prometheus_port/" ${ROOT}/config-server.ini >config.ini
$PWD/pegasus_server config.ini -app_list replica &>result &
PID=$!
ps -ef | grep '/pegasus_server config.ini' | grep "\<$PID\>"
cd ..
done
if [ $COLLECTOR_COUNT -eq 1 ]
then
mkdir -p collector
cd collector
ln -s -f ${SERVER_PATH}/pegasus_server pegasus_server
sed "s/@META_PORT@/34600/;s/@REPLICA_PORT@/34800/;s/@PROMETHEUS_PORT@/9091/" ${ROOT}/config-server.ini >config.ini
$PWD/pegasus_server config.ini -app_list collector &>result &
PID=$!
ps -ef | grep '/pegasus_server config.ini' | grep "\<$PID\>"
cd ..
fi
if [ $WAIT_HEALTHY == "true" ]; then
cd $ROOT
echo "Wait cluster to become healthy..."
sleeped=0
while true; do
sleep 1
sleeped=$((sleeped+1))
echo "Sleeped for $sleeped seconds"
unhealthy_count=`echo "ls -d" | ./run.sh shell | awk 'f{ if(NF<7){f=0} else if($3!=$4){print} } / fully_healthy /{print;f=1}' | wc -l`
if [ $unhealthy_count -eq 1 ]; then
echo "Cluster becomes healthy."
break
fi
done
fi
}
#####################
## stop_onebox
#####################
function usage_stop_onebox()
{
echo "Options for subcommand 'stop_onebox':"
echo " -h|--help print the help info"
}
function run_stop_onebox()
{
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_stop_onebox
exit 0
;;
*)
echo "ERROR: unknown option \"$key\""
echo
usage_stop_onebox
exit 1
;;
esac
shift
done
ps -ef | grep '/pegasus_server config.ini' | grep -E 'app_list meta|app_list replica|app_list collector' | awk '{print $2}' | xargs kill &>/dev/null || true
}
#####################
## list_onebox
#####################
function usage_list_onebox()
{
echo "Options for subcommand 'list_onebox':"
echo " -h|--help print the help info"
}
function run_list_onebox()
{
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_list_onebox
exit 0
;;
*)
echo "ERROR: unknown option \"$key\""
echo
usage_list_onebox
exit 1
;;
esac
shift
done
ps -ef | grep '/pegasus_server config.ini' | grep -E 'app_list meta|app_list replica|app_list collector' | sort -k11
}
#####################
## clear_onebox
#####################
function usage_clear_onebox()
{
echo "Options for subcommand 'clear_onebox':"
echo " -h|--help print the help info"
}
function run_clear_onebox()
{
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_clear_onebox
exit 0
;;
*)
echo "ERROR: unknown option \"$key\""
echo
usage_clear_onebox
exit 1
;;
esac
shift
done
run_stop_onebox
run_clear_zk
rm -rf onebox *.log *.data config-*.ini &>/dev/null
sleep 1
}
#####################
## start_onebox_instance
#####################
function usage_start_onebox_instance()
{
echo "Options for subcommand 'start_onebox_instance':"
echo " -h|--help print the help info"
echo " -m|--meta_id <num> start meta server with id"
echo " -r|--replica_id <num> start replica server with id"
echo " -c|--collector start collector server"
}
function run_start_onebox_instance()
{
META_ID=0
REPLICA_ID=0
COLLECTOR_ID=0
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage_start_onebox_instance
exit 0
;;
-m|--meta_id)
META_ID="$2"
shift
;;
-r|--replica_id)
REPLICA_ID="$2"
shift
;;
-c|--collector)
COLLECTOR_ID=1
;;
*)
echo "ERROR: unknown option \"$key\""
echo
usage_start_onebox_instance
exit 1
;;
esac
shift
done
if [ $META_ID = "0" -a $REPLICA_ID = "0" -a $COLLECTOR_ID = "0" ]; then
echo "ERROR: no meta_id or replica_id or collector set"
exit 1
fi
if [ $META_ID != "0" ]; then
dir=onebox/meta$META_ID
if [ ! -d $dir ]; then
echo "ERROR: invalid meta_id"
exit 1
fi
if ps -ef | grep "/meta$META_ID/pegasus_server config.ini" | grep "app_list meta" ; then
echo "INFO: meta@$META_ID already running"
exit 1
fi
cd $dir
$PWD/pegasus_server config.ini -app_list meta &>result &
PID=$!
ps -ef | grep '/pegasus_server config.ini' | grep "\<$PID\>"
cd ..
echo "INFO: meta@$META_ID started"
fi
if [ $REPLICA_ID != "0" ]; then
dir=onebox/replica$REPLICA_ID
if [ ! -d $dir ]; then
echo "ERROR: invalid replica_id"
exit 1
fi
if ps -ef | grep "/replica$REPLICA_ID/pegasus_server config.ini" | grep "app_list replica" ; then
echo "INFO: replica@$REPLICA_ID already running"
exit 1
fi
cd $dir
$PWD/pegasus_server config.ini -app_list replica &>result &
PID=$!
ps -ef | grep '/pegasus_server config.ini' | grep "\<$PID\>"
cd ..
echo "INFO: replica@$REPLICA_ID started"
fi
if [ $COLLECTOR_ID != "0" ]; then
dir=onebox/collector
if [ ! -d $dir ]; then
echo "ERROR: collector dir $dir not exist"
exit 1
fi
if ps -ef | grep "/collector/pegasus_server config.ini" | grep "app_list collector" ; then
echo "INFO: collector already running"
exit 1
fi
cd $dir