diff options
author | Oliver Tappe | 2007-05-06 18:47:32 +0200 |
---|---|---|
committer | Oliver Tappe | 2007-05-06 18:47:32 +0200 |
commit | d4d2aa85c9484f672eb93518476343276eb07ab0 (patch) | |
tree | f24d94b7ad70386ca88a0218a77c1e503886a321 /config-db | |
parent | * fixed minor problem with superfluous rm warning (diff) | |
download | core-d4d2aa85c9484f672eb93518476343276eb07ab0.tar.gz core-d4d2aa85c9484f672eb93518476343276eb07ab0.tar.xz core-d4d2aa85c9484f672eb93518476343276eb07ab0.zip |
* added new table global_info to DB-schema, in order to support nbd-server-ports
* added transaction support to config-DB
* added support for incrementing global counters (which live in global_info)
git-svn-id: http://svn.openslx.org/svn/openslx/trunk@1011 95ad53e4-c205-0410-b2fa-d234c58c8868
Diffstat (limited to 'config-db')
-rw-r--r-- | config-db/OpenSLX/ConfigDB.pm | 73 | ||||
-rw-r--r-- | config-db/OpenSLX/DBSchema.pm | 21 | ||||
-rw-r--r-- | config-db/OpenSLX/MetaDB/CSV.pm | 55 | ||||
-rw-r--r-- | config-db/OpenSLX/MetaDB/DBI.pm | 42 |
4 files changed, 175 insertions, 16 deletions
diff --git a/config-db/OpenSLX/ConfigDB.pm b/config-db/OpenSLX/ConfigDB.pm index 7143598d..971a9c8e 100644 --- a/config-db/OpenSLX/ConfigDB.pm +++ b/config-db/OpenSLX/ConfigDB.pm @@ -195,6 +195,27 @@ sub disconnect $self->{'meta-db'}->disconnect(); } +sub start_transaction +{ + my $self = shift; + + $self->{'meta-db'}->start_transaction(); +} + +sub commit_transaction +{ + my $self = shift; + + $self->{'meta-db'}->commit_transaction(); +} + +sub rollback_transaction +{ + my $self = shift; + + $self->{'meta-db'}->rollback_transaction(); +} + sub fetchVendorOSByFilter { my $self = shift; @@ -247,6 +268,14 @@ sub fetchExportIDsOfVendorOS return $self->{'meta-db'}->fetchExportIDsOfVendorOS($vendorOSID); } +sub fetchGlobalInfo +{ + my $self = shift; + my $id = shift; + + return $self->{'meta-db'}->fetchGlobalInfo($id); +} + sub fetchSystemByFilter { my $self = shift; @@ -392,22 +421,37 @@ sub changeVendorOS return $self->{'meta-db'}->changeVendorOS($vendorOSIDs, $valRows); } -sub getNextExportCounterForVendorOS +sub incrementExportCounterForVendorOS { my $self = shift; my $id = shift; - # TODO: fix this to be resilient against parallel execution - # (use a transaction) + $self->start_transaction(); my $vendorOS = $self->fetchVendorOSByID($id); return undef unless defined $vendorOS; my $exportCounter = $vendorOS->{export_counter}+1; - $self->changeVendorOS($id, { 'export_counter' => $exportCounter, }); + $self->changeVendorOS($id, { 'export_counter' => $exportCounter }); + $self->commit_transaction(); return $exportCounter; } +sub incrementGlobalCounter +{ + my $self = shift; + my $counterName = shift; + + $self->start_transaction(); + my $value = $self->fetchGlobalInfo($counterName); + return undef unless defined $value; + my $newValue = $value+1; + $self->changeGlobalInfo($counterName, $newValue); + $self->commit_transaction(); + + return $value; +} + sub addExport { my $self = shift; @@ -433,6 +477,15 @@ sub changeExport return $self->{'meta-db'}->changeExport($exportIDs, $valRows); } +sub changeGlobalInfo +{ + my $self = shift; + my $id = shift; + my $value = shift; + + return $self->{'meta-db'}->changeGlobalInfo($id, $value); +} + sub addSystem { my $self = shift; @@ -904,9 +957,17 @@ sub aggregatedSystemFileInfoFor if ($exportURI !~ m[\w]) { # auto-generate export_uri if none has been given: my $type = $export->{'type'}; - my $serverIpToken = generatePlaceholderFor('serverip'); + my $serverIpToken + = length($export->{server_ip}) + ? $export->{server_ip} + : generatePlaceholderFor('serverip'); + my $port + = length($export->{port}) + ? ":$export->{port}" + : ''; $exportURI - = "$type://$serverIpToken$openslxConfig{'export-path'}/$type/$export->{name}"; + = $type.'://'.$serverIpToken.$port.$openslxConfig{'export-path'} + .'/'.$type.'/'.$export->{name}; } my $info = { %$system }; diff --git a/config-db/OpenSLX/DBSchema.pm b/config-db/OpenSLX/DBSchema.pm index 88bbc1a8..ef8ed0d3 100644 --- a/config-db/OpenSLX/DBSchema.pm +++ b/config-db/OpenSLX/DBSchema.pm @@ -111,6 +111,11 @@ $DbSchema = { 'vendor_os_id:fk', # foreign key 'comment:s.1024', # internal comment (optional, for admins) 'type:s.10', # 'nbd-squash', 'nfs', ... + 'server_ip:s.16', # IP of exporting server, if empty the + # boot server will be used + 'port:i', # some export types need to use a specific + # port for each incarnation, if that's the + # case you can specify it here 'uri:s.256', # path to export (squashfs or NFS-path), if # empty it will be auto-generated by # config-demuxer @@ -176,6 +181,11 @@ $DbSchema = { 'group_id:fk', # foreign key 'system_id:fk', # foreign key ], + 'global_info' => [ + # a home for global counters and other info + 'id:s.32', # key + 'value:s.128', # value + ], }, }; @@ -315,6 +325,17 @@ if ($lang =~ m[^\w\w_(\w\w)]) { 'table' => 'group_system_ref', 'cols' => $DbSchema->{'tables'}->{'group_system_ref'}, }, + { + 'cmd' => 'add-table', + 'table' => 'global_info', + 'cols' => $DbSchema->{'tables'}->{'global_info'}, + 'vals' => [ + { # add nbd-server-port + 'id' => 'next-nbd-server-port', + 'value' => '5000', + }, + ], + }, ], ); diff --git a/config-db/OpenSLX/MetaDB/CSV.pm b/config-db/OpenSLX/MetaDB/CSV.pm index b92c2665..5101f939 100644 --- a/config-db/OpenSLX/MetaDB/CSV.pm +++ b/config-db/OpenSLX/MetaDB/CSV.pm @@ -31,7 +31,7 @@ use OpenSLX::MetaDB::DBI $VERSION; my $superVersion = $OpenSLX::MetaDB::DBI::VERSION; if ($superVersion < $VERSION) { - confess _tr('Unable to load module <%s> (Version <%s> required, but <%s> found)', + confess _tr("Unable to load module '%s' (Version '%s' required, but '%s' found)", 'OpenSLX::MetaDB::DBI', $VERSION, $superVersion); } ################################################################################ @@ -68,7 +68,7 @@ sub connect so there is no support for %s available, sorry!\n%s], 'DBD::CSV', 'CSV', $@); $self->{'dbh'} = DBI->connect("dbi:CSV:$dbSpec", undef, undef, {PrintError => 0}) - or confess _tr("Cannot connect to database <%s> (%s)", + or confess _tr("Cannot connect to database '%s' (%s)", $dbSpec, $DBI::errstr); } @@ -82,20 +82,57 @@ sub quote return "'$val'"; } +sub start_transaction +{ # simulate a global transaction by flocking a file: + my $self = shift; + + my $dbh = $self->{'dbh'}; + my $lockFile = "$dbh->{'f_dir'}/transaction-lock"; + sysopen(TRANSFILE, $lockFile, O_RDWR|O_CREAT) + or confess _tr(q[Can't open transaction-file '%s' (%s)], $lockFile, $!); + $self->{"transaction-lock"} = *TRANSFILE; + flock(TRANSFILE, LOCK_EX) + or confess _tr(q[Can't lock transaction-file '%s' (%s)], $lockFile, $!); +} + +sub commit_transaction +{ # free transaction-lock + my $self = shift; + + if (!defined $self->{"transaction-lock"}) { + confess _tr(q[no open transaction-lock found!]); + } + close($self->{"transaction-lock"}); + $self->{"transaction-lock"} = undef; + return 1; +} + +sub rollback_transaction +{ # free transaction-lock + my $self = shift; + + if (!defined $self->{"transaction-lock"}) { + confess _tr(q[no open transaction-lock found!]); + } + close($self->{"transaction-lock"}); + $self->{"transaction-lock"} = undef; + return 1; +} + sub generateNextIdForTable -{ # CSV doesn't provide any mechanism to generate IDs, we just... +{ # CSV doesn't provide any mechanism to generate IDs, we provide one my $self = shift; my $table = shift; return 1 unless defined $table; - # ...fetch the next ID from a table-specific file: + # fetch the next ID from a table-specific file: my $dbh = $self->{'dbh'}; my $idFile = "$dbh->{'f_dir'}/id-$table"; sysopen(IDFILE, $idFile, O_RDWR|O_CREAT) - or confess _tr(q[Can't open ID-file <%s> (%s)], $idFile, $!); + or confess _tr(q[Can't open ID-file '%s' (%s)], $idFile, $!); flock(IDFILE, LOCK_EX) - or confess _tr(q[Can't lock ID-file <%s> (%s)], $idFile, $!); + or confess _tr(q[Can't lock ID-file '%s' (%s)], $idFile, $!); my $nextID = <IDFILE>; if (!$nextID) { # no ID information available, we protect against users having @@ -111,11 +148,11 @@ sub generateNextIdForTable $nextID = 1+$maxID; } seek(IDFILE, 0, 0) - or confess _tr(q[Can't to seek ID-file <%s> (%s)], $idFile, $!); + or confess _tr(q[Can't to seek ID-file '%s' (%s)], $idFile, $!); truncate(IDFILE, 0) - or confess _tr(q[Can't truncate ID-file <%s> (%s)], $idFile, $!); + or confess _tr(q[Can't truncate ID-file '%s' (%s)], $idFile, $!); print IDFILE $nextID+1 - or confess _tr(q[Can't update ID-file <%s> (%s)], $idFile, $!); + or confess _tr(q[Can't update ID-file '%s' (%s)], $idFile, $!); close(IDFILE); return $nextID; diff --git a/config-db/OpenSLX/MetaDB/DBI.pm b/config-db/OpenSLX/MetaDB/DBI.pm index 75d5a001..b7025e65 100644 --- a/config-db/OpenSLX/MetaDB/DBI.pm +++ b/config-db/OpenSLX/MetaDB/DBI.pm @@ -46,12 +46,33 @@ sub disconnect } sub quote -{ # default implementation quotes any given values through the DBD-driver +{ # default implementation quotes any given values through the DBI my $self = shift; return $self->{'dbh'}->quote(@_); } +sub start_transaction +{ # default implementation passes on the request to the DBI + my $self = shift; + + return $self->{'dbh'}->begin_work(); +} + +sub commit_transaction +{ # default implementation passes on the request to the DBI + my $self = shift; + + return $self->{'dbh'}->commit(); +} + +sub rollback_transaction +{ # default implementation passes on the request to the DBI + my $self = shift; + + return $self->{'dbh'}->rollback(); +} + ################################################################################ ### data access functions ################################################################################ @@ -159,6 +180,16 @@ sub fetchExportIDsOfVendorOS return $self->_doSelect($sql, 'id'); } +sub fetchGlobalInfo +{ + my $self = shift; + my $id = shift; + + return if !length($id); + my $sql = "SELECT * FROM global_info WHERE id = ".$self->quote($id); + return $self->_doSelect($sql, 'value'); +} + sub fetchSystemByFilter { my $self = shift; @@ -556,6 +587,15 @@ sub changeExport return $self->_doUpdate('export', $exportIDs, $valRows); } +sub changeGlobalInfo +{ + my $self = shift; + my $id = shift; + my $value = shift; + + return $self->_doUpdate('global_info', [$id], [{ 'value' => $value }] ); +} + sub addSystem { my $self = shift; |