plabmon_goodpool.pm.in 14.3 KB
Newer Older
1 2
# -*- perl -*-
#
3
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2005, 2007 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14
# All rights reserved.
#

#
# plabmonitor node pool module for checking up on "good" plab
# nodes.
#

package plabmon_goodpool;

15
#use strict;
16 17 18 19 20 21 22 23 24
use English;

use POSIX qw(WIFSIGNALED WEXITSTATUS);

$| = 1; # Turn off line buffering on output

use lib "@prefix@/lib";
use libdb;
use libtestbed;
25
use libplabmon;
26 27 28 29 30 31 32
use Node;

my $BADINST = 99;

my $VNODESETUPTIMEOUT = 600; # XXX: need to get from sitevar, or
                             #      pass in via new().
my $ISUPWAITTIME      = 600;      # XXX: likewise...
33
my $TEARDOWNTIMEOUT   = 300;   # XXX: ...
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

my $SETUPMODE        = "SETUPMODE";
my $TEARDOWNGOODMODE = "TEARDOWNGOODMODE";
my $TEARDOWNBADMODE  = "TEARDOWNBADMODE";

my $SETUPFAIL    = "SETUPFAIL";
my $SETUPSUCCESS = "SETUPSUCCESS";

my $BIGINT = 9999999999;

my $CLIENT_BIN = "@CLIENT_BINDIR@";
my $SSH = "@prefix@/bin/sshtb -n";
my $PLABNODE = "@prefix@/sbin/plabnode";
my $VNODE_SETUP = "@prefix@/sbin/vnode_setup";
my $NFREE = "@prefix@/bin/nfree";
# XXX - testing
#my $PLABNODE = "/home/kwebb/bin/randsleep.pl";

my $PLABDOWN_PID    = PLABDOWN_PID();
my $PLABDOWN_EID    = PLABDOWN_EID();
my $PLABTESTING_PID = PLABTESTING_PID();
my $PLABTESTING_EID = PLABTESTING_EID();

David Johnson's avatar
David Johnson committed
57
my $plcname = '';
58
my $plcvtype = '';
David Johnson's avatar
David Johnson committed
59

60 61 62 63 64 65
sub new($$$$$) {

    # The next two lines are some voodoo taken from perltoot(1)
    my $proto = shift;
    my $class = ref($proto) || $proto;

David Johnson's avatar
David Johnson committed
66 67
    my ($plcid, $poolname, $poolpid, $pooleid, $chpid2pool) = @_;
    $plcname = $plcid;
68 69 70 71 72 73 74 75 76 77 78 79 80

    #
    # Create the actual object
    #
    my $self = {};

    $self->{'NAME'} = $poolname;
    $self->{'PID'}  = $poolpid;
    $self->{'EID'}  = $pooleid;
    $self->{'CHPID2POOL'} = $chpid2pool;
    $self->{'PNODES'}  = {};
    $self->{'PENDING'} = {};

81 82 83 84 85 86 87 88 89 90 91 92
    # grab the vtype for this plc:
    my $qr = DBQueryFatal("select node_type from plab_plc_info" . 
			  " where plc_name='$plcname'");
    if ($qr->num_rows() < 1) {
	die "could not find vtype for plc $plcname!";
    }
    elsif ($qr->num_rows() > 1) {
	die "too many possible vtypes for plc $plcname!";
    }
    my @qrow = $qr->fetchrow_array();
    $plcvtype = $qrow[0];

93 94
    # Clean up anything left behind by a terminated monitor
    # XXX: this is kind of hacky, but it works.
95 96 97 98 99 100 101 102
    #
    my @vnodes = ();
    my $qres = DBQueryFatal("select r.node_id" . 
			    " from reserved as r" . 
			    " left join plab_slice_nodes as psn" . 
			    "   on r.node_id=psn.node_id" . 
			    " left join plab_plc_info as ppi" . 
			    "   on psn.plc_idx=ppi.plc_idx" . 
David Johnson's avatar
David Johnson committed
103 104
			    " left join nodes as n" . 
			    "   on r.node_id=n.node_id" . 
105 106
			    " where r.pid='$PLABTESTING_PID'" . 
			    "   and r.eid='$PLABTESTING_EID'" . 
David Johnson's avatar
David Johnson committed
107 108
			    "   and ppi.plc_name='$plcname'" . 
			    "   and n.type=ppi.node_type");
109 110 111 112 113 114
    if ($qres->num_rows()) {
	while (my @row = $qres->fetchrow_array()) {
	    my $vnodename = $row[0];
	    push @vnodes, $vnodename;
	}
    }
115
    if (@vnodes) {
David Johnson's avatar
David Johnson committed
116 117 118
	my $nodelist = join(" ",@vnodes);
        system("$VNODE_SETUP -f -k -n 100" . 
	       " $PLABTESTING_PID $PLABTESTING_EID $nodelist");
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        Node::DeleteVnodes(@vnodes);
    }

    bless($self,$class);
    return $self;
}

