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 2
system sh/cfg.sh -n dnode2 -c walLevel -v 2
system sh/cfg.sh -n dnode3 -c walLevel -v 2
system sh/cfg.sh -n dnode4 -c walLevel -v 2

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 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

$totalTableNum = 40
system sh/cfg.sh -n dnode1 -c maxtablesPerVnode -v $totalTableNum
system sh/cfg.sh -n dnode2 -c maxtablesPerVnode -v $totalTableNum
system sh/cfg.sh -n dnode3 -c maxtablesPerVnode -v $totalTableNum
system sh/cfg.sh -n dnode4 -c maxtablesPerVnode -v $totalTableNum

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 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
system sh/exec_tarbitrator.sh -s start

print ============== step1: start dnode1, only deploy mnode
system sh/exec.sh -n dnode1 -s start
sleep 3000
sql connect

print ============== step2: start dnode2/dnode3/dnode4 and add into cluster , then create database with replica 3, and create table, insert data
system sh/exec.sh -n dnode2 -s start
system sh/exec.sh -n dnode3 -s start
system sh/exec.sh -n dnode4 -s start
sql create dnode $hostname2
sql create dnode $hostname3
sql create dnode $hostname4
sleep 3000

$totalTableNum = 20
$sleepTimer = 3000

$maxTables = $totalTableNum * 2 
$db = db
print create database $db replica 3 maxTables $maxTables
sql create database $db replica 3 maxTables $maxTables
sql use $db

# create table , insert data
$stb = stb
sql create table $stb (ts timestamp, c1 int) tags(t1 int)
$rowNum = 500
$tblNum = 10
$totalRows = 0
$tsStart = 1420041600000
$tsEnd = 0

$i = 0
while $i < $tblNum
  $tb = tb . $i
  sql create table $tb using $stb tags( $i )
 
  $x = 0
  $ts = $tsStart + $x
  sql insert into $tb values ( $ts + 0a , $x ) ( $ts + 1a , $x ) ( $ts + 2a , $x ) ( $ts + 3a , $x ) ( $ts + 4a , $x ) ( $ts + 5a , $x ) ( $ts + 6a , $x ) ( $ts + 7a , $x ) ( $ts + 8a , $x ) ( $ts + 9a , $x )
  $x = $x + 10

  $totalRows = $totalRows + $x
  print       info: inserted $x rows into $tb and totalRows: $totalRows
  $i = $i + 1
endw 
$tsEnd = $tsStart + $totalRows / $tblNum

sql select count(*) from $stb
print data00 $data00
if $data00 != $totalRows then
	return -1
endi

print ============== step3: stop dnode4 
system sh/exec.sh -n dnode4 -s stop -x SIGINT
$cnt = 0
wait_dnode4_offline_0:
$cnt = $cnt + 1
if $cnt == 10 then
  return -1
endi
sql show dnodes
if $rows != 4 then
  sleep 2000
  goto wait_dnode4_offline_0
endi
print $data0_1  $data1_1  $data2_1  $data3_1  $data4_1
print $data0_2  $data1_2  $data2_2  $data3_2  $data4_2
print $data0_3  $data1_3  $data2_3  $data3_3  $data4_3
print $data0_4  $data1_4  $data2_4  $data3_4  $data4_4
$dnode1Status = $data4_1  
$dnode2Status = $data4_2
$dnode3Status = $data4_3  
$dnode4Status = $data4_4 

if $dnode4Status != offline then
  sleep 2000
  goto wait_dnode4_offline_0
endi

$cnt = 0
wait_dnode4_vgroup_offline:
$cnt = $cnt + 1
if $cnt == 10 then
  return -1
endi
sql show vgroups
if $rows != 1 then
  sleep 2000
  goto wait_dnode4_vgroup_offline
endi
print show vgroups:
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
$dnode4Vtatus = $data6_2
$dnode3Vtatus = $data9_2

if $dnode4Vtatus != offline then
  sleep 2000
  goto wait_dnode4_vgroup_offline
endi
if $dnode3Vtatus != master then
  sleep 2000
  goto wait_dnode4_vgroup_offline
endi

print ============== step4: drop some tables
sql drop table tb0
sql drop table tb9
sql drop table tb1
sql drop table tb8

$totalRows = $totalRows - 40
sql select count(*) from $stb
print data00 $data00
if $data00 != $totalRows then
	return -1
endi

print ============== step5: restart dnode4, waiting sync end
system sh/exec.sh -n dnode4 -s start

$cnt = 0
wait_dnode4_ready:
$cnt = $cnt + 1
if $cnt == 10 then
  return -1
endi
sql show dnodes
if $rows != 4 then
  sleep 2000
  goto wait_dnode4_ready
endi
print $data0_1  $data1_1  $data2_1  $data3_1  $data4_1
print $data0_2  $data1_2  $data2_2  $data3_2  $data4_2
print $data0_3  $data1_3  $data2_3  $data3_3  $data4_3
print $data0_4  $data1_4  $data2_4  $data3_4  $data4_4
$dnode1Status = $data4_1  
$dnode2Status = $data4_2
$dnode3Status = $data4_3  
$dnode4Status = $data4_4 

if $dnode4Status != ready then
  sleep 2000
  goto wait_dnode4_ready
endi

$cnt = 0
wait_dnode4_vgroup_slave:
$cnt = $cnt + 1
if $cnt == 10 then
  return -1
endi
sql show vgroups
if $rows != 1 then
  sleep 2000
  goto wait_dnode4_vgroup_slave
endi
print show vgroups:
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
$dnode4Vtatus = $data6_2
$dnode3Vtatus = $data9_2

if $dnode4Vtatus != slave then
  sleep 2000
  goto wait_dnode4_vgroup_slave
endi

print ============== step6: stop dnode2/dnode3 and remove their data dir, and restart dnode2/dnode3
system sh/exec.sh -n dnode2 -s stop -x SIGINT
system sh/exec.sh -n dnode3 -s stop -x SIGINT
sleep 1000
system rm -rf ../../../sim/dnode2/data
system rm -rf ../../../sim/dnode3/data

system sh/exec.sh -n dnode2 -s start
system sh/exec.sh -n dnode3 -s start

$cnt = 0
wait_dnode4_vgroup_master:
$cnt = $cnt + 1
if $cnt == 10 then
  return -1
endi
sql show vgroups
if $rows != 1 then
  sleep 2000
  goto wait_dnode4_vgroup_master
endi
print show vgroups:
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
$dnode4Vtatus = $data6_2
$dnode3Vtatus = $data9_2

if $dnode4Vtatus != master then
  sleep 2000
  goto wait_dnode4_vgroup_master
endi

sql select count(*) from $stb
print data00 $data00
if $data00 != $totalRows then
	return -1
endi


print ============== step7: stop dnode4 and create some new tables
system sh/exec.sh -n dnode4 -s stop -x SIGINT

$ts = $tsStart
sql insert into tb10 using stb tags(10) values ( $ts + 0a , $x ) ( $ts + 1a , $x ) ( $ts + 2a , $x ) ( $ts + 3a , $x ) ( $ts + 4a , $x ) ( $ts + 5a , $x ) ( $ts + 6a , $x ) ( $ts + 7a , $x ) ( $ts + 8a , $x ) ( $ts + 9a , $x )
sql insert into tb11 using stb tags(11) values ( $ts + 0a , $x ) ( $ts + 1a , $x ) ( $ts + 2a , $x ) ( $ts + 3a , $x ) ( $ts + 4a , $x ) ( $ts + 5a , $x ) ( $ts + 6a , $x ) ( $ts + 7a , $x ) ( $ts + 8a , $x ) ( $ts + 9a , $x )
sql insert into tb12 using stb tags(12) values ( $ts + 0a , $x ) ( $ts + 1a , $x ) ( $ts + 2a , $x ) ( $ts + 3a , $x ) ( $ts + 4a , $x ) ( $ts + 5a , $x ) ( $ts + 6a , $x ) ( $ts + 7a , $x ) ( $ts + 8a , $x ) ( $ts + 9a , $x )
sql insert into tb13 using stb tags(13) values ( $ts + 0a , $x ) ( $ts + 1a , $x ) ( $ts + 2a , $x ) ( $ts + 3a , $x ) ( $ts + 4a , $x ) ( $ts + 5a , $x ) ( $ts + 6a , $x ) ( $ts + 7a , $x ) ( $ts + 8a , $x ) ( $ts + 9a , $x )
  
$totalRows = $totalRows + 40
sql select count(*) from $stb
print data00 $data00
if $data00 != $totalRows then
	return -1
endi

print ============== step8: restart dnode4, waiting sync end
system sh/exec.sh -n dnode4 -s start

$cnt = 0
wait_dnode4_ready_2:
$cnt = $cnt + 1
if $cnt == 10 then
  return -1
endi
sql show dnodes
if $rows != 4 then
  sleep 2000
  goto wait_dnode4_ready_2
endi
print $data0_1  $data1_1  $data2_1  $data3_1  $data4_1
print $data0_2  $data1_2  $data2_2  $data3_2  $data4_2
print $data0_3  $data1_3  $data2_3  $data3_3  $data4_3
print $data0_4  $data1_4  $data2_4  $data3_4  $data4_4
$dnode1Status = $data4_1  
$dnode2Status = $data4_2
$dnode3Status = $data4_3  
$dnode4Status = $data4_4 

