plabmon_badpool.pm.in 14.3 KB
Newer Older
1 2
# -*- perl -*-
#
3
# Copyright (c) 2000-2005, 2007 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30
#

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

package plabmon_badpool;

31
#use strict;
32 33 34 35 36 37 38 39 40
use English;

use POSIX qw(WIFSIGNALED WEXITSTATUS);

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

use lib "@prefix@/lib";
use libdb;
use libtestbed;
41
use libplabmon;
42 43 44 45 46

my $BADINST = 99;
my $VNODESETUPTIMEOUT = 600; # XXX: need to get from sitevar, or
                             #      pass in via new().
my $ISUPWAITTIME = 600;      # XXX: likewise...
47
my $TEARDOWNTIMEOUT = 300;   # XXX: ...
48 49 50 51 52 53 54 55 56

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";
57 58
my $PLABDIST = "@prefix@/sbin/plabdist";
my $PLABHTTPD = "@prefix@/sbin/plabhttpd";
59 60 61 62 63 64 65 66
# 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
67
my $plcname = '';
68
my $plcvtype = '';
David Johnson's avatar
David Johnson committed
69

70 71 72 73 74 75
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
76 77
    my ($plcid, $poolname, $poolpid, $pooleid, $chpid2pool) = @_;
    $plcname = $plcid;
78 79 80 81 82 83 84 85 86 87 88 89 90

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

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

91 92 93 94 95 96 97 98 99 100 101 102
    # 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];

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    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) {
122
        return 1;
123 124 125 126 127 128 129
    }

    # 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";
130
        return 1;
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    }

    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;

148
        return 0;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    }

    # 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);
        }

168 169 170 171
	# 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!
172

173 174 175 176 177 178 179 180 181 182 183
	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";
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
    }

    # 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";
266
        return 0;
267 268
    }

269 270 271
    # Setup log entry prefix
    my $logmsg = "$self->{'NAME'}, $pnode->{'name'}, ";

272 273 274 275 276 277
    # 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 ".
278
          "$pnode->{'name'} (pid: $chpid) @ ". `date`;
279 280 281 282 283 284

    # 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
285
            Log(STATUSLOG($plcname),
David Johnson's avatar
David Johnson committed
286
		$logmsg . "teardown, success, teardown succeeded.");
287 288
        } else {
            print "Teardown of $pnode->{'vnode'} failed: $exstat\n";
David Johnson's avatar
David Johnson committed
289
            Log(STATUSLOG($plcname),
David Johnson's avatar
David Johnson committed
290
		$logmsg . "teardown, fail, teardown failed.");
291
        }
292

293 294 295 296 297 298 299
        return 1;
    }

    SWRN1: for ($exstat) {
        WIFSIGNALED($_) && do {
            if ($now > $pnode->{'timeout'}) {
                print "Timeout waiting for $pnode->{'vnode'} to instantiate.\n";
300
                $logmsg .= "setup, fail, timeout waiting for node.";
301 302 303
            }
            else {
                print "Setup of $pnode->{'vnode'} killed for unknown reason.\n";
304
                $logmsg .= "setup, fail, killed - unknown reason.";
305 306 307 308 309 310 311 312
            }
            $bad = 1;
            last SWRN1;
        };

        WEXITSTATUS($_) == $BADINST && do {
            print "Instantiation of $pnode->{'vnode'} failed.\n";
            $bad = 1;
313
            $logmsg .= "setup, fail, vserver instantiation failed.";
314 315 316 317 318
            last SWRN1;
        };

        WEXITSTATUS($_) > 0 && do {
            print "Vnodesetup failed on $pnode->{'vnode'}.\n";
319
            $logmsg .= "setup, fail, vnodesetup failed (excode: $exstat).";
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
            $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
338
        Log(STATUSLOG($plcname), $logmsg);
339 340 341 342 343
    } else {
        # Instantiation was successful.
        # Setup a timeout to wait for ISUP.
        $pnode->{'timeout'} = $now + $ISUPWAITTIME;
    }
344

345
    return 0;
346 347 348 349 350 351 352 353
}

#
# XXX: Comment me
#
sub checkexpiration($) {
    my $self = shift;
    my $now = time();
354
    my $numfinished = 0;
355 356 357 358 359 360

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

        # ISUP or TBFAILED?  Check for these before timeout.
        my $state = TBDB_NODESTATE_UNKNOWN();
        if (TBGetNodeEventState($pnode->{'vnode'}, \$state)) {
361 362 363 364 365
            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
366
                    Log(STATUSLOG($plcname),
David Johnson's avatar
David Johnson committed
367
			"$self->{'NAME'}, $pnode->{'name'}, ".
368
                        "setup, success, node came up successfully.");
369 370 371 372 373 374 375 376 377 378
                    $self->nodesetupcomplete($pnode);
                    $numfinished++;
                    next;
                }
                elsif ($state eq TBDB_NODESTATE_TBFAILED()) {
                    $self->teardownnode($pnode);
                    $self->calcnextcheck($pnode);
                    $pnode->{'consecfails'}++;
                    next;
                }
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
            }
        } 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'}++;
            }
        }
    }

402
    return $numfinished;
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 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
}


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;
447
my $MAXINTERVAL = 12 * 3600;
448 449 450 451 452
sub calcnextcheck($$;$) {
    my ($self, $pnode, $reason) = @_;

    my $now = time();

453 454 455 456
    my $numfails  = $pnode->{'consecfails'} ? $pnode->{'consecfails'} : 1;
    my $nextint = int($numfails * $numfails * $MININTERVAL);
    $pnode->{'nextchecktime'} = 
        $now + MIN($MAXINTERVAL, $nextint) + int(rand(120));
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
}

#
# 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'}};

472 473 474 475 476 477
    my $exptidx;
    if (!TBExptIDX($PLABHOLDING_PID, $PLABHOLDING_EID, \$exptidx)) {
	print "*** WARNING: No such experiment $PLABHOLDING_EID!\n";
	return;
    }

478 479 480 481
    #
    # It came up! Move the pnode out of hwdown and back into
    # normal holding experiment.
    #
482
    DBQueryWarn("update reserved set exptidx=$exptidx, ".
483 484 485 486 487 488 489
                "  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; ".
490
                      "$plcvtype node $pnode->{'vnode'} setup okay by monitor.'");
491 492 493 494 495 496

    # XXX: move to goodpool.

    return;
}

497 498

#
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
# 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];
}

523

524 525
# Make perl happy...
1;