system sh/ip.sh -i 1 -s up
system sh/ip.sh -i 2 -s up
system sh/ip.sh -i 3 -s up
system sh/ip.sh -i 4 -s up
system sh/ip.sh -i 5 -s up

system sh/stop_dnodes.sh
system sh/deploy.sh -n dnode1 -m 192.168.0.1 -i 192.168.0.1
system sh/cfg.sh -n dnode1 -c numOfTotalVnodes -v 4
system sh/cfg.sh -n dnode1 -c tables -v 1000

system sh/deploy.sh -n dnode2 -m 192.168.0.1 -i 192.168.0.2
system sh/cfg.sh -n dnode2 -c numOfTotalVnodes -v 4
system sh/cfg.sh -n dnode2 -c tables -v 1000

system sh/deploy.sh -n dnode3 -m 192.168.0.1 -i 192.168.0.3
system sh/cfg.sh -n dnode3 -c numOfTotalVnodes -v 4
system sh/cfg.sh -n dnode3 -c tables -v 1000

system sh/deploy.sh -n dnode4 -m 192.168.0.1 -i 192.168.0.4
system sh/cfg.sh -n dnode4 -c numOfTotalVnodes -v 4
system sh/cfg.sh -n dnode4 -c tables -v 1000

system sh/deploy.sh -n dnode5 -m 192.168.0.1 -i 192.168.0.5
system sh/cfg.sh -n dnode5 -c numOfTotalVnodes -v 4
system sh/cfg.sh -n dnode5 -c tables -v 1000

print =============== prepare data
system sh/exec.sh -n dnode1 -s start
sleep 3000
sql connect

$i = 0
$tbNum = 4000
$rowNum = 1
$totalNum = 4000 * $rowNum

sql create database db
sql use db
sql create table mt (ts timestamp, tbcol int, tbcol2 float) TAGS(tgcol int)

$i = 0
$tbPrefix = t
while $i < $tbNum
  $tb = $tbPrefix . $i
  sql create table $tb using mt tags( $i ) 
  $i = $i + 1
endw 

$i = 0
while $i < $tbNum
  $tb = $tbPrefix . $i
  sql insert into $tb values (now, $i , $i ) 
  
  $i = $i + 1
endw 

print ========== step0
sql show db.tables
if $rows != 4000 then
  return -1
endi

sql select count(*) from t10
print select count(*) from t10 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from t1010
print select count(*) from t1010 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from t2010
print select count(*) from t2010 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from t3010
print select count(*) from t3010 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from mt
print select count(*) from mt $data00 expect $rowNum 
if $data00 != $totalNum then
  return -1
endi

print ========== step1
sql create dnode 192.168.0.2
system sh/exec.sh -n dnode2 -s start

$x = 0
show1: 
	$x = $x + 1
	sleep 3000
	if $x == 100 then 
		return -1
	endi
	
sql show dnodes
print 192.168.0.1 freeVnodes $data3_192.168.0.1
print 192.168.0.2 freeVnodes $data3_192.168.0.2
if $data3_192.168.0.1 != 2 then
	goto show1
endi
if $data3_192.168.0.2 != 2 then
	goto show1
endi

sql select count(*) from t10
print select count(*) from t10 $data00 expect $rowNum 
if $data00 != $rowNum then
  goto show1
endi

sql select count(*) from t1010
print select count(*) from t1010 $data00 expect $rowNum 
if $data00 != $rowNum then
  goto show1
endi

sql select count(*) from t2010
print select count(*) from t2010 $data00 expect $rowNum 
if $data00 != $rowNum then
  goto show1
endi

sql select count(*) from t3010
print select count(*) from t3010 $data00 expect $rowNum 
if $data00 != $rowNum then
  goto show1
endi

sql select count(*) from mt
print select count(*) from mt $data00 expect $rowNum 
if $data00 != $totalNum then
  goto show1
endi

print ========== step2
sql create dnode 192.168.0.3
system sh/exec.sh -n dnode3 -s start
sleep 10000

print ========== step3
sql drop dnode 192.168.0.2

$x = 0
show3: 
	$x = $x + 1
	sleep 3000
	if $x == 100 then 
		return -1
	endi
	
sql show dnodes
print 192.168.0.1 freeVnodes $data3_192.168.0.1
print 192.168.0.2 freeVnodes $data3_192.168.0.2
print 192.168.0.3 freeVnodes $data3_192.168.0.3
if $data3_192.168.0.1 != 2 then
	goto show3
endi
if $data3_192.168.0.2 != null then
	goto show3
endi
if $data3_192.168.0.3 != 2 then
	goto show3
endi

system sh/exec.sh -n dnode2 -s stop -x SIGINT

sql select count(*) from t10
print select count(*) from t10 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from t1010
print select count(*) from t1010 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from t2010
print select count(*) from t2010 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from t3010
print select count(*) from t3010 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from mt
print select count(*) from mt $data00 expect $rowNum 
if $data00 != $totalNum then
  return -1
endi

print ========== step4
sql drop dnode 192.168.0.3

$x = 0
show4: 
	$x = $x + 1
	sleep 3000
	if $x == 100 then 
		return -1
	endi
	
sql show dnodes
print 192.168.0.1 freeVnodes $data3_192.168.0.1
print 192.168.0.3 freeVnodes $data3_192.168.0.3
if $data3_192.168.0.1 != 0 then
	goto show4
endi
if $data3_192.168.0.3 != null then
	goto show4
endi

system sh/exec.sh -n dnode3 -s stop -x SIGINT

sql select count(*) from t10
print select count(*) from t10 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from t1010
print select count(*) from t1010 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from t2010
print select count(*) from t2010 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from t3010
print select count(*) from t3010 $data00 expect $rowNum 
if $data00 != $rowNum then
  return -1
endi

sql select count(*) from mt
print select count(*) from mt $data00 expect $rowNum 
if $data00 != $totalNum then
  return -1
endi

print ========== step5
sql alter database db replica 2
sql create dnode 192.168.0.4
system sh/exec.sh -n dnode4 -s start

$x = 0
show5: 
	$x = $x + 1
	sleep 3000
	if $x == 100 then 
		return -1
	endi
	
sql show dnodes
print 192.168.0.1 freeVnodes $data3_192.168.0.1
print 192.168.0.4 freeVnodes $data3_192.168.0.4
if $data3_192.168.0.1 != 0 then
	goto show5
endi
if $data3_192.168.0.4 != 0 then
	goto show5
endi

sql select count(*) from t10
print select count(*) from t10 $data00 expect $rowNum 
if $data00 != $rowNum then
  goto show5
endi

sql select count(*) from t1010
print select count(*) from t1010 $data00 expect $rowNum 
if $data00 != $rowNum then
  goto show5
endi

sql select count(*) from t2010
print select count(*) from t2010 $data00 expect $rowNum 
if $data00 != $rowNum then
  goto show5
endi

sql select count(*) from t3010
print select count(*) from t3010 $data00 expect $rowNum 
if $data00 != $rowNum then
  goto show5
endi

sql select count(*) from mt
print select count(*) from mt $data00 expect $rowNum 
if $data00 != $totalNum then
  goto show5
endi
