Commit e4173cfa authored by Mike Hibler's avatar Mike Hibler
Browse files

Reduce from 3 DB queries per node to 1 total query.

Reduce the running time by 50%...from 1 second to 0.5 seconds.
A marvel of pointless optimization!  At the time it seemed like it might
matter, but the case that is really slow (requiring a call to nalloc per
node) is still slow.  But it was a twenty minute diversion.
parent a4abf46f
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# #
# EMULAB-COPYRIGHT # EMULAB-COPYRIGHT
# Copyright (c) 2000-2003, 2007 University of Utah and the Flux Group. # Copyright (c) 2000-2010 University of Utah and the Flux Group.
# All rights reserved. # All rights reserved.
# #
...@@ -79,20 +79,40 @@ if (! $experiment) { ...@@ -79,20 +79,40 @@ if (! $experiment) {
} }
my $exptidx = $experiment->idx(); my $exptidx = $experiment->idx();
#
# Use the awesome power of left join:
#
# - if we get no row returned, then the node does not exist,
# - if we get a nodes entry but no reserved entry, the node is free,
# - if we get both, then it is allocated and pid/eid is where it is now.
#
# This single query on all nodes replaces the three queries per-node
# we used to do.
#
my $nlist = "(" . join(",", map("'$_'", @nodes)) . ")";
my $sth = DBQueryFatal("SELECT n.node_id AS nt,r.node_id AS rt,r.pid,r.eid".
" FROM nodes AS n LEFT JOIN reserved AS r".
" ON n.node_id=r.node_id WHERE n.node_id in $nlist");
#
# Make a list of the nodes we got nodes table info for, others don't exist
#
my %nmap = ();
while (my $rref = $sth->fetchrow_hashref()) {
$nmap{$rref->{'nt'}} = $rref;
}
foreach my $node (@nodes) { foreach my $node (@nodes) {
my $pc = $node; my $pc = $node;
my $allocated = 0; my $allocated = 0;
my $nref = $nmap{$node};
$sth = DBQueryFatal("select * from nodes where node_id='$pc'"); if (!defined($nref)) {
if ($sth->num_rows() != 1) {
print STDERR "Node $pc doesn't exist. Skipping $pc.\n"; print STDERR "Node $pc doesn't exist. Skipping $pc.\n";
next; next;
} }
print "Checking if $pc is reserved..."; print "Checking if $pc is reserved...";
$sth = DBQueryFatal("select * from reserved where node_id='$pc'"); if (!$nref->{'rt'}) {
if ( ($sth->num_rows()) < 1) {
print "Available - Reserving...\n"; print "Available - Reserving...\n";
#print STDERR "Using proj $pid, expt $eid, I am ",`whoami`; #print STDERR "Using proj $pid, expt $eid, I am ",`whoami`;
my $cmd = "$nalloc $pid $eid $pc"; my $cmd = "$nalloc $pid $eid $pc";
...@@ -100,13 +120,11 @@ foreach my $node (@nodes) { ...@@ -100,13 +120,11 @@ foreach my $node (@nodes) {
print STDERR "WARNING: Could not reserve $pc!\n"; print STDERR "WARNING: Could not reserve $pc!\n";
} }
} else { } else {
$sth = DBQueryFatal("select * from reserved where node_id='$pc' and ". if (!($pid eq $nref->{'pid'} && $eid eq $nref->{'eid'})) {
"pid='$pid' and eid='$eid'");
if ( ($sth->num_rows()) < 1) {
print "Reserved - Scheduling next reservation...\n"; print "Reserved - Scheduling next reservation...\n";
$sth = DBQueryFatal("replace into next_reserve ". $sth = DBQueryFatal("REPLACE INTO next_reserve ".
" (node_id,exptidx,pid,eid) ". " (node_id,exptidx,pid,eid) ".
"values ('$pc','$exptidx','$pid','$eid')"); "VALUES ('$pc','$exptidx','$pid','$eid')");
} else { } else {
print "Reserved - Already reserved to $pid/$eid\n"; print "Reserved - Already reserved to $pid/$eid\n";
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment