# scp -r root@114.116.252.220:/home/ubuntu/clusterSimCase unique/.

#taos> show dnodes;
#   id   |           end_point            | vnodes | cores  |     status     |   role   |       create_time       |
#==================================================================================================================
#      1 | ubuntu-OptiPlex-7060:7100      |      0 |     12 | ready          | mnode    | 2020-07-22 06:25:31.677 |
#      2 | ubuntu-OptiPlex-7060:7200      |      4 |     12 | ready          | vnode    | 2020-07-22 06:25:34.795 |
#      3 | ubuntu-OptiPlex-7060:7300      |      4 |     12 | ready          | vnode    | 2020-07-22 06:25:34.803 |
#Query OK, 3 row(s) in set (0.002448s)
#taos> show vgroups;
#    vgId     |   tables    |   status   | onlineVnodes | dnode  |   vstatus   | dnode  |   vstatus   |
#======================================================================================================
#           2 |        1024 | ready      |            2 |      3 | master      |      2 | slave       |
#           3 |        1024 | ready      |            2 |      3 | master      |      2 | slave       |
#           4 |        1024 | ready      |            2 |      3 | master      |      2 | slave       |
#           5 |         718 | ready      |            2 |      3 | master      |      2 | slave       |
#Query OK, 4 row(s) in set (0.002749s)
#
#taos> show mnodes
#   -> ;
#   id   |           end_point            |      role      |       create_time       |
#=====================================================================================
#      1 | ubuntu-OptiPlex-7060:7100      | master         | 2020-07-22 06:25:31.677 |
#Query OK, 1 row(s) in set (0.002126s)


system sh/stop_dnodes.sh
system sh/deploy.sh -n dnode1 -i 1
system sh/deploy.sh -n dnode2 -i 2
system sh/deploy.sh -n dnode3 -i 3
system sh/deploy.sh -n dnode4 -i 4

system sh/cfg.sh -n dnode1 -c numOfMnodes -v 1
system sh/cfg.sh -n dnode2 -c numOfMnodes -v 1
system sh/cfg.sh -n dnode3 -c numOfMnodes -v 1
system sh/cfg.sh -n dnode4 -c numOfMnodes -v 1

system sh/cfg.sh -n dnode1 -c walLevel -v 1
system sh/cfg.sh -n dnode2 -c walLevel -v 1
system sh/cfg.sh -n dnode3 -c walLevel -v 1
system sh/cfg.sh -n dnode4 -c walLevel -v 1

system sh/cfg.sh -n dnode1 -c balanceInterval -v 10
system sh/cfg.sh -n dnode2 -c balanceInterval -v 10
system sh/cfg.sh -n dnode3 -c balanceInterval -v 10
system sh/cfg.sh -n dnode4 -c balanceInterval -v 10

system sh/cfg.sh -n dnode1 -c numOfTotalVnodes -v 256
system sh/cfg.sh -n dnode2 -c numOfTotalVnodes -v 256
system sh/cfg.sh -n dnode3 -c numOfTotalVnodes -v 256
system sh/cfg.sh -n dnode4 -c numOfTotalVnodes -v 256

#system sh/cfg.sh -n dnode1 -c maxVgroupsPerDb -v 1
#system sh/cfg.sh -n dnode2 -c maxVgroupsPerDb -v 1
#system sh/cfg.sh -n dnode3 -c maxVgroupsPerDb -v 1
#system sh/cfg.sh -n dnode4 -c maxVgroupsPerDb -v 1

#system sh/cfg.sh -n dnode1 -c mnodeEqualVnodeNum -v 256  
#system sh/cfg.sh -n dnode2 -c mnodeEqualVnodeNum -v 256  
#system sh/cfg.sh -n dnode3 -c mnodeEqualVnodeNum -v 256  
#system sh/cfg.sh -n dnode4 -c mnodeEqualVnodeNum -v 256

system sh/cfg.sh -n dnode1 -c role -v 1
system sh/cfg.sh -n dnode2 -c role -v 2
system sh/cfg.sh -n dnode3 -c role -v 2
system sh/cfg.sh -n dnode4 -c role -v 2

system sh/cfg.sh -n dnode1 -c maxtablesPerVnode -v 5000
system sh/cfg.sh -n dnode2 -c maxtablesPerVnode -v 5000
system sh/cfg.sh -n dnode3 -c maxtablesPerVnode -v 5000
system sh/cfg.sh -n dnode4 -c maxtablesPerVnode -v 5000

system sh/cfg.sh -n dnode1 -c arbitrator -v $arbitrator
system sh/cfg.sh -n dnode2 -c arbitrator -v $arbitrator
system sh/cfg.sh -n dnode3 -c arbitrator -v $arbitrator
system sh/cfg.sh -n dnode4 -c arbitrator -v $arbitrator

print ============== step0: start tarbitrator and dnode1
system sh/exec_tarbitrator.sh -s start
system sh/exec.sh -n dnode1 -s start

print ============== step2: start dnode2/dnode3
system sh/exec.sh -n dnode2 -s start
system sh/exec.sh -n dnode3 -s start
sleep 1000
sql connect

print ============== step1: add dnode2/dnode3 into cluster
sql create dnode $hostname2
sql create dnode $hostname3
sleep 3000
sleep 3000
print ============== step3: start back client-01.sim
#run_back unique/clusterSimCase/client-01.sim
#run_back unique/clusterSimCase/client-02.sim
#run_back unique/clusterSimCase/client-03.sim
#run_back unique/clusterSimCase/client-04.sim
#run_back unique/clusterSimCase/client-05.sim
#run_back unique/clusterSimCase/client-06.sim
#run_back unique/clusterSimCase/client-07.sim
run_back unique/clusterSimCase/client-08.sim
#run_back unique/clusterSimCase/client-01.sim
#run_back unique/clusterSimCase/client-01.sim
sleep 20000

$db = db
sql use $db

$loop_cnt = 0
loop_cluster_do:
print **** **** **** START loop cluster do (loop_cnt: $loop_cnt )**** **** **** ****

print ============== step4: stop dnode3
system sh/exec.sh -n dnode3 -s stop -x SIGINT

$checkCnt = 0
wait_vgroup_chang_0:
$checkCnt = $checkCnt + 1
if $loopCnt == 10 then
  return -1
endi
print show vgroups
sql show vgroups
#if $rows != 4 then
#  print rows: $rows
#  sleep 2000
#  goto wait_vgroup_chang_0
#endi
print $data0_1  $data1_1  $data2_1  $data3_1  $data4_1  $data5_1  $data6_1  $data7_1  $data8_1  $data9_1
print $data0_2  $data1_2  $data2_2  $data3_2  $data4_2  $data5_2  $data6_2  $data7_2  $data8_2  $data9_2
print $data0_3  $data1_3  $data2_3  $data3_3  $data4_3  $data5_3  $data6_3  $data7_3  $data8_3  $data9_3
$vg2Dnode3Status = $data5_2
$vg2Dnode2Status = $data7_2

if $vg2Dnode3Status != offline then
  sleep 2000
  goto wait_vgroup_chang_0
endi
if $vg2Dnode2Status != master then
  sleep 2000
  goto wait_vgroup_chang_0
endi


sleep 3000
print ============== step3: restart dnode3
system sh/exec.sh -n dnode3 -s start

$checkCnt = 0
wait_vgroup_chang_1:
$checkCnt = $checkCnt + 1
if $loopCnt == 10 then
  return -1
endi
print show vgroups
sql show vgroups
#if $rows != 4 then
#  print rows: $rows
#  sleep 2000
#  goto wait_vgroup_chang_1
#endi
print $data0_1  $data1_1  $data2_1  $data3_1  $data4_1  $data5_1  $data6_1  $data7_1  $data8_1  $data9_1
print $data0_2  $data1_2  $data2_2  $data3_2  $data4_2  $data5_2  $data6_2  $data7_2  $data8_2  $data9_2
print $data0_3  $data1_3  $data2_3  $data3_3  $data4_3  $data5_3  $data6_3  $data7_3  $data8_3  $data9_3
$vg2Dnode3Status = $data5_2
$vg2Dnode2Status = $data7_2

if $vg2Dnode3Status != slave then
  sleep 2000
  goto wait_vgroup_chang_1
endi
if $vg2Dnode2Status != master then
  sleep 2000
  goto wait_vgroup_chang_1
endi

sleep 3000
print ============== step4: stop dnode2
system sh/exec.sh -n dnode2 -s stop -x SIGINT

$checkCnt = 0
wait_vgroup_chang_2:
$checkCnt = $checkCnt + 1
if $loopCnt == 10 then
  return -1
endi
print show vgroups
sql show vgroups
#if $rows != 4 then
#  print rows: $rows
#  sleep 2000
#  goto wait_vgroup_chang_2
#endi
print $data0_1  $data1_1  $data2_1  $data3_1  $data4_1  $data5_1  $data6_1  $data7_1  $data8_1  $data9_1
print $data0_2  $data1_2  $data2_2  $data3_2  $data4_2  $data5_2  $data6_2  $data7_2  $data8_2  $data9_2
print $data0_3  $data1_3  $data2_3  $data3_3  $data4_3  $data5_3  $data6_3  $data7_3  $data8_3  $data9_3
$vg2Dnode3Status = $data5_2
$vg2Dnode2Status = $data7_2

if $vg2Dnode3Status != master then
  sleep 2000
  goto wait_vgroup_chang_2
endi
if $vg2Dnode2Status != offline then
  sleep 2000
  goto wait_vgroup_chang_2
endi


sleep 3000
print ============== step5: restart dnode2
system sh/exec.sh -n dnode2 -s start

$checkCnt = 0
wait_vgroup_chang_3:
$checkCnt = $checkCnt + 1
if $loopCnt == 10 then
  return -1
endi
print show vgroups
sql show vgroups
#if $rows != 4 then
#  print rows: $rows
#  sleep 2000
#  goto wait_vgroup_chang_3
#endi
print $data0_1  $data1_1  $data2_1  $data3_1  $data4_1  $data5_1  $data6_1  $data7_1  $data8_1  $data9_1
print $data0_2  $data1_2  $data2_2  $data3_2  $data4_2  $data5_2  $data6_2  $data7_2  $data8_2  $data9_2
print $data0_3  $data1_3  $data2_3  $data3_3  $data4_3  $data5_3  $data6_3  $data7_3  $data8_3  $data9_3
$vg2Dnode3Status = $data5_2
$vg2Dnode2Status = $data7_2

if $vg2Dnode2Status != slave then
  sleep 2000
  goto wait_vgroup_chang_3
endi
if $vg2Dnode3Status != master then
  sleep 2000
  goto wait_vgroup_chang_3
endi

sleep 3000
print **** **** **** (loop_cnt: $loop_cnt ) end, continue...... **** **** **** ****
$loop_cnt = $loop_cnt + 1
if $loop_cnt == 50 then
  return 0
endi
goto loop_cluster_do