#
# Things to do to check a node in the good pool:
# 1) Ping the node (maybe with ssh) (?) - not done right now
# 2) Try to instantiate a sliver via plabnode
# 3) Try to run vnodesetup on the instantiated node.
# 4) Wait for ISUP (or failure/timeout).
#
sub checknextnode($) {
    my $self = shift;

    my $now = time();
    my $pnode = $self->getnextchecknode();

139
    # Nothing to check!
140
    if (!$pnode) {
141
        return 1;
142 143 144 145 146 147 148
    }

    # Grab a new sliver to test with
    my @vnodes = ();
    my %options = ( 'pid'    => $PLABTESTING_PID,
                    'eid'    => $PLABTESTING_EID,
                    'count'  => 1,
149
                    'vtype'  => "$plcvtype",
150 151 152
                    'nodeid' => $pnode->{'name'});
    if (Node::CreateVnodes(\@vnodes, \%options)) {
        print "Failed to allocate vnode for $pnode->{'name'}!\n";
153
        return 1;
154 155 156 157 158 159
    }

    my $vnode = $vnodes[0];

    if (!defined($vnode)) {
        print "Could not create vnode associated with $pnode!\n";
160
        return 1;
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    }

    print "Pool: $self->{'NAME'}: Testing node $pnode->{'name'} at ".
        TimeStamp() . "\n";

    my $chpid = fork();

    if ($chpid) {
        # Update node attributes
        $pnode->{'lastcheckstart'} = $now;
        $pnode->{'vnode'} = $vnode;
        $pnode->{'mode'} = $SETUPMODE;
        $pnode->{'pid'} = $chpid;
        $pnode->{'timeout'} = $now + $VNODESETUPTIMEOUT;
        $self->{'PENDING'}->{$pnode->{'name'}} = $pnode;
        $self->{'CHPID2POOL'}->{$chpid} = $self;

178
        return 0;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    }

    # Worker process.
    else {
        TBdbfork();	# So we get the event system fork too ...
        # Make sure vnode is in the proper state before trying to
        # bring it up.
        TBSetNodeEventState($vnode, TBDB_NODESTATE_SHUTDOWN());

        # XXX: should probably look to see if we have an RCAP for this
        #      vnode and try to clean it up first if so.

        if (TBForkCmd("$PLABNODE -f alloc $PLABTESTING_PID ".
                      "$PLABTESTING_EID $vnode",1)) {
            print "*** Vserver instantiation failed: $vnode\n";
            # XXX: Should check DB state instead.
            exit($BADINST);
        }

        exec "$SSH -host $vnode $CLIENT_BIN/vnodesetup -p $vnode" or
            die "Yike! Can't exec command!\n";
    }

    # NOTREACHED
}

sub getnextchecknode($) {
    my $self = shift;

    my $retnode = "";
    my $nextcheck = $BIGINT;

    foreach my $pnode (values %{$self->{'PNODES'}}) {
        my $nchecktime = $pnode->{'nextchecktime'};
        if (!exists($self->{'PENDING'}->{$pnode->{'name'}}) and
            $nchecktime < $nextcheck) {
            $nextcheck = $nchecktime;
            $retnode = $pnode;
        }
    }

    return $retnode;
}
    

#
# XXX: comment.
#
sub getnextchecktime($) {
    my $self = shift;
    
    my $nextnode = $self->getnextchecknode();

    if ($nextnode) {
        return $nextnode->{'nextchecktime'}
    }

    return $BIGINT; # XXX
}

sub getnexttimeout($) {
    my $self = shift;

    my $timeout = $BIGINT; # XXX

    foreach my $pnode (values %{$self->{'PNODES'}}) {
        my $ntmo = $pnode->{'timeout'};
        if ($ntmo && exists($self->{'PENDING'}->{$pnode->{'name'}})) {
            $timeout = MIN($ntmo, $timeout);
        }
    }

    return $timeout;
}

sub getnextservicetime($) {
    my $self = shift;

    return MIN($self->getnexttimeout(), $self->getnextchecktime());
}


