Blockstore.pm 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 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 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
#!/usr/bin/perl -wT
#
# Copyright (c) 2012-2013 University of Utah and the Flux Group.
# 
# {{{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/>.
# 
# }}}
#
package Blockstore;

use strict;
use Carp;
use Exporter;
use vars qw(@ISA @EXPORT $AUTOLOAD);

@ISA = qw(Exporter);
@EXPORT = qw ( );

use libdb;
use libtestbed;
use English;
use Data::Dumper;
use overload ('""' => 'Stringify');

my $debug	= 0;

#
# Lookup a (physical) storage object type and create a class instance to 
# return.
#
sub Lookup($$$)
{
    my ($class, $nodeid, $bsid) = @_;

    return undef
	if (! ($nodeid =~ /^[-\w]+$/ && $bsid =~ /^[-\w]+$/));

    my $query_result =
	DBQueryWarn("select * from blockstores ".
		    "where node_id='$nodeid' and bs_id='$bsid'");

    return undef
	if (!$query_result || !$query_result->numrows);

    my $self         = {};
    $self->{"HASH"}  = {};
    $self->{"DBROW"} = $query_result->fetchrow_hashref();

    bless($self, $class);
    return $self;
}

# To avoid writing out all the methods.
AUTOLOAD {
    my $self  = $_[0];
    my $name  = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    # A DB row proxy method call.
    if (exists($self->{'DBROW'}->{$name})) {
	return $self->{'DBROW'}->{$name};
    }

    # Local storage slot.
    if ($name =~ /^_.*$/) {
	if (scalar(@_) == 2) {
	    return $self->{'HASH'}->{$name} = $_[1];
	}
	elsif (exists($self->{'HASH'}->{$name})) {
	    return $self->{'HASH'}->{$name};
	}
    }
    carp("No such slot '$name' field in $self");
    return undef;
}

# Break circular reference someplace to avoid exit errors.
sub DESTROY {
    my $self = shift;

    $self->{"DBROW"}    = undef;
    $self->{"HASH"}     = undef;
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $bsidx   = $self->bsidx();
    my $bs_id   = $self->bs_id();
    my $node_id = $self->node_id();

    return "[BlockStore:$bsidx, $bs_id, $node_id]";
}

#
# Blockstores are reserved to a pcvm; that is how we do the
# bookkeeping. When a node is released (nfree), we can find
# the reserved blockstores for that node, reset the capacity
# in the blockstore_state table, and delete the row(s).
#
sub Reserve($$$$$)
{
    my ($self, $experiment, $vnode_id, $bs_name, $bs_size) = @_;
    my $exptidx    = $experiment->idx();
    my $pid        = $experiment->pid();
    my $eid        = $experiment->eid();
    my $bsidx      = $self->bsidx();
    my $bs_id      = $self->bs_id();
    my $bs_node_id = $self->node_id();
    my $remaining_capacity;

    DBQueryWarn("lock tables blockstores read, ".
		"            reserved_blockstores write, ".
		"            blockstore_state write")
	or return -1;

    #
    # Need the remaining size to make sure we can allocate it.
    #
    my $query_result =
	DBQueryWarn("select remaining_capacity from blockstore_state ".
		    "where bsidx='$bsidx'");
    goto bad
	if (!$query_result);

    #
    # Just in case the state row is missing, okay to create it.
    #
    if (!$query_result->numrows) {
	$remaining_capacity = $self->total_size();

	DBQueryWarn("insert into blockstore_state set ".
		    "  bsidx='$bsidx', node_id='$bs_node_id', bs_id='$bs_id', ".
		    "  remaining_capacity='$remaining_capacity', 'ready=1'")
	    or goto bad;
    }
    else {
	($remaining_capacity) = $query_result->fetchrow_array();
    }
    if ($bs_size > $remaining_capacity) {
	print STDERR "Not enough remaining capacity on $bsidx\n";
	goto bad;
    }

    #
    # If we do not have a reservation row, create one with a zero
    # size, to indicate nothing has actually been reserved in the
    # blockstore_state table.
    #
    $query_result =
	DBQueryWarn("select size from reserved_blockstores ".
		    "where exptidx='$exptidx' and bsidx='$bsidx' and ".
		    "       vname='$bs_name'");
    goto bad
	if (!$query_result);

    if (! $query_result->numrows) {
	if (! DBQueryWarn("insert into reserved_blockstores set ".
	        "  bsidx='$bsidx', node_id='$bs_node_id', bs_id='$bs_id', ".
	        "  vname='$bs_name', pid='$pid', eid='$eid', ".
		"  size='0', vnode_id='$vnode_id', ".
	        "  exptidx='$exptidx', rsrv_time=now()")) {
	    goto bad;
	}
    }
    else {
	my ($current_size) = $query_result->fetchrow_array();

	#
	# At the moment, I am not going to allow the blockstore
	# to change size. 
	#
	if ($current_size && $current_size != $bs_size) {
	    print STDERR "Not allowed to change size of existing store\n";
	    goto bad;
	}

	#
	# If already have a reservation size, then this is most
	# likely a swapmod, and we can just return without doing
	# anything.
	#
	goto done
	    if ($current_size);
    }

    #
    # Now do an atomic update that changes both tables.
    #
    if (!DBQueryWarn("update blockstore_state,reserved_blockstores set ".
		"     remaining_capacity=remaining_capacity-${bs_size}, ".
		"     size='$bs_size' ".
		"where blockstore_state.bsidx=reserved_blockstores.bsidx and ".
		"      blockstore_state.bs_id=reserved_blockstores.bs_id and ".
		"      reserved_blockstores.bsidx='$bsidx' and ".
		"      reserved_blockstores.exptidx='$exptidx' and ".
		"      reserved_blockstores.vnode_id='$vnode_id'")) {
	goto bad;
    }
done:
    DBQueryWarn("unlock tables");
    return 0;
  bad:
    DBQueryWarn("unlock tables");
    return -1;
}