if $dnode4Status != ready then
  sleep 2000
  goto wait_dnode4_ready_2
endi

$cnt = 0
wait_dnode4_vgroup_slave_2:
$cnt = $cnt + 1
if $cnt == 10 then
  return -1
endi
sql show vgroups
if $rows != 1 then
  sleep 2000
  goto wait_dnode4_vgroup_slave_2
endi
print show vgroups:
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
$dnode4Vtatus = $data6_2
$dnode3Vtatus = $data9_2

if $dnode4Vtatus != slave then
  sleep 2000
  goto wait_dnode4_vgroup_slave_2
endi

print ============== step9: stop dnode2/dnode3 and remove their data dir, and restart dnode2/dnode3
system sh/exec.sh -n dnode2 -s stop -x SIGINT
system sh/exec.sh -n dnode3 -s stop -x SIGINT
sleep 1000
system rm -rf ../../../sim/dnode2/data
system rm -rf ../../../sim/dnode3/data

system sh/exec.sh -n dnode2 -s start
system sh/exec.sh -n dnode3 -s start

$cnt = 0
wait_dnode4_vgroup_master_2:
$cnt = $cnt + 1
if $cnt == 10 then
  return -1
endi
sql show vgroups
if $rows != 1 then
  sleep 2000
  goto wait_dnode4_vgroup_master_2
endi
print show vgroups:
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
$dnode4Vtatus = $data6_2
$dnode3Vtatus = $data9_2

if $dnode4Vtatus != master then
  sleep 2000
  goto wait_dnode4_vgroup_master_2
endi

sql select count(*) from $stb
print data00 $data00
if $data00 != $totalRows then
	return -1
endi


print ============== step10: stop dnode4 and alter table column
system sh/exec.sh -n dnode4 -s stop -x SIGINT

sql alter table stb add column c2 int
sql alter table stb add column c3 int
sql alter table stb add column c4 int
sql alter table stb drop column c1

sql alter table stb add tag t2 int
sql alter table stb add tag t3 int
sql_error alter table stb drop tag t1

$ts = $tsEnd + 10000
sql insert into tb2 values ( $ts + 0a , $x , $x , $x ) ( $ts + 1a , $x , $x , $x ) ( $ts + 2a , $x , $x , $x ) ( $ts + 3a , $x , $x , $x ) ( $ts + 4a , $x , $x , $x ) ( $ts + 5a , $x , $x , $x ) ( $ts + 6a , $x , $x , $x ) ( $ts + 7a , $x , $x , $x ) ( $ts + 8a , $x , $x , $x ) ( $ts + 9a , $x , $x , $x )
sql insert into tb3 values ( $ts + 0a , $x , $x , $x ) ( $ts + 1a , $x , $x , $x ) ( $ts + 2a , $x , $x , $x ) ( $ts + 3a , $x , $x , $x ) ( $ts + 4a , $x , $x , $x ) ( $ts + 5a , $x , $x , $x ) ( $ts + 6a , $x , $x , $x ) ( $ts + 7a , $x , $x , $x ) ( $ts + 8a , $x , $x , $x ) ( $ts + 9a , $x , $x , $x )
sql insert into tb4 values ( $ts + 0a , $x , $x , $x ) ( $ts + 1a , $x , $x , $x ) ( $ts + 2a , $x , $x , $x ) ( $ts + 3a , $x , $x , $x ) ( $ts + 4a , $x , $x , $x ) ( $ts + 5a , $x , $x , $x ) ( $ts + 6a , $x , $x , $x ) ( $ts + 7a , $x , $x , $x ) ( $ts + 8a , $x , $x , $x ) ( $ts + 9a , $x , $x , $x )
sql insert into tb5 values ( $ts + 0a , $x , $x , $x ) ( $ts + 1a , $x , $x , $x ) ( $ts + 2a , $x , $x , $x ) ( $ts + 3a , $x , $x , $x ) ( $ts + 4a , $x , $x , $x ) ( $ts + 5a , $x , $x , $x ) ( $ts + 6a , $x , $x , $x ) ( $ts + 7a , $x , $x , $x ) ( $ts + 8a , $x , $x , $x ) ( $ts + 9a , $x , $x , $x )


$ts = $tsStart
sql insert into tb14 using stb tags(14, 14, 14) values ( $ts + 0a , $x , $x , $x ) ( $ts + 1a , $x , $x , $x ) ( $ts + 2a , $x , $x , $x ) ( $ts + 3a , $x , $x , $x ) ( $ts + 4a , $x , $x , $x ) ( $ts + 5a , $x , $x , $x ) ( $ts + 6a , $x , $x , $x ) ( $ts + 7a , $x , $x , $x ) ( $ts + 8a , $x , $x , $x ) ( $ts + 9a , $x , $x , $x )  
sql insert into tb15 using stb tags(15, 15, 15) values ( $ts + 0a , $x , $x , $x ) ( $ts + 1a , $x , $x , $x ) ( $ts + 2a , $x , $x , $x ) ( $ts + 3a , $x , $x , $x ) ( $ts + 4a , $x , $x , $x ) ( $ts + 5a , $x , $x , $x ) ( $ts + 6a , $x , $x , $x ) ( $ts + 7a , $x , $x , $x ) ( $ts + 8a , $x , $x , $x ) ( $ts + 9a , $x , $x , $x )  
sql insert into tb16 using stb tags(16, 16, 16) values ( $ts + 0a , $x , $x , $x ) ( $ts + 1a , $x , $x , $x ) ( $ts + 2a , $x , $x , $x ) ( $ts + 3a , $x , $x , $x ) ( $ts + 4a , $x , $x , $x ) ( $ts + 5a , $x , $x , $x ) ( $ts + 6a , $x , $x , $x ) ( $ts + 7a , $x , $x , $x ) ( $ts + 8a , $x , $x , $x ) ( $ts + 9a , $x , $x , $x )  
sql insert into tb17 using stb tags(17, 17, 17) values ( $ts + 0a , $x , $x , $x ) ( $ts + 1a , $x , $x , $x ) ( $ts + 2a , $x , $x , $x ) ( $ts + 3a , $x , $x , $x ) ( $ts + 4a , $x , $x , $x ) ( $ts + 5a , $x , $x , $x ) ( $ts + 6a , $x , $x , $x ) ( $ts + 7a , $x , $x , $x ) ( $ts + 8a , $x , $x , $x ) ( $ts + 9a , $x , $x , $x )  
  
$totalRows = $totalRows + 80
sql select count(*) from $stb
print data00 $data00
if $data00 != $totalRows then
	return -1
endi

print ============== step11: restart dnode4, waiting sync end
system sh/exec.sh -n dnode4 -s start

$cnt = 0
wait_dnode4_ready_3:
$cnt = $cnt + 1
if $cnt == 10 then
  return -1
endi
sql show dnodes
if $rows != 4 then
  sleep 2000
  goto wait_dnode4_ready_3
endi
print $data0_1  $data1_1  $data2_1  $data3_1  $data4_1
print $data0_2  $data1_2  $data2_2  $data3_2  $data4_2
print $data0_3  $data1_3  $data2_3  $data3_3  $data4_3
print $data0_4  $data1_4  $data2_4  $data3_4  $data4_4
$dnode1Status = $data4_1  
$dnode2Status = $data4_2
$dnode3Status = $data4_3  
$dnode4Status = $data4_4 

if $dnode4Status != ready then
  sleep 2000
  goto wait_dnode4_ready_3
endi

$cnt = 0
wait_dnode4_vgroup_slave_3:
$cnt = $cnt + 1
if $cnt == 10 then
  return -1
endi
sql show vgroups
if $rows != 1 then
  sleep 2000
  goto wait_dnode4_vgroup_slave_3
endi
print show vgroups:
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
$dnode4Vtatus = $data6_2
$dnode3Vtatus = $data9_2

if $dnode4Vtatus != slave then
  sleep 2000
  goto wait_dnode4_vgroup_slave_3
endi

print ============== step12: stop dnode2/dnode3 and remove their data dir, and restart dnode2/dnode3
system sh/exec.sh -n dnode2 -s stop -x SIGINT
system sh/exec.sh -n dnode3 -s stop -x SIGINT
sleep 1000
system rm -rf ../../../sim/dnode2/data
system rm -rf ../../../sim/dnode3/data

system sh/exec.sh -n dnode2 -s start
system sh/exec.sh -n dnode3 -s start

$cnt = 0
wait_dnode4_vgroup_master_3:
$cnt = $cnt + 1
if $cnt == 10 then
  return -1
endi
sql show vgroups
if $rows != 1 then
  sleep 2000
  goto wait_dnode4_vgroup_master_3
endi
print show vgroups:
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
$dnode4Vtatus = $data6_2
$dnode3Vtatus = $data9_2

if $dnode4Vtatus != master then
  sleep 2000
  goto wait_dnode4_vgroup_master_3
endi

sql select count(*) from $stb
print data00 $data00
if $data00 != $totalRows then
	return -1
endi
