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

print ========== step0
system sh/exec.sh -n dnode1 -s start
sql connect

sql create dnode $hostname port 7200
sql create dnode $hostname port 7300
sql create dnode $hostname port 7400
system sh/exec.sh -n dnode2 -s start
system sh/exec.sh -n dnode3 -s start
system sh/exec.sh -n dnode4 -s start

$x = 0
step0:
	$x = $x + 1
	sleep 1000
	if $x == 10 then
	  print ====> dnode not ready!
		return -1
	endi
sql select * from information_schema.ins_dnodes
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
print ===> $data30 $data31 $data32 $data33 $data34 $data35
if $rows != 4 then
  return -1
endi
if $data(1)[4] != ready then
  goto step0
endi
if $data(2)[4] != ready then
  goto step0
endi
if $data(3)[4] != ready then
  goto step0
endi
if $data(4)[4] != ready then
  goto step0
endi

print ========= step1
sql create database db replica 3 vgroups 1
$x = 0
step1:
	$x = $x + 1
	sleep 1000
	if $x == 60 then
	  print ====> db not ready!
		return -1
	endi
sql show db.vgroups
print ===> $data00 $data01 $data02 $data03 $data04 $data05 $data06 $data07 $data08 $data09
if $rows != 1 then
  return -1
endi
if $data(2)[4] == leader then
  $leaderExist = 1
endi
if $data(2)[6] == leader then
  $leaderExist = 1
endi
if $data(2)[8] == leader then
  $leaderExist = 1
endi
if  $leaderExist != 1 then
  goto step1
endi

sql create table db.tb (ts timestamp, i int)
sql insert into db.tb values(now, 1)
sql select count(*) from db.tb
$lastRows = $rows

print ======== step2
run_back tsim/vnode/back_insert.sim
sleep 2000

print ======== step3
system sh/exec.sh -n dnode2 -s stop
sleep 3000

$t = 0
$x = 0
loop:

print ======== step4
system sh/exec.sh -n dnode2 -s start
sleep 3000
system sh/exec.sh -n dnode3 -s stop
sleep 3000

print ======== step5
system sh/exec.sh -n dnode3 -s start
sleep 3000
system sh/exec.sh -n dnode4 -s stop
sleep 3000

print ======== step6
system sh/exec.sh -n dnode4 -s start
sleep 3000
system sh/exec.sh -n dnode2 -s stop
sleep 3000

print ======== step7
$x = 0
step7:
	$x = $x + 1
	sleep 1000
	if $x == 30 then
	  print ====> dnode not ready!
		return -1
	endi
sql select count(*) from db.tb -x step7

print select count(*) from db.tb ==> $data00 $lastRows 
if $data00 < $lastRows then
  return -1
endi

print ======== step8
$lastRows = $data00
print ======== loop Times $x

if $t < 2 then
  $t = $t + 1
  goto loop
endi

system sh/exec.sh -n dnode1 -s stop  -x SIGINT
system sh/exec.sh -n dnode2 -s stop  -x SIGINT
system sh/exec.sh -n dnode3 -s stop  -x SIGINT
system sh/exec.sh -n dnode4 -s stop  -x SIGINT
system sh/exec.sh -n dnode5 -s stop  -x SIGINT
system sh/exec.sh -n dnode6 -s stop  -x SIGINT
system sh/exec.sh -n dnode7 -s stop  -x SIGINT
system sh/exec.sh -n dnode8 -s stop  -x SIGINT
