# testing TR295, kill non-master when recovering in phase 4

use strict;
use NDB::Run;

my $env = NDB::Run->getenv;
my $log = $env->getlog;
$log->setpart(time => 1, line => 0);
$log->setprio("info");

my $database = $ENV{NDB_DATABASE};
$log->put("start test database=$database");
$env->init or $log->push("init failed")->fatal;

my $db = $env->getdb($database) or $log->push->fatal;
my $mgm = $db->getnode(1) or $log->push->fatal;
my $db1 = $db->getnode(2) or $log->push->fatal;
my $db2 = $db->getnode(3) or $log->push->fatal;

$db->kill;
$db->start({init_rm=>1}) or $log->push->fatal;
sleep 10;

# should be option (or default) in $db->start
sub wait_until_started {
    my $local_cnt = 100;
    while (--$local_cnt > 0) {
	sleep 2;
	my $ret = $mgm->write("all status", { wait => 2 });
	$ret or $log->fatal;
	my $output = $ret->{output};
	if ($output =~ /\bstarted\b(.|\n)*started\b/i) {
	    $log->put("*** db is started ***")->info;
	    return;
	}
	if ($output =~ /\bno.contact\b(.|\n)*no.contact\b/i) {
	    print "NDBT_ProgramExit: 1 - test error\n";
	    $log->put("*** db is dead ***")->fatal;
	    # Maybe try a system restart?
	}
    }
    print "NDBT_ProgramExit: 1 - test error\n";
    $log->put("*** node recovery failed ***")->fatal;
}
sub wait_until_phase4_and_kill {
# kill db2 as quick as possible when it has reached phase4
my $cnt = 1000;
  while (--$cnt > 0) {
    my $ret = $mgm->write("3 status", {wait => 0});
    #$ret or log->fatal;
    my $output = $ret->{output};
    if ($output =~ /\bphase 4\b/i) {
	$db2->kill or log->fatal;
	return;
    }
  }
  print "NDBT_ProgramExit: 1 -test error\n";
  $log->put("*** node restart failed after 1000 loops ***")->fatal;
}

my $cnt = 0;
wait_until_started();
while (1) {
    wait_until_started();
    sleep 2;
    $db2->kill or $log->fatal;
    $db2->start;	# start the node
    wait_until_phase4_and_kill();
    sleep 10;
    $db2->start;
}

$db->kill;

# vim: set sw=4:
