# Test case describe: dnode1/dnode2 include mnode and vnode roles
# step 1: start dnode1/dnode2, and added into cluster
# step 2: create db(repl = 2), table, insert data, 
# step 4: stop dnode1, remove its mnode dir, and copy mnode dir of dnode2 to dnode1
# step 5: restart dnode1, waiting sync end
# step 6: stop dnode2, reset query cache, and query

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 2
system sh/cfg.sh -n dnode2 -c numOfMnodes -v 2
#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 0
system sh/cfg.sh -n dnode2 -c role -v 0
#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 4
system sh/cfg.sh -n dnode2 -c maxtablesPerVnode -v 4
#system sh/cfg.sh -n dnode3 -c maxtablesPerVnode -v 4
#system sh/cfg.sh -n dnode4 -c maxtablesPerVnode -v 4
#system sh/cfg.sh -n dnode5 -c maxtablesPerVnode -v 4

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

print ============== step0: start tarbitrator
system sh/exec_tarbitrator.sh -s start

print ============== step1: start dnode1/dnode2 and add into cluster
system sh/exec.sh -n dnode1 -s start
system sh/exec.sh -n dnode2 -s start
sleep 1000
sql connect
sleep 1000
sql create dnode $hostname2
sleep 1000

print ============== step2: create database with replica 2, and create table, insert data
$totalTableNum = 10
$sleepTimer = 3000

$db = db
sql create database $db replica 2 cache 1
sql use $db

# create table , insert data
$stb = stb
sql create table $stb (ts timestamp, c1 double) tags(t1 int)
$rowNum = 1200
$tblNum = $totalTableNum
$totalRows = 0
$tsStart = 1577808000000  # 2020-01-01 00:00:00.000

$i = 0
while $i < $tblNum
  $tb = tb . $i
  sql create table $tb using $stb tags( $i )
 
  $x = 0
  while $x < $rowNum
    $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 ) ( $ts + 10a , $x ) ( $ts + 11a , $x ) ( $ts + 12a , $x ) ( $ts + 13a , $x ) ( $ts + 14a , $x ) ( $ts + 15a , $x ) ( $ts + 16a , $x ) ( $ts + 17a , $x ) ( $ts + 18a , $x ) ( $ts + 19a , $x ) ( $ts + 20a , $x ) ( $ts + 21a , $x ) ( $ts + 22a , $x ) ( $ts + 23a , $x ) ( $ts + 24a , $x ) ( $ts + 25a , $x ) ( $ts + 26a , $x ) ( $ts + 27a , $x ) ( $ts + 28a , $x ) ( $ts + 29a , $x ) ( $ts + 30a , $x ) ( $ts + 31a , $x ) ( $ts + 32a , $x ) ( $ts + 33a , $x ) ( $ts + 34a , $x ) ( $ts + 25a , $x ) ( $ts + 26a , $x ) ( $ts + 27a , $x ) ( $ts + 28a , $x ) ( $ts + 29a , $x ) ( $ts + 30a , $x ) ( $ts + 31a , $x ) ( $ts + 32a , $x ) ( $ts + 33a , $x ) ( $ts + 34a , $x ) ( $ts + 35a , $x ) ( $ts + 36a , $x ) ( $ts + 37a , $x ) ( $ts + 38a , $x ) ( $ts + 39a , $x ) ( $ts + 40a , $x ) ( $ts + 41a , $x ) ( $ts + 42a , $x ) ( $ts + 43a , $x ) ( $ts + 44a , $x ) ( $ts + 45a , $x ) ( $ts + 46a , $x ) ( $ts + 47a , $x ) ( $ts + 48a , $x ) ( $ts + 49a , $x ) ( $ts + 50a , $x ) ( $ts + 51a , $x ) ( $ts + 52a , $x ) ( $ts + 53a , $x ) ( $ts + 54a , $x ) ( $ts + 55a , $x ) ( $ts + 56a , $x ) ( $ts + 57a , $x ) ( $ts + 58a , $x ) ( $ts + 59a , $x ) 
    $x = $x + 60
  endw 
  $totalRows = $totalRows + $x
  print       info: inserted $x rows into $tb and totalRows: $totalRows
  $i = $i + 1
