resources.pl 21.5 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2007, 2008 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 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
#
use English;

use lib "/usr/testbed/lib";
use libdb;
use libtestbed;

$| = 1;

my $impotent      = 0;
my %bytimestamp   = ();
my %resources     = ();
my %mapping       = ();
my %testbed_stats = ();
my $nextidx;
my $nullrowref    = {};

sub copy_hash($$) {
    my ($new, $old) = @_;
    
    foreach (keys(%$old)) {
	$new->{$_} = $old->{$_};
    }
}

sub InsertRecord($$$$$;$)
{
    my ($exptidx, $copyidx, $statidx, $unixtime, $uid_idx, $newrsrc) = @_;
    
    $unixtime++;

    my $query_result = DBQueryFatal("select FROM_UNIXTIME('$unixtime')");
    ($tstamp) = $query_result->fetchrow_array();
    my $rowref = {};
    $newrsrc = $nextidx++
	if (!defined($newrsrc));

    copy_hash($rowref, $resources{$copyidx});
    $rowref->{'idx'}             = $newrsrc;
    $rowref->{'tstamp'}          = $tstamp;
    $rowref->{'swapin_time'}     = 0;
    $rowref->{'uid_idx'}         = $uid_idx;
    $rowref->{'swapout_time'}    = 0;
    $rowref->{'swapmod_time'}    = 0;
    
    $rowref->{'save'}            = 1;
    $rowref->{'unixtime'}        = $unixtime;

    $resources{$newrsrc} = $rowref;
    $bytimestamp{$unixtime} = $rowref;
    
    # testbed_stats gets the new pointer.
    $testbed_stats{$statidx} = $newrsrc;

    $mapping{$copyidx} = $newrsrc;
    print "Mapping $copyidx to $newrsrc ($tstamp)\n";
    return $newrsrc;
}

sub UpdateResourceRecord($$$$;$$$)
{
    my ($exptidx, $rsrcidx, $action, $tstamp, $uididx,
	$byswapmod, $byswapin) = @_;

    print "Updating resource record $rsrcidx. action=$action\n";
    my $rowref = $resources{$rsrcidx};

    $rowref->{'byswapin'}  = $byswapin
	if (defined($byswapin));
    $rowref->{'byswapmod'} = $byswapmod
	if (defined($byswapmod));
	
    if ($action eq "swapin" || $action eq "start") {
	$rowref->{'swapin_time'}    = $tstamp;
	$rowref->{'uid_idx'}        = $uididx;
    }
    elsif ($action eq "swapout") {
	$rowref->{'swapout_time'}   = $tstamp;
    }
    elsif ($action eq "preload") {
	$rowref->{'uid_idx'}        = $uididx;
    }
    elsif ($action eq "swapmod") {
	$rowref->{'swapmod_time'}   = $tstamp;
	$rowref->{'uid_idx'}        = $uididx
	    if (defined($uididx));
    }
    elsif ($action eq "noaction") {
	$rowref->{'uid_idx'}        = $uididx
	    if (defined($uididx));
    }
    $rowref->{'save'} = 1;
}

sub ClearResourceRecord($)
{
    my ($rsrcidx) = @_;
    my $rowref = $resources{$rsrcidx};

    foreach my $slot ("vnodes",
		      "pnodes",
		      "wanodes",
		      "plabnodes",
		      "simnodes",
		      "jailnodes",
		      "delaynodes",
		      "linkdelays",
		      "walinks",
		      "links",
		      "lans",
		      "shapedlinks",
		      "shapedlans",
		      "wirelesslans",
		      "minlinks",
		      "maxlinks") {

	$rowref->{$slot} = 0;
    }
    $rowref->{'save'} = 1;
}

DBQueryFatal("create table if not exists experiment_resources_new ".
	     "like experiment_resources");
DBQueryFatal("delete from experiment_resources_new");
DBQueryFatal("insert into experiment_resources_new ".
	     "select * from experiment_resources");