#
# XXX: Comment me
#
sub processchild($$$) {
    my $self   = shift;
    my $chpid  = shift;
    my $exstat = shift;

    my $pnode;
    my $now = time();
    my $bad = 0;

    foreach my $findpnode (values %{$self->{'PENDING'}}) {
        if (defined($findpnode->{'pid'}) and $findpnode->{'pid'} == $chpid) {
            $pnode = $findpnode;
            last;
        }
    }

    if (!defined($pnode)) {
        print "Pool: $self->{'NAME'}: $chpid not found in pending list!\n";
282
        return 0;
283 284
    }

285 286 287
    # Setup log entry prefix
    my $logmsg = "$self->{'NAME'}, $pnode->{'name'}, ";

288 289 290 291 292 293
    # Clear pid entry - child has gone away.
    delete $pnode->{'pid'};

    # Check the nodes to find out which are up, and which failed
    # in the vnode_setup we just ran.
    print "Pool: $self->{'NAME'}: Checking status of ".
294
          "$pnode->{'name'} (pid: $chpid) @ ". `date`;
295 296 297 298 299 300 301

    # Handle vnode teardown
    if ($pnode->{'mode'} eq $TEARDOWNGOODMODE or
        $pnode->{'mode'} eq $TEARDOWNBADMODE) {

        if (!$exstat) {
            print "Teardown of $pnode->{'vnode'} complete\n";
302
            Log(STATUSLOG, $logmsg . "teardown, success, teardown succeeded.");
303 304
        } else {
            print "Teardown of $pnode->{'vnode'} failed: $exstat\n";
305
            Log(STATUSLOG, $logmsg . "teardown, fail, teardown failed.");
306 307 308 309 310 311 312 313 314 315 316 317 318 319
        }

        delete $self->{'PENDING'}->{$pnode->{'name'}};
        # Delete the vnode created to test this node - we're done with it.
        my @vnodes = ($pnode->{'vnode'},);
        Node::DeleteVnodes(@vnodes);
        $pnode->{'vnode'} = "";

        if ($pnode->{'mode'} eq $TEARDOWNGOODMODE) {
            $self->nodesetupcomplete($pnode);
        }
        elsif ($pnode->{'mode'} eq $TEARDOWNBADMODE) {
            $self->movetodownpool($pnode);
        }
320

321 322 323
        return 1;
    }

324
    # Test exit status for setup mode(s)
325 326 327 328
    SWRN1: for ($exstat) {
        WIFSIGNALED($_) && do {
            if ($now > $pnode->{'timeout'}) {
                print "Timeout waiting for $pnode->{'vnode'} to instantiate.\n";
329
                $logmsg .= "setup, fail, timeout waiting for node.";
330 331 332
            }
            else {
                print "Setup of $pnode->{'vnode'} killed for unknown reason.\n";
333
                $logmsg .= "setup, fail, killed - unknown reason.";
334 335 336 337 338 339 340
            }
            $bad = 1;
            last SWRN1;
        };

        WEXITSTATUS($_) == $BADINST && do {
            print "Instantiation of $pnode->{'vnode'} failed.\n";
341
            $logmsg .= "setup, fail, vserver instantiation failed.";
342 343 344 345 346 347
            $bad = 1;
            last SWRN1;
        };

        WEXITSTATUS($_) > 0 && do {
            print "Vnodesetup failed on $pnode->{'vnode'}.\n";
348
            $logmsg .= "setup, fail, vnodesetup failed (excode: $exstat).";
349 350 351 352 353 354 355 356 357 358 359 360 361
            $bad = 1;
            last SWRN1;
        };

        # default
        print "Node setup succeeded on $pnode->{'vnode'}.\n".
              "   Waiting for node to hit ISUP.\n";
        $bad = 0;
    }

    if ($bad) {
        # If setup failed, schedule a vnode teardown.
        $self->teardownnode($pnode, $SETUPFAIL);
362
        Log(STATUSLOG, $logmsg);
363 364 365
    } else {
        # Setup a timeout to wait for ISUP.
        $pnode->{'timeout'} = $now + $ISUPWAITTIME;
366
    }
367
    return 0;
368 369 370 371 372 373 374 375
}

#
# XXX: Comment me
#
sub checkexpiration($) {
    my $self = shift;
    my $now = time();
376
    my $numfinished = 0;
377

378
    foreach my $pnode (values %{ $self->{'PENDING'} }) {        
379 380 381 382

        # ISUP or TBFAILED?  Check for these before timeout.
        my $state = TBDB_NODESTATE_UNKNOWN();
        if (TBGetNodeEventState($pnode->{'vnode'}, \$state)) {
383 384 385 386 387
            if ($pnode->{'mode'} eq $SETUPMODE) {
                if ($state eq TBDB_NODESTATE_ISUP()) {
                    # Yes!  Node is up.
                    print "Setup of $pnode->{'vnode'} on $pnode->{'name'} ".
                        " succeeded\n";
388 389
                    Log(STATUSLOG, "$self->{'NAME'}, $pnode->{'name'}, ".
                        "setup, success, node came up successfully.");
390 391 392 393 394 395 396
                    $self->teardownnode($pnode, $SETUPSUCCESS);
                    next;
                }
                elsif ($state eq TBDB_NODESTATE_TBFAILED()) {
                    $self->teardownnode($pnode, $SETUPFAIL);
                    next;
                }
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
            }
        } else {
            print "Error getting event state for $pnode->{'vnode'}\n";
        }

        # Have we timed out waiting for this node?
        if ($pnode->{'timeout'} <= $now) {
            $pnode->{'timeout'} = 0;
            print "Pool: $self->{'NAME'}: $pnode->{'vnode'} timeout.\n";
            # Node has an associated PID
            if (defined($pnode->{'pid'})) {
                kill("TERM", $pnode->{'pid'});
                # Cleanup/processing handled in processchild()
            }
            # ... else we were waiting for an ISUP
            else {
                $self->teardownnode($pnode, $SETUPFAIL);
            }
        }
    }

418
    return $numfinished;
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
}


