plabmon_goodpool.pm.in 13.2 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

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();

sub new($$$$$) {

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

    my ($poolname, $poolpid, $pooleid, $chpid2pool) = @_;

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

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

    # Clean up anything left behind by a terminated monitor
    # XXX: this is kind of hacky, but it works.
    my @vnodes = ExpNodes($PLABTESTING_PID, $PLABTESTING_EID);
    if (@vnodes) {
        system("$VNODE_SETUP -f -k -n 100 $PLABTESTING_PID $PLABTESTING_EID");
        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();

102
    # Nothing to check!
103
    if (!$pnode) {
104
        return 1;
105 106 107 108 109 110 111 112 113 114 115
    }

    # Grab a new sliver to test with
    my @vnodes = ();
    my %options = ( 'pid'    => $PLABTESTING_PID,
                    'eid'    => $PLABTESTING_EID,
                    'count'  => 1,
                    'vtype'  => 'pcplab',
                    'nodeid' => $pnode->{'name'});
    if (Node::CreateVnodes(\@vnodes, \%options)) {
        print "Failed to allocate vnode for $pnode->{'name'}!\n";
116
        return 1;
117 118 119 120 121 122
    }

    my $vnode = $vnodes[0];

    if (!defined($vnode)) {
        print "Could not create vnode associated with $pnode!\n";
123
        return 1;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    }

    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;

141
        return 0;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 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
    }

    # 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";
245
        return 0;
246 247
    }

248 249 250
    # Setup log entry prefix
    my $logmsg = "$self->{'NAME'}, $pnode->{'name'}, ";

251 252 253 254 255 256
    # 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 ".
257
          "$pnode->{'name'} (pid: $chpid) @ ". `date`;
258 259 260 261 262 263 264

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

        if (!$exstat) {
            print "Teardown of $pnode->{'vnode'} complete\n";
265
            Log(STATUSLOG, $logmsg . "teardown, success, teardown succeeded.");
266 267
        } else {
            print "Teardown of $pnode->{'vnode'} failed: $exstat\n";
268
            Log(STATUSLOG, $logmsg . "teardown, fail, teardown failed.");
269 270 271 272 273 274 275 276 277 278 279 280 281 282
        }

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

284 285 286
        return 1;
    }

287
    # Test exit status for setup mode(s)
288 289 290 291
    SWRN1: for ($exstat) {
        WIFSIGNALED($_) && do {
            if ($now > $pnode->{'timeout'}) {
                print "Timeout waiting for $pnode->{'vnode'} to instantiate.\n";
292
                $logmsg .= "setup, fail, timeout waiting for node.";
293 294 295
            }
            else {
                print "Setup of $pnode->{'vnode'} killed for unknown reason.\n";
296
                $logmsg .= "setup, fail, killed - unknown reason.";
297 298 299 300 301 302 303
            }
            $bad = 1;
            last SWRN1;
        };

        WEXITSTATUS($_) == $BADINST && do {
            print "Instantiation of $pnode->{'vnode'} failed.\n";
304
            $logmsg .= "setup, fail, vserver instantiation failed.";
305 306 307 308 309 310
            $bad = 1;
            last SWRN1;
        };

        WEXITSTATUS($_) > 0 && do {
            print "Vnodesetup failed on $pnode->{'vnode'}.\n";
311
            $logmsg .= "setup, fail, vnodesetup failed (excode: $exstat).";
312 313 314 315 316 317 318 319 320 321 322 323 324
            $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);
325
        Log(STATUSLOG, $logmsg);
326 327 328
    } else {
        # Setup a timeout to wait for ISUP.
        $pnode->{'timeout'} = $now + $ISUPWAITTIME;
329
    }
330
    return 0;
331 332 333 334 335 336 337 338
}

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

341
    foreach my $pnode (values %{ $self->{'PENDING'} }) {        
342 343 344 345

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

381
    return $numfinished;
382 383 384 385 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
}


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

414
        # Try to ssh in and kill processes running in the vserver.
415 416 417 418 419 420
        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());

421 422 423
        # Free the vserver, if possible.
        exec "$PLABNODE -f free $PLABTESTING_PID $PLABTESTING_EID $vnode" or
            die "Doh!  Can't exec command!\n";
424 425 426 427 428 429 430
    }

    # NOTREACHED
}


# XXX: may be bogus, but it'll do for now.
431
my $MININTERVAL = 600;
432 433 434 435 436 437
my $MAXINTERVAL = 3600;
sub calcnextcheck($$;$) {
    my ($self, $pnode, $reason) = @_;

    my $now = time();

438 439 440 441
    my $numsuccess = $pnode->{'consecsuccess'} ? $pnode->{'consecsuccess'} : 1;
    my $nextint    = int(2 * $numsuccess * $MININTERVAL);
    $pnode->{'nextchecktime'} = 
        $now + MIN($MAXINTERVAL, $nextint) + int(rand(120));
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
}

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

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

477 478 479
    #
    # It failed to come up. Move the pnode to hwdown...
    #
480
    DBQueryWarn("update reserved set exptidx=$exptidx, ".
481 482 483 484 485 486 487 488 489 490 491 492
                "  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; ".
                      "plab node $pnode->{'vnode'} setup failed by monitor.'");
}

# Make perl happy...
1;