DBQueryFatal("create table if not exists experiment_stats_new ".
	     "like experiment_stats");
DBQueryFatal("delete from experiment_stats_new");
DBQueryFatal("insert into experiment_stats_new ".
	     "select * from experiment_stats");

DBQueryFatal("create table if not exists testbed_stats_new ".
	     "like testbed_stats");
DBQueryFatal("delete from testbed_stats_new");
DBQueryFatal("insert into testbed_stats_new ".
	     "select * from testbed_stats");

if (0) {
    DBQueryFatal("lock tables ".
		 "   experiments write, " .
		 "   testbed_stats write, " .
		 "   experiment_stats write, " .
		 "   experiment_resources write, " .
		 "   testbed_stats_new write, " .
		 "   experiment_stats_new write, " .
		 "   experiment_resources_new write");
}

my $query_result =
    DBQueryFatal("select MAX(idx) from experiment_resources_new");
($nextidx) = $query_result->fetchrow_array();
$nextidx += 100000;

178 179 180 181 182 183 184 185 186 187 188
$query_result =
     DBQueryFatal("select e.pid,e.eid,e.idx,r.idx,".
		 "     e.expt_head_uid,e.creator_idx,e.expt_created ".
		 "  from experiments as e ".
		 "left join testbed_stats_new as s on e.idx=s.exptidx ".
		 "left join experiment_resources_new as r on ".
		 "     r.exptidx=e.idx ".
		 "where s.exptidx is null and e.pid='emulab-ops'");

while (my ($pid,$eid,$exptidx,$rsrcidx,$uid,$uid_idx,$created) =
	$query_result->fetchrow_array()) {
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
     print "Adding missing testbed _stats records for $pid,$eid,$exptidx\n";

     if (!defined($rsrcidx)) {
	 if (!$impotent) {
	     my $resource_result =
		 DBQueryWarn("insert into experiment_resources_new ".
			     "(tstamp, exptidx, uid_idx) ".
			     "values ('$created', $exptidx, $uid_idx)");
	     $rsrcidx = $resource_result->insertid;
	 }
	 else {
	     $rsrcidx = 1;
	 }
	 DBQueryWarn("update experiment_stats_new set ".
		     " rsrcidx=$rsrcidx ".
		     "where exptidx=$exptidx")
	     if (!$impotent);
     }
     
     my $query = "insert into testbed_stats_new ".
	 "(idx, start_time, end_time, exptidx, rsrcidx, action, ".
	 " exitcode, uid, uid_idx) values ".
	 "(NULL, '$created', '$created', $exptidx, $rsrcidx, ".
	 " 'new', 0, '$uid', '$uid_idx')";
     if ($impotent) {
	 print "$query\n";
     }
     else {
	 DBQueryFatal($query);
     }
219 220
}

221
$query_result =
222
    DBQueryFatal("select s.exptidx,e.state from experiment_stats_new as s ".
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 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 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 430 431 432 433 434 435 436 437 438 439 440 441 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 471 472 473 474 475 476 477 478 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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
		 "left join experiments as e on e.idx=s.exptidx ".
#		 "where s.exptidx=8751 or s.exptidx=30605 or s.exptidx=13307 ".
#		 "  or s.exptidx=19412 ".
#		 "where s.exptidx=30605  ".
		 "order by idx");

while (my ($exptidx,$expstate) = $query_result->fetchrow_array()) {
    my $state = "nostate";
    my $skip  = 0;
    my $munge  = 0;
    my $lastrsrc = 0;
    my $swapins = 0;
    my @stats = ();
    my $laststamp = 0;
    %resources = ();
    %testbed_stats = ();
    %mapping   = ();
    %bytimestamp = ();

    # See where the current rsrcidx and lastidx are pointing in case
    # they need to be updated.
    my $estats_result =
	DBQueryFatal("select rsrcidx,lastrsrc from experiment_stats_new ".
		     "where exptidx='$exptidx'");
    my ($expt_rsrcidx,$expt_lastidx) = $estats_result->fetchrow_array();

    print "\n";
    print "--------------------------------------------------\n";
    print "$exptidx $expt_rsrcidx " .
	(defined($expt_lastidx) ? $expt_lastidx : "") . "\n";

    #
    # Generate a list of the resource records that we can munge
    # here, and then write back later.
    #
    my $resources_result =
	DBQueryFatal("select *,UNIX_TIMESTAMP(tstamp) as unixtime ".
		     "   from experiment_resources_new ".
		     "where exptidx='$exptidx'");

    while (my $rowref = $resources_result->fetchrow_hashref()) {
	my $idx = $rowref->{'idx'};
	my $unixtime = $rowref->{'unixtime'};

	$resources{$idx} = $rowref;
	$bytimestamp{$unixtime} = $rowref;
    }

    my $stats_result =
	DBQueryFatal("select *,UNIX_TIMESTAMP(start_time) as start_time, ".
		     "       UNIX_TIMESTAMP(end_time) as end_time ".
		     "  from testbed_stats_new ".
		     "where exptidx='$exptidx' ".
		     "order by UNIX_TIMESTAMP(end_time)");

    while (my $rowref = $stats_result->fetchrow_hashref()) {
	push(@stats, $rowref);

	my $rsrcidx = $rowref->{'rsrcidx'};
	$resources{$rsrcidx}->{'save'} = 1;
    }
    
    foreach my $rowref (@stats) {
	my $idx     = $rowref->{'idx'};
	my $rsrcidx = $rowref->{'rsrcidx'};
	my $lastidx = $resources{$rsrcidx}->{'lastidx'};
	my $action  = $rowref->{'action'};
	my $ecode   = $rowref->{'exitcode'};
	my $tstamp  = $rowref->{'start_time'};
	my $uid_idx = $rowref->{'uid_idx'};
	my $lastecode = 0;
	my $lastaction = "";
	$tstamp = $rowref->{'end_time'}
	    if (!$tstamp);

	$lastrsrc = $rsrcidx
	    if (!$lastrsrc);
	$lastidx = 0
	    if (!defined($lastidx));

	print "$idx $rsrcidx $lastidx $tstamp $action $ecode\n";

	# Not sure how this happens, but want to kill this record.
	if ($action eq "swapin" && $state eq "active" &&
	    $rsrcidx != $lastrsrc) {
	    next;
	}

	# Skip errors unless its a swapout. Odds are it really did swapout
	# and we can just ignore a subsequent swapout if there is one.
	if ($ecode) {
	    goto skip
		if ($action ne "swapout");
	}
	
	#
	# Figure out experiment state as we process the actions. If we
	# get into trouble, we bail.
	#
	if ($state eq "nostate") {
	    # Must start with a preload or a start, although some early
	    # records start with a swapin.
	    if ($action eq "preload") {
		$state = "swapped";
	    }
	    elsif ($action eq "start" || $action eq "swapin") {
		$state = "active";
		$swapins++;
	    }
	    else {
		print "Bad action ($action) in state $state for $exptidx\n";
		$skip = 1;
		last;
	    }
	}
	elsif ($state eq "swapped") {
	    # Must be a swapmod or a swapin or a destroy
	    if ($action eq "swapin") {
		$state = "active";
		if ($rsrcidx == $lastrsrc) {
		    print "swapped/active: need to fix records for $exptidx\n";
		    $munge = 1;
		}
		$swapins++;
	    }
	    elsif ($action eq "swapmod") {
		# No change to state.
		if ($rsrcidx == $lastrsrc && $lastidx) {
		    print "swapped/swapmod: ".
			"Need to fix records for $exptidx\n";
		    $munge = 1;
		}
	    }
	    elsif ($action eq "destroy") {
		$state = "done";
	    }
	    elsif ($action eq "swapout") {
		# No change; assume a swapout error with no ecode.
		;
	    }
	    else {
		print "Bad action ($action) in state $state for $exptidx\n";
		$skip = 1;
		last;
	    }
	}
	elsif ($state eq "active") {
	    # Must be a swapmod or a swapout
	    if ($action eq "swapout") {
		if ($rsrcidx != $lastrsrc) {
		    print "swapout/active: Need to fix records for $exptidx\n";
		    $munge = 1;
		}
		$state = "swapped";
	    }
	    elsif ($action eq "swapmod") {
		# No change to state.
		if ($rsrcidx == $lastrsrc) {
		    print "swapmod/active: Need to fix records for $exptidx\n";
		    $munge = 1;
		}
	    }
	    elsif ($action eq "swapin") {
		# This seems to happen a lot; ignore the record. 
		;
	    }
	    else {
		print "Bad action ($action) in state $state for $exptidx\n";
		$skip = 1;
		last;
	    }
	}
	elsif ($state eq "done") {
	    print "Bad action ($action) in state $state for $exptidx\n";
	    $skip = 1;
	    last;
	}
      skip:
	$lastrsrc   = $rsrcidx;
	$lastecode  = $ecode;
	$lastaction = $action;
    }
    #
    # If we get here and the state is still active, something went wrong
    # in the testbed_stats gathering, and we need to fix things up by hand.
    #
    if ($state eq "active") {
	print "Experiment still active at end of stats: $exptidx\n";
	$munge = 1;
    }
    next
	if (0);

    next
	if ($skip);

    print "-----\n";

    $state = "nostate";
    $lastrsrc = 0;
    $swapins = 0;

    foreach my $rowref (@stats) {
	my $idx     = $rowref->{'idx'};
	my $copyidx = $rowref->{'rsrcidx'};
	my $rsrcidx = $copyidx;
	my $action  = $rowref->{'action'};
	my $ecode   = $rowref->{'exitcode'};
	my $uid_idx = $rowref->{'uid_idx'};
	my $tstamp  = $rowref->{'start_time'};
	$tstamp = $rowref->{'end_time'}
    	    if (!$tstamp);

	if (exists($mapping{$rsrcidx})) {
	    my $newrsrc = $mapping{$rsrcidx};
	    # testbed_stats gets the new pointer.
	    $testbed_stats{$idx} = $newrsrc;
	    $rowref->{'rsrcidx'} = $newrsrc;
	    $rsrcidx = $newrsrc;
	}
	my $lastidx = $resources{$rsrcidx}->{'lastidx'};
	$lastidx = 0
	    if (!defined($lastidx));
	
	$lastrsrc = $rsrcidx
	    if (!$lastrsrc);

	print "$idx $rsrcidx $lastidx $tstamp $action $ecode\n";

	# Not sure how this happens, but want to kill this record.
	if ($action eq "swapin" && $state eq "active" &&
	    $rsrcidx != $lastrsrc) {
	    next;
	}
	
	# Skip errors unless its a swapout. Odds are it really did swapout
	# and we can just ignore a subsequent swapout if there is one.
	goto skip
	    if ($ecode && $action ne "swapout");

	#
	# Figure out experiment state as we process the actions. If we
	# get into trouble, we bail.
	#
	if ($state eq "nostate") {
	    # Must start with a preload or a start, although some early
	    # records start with a swapin.
	    if ($action eq "preload") {
		$state = "swapped";
	    }
	    elsif ($action eq "start" || $action eq "swapin") {
		$state = "active";
		$swapins++;
	    }
	    else {
		print "Bad action ($action) in state $state for $exptidx\n";
		$skip = 1;
		last;
	    }
	}
	elsif ($state eq "swapped") {
	    # Must be a swapmod or a swapin or a destroy
	    if ($action eq "swapin") {
		$state = "active";
		if ($rsrcidx == $lastrsrc) {
		    print "swapped/active: fixing $rsrcidx for $exptidx\n";
		    $rsrcidx = InsertRecord($exptidx, $copyidx,
					    $idx, $tstamp, $uid_idx);
		    $rowref->{'rsrcidx'} = $rsrcidx;
		}
		$swapins++;
	    }
	    elsif ($action eq "swapmod") {
		# No change to state.
		if ($rsrcidx == $lastrsrc && $lastidx) {
		    print "swapped/swapmod: Fixing record for $exptidx\n";
		    $rsrcidx = InsertRecord($exptidx, $copyidx,
					    $idx, $tstamp, $uid_idx);
		    $rowref->{'rsrcidx'} = $rsrcidx;
		}
	    }
	    elsif ($action eq "destroy") {
		$state = "done";
	    }
	    elsif ($action eq "swapout") {
		# No change; assume a swapout error with no ecode.
		;
	    }
	    else {
		print "Bad action ($action) in state $state for $exptidx\n";
		$skip = 1;
		last;
	    }
	}
	elsif ($state eq "active") {
	    # Must be a swapmod or a swapout
	    if ($action eq "swapout") {
		if ($rsrcidx != $lastrsrc) {
		    print "swapout/active: Backing up one for $exptidx\n";
		    next;
		}
		$state = "swapped";
	    }
	    elsif ($action eq "swapmod") {
		# No change to state.
		if ($rsrcidx == $lastrsrc) {
		    print "swapmod/active: Need to fix records for $exptidx\n";
		    $rsrcidx = InsertRecord($exptidx, $copyidx,
					    $idx, $tstamp, $uid_idx);
		    $rowref->{'rsrcidx'} = $rsrcidx;
		}
	    }
	    elsif ($action eq "swapin") {
		# This seems to happen a lot; ignore the record. 
		;
	    }
	    else {
		print "Bad action ($action) in state $state for $exptidx\n";
		$skip = 1;
		last;
	    }
	}
	elsif ($state eq "done") {
	    print "Bad action ($action) in state $state for $exptidx\n";
	    $skip = 1;
	    last;
	}
      skip:
	$lastrsrc = $rsrcidx;
    }

    # Kill off ones we did not use.
    foreach my $idx (keys(%resources)) {
	my $rowref   = $resources{$idx};
	my $save     = $rowref->{'save'};
	my $unixtime = $rowref->{'unixtime'};

	if (!$save) {
	    print "D: $idx\n";
	    DBQueryFatal("delete from experiment_resources_new ".
563 564
			 "where idx=$idx")
		if (!$impotent);
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
	    delete($bytimestamp{$unixtime});
	    delete($resources{$idx});
	}
    }

    # Reorder the records.
    my @order = sort {$a <=> $b} keys(%bytimestamp);
    my $this  = pop(@order);
    while (@order) {
	my $next = pop(@order);
	my $thisidx = $bytimestamp{$this}->{'idx'};
	my $nextidx = $bytimestamp{$next}->{'idx'};
	my $lastidx = $bytimestamp{$this}->{'lastidx'};
	print "$this $next $thisidx $nextidx\n";
	if (!defined($lastidx) || $lastidx != $bytimestamp{$next}->{'idx'}) {
	    $bytimestamp{$this}->{'lastidx'} = $bytimestamp{$next}->{'idx'};
	}
	$this = $next;
    }

    print "-----\n";

    $state = "nostate";
    $lastrsrc = 0;
    $swapins = 0;

    foreach my $rowref (@stats) {
	my $idx     = $rowref->{'idx'};
	my $rsrcidx = $rowref->{'rsrcidx'};
	my $action  = $rowref->{'action'};
	my $ecode   = $rowref->{'exitcode'};
	my $uid_idx = $rowref->{'uid_idx'};
	my $lastidx = $resources{$rsrcidx}->{'lastidx'};
	my $tstamp  = $rowref->{'start_time'};
	
	$tstamp = $rowref->{'end_time'}
    	    if (!$tstamp);
	$lastidx = 0
	    if (!defined($lastidx));
	
	$lastrsrc = $rsrcidx
	    if (!$lastrsrc);

	print "$idx $rsrcidx $lastidx $action $ecode\n";

	# Not sure how this happens, but want to kill this record.
	if ($action eq "swapin" && $state eq "active" &&
	    $rsrcidx != $lastrsrc) {
	    next;
	}

	# Skip errors unless its a swapout. Odds are it really did swapout
	# and we can just ignore a subsequent swapout if there is one.
	goto skip
	    if ($ecode && $action ne "swapout");

	#
	# Figure out experiment state as we process the actions. If we
	# get into trouble, we bail.
	#
	if ($state eq "nostate") {
	    # Must start with a preload or a start, although some early
	    # records start with a swapin.
	    if ($action eq "preload") {
		UpdateResourceRecord($exptidx, $rsrcidx, $action,
				     $tstamp, $uid_idx);
		$state = "swapped";
	    }
	    elsif ($action eq "start" || $action eq "swapin") {
		$state = "active";
		UpdateResourceRecord($exptidx, $rsrcidx, $action,
				     $tstamp, $uid_idx, 0,
				     ($action eq "swapin" ? 1 : 0));
		$swapins++;
	    }
	    else {
		print "Bad action ($action) in state $state for $exptidx\n";
	    }
	}
	elsif ($state eq "swapped") {
	    # Must be a swapmod or a swapin or a destroy
	    if ($action eq "swapin") {
		$state = "active";
		UpdateResourceRecord($exptidx, $rsrcidx, $action,
				     $tstamp, $uid_idx, 0, 1);
		$swapins++;
	    }
	    elsif ($action eq "swapmod") {
		# No change to state.
		UpdateResourceRecord($exptidx, $lastrsrc, $action,
				     $tstamp, $uid_idx);
		UpdateResourceRecord($exptidx, $rsrcidx,
				     "noaction", $tstamp, $uid_idx, 1, 0);
		ClearResourceRecord($rsrcidx);
	    }
	    elsif ($action eq "destroy") {
		$state = "done";
	    }
	    elsif ($action eq "swapout") {
		# No change; assume a swapout error with no ecode.
		;
	    }
	    else {
		print "Bad action ($action) in state $state for $exptidx\n";
	    }
	}
	elsif ($state eq "active") {
	    # Must be a swapmod or a swapout
	    if ($action eq "swapout") {
		my $change = ($rsrcidx != $lastrsrc ? $lastrsrc : $rsrcidx);
		
		UpdateResourceRecord($exptidx, $change, $action,
				     $tstamp, $uid_idx);
		$state = "swapped";
	    }
	    elsif ($action eq "swapmod") {
		# No change to state.
		# Previous record gets swapmod timestamp
		UpdateResourceRecord($exptidx, $lastidx, $action, $tstamp);
		# Current record gets swapin timestamp
		UpdateResourceRecord($exptidx, $rsrcidx, "swapin",
				     $tstamp, $uid_idx, 1, 0);
	    }
	    elsif ($action eq "swapin") {
		# This seems to happen a lot; ignore the record. 
		;
	    }
	    else {
		print "Bad action ($action) in state $state for $exptidx\n";
	    }
	}
	elsif ($state eq "done") {
	    print "Bad action ($action) in state $state for $exptidx\n";
	}
      skip:
	$lastrsrc = $rsrcidx;
	$laststamp = $tstamp;
    }
    if ($state eq "active" &&
	(!defined($expstate) || $expstate eq "swapped" ||
	 ($expstate eq "swapping" && (time() - $laststamp) > (3600 * 24)))) {
	print "Fixing still active at end of stats: $exptidx\n";

	UpdateResourceRecord($exptidx, $lastrsrc, "swapout", $laststamp + 1);
    }
    
    foreach my $idx (keys(%resources)) {
	my $rowref = $resources{$idx};
	my $lastidx = $rowref->{'lastidx'};
	my $swapin_time = $rowref->{'swapin_time'};
	my $swapout_time = $rowref->{'swapout_time'};
	my $tstamp = $rowref->{'tstamp'};
	$lastidx = 0
	    if (! defined($lastidx));

	next
	    if (!defined($tstamp));

	print "$idx, $lastidx, $tstamp, $swapin_time, $swapout_time\n";

	# We added this above.
	delete($rowref->{'unixtime'});
	delete($rowref->{'save'});

	my $query = "replace into experiment_resources_new set ";
	my @keys  = keys(%$rowref);

	while (@keys) {
	    my $key = pop(@keys);
	    my $val = $rowref->{$key};
		
	    if ($key eq "thumbnail") {
737 738
		$query .= "thumbnail=" . DBQuoteSpecial($val)
		    if (!$impotent);
739 740 741 742 743 744 745 746 747 748
	    }
	    elsif (!defined($val)) {
		$query .= "$key=NULL";
	    }
	    else {
		$query .= "$key='$val'";
	    }
	    $query .= ","
		if (@keys);
	}
749 750 751 752 753 754
	if ($impotent) {
	    print "$query\n";
	}
	else {
	    DBQueryFatal($query);
	}
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
    }

    foreach my $idx (keys(%testbed_stats)) {
	my $rsrcidx = $testbed_stats{$idx};

	print "Update testbed_stats:$idx to $rsrcidx\n";

	if (! $impotent) {
	    DBQueryFatal("update testbed_stats_new set rsrcidx=$rsrcidx ".
			 "where idx=$idx");
	}
    }
    if (exists($mapping{$expt_rsrcidx})) {
	my $rsrcidx = $mapping{$expt_rsrcidx};
	
	print "Update experiment_stats:$expt_rsrcidx to $rsrcidx\n";

	if (! $impotent) {
	    DBQueryFatal("update experiment_stats_new set rsrcidx=$rsrcidx ".
			 "where exptidx=$exptidx");
	}
    }
Leigh Stoller's avatar
Leigh Stoller committed
777 778 779 780
    if (! $impotent) {
	DBQueryFatal("update experiment_stats_new set lastrsrc=NULL ".
		     "where exptidx=$exptidx");
    }    
781 782 783 784 785 786 787 788 789 790 791 792
}

# Last bit of cleanup for records that are too hosed to worry about.
$query_result =
    DBQueryFatal("select e.idx,r.idx from experiment_resources_new as r ".
		 " left join experiments as e on e.idx=r.exptidx ".
		 "where e.state='swapped' and swapin_time!=0 and ".
		 "      swapout_time=0 and swapmod_time=0 and pnodes>0");
while (my ($exptidx,$rsrcidx) = $query_result->fetchrow_array()) {
    print "Cleaning up $exptidx,$rsrcidx\n";
    DBQueryFatal("update experiment_resources_new set ".
		 "  swapout_time=swapout_time+1 ".
793 794
		 "where idx=$rsrcidx")
	if (!$impotent);
795 796
}

797
if (!$impotent) {
Leigh Stoller's avatar
Leigh Stoller committed
798
#   DBQueryFatal("unlock tables");
799 800 801 802 803 804 805 806
    DBQueryFatal("rename table ".
		 "testbed_stats to testbed_stats_backup, ".
		 "experiment_stats to experiment_stats_backup, ".
		 "experiment_resources to experiment_resources_backup, ".
		 "testbed_stats_new to testbed_stats, ".
		 "experiment_stats_new to experiment_stats, ".
		 "experiment_resources_new to experiment_resources");
}