plabmon_badpool.pm.in 13.7 KB
Newer Older
Kirk Webb's avatar
 
Kirk Webb committed
1 2
# -*- perl -*-
#
3
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2005, 2007 University of Utah and the Flux Group.
Kirk Webb's avatar
 
Kirk Webb committed
5 6 7 8 9 10 11 12 13
# All rights reserved.
#

#
# plabmonitor node pool module for bad (malfunctional) nodes.
#

package plabmon_badpool;

Kirk Webb's avatar
 
Kirk Webb committed
14
#use strict;
Kirk Webb's avatar
 
Kirk Webb committed
15 16 17 18 19 20 21 22 23
use English;

use POSIX qw(WIFSIGNALED WEXITSTATUS);

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

use lib "@prefix@/lib";
use libdb;
use libtestbed;
Kirk Webb's avatar
 
Kirk Webb committed
24
use libplabmon;
Kirk Webb's avatar
 
Kirk Webb committed
25 26 27 28 29

my $BADINST = 99;
my $VNODESETUPTIMEOUT = 600; # XXX: need to get from sitevar, or
                             #      pass in via new().
my $ISUPWAITTIME = 600;      # XXX: likewise...
Kirk Webb's avatar
 
Kirk Webb committed
30
my $TEARDOWNTIMEOUT = 300;   # XXX: ...
Kirk Webb's avatar
 
Kirk Webb committed
31 32 33 34 35 36 37 38 39

my $SETUPMODE    = "SETUPMODE";
my $TEARDOWNMODE = "TEARDOWNMODE";

my $BIGINT = 9999999999;

my $CLIENT_BIN = "@CLIENT_BINDIR@";
my $SSH = "@prefix@/bin/sshtb -n";
my $PLABNODE = "@prefix@/sbin/plabnode";
40 41
my $PLABDIST = "@prefix@/sbin/plabdist";
my $PLABHTTPD = "@prefix@/sbin/plabhttpd";
Kirk Webb's avatar
 
Kirk Webb committed
42 43 44 45 46 47 48 49
# XXX - testing
#my $PLABNODE = "/home/kwebb/bin/randsleep.pl";

my $PLABMOND_PID    = PLABMOND_PID();
my $PLABMOND_EID    = PLABMOND_EID();
my $PLABHOLDING_PID = PLABHOLDING_PID();
my $PLABHOLDING_EID = PLABHOLDING_EID();

David Johnson's avatar
David Johnson committed
50
my $plcname = '';
51
my $plcvtype = '';
David Johnson's avatar
David Johnson committed
52

Kirk Webb's avatar
 
Kirk Webb committed
53 54 55 56 57 58
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
59 60
    my ($plcid, $poolname, $poolpid, $pooleid, $chpid2pool) = @_;
    $plcname = $plcid;
Kirk Webb's avatar
 
Kirk Webb committed
61 62 63 64 65 66 67 68 69 70 71 72 73

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

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

74 75 76 77 78 79 80 81 82 83 84 85
    # 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];

Kirk Webb's avatar
 
Kirk Webb committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    bless($self,$class);
    return $self;
}

#
# Things to do to check a node in the bad pool:
# 1) Ping the node (maybe with ssh) (?) - not done right now
# 2) Try to instantiate the 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();

    # Nothinig to check!
    if (!$pnode) {
105
        return 1;
Kirk Webb's avatar
 
Kirk Webb committed
106 107 108 109 110 111 112
    }

    # Grab the vnode for this pnode (service sliver vnode)
    my $vnode = $self->getsvcvnode($pnode->{'name'});

    if (!defined($vnode)) {
        print "Could not find vnode associated with $pnode!\n";
113
        return 1;
Kirk Webb's avatar
 
Kirk Webb committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }

    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;

131
        return 0;
Kirk Webb's avatar
 
Kirk Webb committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    }

    # 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 $PLABMOND_PID ".
                      "$PLABMOND_EID $vnode",1)) {
            print "*** Vserver instantiation failed: $vnode\n";
            # XXX: Should check DB state instead.
            exit($BADINST);
        }

151 152 153 154
	# Do this before rootball/httpd to avoid wasted bandwidth/time.
	# However, wastes resources on boss by not exec'ing this; we have a
	# whole extra process hanging around just so we can do the rootball 
	# and httpd stuff after.  XXX!
155

156 157 158 159 160 161 162 163 164 165 166
	my $vres = TBForkCmd("$SSH -host $vnode $CLIENT_BIN/vnodesetup" . 
			     " -p $vnode");
	if ($vres) {
	    print "*** Vnodesetup failed: $vnode\n";
	    exit($vres);
	}

	exit($vres);

        #exec "$SSH -host $vnode $CLIENT_BIN/vnodesetup -p $vnode" or
        #    die "Yike! Can't exec command!\n";
Kirk Webb's avatar
 
Kirk Webb committed
167 168 169 170 171 172 173 174 175 176 177 178 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
    }

    # 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";