############################################################################
#
# Package to describe a specific reservation of a blockstore.
#
package Blockstore::Reservation;
use libdb;
use libtestbed;
use English;
use vars qw($AUTOLOAD);
use overload ('""' => 'Stringify');

#
# Lookup a blockstore reservation.
#
sub Lookup($$$$)
{
    my ($class, $blockstore, $experiment, $vname) = @_;

    return undef
	if (! ($vname =~ /^[-\w]+$/ && ref($blockstore) && ref($experiment)));

    my $exptidx = $experiment->idx();
    my $bsidx   = $blockstore->bsidx();

    my $query_result =
	DBQueryWarn("select * from reserved_blockstores ".
		    "where exptidx='$exptidx' and bsidx='$bsidx' and ".
		    "       vname='$vname'");

    return undef
	if (!$query_result || !$query_result->numrows);

    my $self         = {};
    $self->{"HASH"}  = {};
    $self->{"DBROW"} = $query_result->fetchrow_hashref();

    bless($self, $class);
    return $self;
}

#
# Look for the blockstore associated with a pcvm. At the present
# time, one blockstore is mapped to one pcvm. 
#
sub LookupByNodeid($$)
{
    my ($class, $vnode_id) = @_;

    my $query_result =
	DBQueryWarn("select * from reserved_blockstores ".
		    "where vnode_id='$vnode_id'");

    return undef
	if (!$query_result || !$query_result->numrows);

    if ($query_result->numrows != 1) {
	print STDERR "Too many blockstores for $vnode_id!\n";
	return -1;
    }

    my $self         = {};
    $self->{"HASH"}  = {};
    $self->{"DBROW"} = $query_result->fetchrow_hashref();

    bless($self, $class);
    return $self;
}

# To avoid writing out all the methods.
AUTOLOAD {
    my $self  = $_[0];
    my $name  = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    # A DB row proxy method call.
    if (exists($self->{'DBROW'}->{$name})) {
	return $self->{'DBROW'}->{$name};
    }
    carp("No such slot '$name' field in $self");
    return undef;
}

# Break circular reference someplace to avoid exit errors.
sub DESTROY {
    my $self = shift;

    $self->{"DBROW"}    = undef;
    $self->{"HASH"}     = undef;
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $bsidx   = $self->bsidx();
    my $bs_id   = $self->bs_id();
    my $node_id = $self->node_id();
    my $vname   = $self->vname();

    return "[BlockStore:$bsidx, $bs_id, $node_id ($vname)]";
}

#
# Blockstores are reserved to a pcvm; that is how we do the
# bookkeeping. When a node is released (nfree), we can find
# the reserved blockstore for that node, reset the capacity
# in the blockstore_state table, and delete the row(s).
#
sub Release($)
{
    my ($self)     = @_;
    my $exptidx    = $self->exptidx();
    my $bsidx      = $self->bsidx();
    my $bs_id      = $self->bs_id();
    my $bs_node_id = $self->node_id();
    my $vnode_id   = $self->vnode_id();
    my $size       = $self->size();

    DBQueryWarn("lock tables blockstores read, ".
		"            reserved_blockstores write, ".
		"            blockstore_state write")
	or return -1;

    #
    # Need the remaining size to deallocate.
    #
    my $query_result =
	DBQueryWarn("select remaining_capacity from blockstore_state ".
		    "where bsidx='$bsidx'");
    goto bad
	if (!$query_result);

    if (!$query_result->numrows) {
	print STDERR "No blockstore state for $bsidx\n";
	goto bad;
    }

    #
    # We want to atomically uupdate update remaining_capacity and
    # set the size in the reservation to zero, so that if we fail,
    # nothing has changed.
    #
    if (!DBQueryWarn("update blockstore_state,reserved_blockstores set ".
		"     remaining_capacity=remaining_capacity+size, ".
		"     size=0-size ".
		"where blockstore_state.bsidx=reserved_blockstores.bsidx and ".
		"      blockstore_state.bs_id=reserved_blockstores.bs_id and ".
		"      reserved_blockstores.bsidx='$bsidx' and ".
		"      reserved_blockstores.exptidx='$exptidx' and ".
		"      reserved_blockstores.vnode_id='$vnode_id'")) {
	goto bad;
    }
    # That worked, so now we can delete the reservation row.
    DBQueryWarn("delete from reserved_blockstores ".
		"where reserved_blockstores.bsidx='$bsidx' and ".
		"      reserved_blockstores.exptidx='$exptidx' and ".
		"      reserved_blockstores.vnode_id='$vnode_id'")
	or goto bad;

    DBQueryWarn("unlock tables");
    return 0;
  bad:
    DBQueryWarn("unlock tables");
    return -1;
}

# _Always_ make sure that this 1 is at the end of the file...
1;