endw 

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

if $data00 != $totalRows then
  return -1
endi


print ============== step3: insert old data(now-15d) and new data(now+15d), control data rows  in order to save in cache, not falling disc
sql insert into $tb values ( now - 20d , -20 )
sql insert into $tb values ( now - 40d , -40 )
$totalRows = $totalRows + 2

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

$loopCnt = 0
wait_dnode1_offline:
$loopCnt = $loopCnt + 1
if $loopCnt == 10 then 
  return -1
endi

sql show dnodes
if $rows != 2 then
  sleep 2000
  goto wait_dnode1_offline
endi
print $data0_1  $data1_1  $data2_1  $data3_1  $data4_1
print $data0_2  $data1_2  $data2_2  $data3_2  $data4_2
$dnode1Status = $data4_1  
$dnode2Status = $data4_2

if $dnode1Status != offline then
  sleep 2000
  goto wait_dnode1_offline
endi
if $dnode2Status != ready then
  sleep 2000
  goto wait_dnode1_offline
endi

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

#sql show vgroups
#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

print ============== step5: remove the mnode dir of dnode1, then copy the monde dir of dnode2
system_content rm -rf ../../../sim/dnode1/data/mnode
system_content cp -rf ../../../sim/dnode2/data/mnode ../../../sim/dnode1/data/

print ============== step6: restart dnode1, waiting sync end
system sh/exec.sh -n dnode1 -s start
sleep 1000

$loopCnt = 0
wait_dnode1_ready:
$loopCnt = $loopCnt + 1
if $loopCnt == 20 then 
  return -1
endi

sql show dnodes -x wait_dnode1_ready
if $rows != 2 then
  sleep 2000
  goto wait_dnode1_ready
endi
print $data0_1  $data1_1  $data2_1  $data3_1  $data4_1
print $data0_2  $data1_2  $data2_2  $data3_2  $data4_2
$dnode1Status = $data4_1  
$dnode2Status = $data4_2

if $dnode1Status != ready then
  sleep 2000
  goto wait_dnode1_ready
endi
if $dnode2Status != ready then
  sleep 2000
  goto wait_dnode1_ready
endi

$loopCnt = 0
wait_dnode1_vgroup_slave:
$loopCnt = $loopCnt + 1
if $loopCnt == 10 then 
  return -1
endi

sql show vgroups
if $rows != 3 then
  sleep 2000
  goto wait_dnode1_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
print $data0_4  $data1_4  $data2_4  $data3_4  $data4_4  $data5_4  $data6_4  $data7_4  $data8_4  $data9_4
$d2v2status = $data5_4
$d2v3status = $data5_2
$d2v4status = $data5_3

$d1v2status = $data7_4
$d1v3status = $data7_2
$d1v4status = $data7_3

if $d2v2status != master then
  sleep 2000
  goto wait_dnode1_vgroup_slave
endi
if $d2v3status != master then
  sleep 2000
  goto wait_dnode1_vgroup_slave
endi
if $d2v4status != master then
  sleep 2000
  goto wait_dnode1_vgroup_slave
endi

if $d1v2status != slave then
  sleep 2000
  goto wait_dnode1_vgroup_slave
endi
if $d1v3status != slave then
  sleep 2000
  goto wait_dnode1_vgroup_slave
endi
if $d1v4status != slave then
  sleep 2000
  goto wait_dnode1_vgroup_slave
endi

print ============== step7: stop dnode2
system sh/exec.sh -n dnode2 -s stop -x SIGINT

$loopCnt = 0
wait_dnode2_offline:
$loopCnt = $loopCnt + 1
if $loopCnt == 10 then 
  return -1
endi

sql show dnodes
if $rows != 2 then
  sleep 2000
  goto wait_dnode2_offline
endi
print $data0_1  $data1_1  $data2_1  $data3_1  $data4_1
print $data0_2  $data1_2  $data2_2  $data3_2  $data4_2
$dnode1Status = $data4_1  
$dnode2Status = $data4_2

if $dnode1Status != ready then
  sleep 2000
  goto wait_dnode2_offline
endi
if $dnode2Status != offline then
  sleep 2000
  goto wait_dnode2_offline
endi

sql reset query cache 

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