249
        return 0;
Kirk Webb's avatar
 
Kirk Webb committed
250 251
    }

Kirk Webb's avatar
 
Kirk Webb committed
252 253 254
    # Setup log entry prefix
    my $logmsg = "$self->{'NAME'}, $pnode->{'name'}, ";

Kirk Webb's avatar
 
Kirk Webb committed
255 256 257 258 259 260
    # 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 ".
Kirk Webb's avatar
 
Kirk Webb committed
261
          "$pnode->{'name'} (pid: $chpid) @ ". `date`;
Kirk Webb's avatar
 
Kirk Webb committed
262 263 264 265 266 267

    # XXX: ignore teardown mode for now (except to clear from pending list).
    if ($pnode->{'mode'} eq $TEARDOWNMODE) {
        delete $self->{'PENDING'}->{$pnode->{'name'}};
        if (!$exstat) {
            print "Teardown of $pnode->{'vnode'} complete\n";
David Johnson's avatar
David Johnson committed
268
            Log(STATUSLOG($plcname),
David Johnson's avatar
Fixes.  
David Johnson committed
269
		$logmsg . "teardown, success, teardown succeeded.");
Kirk Webb's avatar
 
Kirk Webb committed
270 271
        } else {
            print "Teardown of $pnode->{'vnode'} failed: $exstat\n";
David Johnson's avatar
David Johnson committed
272
            Log(STATUSLOG($plcname),
David Johnson's avatar
Fixes.  
David Johnson committed
273
		$logmsg . "teardown, fail, teardown failed.");
Kirk Webb's avatar
 
Kirk Webb committed
274
        }
Kirk Webb's avatar
 
Kirk Webb committed
275

Kirk Webb's avatar
 
Kirk Webb committed
276 277 278 279 280 281 282
        return 1;
    }

    SWRN1: for ($exstat) {
        WIFSIGNALED($_) && do {
            if ($now > $pnode->{'timeout'}) {
                print "Timeout waiting for $pnode->{'vnode'} to instantiate.\n";
Kirk Webb's avatar
 
Kirk Webb committed
283
                $logmsg .= "setup, fail, timeout waiting for node.";
Kirk Webb's avatar
 
Kirk Webb committed
284 285 286
            }
            else {
                print "Setup of $pnode->{'vnode'} killed for unknown reason.\n";
Kirk Webb's avatar
 
Kirk Webb committed
287
                $logmsg .= "setup, fail, killed - unknown reason.";
Kirk Webb's avatar
 
Kirk Webb committed
288 289 290 291 292 293 294 295
            }
            $bad = 1;
            last SWRN1;
        };

        WEXITSTATUS($_) == $BADINST && do {
            print "Instantiation of $pnode->{'vnode'} failed.\n";
            $bad = 1;
Kirk Webb's avatar
 
Kirk Webb committed
296
            $logmsg .= "setup, fail, vserver instantiation failed.";
Kirk Webb's avatar
 
Kirk Webb committed
297 298 299 300 301
            last SWRN1;
        };

        WEXITSTATUS($_) > 0 && do {
            print "Vnodesetup failed on $pnode->{'vnode'}.\n";
Kirk Webb's avatar
 
Kirk Webb committed
302
            $logmsg .= "setup, fail, vnodesetup failed (excode: $exstat).";
Kirk Webb's avatar
 
Kirk Webb committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
            $bad = 1;
            last SWRN1;
        };

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

    # Log success/failure in any case.
    # XXX: do this!
                
    if ($bad) {
        # If setup failed, schedule a vnode teardown.
        $self->teardownnode($pnode);
        $self->calcnextcheck($pnode);
        $pnode->{'consecfails'}++;
David Johnson's avatar
David Johnson committed
321
        Log(STATUSLOG($plcname), $logmsg);
Kirk Webb's avatar
 
Kirk Webb committed
322 323 324 325 326
    } else {
        # Instantiation was successful.
        # Setup a timeout to wait for ISUP.
        $pnode->{'timeout'} = $now + $ISUPWAITTIME;
    }
Kirk Webb's avatar
 
Kirk Webb committed
327

Kirk Webb's avatar
 
Kirk Webb committed
328
    return 0;
Kirk Webb's avatar
 
Kirk Webb committed
329 330 331 332 333 334 335 336
}

