plabmon_badpool.pm.in 12.6 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
# All rights reserved.
#

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

package plabmon_badpool;

14
#use strict;
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;
24
use libplabmon;
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...
30
my $TEARDOWNTIMEOUT = 300;   # XXX: ...
31 32 33 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 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

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

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

    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) {
        return;
    }

    # 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";
        return;
    }

    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;

        return
    }

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

        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";
217
        return 1;
218 219
    }

220 221 222
    # Setup log entry prefix
    my $logmsg = "$self->{'NAME'}, $pnode->{'name'}, ";

223 224 225 226 227 228
    # 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 ".
229
          "$pnode->{'name'} (pid: $chpid) @ ". `date`;
230 231 232 233 234 235

    # 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";
236
            Log(STATUSLOG, $logmsg . "teardown, success, teardown succeeded.");
237 238
        } else {
            print "Teardown of $pnode->{'vnode'} failed: $exstat\n";
239
            Log(STATUSLOG, $logmsg . "teardown, fail, teardown failed.");
240
        }
241

242 243 244 245 246 247 248
        return 1;
    }

    SWRN1: for ($exstat) {
        WIFSIGNALED($_) && do {
            if ($now > $pnode->{'timeout'}) {
                print "Timeout waiting for $pnode->{'vnode'} to instantiate.\n";
249
                $logmsg .= "setup, fail, timeout waiting for node.";
250 251 252
            }
            else {
                print "Setup of $pnode->{'vnode'} killed for unknown reason.\n";
253
                $logmsg .= "setup, fail, killed - unknown reason.";
254 255 256 257 258 259 260 261
            }
            $bad = 1;
            last SWRN1;
        };

        WEXITSTATUS($_) == $BADINST && do {
            print "Instantiation of $pnode->{'vnode'} failed.\n";
            $bad = 1;
262
            $logmsg .= "setup, fail, vserver instantiation failed.";
263 264 265 266 267
            last SWRN1;
        };

        WEXITSTATUS($_) > 0 && do {
            print "Vnodesetup failed on $pnode->{'vnode'}.\n";
268
            $logmsg .= "setup, fail, vnodesetup failed (excode: $exstat).";
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
            $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'}++;
287
        Log(STATUSLOG, $logmsg);
288 289 290 291 292
    } else {
        # Instantiation was successful.
        # Setup a timeout to wait for ISUP.
        $pnode->{'timeout'} = $now + $ISUPWAITTIME;
    }
293

294
    return 0;
295 296 297 298 299 300 301 302
}

#
# XXX: Comment me
#
sub checkexpiration($) {
    my $self = shift;
    my $now = time();
303
    my $numfinished = 0;
304 305 306 307 308 309

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

        # ISUP or TBFAILED?  Check for these before timeout.
        my $state = TBDB_NODESTATE_UNKNOWN();
        if (TBGetNodeEventState($pnode->{'vnode'}, \$state)) {
310 311 312 313 314
            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";
315 316
                    Log(STATUSLOG, "$self->{'NAME'}, $pnode->{'name'}, ".
                        "setup, success, node came up successfully.");
317 318 319 320 321 322 323 324 325 326
                    $self->nodesetupcomplete($pnode);
                    $numfinished++;
                    next;
                }
                elsif ($state eq TBDB_NODESTATE_TBFAILED()) {
                    $self->teardownnode($pnode);
                    $self->calcnextcheck($pnode);
                    $pnode->{'consecfails'}++;
                    next;
                }
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
            }
        } 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'}++;
            }
        }
    }

350
    return $numfinished;
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
}


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;
395
my $MAXINTERVAL = 12 * 3600;
396 397 398 399 400
sub calcnextcheck($$;$) {
    my ($self, $pnode, $reason) = @_;

    my $now = time();

401 402 403 404
    my $numfails  = $pnode->{'consecfails'} ? $pnode->{'consecfails'} : 1;
    my $nextint = int($numfails * $numfails * $MININTERVAL);
    $pnode->{'nextchecktime'} = 
        $now + MIN($MAXINTERVAL, $nextint) + int(rand(120));
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
}

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

420 421 422 423 424 425
    my $exptidx;
    if (!TBExptIDX($PLABHOLDING_PID, $PLABHOLDING_EID, \$exptidx)) {
	print "*** WARNING: No such experiment $PLABHOLDING_EID!\n";
	return;
    }

426 427 428 429
    #
    # It came up! Move the pnode out of hwdown and back into
    # normal holding experiment.
    #
430
    DBQueryWarn("update reserved set exptidx=$exptidx, ".
431 432 433 434 435 436 437 438 439 440 441 442 443 444
                "  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; ".
                      "plab node $pnode->{'vnode'} setup okay by monitor.'");

    # XXX: move to goodpool.

    return;
}

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

471

472 473
# Make perl happy...
1;