sub teardownnode($$;$) {
    my ($self, $pnode, $reason) = @_;

    my $now = time();

    my $chpid = fork();

    if ($chpid) {
        # Update node attributes, return pid of worker proc.
        $pnode->{'pid'} = $chpid;
        $pnode->{'timeout'} = $now + $TEARDOWNTIMEOUT;
        $self->{'PENDING'}->{$pnode->{'name'}} = $pnode;
        $self->{'CHPID2POOL'}->{$chpid} = $self;

        if ($reason eq $SETUPFAIL) {
            $pnode->{'mode'} = $TEARDOWNBADMODE;
        }
        elsif ($reason eq $SETUPSUCCESS) {
            $pnode->{'mode'} = $TEARDOWNGOODMODE;
        }

        return;
    }

    # Worker process.
    else {
        TBdbfork();	# So we get the event system fork too ...
        my $vnode = $pnode->{'vnode'};

451
        # Try to ssh in and kill processes running in the vserver.
452 453 454 455 456 457
        TBForkCmd("$SSH -host $vnode $CLIENT_BIN/vnodesetup -p -k $vnode",1);

        # Make sure vnode is in the proper state (regardless of the success
        # or failure of the previous command.
        TBSetNodeEventState($vnode, TBDB_NODESTATE_SHUTDOWN());

458 459 460
        # Free the vserver, if possible.
        exec "$PLABNODE -f free $PLABTESTING_PID $PLABTESTING_EID $vnode" or
            die "Doh!  Can't exec command!\n";
461 462 463 464 465 466 467
    }

    # NOTREACHED
}


# XXX: may be bogus, but it'll do for now.
468
my $MININTERVAL = 600;
469 470 471 472 473 474
my $MAXINTERVAL = 3600;
sub calcnextcheck($$;$) {
    my ($self, $pnode, $reason) = @_;

    my $now = time();

475 476 477 478
    my $numsuccess = $pnode->{'consecsuccess'} ? $pnode->{'consecsuccess'} : 1;
    my $nextint    = int(2 * $numsuccess * $MININTERVAL);
    $pnode->{'nextchecktime'} = 
        $now + MIN($MAXINTERVAL, $nextint) + int(rand(120));
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
}

#
# Check vnode status, moving nodes back into production if they booted up,
# and leaving them in hwdown if they didn't.
#
sub nodesetupcomplete($$) {
    my $self  = shift;
    my $pnode = shift;

    $pnode->{'timeout'} = 0;
    $pnode->{'consecfails'} = 0;
    $pnode->{'consecsuccess'}++;

    #
    # It came up!  Calculate the next check time and move on...
    #
    $self->calcnextcheck($pnode);

    return;
}

sub movetodownpool($$) {
    my $self  = shift;
    my $pnode = shift;

    $pnode->{'consecsuccess'} = 0;
    $pnode->{'consecfails'} = 1;

508 509 510 511 512 513
    my $exptidx;
    if (!TBExptIDX($PLABDOWN_PID, $PLABDOWN_EID, \$exptidx)) {
	print "*** WARNING: No such experiment $PLABDOWN_EID!\n";
	return;
    }

514 515 516
    #
    # It failed to come up. Move the pnode to hwdown...
    #
517
    DBQueryWarn("update reserved set exptidx=$exptidx, ".
518 519 520 521 522 523 524
                "  pid='$PLABDOWN_PID',eid='$PLABDOWN_EID' ".
                "   where node_id=\"$pnode->{'name'}\"");
    print "$pnode->{'name'} failed testing; sent to hwdown at ".
        TimeStamp() . "\n";
    
    TBSetNodeLogEntry($pnode->{'name'}, "root", TB_DEFAULT_NODELOGTYPE(),
                      "'Moved to $PLABDOWN_EID; ".
525
                      "$plcvtype node $pnode->{'vnode'} setup failed by monitor.'");
526 527 528 529
}

# Make perl happy...
1;