#
# XXX: Comment me
#
sub checkexpiration($) {
    my $self = shift;
    my $now = time();
Kirk Webb's avatar
 
Kirk Webb committed
337
    my $numfinished = 0;
Kirk Webb's avatar
 
Kirk Webb committed
338 339 340 341 342 343

    foreach my $pnode (values %{ $self->{'PENDING'} }) {

        # ISUP or TBFAILED?  Check for these before timeout.
        my $state = TBDB_NODESTATE_UNKNOWN();
        if (TBGetNodeEventState($pnode->{'vnode'}, \$state)) {
Kirk Webb's avatar
 
Kirk Webb committed
344 345 346 347 348
            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";
David Johnson's avatar
David Johnson committed
349
                    Log(STATUSLOG($plcname),
David Johnson's avatar
Fixes.  
David Johnson committed
350
			"$self->{'NAME'}, $pnode->{'name'}, ".
Kirk Webb's avatar
 
Kirk Webb committed
351
                        "setup, success, node came up successfully.");
Kirk Webb's avatar
 
Kirk Webb committed
352 353 354 355 356 357 358 359 360 361
                    $self->nodesetupcomplete($pnode);
                    $numfinished++;
                    next;
                }
                elsif ($state eq TBDB_NODESTATE_TBFAILED()) {
                    $self->teardownnode($pnode);
                    $self->calcnextcheck($pnode);
                    $pnode->{'consecfails'}++;
                    next;
                }
Kirk Webb's avatar
 
Kirk Webb committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
            }
        } 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);
                $self->calcnextcheck($pnode);
                $pnode->{'consecfails'}++;
            }
        }
    }

Kirk Webb's avatar
 
Kirk Webb committed
385
    return $numfinished;
Kirk Webb's avatar
 
Kirk Webb committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
}


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->{'mode'} = $TEARDOWNMODE;
        $pnode->{'timeout'} = $now + $TEARDOWNTIMEOUT;
        $self->{'PENDING'}->{$pnode->{'name'}} = $pnode;
        $self->{'CHPID2POOL'}->{$chpid} = $self;

        return;
    }

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

        # Free the vserver, if possible.
        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());

        # Try to ssh in and kill the vserver.
        exec "$PLABNODE -f free $PLABMOND_PID $PLABMOND_EID $vnode" or
            die "Doh!  Can't exec command!\n";
    }

    # NOTREACHED
}


# XXX: may be bogus, but it'll do for now.
my $MININTERVAL = 300;
Kirk Webb's avatar
 
Kirk Webb committed
430
my $MAXINTERVAL = 12 * 3600;
Kirk Webb's avatar
 
Kirk Webb committed
431 432 433 434 435
sub calcnextcheck($$;$) {
    my ($self, $pnode, $reason) = @_;

    my $now = time();

Kirk Webb's avatar
 
Kirk Webb committed
436 437 438 439
    my $numfails  = $pnode->{'consecfails'} ? $pnode->{'consecfails'} : 1;
    my $nextint = int($numfails * $numfails * $MININTERVAL);
    $pnode->{'nextchecktime'} = 
        $now + MIN($MAXINTERVAL, $nextint) + int(rand(120));
Kirk Webb's avatar
 
Kirk Webb committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
}

#
# 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'} = 1;
    delete $self->{'PENDING'}->{$pnode->{'name'}};

455 456 457 458 459 460
    my $exptidx;
    if (!TBExptIDX($PLABHOLDING_PID, $PLABHOLDING_EID, \$exptidx)) {
	print "*** WARNING: No such experiment $PLABHOLDING_EID!\n";
	return;
    }

Kirk Webb's avatar
 
Kirk Webb committed
461 462 463 464
    #
    # It came up! Move the pnode out of hwdown and back into
    # normal holding experiment.
    #
465
    DBQueryWarn("update reserved set exptidx=$exptidx, ".
Kirk Webb's avatar
 
Kirk Webb committed
466 467 468 469 470 471 472
                "  pid='$PLABHOLDING_PID',eid='$PLABHOLDING_EID' ".
                "   where node_id=\"$pnode->{'name'}\"");
    print "$pnode->{'name'} brought back from the afterworld at ".
        TimeStamp() . "\n";
    
    TBSetNodeLogEntry($pnode->{'name'}, "root", TB_DEFAULT_NODELOGTYPE(),
                      "'Moved to $PLABHOLDING_EID; ".
473
                      "$plcvtype node $pnode->{'vnode'} setup okay by monitor.'");
Kirk Webb's avatar
 
Kirk Webb committed
474 475 476 477 478 479

    # XXX: move to goodpool.

    return;
}

Kirk Webb's avatar
 
Kirk Webb committed
480 481

#
Kirk Webb's avatar
 
Kirk Webb committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
# Get vnode entries (and their corresponding pnodes) 
# for the service slice vservers.
# 
sub getsvcvnode($$) {
    my $self  = shift;
    my $pnode = shift;

    my $qres =
        DBQueryWarn("select r.node_id from reserved as r ".
                    "left join nodes as n on n.node_id=r.node_id ".
                    "where r.pid='$PLABMOND_PID' and ".
                    "      r.eid='$PLABMOND_EID' and ".
                    "      n.phys_nodeid='$pnode'");
    
    if (!$qres || !$qres->num_rows()) {
        print "Failed to get vnode from DB in getsvcvnode()! \n";
        return undef;
    }

    my @row = $qres->fetchrow_array();

    return $row[0];
}

Kirk Webb's avatar
 
Kirk Webb committed
506

Kirk Webb's avatar
 
Kirk Webb committed
507 508
# Make perl happy...
1;