IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 20965


Ignore:
Timestamp:
Dec 12, 2008, 11:13:41 AM (17 years ago)
Author:
jhoblitt
Message:

first attempt at supporting multiple databases

Location:
trunk
Files:
1 added
18 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous-Server/MANIFEST

    r20091 r20965  
    3535t/00_distribution.t
    3636t/01_load.t
     37t/02_config.t
    3738t/02_server_setup.t
    3839t/03_server_create_object.t
  • trunk/Nebulous-Server/lib/Nebulous/Server.pm

    r20091 r20965  
    11# Copyright (c) 2004-2008  Joshua Hoblitt
    22#
    3 # $Id: Server.pm,v 1.93 2008-10-13 20:41:17 jhoblitt Exp $
     3# $Id: Server.pm,v 1.94 2008-12-12 21:13:41 jhoblitt Exp $
    44
    55package Nebulous::Server;
     
    99no warnings qw( uninitialized );
    1010
    11 our $VERSION = '0.15';
     11our $VERSION = '0.16';
    1212
    1313use base qw( Class::Accessor::Fast );
     
    1919use File::Spec;
    2020use Log::Log4perl;
     21use Nebulous::Keys qw( parse_neb_key parse_neb_volume );
    2122use Nebulous::Server::Config;
    2223use Nebulous::Server::Log;
    2324use Nebulous::Server::SQL;
    24 use Nebulous::Keys qw( parse_neb_key parse_neb_volume );
    2525use Params::Validate qw( validate_pos SCALAR SCALARREF UNDEF );
    2626use URI::file;
     
    3636
    3737    # let Nebulous::Server::Config validate our params
    38     my $config = Nebulous::Server::Config->init( @_ );
     38    my $config = Nebulous::Server::Config->new( @_ );
    3939
    4040    # log4perl is not avaliable until we call init()
     
    5151    $self->config($config);
    5252
    53     # ask for the db handle as a means of validating the database parameters
    54     $self->db;
    55 
    5653    $log->debug( "leaving" );
    5754   
     
    6259sub db
    6360{
    64     my $self = shift;
    65 
    66     if (@_) {
    67         $self->{db} = $_[0];
    68         return $self;
    69     }
     61    my ($self, $key) = @_;
    7062
    7163    my $log     = $self->log;
     
    7365    my $config  = $self->config;
    7466
     67    my $db_index = 0;
     68    if (defined $key) {
     69        # hash the key to select the correct database instance
     70        $db_index = unpack("%20h", sha1_hex("$key")) % $config->n_db;
     71    }
     72
     73    # lookup to see if we have a stored dbh for this database
     74    my $dbh = $self->{dbs}[$db_index];
    7575    # if the dbh is still alive, return it
    76     if (defined $self->{db} and $self->{db}->ping) {
     76    if (defined $dbh and $dbh->ping) {
    7777        $log->debug("db handle is still alive");
    78         return $self->{db};
     78        return $dbh;
    7979    }
    8080    # otherwise create a new connection
    8181    $log->debug("db handle is dead/unopened");
     82
     83    # lookup database info
     84    my $db_config = $config->db($db_index);
     85    die "can't find database configuration info for database # $db_index"
     86        unless $db_config;
    8287
    8388    # if we're running under mod_perl & Apache::DBI is loaded we want to
     
    8691    # processes and the database might have gone away on us.  Apache::DBI will
    8792    # take care of getting a valid dbh back.
    88     my $db;
    89     eval {
    90         $db = DBI->connect_cached(
    91             $config->dsn,
    92             $config->dbuser,
    93             $config->dbpasswd,
     93    eval {
     94        $dbh = DBI->connect_cached(
     95            $db_config->dsn,
     96            $db_config->dbuser,
     97            $db_config->dbpasswd,
    9498            {
    9599                RaiseError => 1,
     
    99103        );
    100104
    101         $db->do( $sql->set_transaction_model );
    102         $log->debug( "connected to database: ", sub { $db->data_sources; } );
    103         $db->commit;
     105        $dbh->do( $sql->set_transaction_model );
     106        $log->debug( "connected to database: ", sub { $dbh->data_sources; } );
     107        $dbh->commit;
    104108        $log->debug("commit");
    105109    };
    106110    if ( $@ ) {
    107         $db->rollback if $db;
     111        $dbh->rollback if $dbh;
    108112        $log->debug("rollback");
    109113        $log->logdie( "database error: $@" );
    110114    }
    111115
    112     $self->{db} = $db;
    113 
    114     return $db;
     116    $self->{dbs}[$db_index] = $dbh;
     117
     118    return $dbh;
    115119}
    116120
     
    139143    my $log = $self->log;
    140144    my $sql = $self->sql;
    141     my $db  =$self->db;
     145    my $db  = $self->db($key);
    142146
    143147    $log->debug( "entered - @_" );
     
    150154    # to check it after parsing the key
    151155    if (defined $vol_name
    152         and not $self->_is_valid_volume_name($key->volume)) {
     156        and not $self->_is_valid_volume_name($key, $key->volume)) {
    153157        if ($key->soft_volume) {
    154158            $log->warn( "$vol_name is not a known volume name" );
     
    160164       
    161165    my ($vol_id, $vol_host, $vol_path, $vol_xattr)
    162         = $self->_get_storage_volume($vol_name, $key->soft_volume);
     166        = $self->_get_storage_volume($key, $vol_name, $key->soft_volume);
    163167
    164168    my $uri;
     
    261265    my $log = $self->log;
    262266    my $sql = $self->sql;
    263     my $db  =$self->db;
     267    my $db  = $self->db($key);
    264268
    265269    $log->debug("entered - @_");
     
    296300
    297301
    298 sub swap_objects
    299 {
    300     my $self = shift;
    301 
    302     my ($key1, $key2) = validate_pos(@_,
    303         {
    304             type        => SCALAR,
    305             callbacks   => {
    306                 'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
    307             },
    308         },
    309         {
    310             type        => SCALAR,
    311             callbacks   => {
    312                 'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
    313             },
    314         },
    315     );
    316 
    317     my $log = $self->log;
    318     my $sql = $self->sql;
    319     my $db  =$self->db;
    320 
    321     $log->debug("entered - @_");
    322 
    323     # ignore volumes
    324     $key1 = parse_neb_key($key1);
    325     $key2 = parse_neb_key($key2);
    326 
    327     # order of operations for the swap:
    328     # key1 -> key1.swap
    329     # key2 -> key1
    330     # key1.swap -> key2
    331 
    332     eval {
    333         {
    334             # key1 -> key1.swap
    335             my $query = $db->prepare_cached($sql->rename_object);
    336             # this SQL statment takes the new key name as the first param
    337             my $rows = $query->execute($key1->path . ".swap", $key1->path);
    338 
    339             # if we affected more then one row something very bad has happened.
    340             unless ($rows == 1) {
    341                 $query->finish;
    342                 $log->logdie("affected row count is $rows instead of 1");
    343             }
    344         }
    345 
    346         {
    347             # key2 -> key1
    348             my $query = $db->prepare_cached($sql->rename_object);
    349             # this SQL statment takes the new key name as the first param
    350             my $rows = $query->execute($key1->path, $key2->path);
    351 
    352             # if we affected more then one row something very bad has happened.
    353             unless ($rows == 1) {
    354                 $query->finish;
    355                 $log->logdie("affected row count is $rows instead of 1");
    356             }
    357         }
    358 
    359         {
    360             # key1.swap -> key2
    361             my $query = $db->prepare_cached($sql->rename_object);
    362             # this SQL statment takes the new key name as the first param
    363             my $rows = $query->execute($key2->path, $key1->path . ".swap");
    364 
    365             # if we affected more then one row something very bad has happened.
    366             unless ($rows == 1) {
    367                 $query->finish;
    368                 $log->logdie("affected row count is $rows instead of 1");
    369             }
    370         }
    371 
    372         $db->commit;
    373         $log->debug("commit");
    374     };
    375     if ($@) {
    376         $db->rollback;
    377         $log->debug("rollback");
    378         $log->logdie("database error: $@");
    379     }
    380 
    381     $log->debug("leaving");
    382 
    383     return 1;
    384 }
     302# sub swap_objects
     303# {
     304#     my $self = shift;
     305#
     306#     my ($key1, $key2) = validate_pos(@_,
     307#         {
     308#             type        => SCALAR,
     309#             callbacks   => {
     310#                 'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
     311#             },
     312#         },
     313#         {
     314#             type        => SCALAR,
     315#             callbacks   => {
     316#                 'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
     317#             },
     318#         },
     319#     );
     320#
     321#     my $log  = $self->log;
     322#     my $sql  = $self->sql;
     323#     my $dbh1 = $self->db($key1);
     324#     my $dbh2 = $self->db($key2);
     325#
     326#     $log->debug("entered - @_");
     327#
     328#     # ignore volumes
     329#     $key1 = parse_neb_key($key1);
     330#     $key2 = parse_neb_key($key2);
     331#
     332#     # order of operations for the swap with a single db is:
     333#     # key1 -> key1.swap
     334#     # key2 -> key1
     335#     # key1.swap -> key2
     336#
     337#     # XXX this cmp will only work if ->db() returns the same exact (cached) dbh
     338#     if ($dbh1 == $dbh2) {
     339#         my $dbh = $dbh1;
     340#         eval {
     341#             {
     342#                 # key1 -> key1.swap
     343#                 my $query = $dbh->prepare_cached($sql->rename_object);
     344#                 # this SQL statment takes the new key name as the first param
     345#                 my $rows = $query->execute($key1->path . ".swap", $key1->path);
     346#
     347#                 # if we affected more then one row something very bad has happened.
     348#                 unless ($rows == 1) {
     349#                     $query->finish;
     350#                     $log->logdie("affected row count is $rows instead of 1");
     351#                 }
     352#             }
     353#
     354#             {
     355#                 # key2 -> key1
     356#                 my $query = $dbh->prepare_cached($sql->rename_object);
     357#                 # this SQL statment takes the new key name as the first param
     358#                 my $rows = $query->execute($key1->path, $key2->path);
     359#
     360#                 # if we affected more then one row something very bad has happened.
     361#                 unless ($rows == 1) {
     362#                     $query->finish;
     363#                     $log->logdie("affected row count is $rows instead of 1");
     364#                 }
     365#             }
     366#
     367#             {
     368#                 # key1.swap -> key2
     369#                 my $query = $dbh->prepare_cached($sql->rename_object);
     370#                 # this SQL statment takes the new key name as the first param
     371#                 my $rows = $query->execute($key2->path, $key1->path . ".swap");
     372#
     373#                 # if we affected more then one row something very bad has happened.
     374#                 unless ($rows == 1) {
     375#                     $query->finish;
     376#                     $log->logdie("affected row count is $rows instead of 1");
     377#                 }
     378#             }
     379#
     380#             $dbn->commit;
     381#             $log->debug("commit");
     382#         };
     383#         if ($@) {
     384#             $dbh->rollback;
     385#             $log->debug("rollback");
     386#             $log->logdie("database error: $@");
     387#         }
     388#     }
     389#
     390#     # order of operations for the swap between two dbs is:
     391#     # key1 start transaction
     392#     # key1 -> read all instances
     393#     # key1 -> remove all instances
     394#     # key2 start transaction
     395#     # key2 -> read all instances
     396#     # key2 -> remove all instances
     397#     # key1 -> insert key 2 instances
     398#     # key2 -> insert key 1 instances
     399#     # key1,2 commit
     400#
     401#     eval {
     402#         {
     403#             # key1 -> read all instances
     404#             my $query = $dbh->prepare_cached($sql->rename_object);
     405#             # this SQL statment takes the new key name as the first param
     406#             my $rows = $query->execute($key1->path . ".swap", $key1->path);
     407#
     408#             # if we affected more then one row something very bad has happened.
     409#             unless ($rows == 1) {
     410#                 $query->finish;
     411#                 $log->logdie("affected row count is $rows instead of 1");
     412#             }
     413#         }
     414#
     415#         {
     416#             # key2 -> key1
     417#             my $query = $db->prepare_cached($sql->rename_object);
     418#             # this SQL statment takes the new key name as the first param
     419#             my $rows = $query->execute($key1->path, $key2->path);
     420#
     421#             # if we affected more then one row something very bad has happened.
     422#             unless ($rows == 1) {
     423#                 $query->finish;
     424#                 $log->logdie("affected row count is $rows instead of 1");
     425#             }
     426#         }
     427#
     428#         {
     429#             # key1.swap -> key2
     430#             my $query = $db->prepare_cached($sql->rename_object);
     431#             # this SQL statment takes the new key name as the first param
     432#             my $rows = $query->execute($key2->path, $key1->path . ".swap");
     433#
     434#             # if we affected more then one row something very bad has happened.
     435#             unless ($rows == 1) {
     436#                 $query->finish;
     437#                 $log->logdie("affected row count is $rows instead of 1");
     438#             }
     439#         }
     440#
     441#         $db->commit;
     442#         $log->debug("commit");
     443#     };
     444#         if ($@) {
     445#             $db->rollback;
     446#             $log->debug("rollback");
     447#             $log->logdie("database error: $@");
     448#         }
     449#
     450#
     451#     $log->debug("leaving");
     452#
     453#     return 1;
     454# }
    385455
    386456
     
    420490    my $log = $self->log;
    421491    my $sql = $self->sql;
    422     my $db  =$self->db;
     492    my $db  = $self->db($key);
    423493
    424494    $log->debug("entered - @_");
     
    429499
    430500    if (defined $vol_name
    431         and not $self->_is_valid_volume_name($key->volume)) {
     501        and not $self->_is_valid_volume_name($key, $key->volume)) {
    432502        if ($key->soft_volume) {
    433503            $log->warn( "$vol_name is not a known volume name" );
     
    441511    if (defined $vol_name) {
    442512        ($vol_id, $vol_host, $vol_path, $vol_xattr)
    443             = $self->_get_storage_volume($vol_name);
     513            = $self->_get_storage_volume($key, $vol_name);
    444514    } else {
    445515        ($vol_id, $vol_host, $vol_path, $vol_xattr)
     
    534604    my $log = $self->log;
    535605    my $sql = $self->sql;
    536     my $db  =$self->db;
     606    my $db  = $self->db($key);
    537607
    538608    $log->debug( "entered - @_" );
     
    637707    my $log = $self->log;
    638708    my $sql = $self->sql;
    639     my $db  =$self->db;
     709    my $db  = $self->db($key);
    640710
    641711    $log->debug( "entered - @_" );
     
    749819    my $log = $self->log;
    750820    my $sql = $self->sql;
    751     my $db  =$self->db;
     821    my $db  = $self->db($key);
    752822
    753823    $log->debug("entered - @_");
     
    816886    my $log = $self->log;
    817887    my $sql = $self->sql;
    818     my $db  =$self->db;
     888    my $db  = $self->db($key);
    819889
    820890    $log->debug("entered - @_");
     
    869939    my $log = $self->log;
    870940    my $sql = $self->sql;
    871     my $db  =$self->db;
     941    my $db  = $self->db($key);
    872942
    873943    $log->debug("entered - @_");
     
    912982    my $log = $self->log;
    913983    my $sql = $self->sql;
    914     my $db  =$self->db;
     984    my $db  = $self->db($key);
    915985
    916986    $log->debug("entered - @_");
     
    9471017sub find_objects
    9481018{
    949     my $self = shift;
    950 
    951     my ( $pattern ) = validate_pos( @_,
     1019    # XXX: this will only search one db
     1020
     1021    my $self = shift;
     1022
     1023    my ($pattern) = validate_pos( @_,
    9521024        {
    9531025            type        => SCALAR,
     
    9581030    my $log = $self->log;
    9591031    my $sql = $self->sql;
    960     my $db  =$self->db;
     1032    my $db  = $self->db;
    9611033
    9621034    $log->debug( "entered - @_" );
     
    10161088    my $log = $self->log;
    10171089    my $sql = $self->sql;
    1018     my $db  =$self->db;
     1090    my $db  = $self->db($key);
    10191091
    10201092    $log->debug("entered - @_");
     
    10271099    # to check it after parsing the key
    10281100    if (defined $vol_name
    1029         and not $self->_is_valid_volume_name($key->volume)) {
     1101        and not $self->_is_valid_volume_name($key, $key->volume)) {
    10301102        if ($key->soft_volume) {
    10311103            $log->warn( "$vol_name is not a known volume name" );
     
    10871159    my $self = shift;
    10881160
    1089     my ( $uri ) = validate_pos( @_,
     1161    my ($key, $uri) = validate_pos( @_,
     1162        {
     1163            type        => SCALAR,
     1164            callbacks   => {
     1165                'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
     1166            },
     1167        },
    10901168        {
    10911169            type => SCALAR|SCALARREF,
     
    10951173    my $log = $self->log;
    10961174    my $sql = $self->sql;
    1097     my $db  =$self->db;
     1175    my $db  = $self->db($key);
    10981176
    10991177    $log->debug( "entered - @_" );
     
    11821260    my $log = $self->log;
    11831261    my $sql = $self->sql;
    1184     my $db  =$self->db;
     1262    my $db  = $self->db($key);
    11851263
    11861264    $log->debug("entered - @_");
     
    12111289sub mounts
    12121290{
     1291    # XXX: this will only pull the mounts from one db
    12131292    my $self = shift;
    12141293
     
    12171296    my $log = $self->log;
    12181297    my $sql = $self->sql;
    1219     my $db  =$self->db;
     1298    my $db  = $self->db;
    12201299
    12211300    $log->debug("entered - @_");
     
    12461325    my $self = shift;
    12471326
    1248     my $log = $self->log;
    1249     my $sql = $self->sql;
    1250     my $db  = $self->db;
     1327    my ($key, $name, $soft_volume) = @_;
     1328
     1329    my $log = $self->log;
     1330    my $sql = $self->sql;
     1331    my $db  = $self->db($key);
    12511332
    12521333    no warnings qw( uninitialized );
    12531334    $log->debug( "entered - @_" );
    12541335    use warnings;
    1255 
    1256     my ($name, $soft_volume) = @_;
    12571336
    12581337    my ($vol_id, $vol_host, $vol_path, $xattr);
     
    12711350                # find it, fall back to any volume
    12721351                if ($soft_volume) {
    1273                     ($vol_id, $vol_host, $vol_path, $xattr) = $self->_get_storage_volume;
     1352                    ($vol_id, $vol_host, $vol_path, $xattr) = $self->_get_storage_volume($key);
    12741353                    return; # this just returns out of the eval not from the subroutine
    12751354                }
     
    13111390    my $self = shift;
    13121391
    1313     my $log = $self->log;
    1314     my $sql = $self->sql;
    1315     my $db  =$self->db;
     1392    my $key = shift;
     1393
     1394    my $log = $self->log;
     1395    my $sql = $self->sql;
     1396    my $db  = $self->db($key);
    13161397
    13171398    no warnings qw( uninitialized );
     
    13191400    use warnings;
    13201401
    1321     my $key = shift;
    13221402
    13231403    $key = parse_neb_key($key);
     
    13611441    my $log = $self->log;
    13621442    my $sql = $self->sql;
    1363     my $db  =$self->db;
     1443    my $db  = $self->db($key);
    13641444
    13651445    $key = parse_neb_key($key);
     
    13881468sub _is_valid_volume_name
    13891469{
    1390     my ($self, $vol_name) = @_;
    1391 
    1392     my $log = $self->log;
    1393     my $sql = $self->sql;
    1394     my $db  =$self->db;
     1470    my ($self, $key, $vol_name) = @_;
     1471
     1472    my $log = $self->log;
     1473    my $sql = $self->sql;
     1474    my $db  = $self->db($key);
    13951475
    13961476    my $volume_info = parse_neb_volume($vol_name);
     
    14291509    my $log = $self->log;
    14301510    my $sql = $self->sql;
    1431     my $db  = $self->db;
     1511    my $db  = $self->db($key);
    14321512
    14331513    my $uri;
     
    15581638    my $log = $self->log;
    15591639    my $sql = $self->sql;
    1560     my $db  =$self->db;
     1640#    my $db  = $self->db;
    15611641
    15621642    $log->debug( "entered" );
    15631643
    1564     $self->db->disconnect;       
    1565 
    1566     $log->debug( "disconnected from database: ", sub { $db->data_sources; } );
     1644#    $self->db->disconnect;       
     1645
     1646#    $log->debug( "disconnected from database: ", sub { $db->data_sources; } );
    15671647
    15681648    $log->debug( "leaving" );
  • trunk/Nebulous-Server/lib/Nebulous/Server/Config.pm

    r17072 r20965  
    11# Copyright (C) 2005  Joshua Hoblitt
    22#
    3 # $Id: Config.pm,v 1.3 2008-03-20 21:10:57 jhoblitt Exp $
     3# $Id: Config.pm,v 1.4 2008-12-12 21:13:41 jhoblitt Exp $
    44
    55package Nebulous::Server::Config;
     
    88use warnings FATAL => qw( all );
    99
    10 our $VERSION = 0.02;
     10our $VERSION = 0.03;
    1111
    1212use base qw( Class::Accessor::Fast );
    1313
    1414use Log::Log4perl qw( :levels );
    15 use Params::Validate qw( validate SCALAR );
     15use Params::Validate qw( validate validate_pos SCALAR );
    1616
    1717our %LEVELS = (
     
    2525);
    2626
    27 my $new_validate = {
     27my $db_validate = {
     28    dbindex       => {
     29        type => SCALAR,
     30        regex => qr/^\d+$/,
     31    },
    2832    dsn         => { type => SCALAR },
    2933    dbuser      => { type => SCALAR },
    3034    dbpasswd    => { type => SCALAR },
     35};
     36
     37my $new_validate = {
    3138    log_level   => {
    3239        type        => SCALAR,
     
    3946        },
    4047    },
     48    dsn         => { type => SCALAR, optional => 1 },
     49    dbuser      => { type => SCALAR, optional => 1 },
     50    dbpasswd    => { type => SCALAR, optional => 1 },
    4151};
    4252
    4353__PACKAGE__->mk_ro_accessors( keys %$new_validate );
    4454
    45 sub init {
     55
     56sub new
     57{
    4658    my $class = shift;
    4759
     
    4961
    5062    # normalize log levels to lower-case
    51     $p{ log_level } = lc $p{ log_level };
    52 
    53     my $self = \%p;
     63    my $self ={ log_level => lc $p{ log_level } };
    5464
    5565    bless $self, $class || ref $class;
     66
     67    my @dbs;
     68    $self->{dbs} = \@dbs;
     69
     70    if (defined $p{dsn} or defined $p{dbuser} or defined $p{dbpasswd}) {
     71        $self->add_db(
     72            dbindex     => 0,
     73            dsn         => $p{dsn},
     74            dbuser      => $p{dbuser},
     75            dbpasswd    => $p{dbpasswd},
     76        );
     77    }
    5678
    5779    return $self;
    5880}
    5981
     82
     83sub add_db
     84{
     85    my $self = shift;
     86   
     87    my %p = validate( @_, $db_validate );
     88
     89    my $config_db = Nebulous::Server::Config::DB->new({
     90        dsn         => $p{dsn},
     91        dbuser      => $p{dbuser},
     92        dbpasswd    => $p{dbpasswd},
     93    });
     94
     95    $self->{dbs}->[$p{dbindex}] = $config_db;
     96
     97    return $self;
     98}
     99
     100
     101sub db
     102{
     103    my $self = shift;
     104
     105    my ($db_index) = validate_pos( @_, { type => SCALAR, optional => 1, });
     106
     107    # default to 0
     108    $db_index ||= 0;
     109
     110    return $self->{dbs}->[$db_index];
     111}
     112
     113
     114sub n_db
     115{
     116    my $self = shift;
     117
     118    return scalar @{ $self->{dbs} };
     119}
     120
     121
     122package Nebulous::Server::Config::DB;
     123
     124use strict;
     125use warnings FATAL => qw( all );
     126
     127our $VERSION = 0.01;
     128
     129use base qw( Class::Accessor::Fast );
     130
     131__PACKAGE__->mk_ro_accessors(qw( dsn dbuser dbpasswd ));
     132
    601331;
    61134
  • trunk/Nebulous-Server/lib/Nebulous/Server/Log.pm

    r17546 r20965  
    11# Copyright (c) 2004  Joshua Hoblitt
    22#
    3 # $Id: Log.pm,v 1.7 2008-05-07 00:02:10 jhoblitt Exp $
     3# $Id: Log.pm,v 1.8 2008-12-12 21:13:41 jhoblitt Exp $
    44
    55package Nebulous::Server::Log;
     
    1616    my ($self, $config) = @_;
    1717
    18     my $dsn         = $config->dsn;
    19     my $dbuser      = $config->dbuser;
    20     my $dbpasswd    = $config->dbpasswd;
     18#    my $dsn         = $config->db->dsn;
     19#    my $dbuser      = $config->db->dbuser;
     20#    my $dbpasswd    = $config->db->dbpasswd;
    2121
    2222    my $conf = <<END;
     
    3030#   date | hostname | priority | method/sub - message\n
    3131
    32     log4perl.appender.SQLLOG            = Log::Log4perl::Appender::DBI
    33     log4perl.appender.SQLLOG.datasource = $dsn
    34     log4perl.appender.SQLLOG.username   = $dbuser
    35     log4perl.appender.SQLLOG.password   = $dbpasswd
    36     log4perl.appender.SQLLOG.sql        = \
     32#    log4perl.appender.SQLLOG            = Log::Log4perl::Appender::DBI
     33#    log4perl.appender.SQLLOG.sql        = \
     34#    log4perl.appender.SQLLOG.datasource =
     35#    log4perl.appender.SQLLOG.username   =
     36#    log4perl.appender.SQLLOG.password   =
    3737    INSERT INTO log (timestamp, hostname, level, sub, message) \
    3838    VALUES          (%d,        %H,       %p,    %M,  %m)
  • trunk/Nebulous-Server/lib/Nebulous/Server/SQL.pm

    r20175 r20965  
    11# Copyright (c) 2004  Joshua Hoblitt
    22#
    3 # $Id: SQL.pm,v 1.75 2008-10-15 20:45:51 jhoblitt Exp $
     3# $Id: SQL.pm,v 1.76 2008-12-12 21:13:41 jhoblitt Exp $
    44
    55package Nebulous::Server::SQL;
     
    88use warnings FATAL => qw( all );
    99
    10 our $VERSION = '0.02';
     10our $VERSION = '0.03';
    1111
    1212use base qw( Class::Accessor::Fast );
     
    4747        (so_id, vol_id, uri)
    4848        VALUES (?, ?, 'error')
     49    },
     50    get_all_instances   => qq{
     51        SELECT * FROM INSTANCE
     52        WHERE so_id = ?
    4953    },
    5054    get_object          => qq{
  • trunk/Nebulous-Server/t/02_server_setup.t

    r16281 r20965  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 02_server_setup.t,v 1.6 2008-02-02 01:51:29 jhoblitt Exp $
     5# $Id: 02_server_setup.t,v 1.7 2008-12-12 21:13:41 jhoblitt Exp $
    66
    77use strict;
    88use warnings;
    99
    10 use Test::More tests => 2;
     10use Test::More tests => 6;
    1111
    1212use lib qw( ./t ./lib );
     
    1717Test::Nebulous->setup;
    1818
    19 isa_ok(
    20     Nebulous::Server->new(
    21         dsn         => $NEB_DB,
    22         dbuser      => $NEB_USER,
    23         dbpasswd    => $NEB_PASS,
    24     ),
    25     "Nebulous::Server"
    26 );
     19isa_ok(Nebulous::Server->new(), "Nebulous::Server");
    2720
    2821Test::Nebulous->setup;
    2922
    30 eval {
    31     Nebulous::Server->new(
     23# ->new()
     24{
     25    my $neb = Nebulous::Server->new( log_level => 'off' );
     26
     27    ok($neb, "set log level");
     28}
     29
     30Test::Nebulous->setup;
     31
     32{
     33    my $neb = Nebulous::Server->new(
    3234        dsn         => "DBI:mysql:database=foobar:host=localhost",
    3335        dbuser      => "baz",
    3436        dbpasswd    =>"boo",
    3537    );
    36 };
    37 like( $@, qr/DBI connect.*? failed/, "bad dsn/user/pass" );
     38
     39    ok($neb, "set database");
     40}
     41
     42Test::Nebulous->setup;
     43
     44# add dbs after ->new()
     45{
     46    my $neb = Nebulous::Server->new;
     47
     48    ok($neb->config->add_db(
     49        dbindex    => 0,
     50        dsn         => "DBI:mysql:database=foobar:host=localhost",
     51        dbuser      => "baz",
     52        dbpasswd    =>"boo",
     53    ), "add db");
     54   
     55    ok($neb->config->add_db(
     56        dbindex    => 1,
     57        dsn         => "DBI:mysql:database=foobar:host=localhost",
     58        dbuser      => "baz",
     59        dbpasswd    =>"boo",
     60    ), "add dbs");
     61
     62    is($neb->config->n_db, 2, "n dbs")
     63}
    3864
    3965Test::Nebulous->cleanup;
  • trunk/Nebulous-Server/t/08_server_delete_instance.t

    r17072 r20965  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 08_server_delete_instance.t,v 1.10 2008-03-20 21:10:14 jhoblitt Exp $
     5# $Id: 08_server_delete_instance.t,v 1.11 2008-12-12 21:13:41 jhoblitt Exp $
    66
    77use strict;
     
    2424
    2525{
    26     my $uri = $neb->create_object("foo");
     26    my $key = "foo";
     27    my $uri = $neb->create_object($key);
    2728
    28     ok($neb->delete_instance($uri), "delete instance");
     29    ok($neb->delete_instance($key, $uri), "delete instance");
    2930}
    3031
     
    3233
    3334{
    34     my $uri1 = $neb->create_object("foo");
    35     my $uri2 = $neb->replicate_object("foo");
     35    my $key = "foo";
     36    my $uri1 = $neb->create_object($key);
     37    my $uri2 = $neb->replicate_object($key);
    3638
    37     ok($neb->delete_instance($uri1), "delete instance");
     39    ok($neb->delete_instance($key, $uri1), "delete instance");
    3840
    39     my $locations = $neb->find_instances("foo");
     41    my $locations = $neb->find_instances($key);
    4042
    4143    is($locations->[0], $uri2, "instance remains");
    4244
    43     ok($neb->delete_instance( $uri2 ), "delete instance");
     45    ok($neb->delete_instance($key, $uri2), "delete instance");
    4446
    4547    eval {
    46         $neb->find_instances("foo");
     48        $neb->find_instances($key);
    4749    };
    4850    like($@, qr/is valid object key/, "storage object was deleted");
     
    5254
    5355eval {
    54     $neb->delete_instance("file:/foo");
     56    my $key = "foo";
     57    my $uri1 = $neb->create_object($key);
     58
     59    $neb->delete_instance($key, "file:/foo");
    5560};
    5661like($@, qr/no instance is associated with uri/, "uri does not exist");
     
    6166    $neb->delete_instance();
    6267};
    63 like($@, qr/1 was expected/, "no params");
     68like($@, qr/2 were expected/, "no params");
    6469
    6570Test::Nebulous->setup;
    6671
    6772eval {
    68     $neb->delete_instance("foo", 2);
     73    my $key = "foo";
     74    my $uri1 = $neb->create_object($key);
     75
     76    $neb->delete_instance("foo", 2, 3);
    6977};
    70 like($@, qr/1 was expected/, "too many params");
     78like($@, qr/2 were expected/, "too many params");
    7179
    7280Test::Nebulous->cleanup;
  • trunk/Nebulous-Server/t/10_server_is_valid_volume_name.t

    r17072 r20965  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 10_server_is_valid_volume_name.t,v 1.5 2008-03-20 21:10:14 jhoblitt Exp $
     5# $Id: 10_server_is_valid_volume_name.t,v 1.6 2008-12-12 21:13:41 jhoblitt Exp $
    66
    77use strict;
     
    2323Test::Nebulous->setup;
    2424
    25 ok($neb->_is_valid_volume_name('node01'), "valid volume name");
     25ok($neb->_is_valid_volume_name('foo', 'node01'), "valid volume name");
    2626
    2727Test::Nebulous->setup;
    2828
    29 ok($neb->_is_valid_volume_name('node02'), "valid volume name");
     29ok($neb->_is_valid_volume_name('foo', 'node02'), "valid volume name");
    3030
    3131Test::Nebulous->setup;
    3232
    33 is($neb->_is_valid_volume_name('node99'), undef, "invalid volume name");
     33is($neb->_is_valid_volume_name('foo', 'node99'), undef, "invalid volume name");
    3434
    3535Test::Nebulous->cleanup;
  • trunk/Nebulous/lib/Nebulous/Client.pm

    r20094 r20965  
    11# Copyright (c) 2004-2008  Joshua Hoblitt
    22#
    3 # $Id: Client.pm,v 1.62 2008-10-13 21:20:33 jhoblitt Exp $
     3# $Id: Client.pm,v 1.63 2008-12-12 21:13:41 jhoblitt Exp $
    44
    55package Nebulous::Client;
     
    243243        # if the copy failed we now have a zero length instances floating
    244244        # around that must be removed
    245         unless ($self->delete_instance("$uri")) {
     245        unless ($self->delete_instance($key, "$uri")) {
    246246            $log->logdie( "can not copy instance $uri AND FAILED TO CLEANUP EMPTY INSTANCE" );
    247247        }
     
    256256
    257257    unless ($src_md5 eq $dst_md5) {
    258         $self->delete_instance("$uri");
     258        $self->delete_instance($key, "$uri");
    259259        $log->logdie( "md5sum mismatch" );
    260260    }
     
    327327    }
    328328
    329     my $uri = $self->delete_instance( @$locations[0] );
     329    my $uri = $self->delete_instance($key, @$locations[0]);
    330330
    331331    $log->debug("leaving");
     
    813813    # a lock is implicitly removed when the last storage object is deleted
    814814    foreach my $uri ( @$locations ) {
    815         $self->delete_instance( $uri ) or return undef;
     815        $self->delete_instance($key, $uri) or return undef;
    816816    }
    817817
     
    944944    my $self = shift;
    945945
    946     my ($uri) = validate_pos(@_,
     946    my ($key, $uri) = validate_pos(@_,
     947        {
     948            type => SCALAR,
     949        },
    947950        {
    948951            type => SCALAR,
     
    961964    $log->logdie( $@ ) if $@;
    962965
    963     my $response = $self->{ 'server' }->delete_instance( $uri );
     966    my $response = $self->{ 'server' }->delete_instance($key, $uri);
    964967    if ( $response->fault ) {
    965968        $self->set_err($response->faultstring);
  • trunk/Nebulous/nebclient/nebulous.wsdl

    r20208 r20965  
    125125
    126126    <message name="delete_instanceRequest">
     127        <part name="key" type="xsd:string" />
    127128        <part name="uri" type="xsd:string" />
    128129    </message>
  • trunk/Nebulous/nebclient/src/nebclient.c

    r20344 r20965  
    44 * Copyright (C) 2005  Joshua Hoblitt
    55 *
    6  * $Id: nebclient.c,v 1.54 2008-10-23 22:30:44 bills Exp $
     6 * $Id: nebclient.c,v 1.55 2008-12-12 21:13:41 jhoblitt Exp $
    77 */
    88
     
    253253    }
    254254
    255     if (!nebDeleteInstance(server, locations->URI[0])) {
     255    if (!nebDeleteInstance(server, key, locations->URI[0])) {
    256256        nebObjectInstancesFree(locations);
    257257
     
    539539
    540540    for (int i = 0; i < locations->n; i++) {
    541         if (!nebDeleteInstance(server, locations->URI[i])) {
     541        if (!nebDeleteInstance(server, key, locations->URI[i])) {
    542542            nebSetErr(server, "can not delete instance");
    543543            nebObjectInstancesFree(locations);
     
    655655}
    656656
    657 bool nebDeleteInstance(nebServer *server, const char *URI)
     657bool nebDeleteInstance(nebServer *server, const char *key, const char *URI)
    658658{
    659659    int             response;
     
    676676
    677677    if (soap_call_ns1__delete_USCOREinstance(server->soap, server->endpoint,
    678             NULL, (char *)URI, &response) != SOAP_OK) {
     678            NULL, (char *)key, (char *)URI, &response) != SOAP_OK) {
    679679        nebFree(filename);
    680680
  • trunk/Nebulous/nebclient/src/nebclient.h

    r20344 r20965  
    44 * Copyright (C) 2005  Joshua Hoblitt
    55 *
    6  * $Id: nebclient.h,v 1.36 2008-10-23 22:30:44 bills Exp $
     6 * $Id: nebclient.h,v 1.37 2008-12-12 21:13:41 jhoblitt Exp $
    77 */
    88
     
    248248bool nebDeleteInstance(
    249249    nebServer *server,                  ///< nebServer object
     250    const char *key,                    ///< storage object key (name)
    250251    const char *URI                     ///< URL of instance to remove
    251252);
  • trunk/Nebulous/nebclient/src/nebulous.h

    r20208 r20965  
    11/* src/nebulous.h
    22   Generated by wsdl2h 1.2.11 from nebulous.wsdl and typemap.dat
    3    2008-10-16 23:26:37 GMT
     3   2008-12-11 21:42:13 GMT
    44   Copyright (C) 2001-2008 Robert van Engelen, Genivia Inc. All Rights Reserved.
    55   This part of the software is released under one of the following licenses:
     
    808808    NULL, // char *action = NULL selects default action for this operation
    809809    // request parameters:
     810    char*                               key,
    810811    char*                               uri,
    811812    // response parameters:
     
    819820    struct soap *soap,
    820821    // request parameters:
     822    char*                               key,
    821823    char*                               uri,
    822824    // response parameters:
     
    831833//gsoap ns1  service method-action:     delete_USCOREinstance urn:Nebulous/Server/SOAP#delete_instance
    832834int ns1__delete_USCOREinstance(
     835    char*                               key,    ///< Request parameter
    833836    char*                               uri,    ///< Request parameter
    834837    int                                *result  ///< Response parameter
  • trunk/Nebulous/nebclient/src/soapC.c

    r20208 r20965  
    1212#endif
    1313
    14 SOAP_SOURCE_STAMP("@(#) soapC.c ver 2.7.11 2008-10-16 23:26:39 GMT")
     14SOAP_SOURCE_STAMP("@(#) soapC.c ver 2.7.11 2008-12-11 21:42:14 GMT")
    1515
    1616
     
    13421342{
    13431343        (void)soap; (void)a; /* appease -Wall -Werror */
     1344        soap_default_string(soap, &a->key);
    13441345        soap_default_string(soap, &a->uri);
    13451346}
     
    13481349{
    13491350        (void)soap; (void)a; /* appease -Wall -Werror */
     1351        soap_serialize_string(soap, &a->key);
    13501352        soap_serialize_string(soap, &a->uri);
    13511353}
     
    13631365        if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, a, SOAP_TYPE_ns1__delete_USCOREinstance), type))
    13641366                return soap->error;
     1367        if (soap_out_string(soap, "key", -1, &a->key, ""))
     1368                return soap->error;
    13651369        if (soap_out_string(soap, "uri", -1, &a->uri, ""))
    13661370                return soap->error;
     
    13781382SOAP_FMAC3 struct ns1__delete_USCOREinstance * SOAP_FMAC4 soap_in_ns1__delete_USCOREinstance(struct soap *soap, const char *tag, struct ns1__delete_USCOREinstance *a, const char *type)
    13791383{
     1384        size_t soap_flag_key = 1;
    13801385        size_t soap_flag_uri = 1;
    13811386        if (soap_element_begin_in(soap, tag, 0, type))
     
    13891394                for (;;)
    13901395                {       soap->error = SOAP_TAG_MISMATCH;
     1396                        if (soap_flag_key && (soap->error == SOAP_TAG_MISMATCH || soap->error == SOAP_NO_TAG))
     1397                                if (soap_in_string(soap, "key", &a->key, "xsd:string"))
     1398                                {       soap_flag_key--;
     1399                                        continue;
     1400                                }
    13911401                        if (soap_flag_uri && (soap->error == SOAP_TAG_MISMATCH || soap->error == SOAP_NO_TAG))
    13921402                                if (soap_in_string(soap, "uri", &a->uri, "xsd:string"))
  • trunk/Nebulous/nebclient/src/soapClient.c

    r20208 r20965  
    1010#endif
    1111
    12 SOAP_SOURCE_STAMP("@(#) soapClient.c ver 2.7.11 2008-10-16 23:26:38 GMT")
     12SOAP_SOURCE_STAMP("@(#) soapClient.c ver 2.7.11 2008-12-11 21:42:13 GMT")
    1313
    1414
     
    700700}
    701701
    702 SOAP_FMAC5 int SOAP_FMAC6 soap_call_ns1__delete_USCOREinstance(struct soap *soap, const char *soap_endpoint, const char *soap_action, char *uri, int *result)
     702SOAP_FMAC5 int SOAP_FMAC6 soap_call_ns1__delete_USCOREinstance(struct soap *soap, const char *soap_endpoint, const char *soap_action, char *key, char *uri, int *result)
    703703{       struct ns1__delete_USCOREinstance soap_tmp_ns1__delete_USCOREinstance;
    704704        struct ns1__delete_USCOREinstanceResponse *soap_tmp_ns1__delete_USCOREinstanceResponse;
     
    708708                soap_action = "urn:Nebulous/Server/SOAP#delete_instance";
    709709        soap->encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/";
     710        soap_tmp_ns1__delete_USCOREinstance.key = key;
    710711        soap_tmp_ns1__delete_USCOREinstance.uri = uri;
    711712        soap_begin(soap);
  • trunk/Nebulous/nebclient/src/soapServer.c

    r20208 r20965  
    1010#endif
    1111
    12 SOAP_SOURCE_STAMP("@(#) soapServer.c ver 2.7.11 2008-10-16 23:26:38 GMT")
     12SOAP_SOURCE_STAMP("@(#) soapServer.c ver 2.7.11 2008-12-11 21:42:13 GMT")
    1313
    1414
     
    643643         || soap_end_recv(soap))
    644644                return soap->error;
    645         soap->error = ns1__delete_USCOREinstance(soap, soap_tmp_ns1__delete_USCOREinstance.uri, &soap_tmp_int);
     645        soap->error = ns1__delete_USCOREinstance(soap, soap_tmp_ns1__delete_USCOREinstance.key, soap_tmp_ns1__delete_USCOREinstance.uri, &soap_tmp_int);
    646646        if (soap->error)
    647647                return soap->error;
  • trunk/Nebulous/nebclient/src/soapStub.h

    r20208 r20965  
    283283struct ns1__delete_USCOREinstance
    284284{
     285        char *key;      /* optional element of type xsd:string */
    285286        char *uri;      /* optional element of type xsd:string */
    286287};
     
    432433SOAP_FMAC5 int SOAP_FMAC6 ns1__find_USCOREinstances(struct soap*, char *key, char *volume, struct ns1__find_USCOREinstancesResponse *_param_3);
    433434
    434 SOAP_FMAC5 int SOAP_FMAC6 ns1__delete_USCOREinstance(struct soap*, char *uri, int *result);
     435SOAP_FMAC5 int SOAP_FMAC6 ns1__delete_USCOREinstance(struct soap*, char *key, char *uri, int *result);
    435436
    436437SOAP_FMAC5 int SOAP_FMAC6 ns1__stat_USCOREobject(struct soap*, char *key, struct ns1__stat_USCOREobjectResponse *_param_4);
     
    467468SOAP_FMAC5 int SOAP_FMAC6 soap_call_ns1__find_USCOREinstances(struct soap *soap, const char *soap_endpoint, const char *soap_action, char *key, char *volume, struct ns1__find_USCOREinstancesResponse *_param_3);
    468469
    469 SOAP_FMAC5 int SOAP_FMAC6 soap_call_ns1__delete_USCOREinstance(struct soap *soap, const char *soap_endpoint, const char *soap_action, char *uri, int *result);
     470SOAP_FMAC5 int SOAP_FMAC6 soap_call_ns1__delete_USCOREinstance(struct soap *soap, const char *soap_endpoint, const char *soap_action, char *key, char *uri, int *result);
    470471
    471472SOAP_FMAC5 int SOAP_FMAC6 soap_call_ns1__stat_USCOREobject(struct soap *soap, const char *soap_endpoint, const char *soap_action, char *key, struct ns1__stat_USCOREobjectResponse *_param_4);
  • trunk/Nebulous/t/62_client_delete_instance.t

    r5667 r20965  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 62_client_delete_instance.t,v 1.1 2005-12-03 02:52:31 jhoblitt Exp $
     5# $Id: 62_client_delete_instance.t,v 1.2 2008-12-12 21:13:41 jhoblitt Exp $
    66
    77use strict;
     
    1010use Apache::Test qw( -withtestmore );
    1111
    12 plan tests => 9;
     12plan tests => 11;
    1313
    1414use lib qw( ./t ./lib );
     
    2626        proxy => "http://$hostport/nebulous",
    2727    );
    28     $neb->create( "foo" );
    2928
    30     my $locations = $neb->find_instances( "foo" );
     29    my $key = "foo";
     30    $neb->create($key);
    3131
    32     my $uri = $neb->delete_instance( @$locations[0] );
     32    my $locations = $neb->find_instances($key);
     33
     34    my $uri = $neb->delete_instance($key, @$locations[0]);
    3335
    3436    is( $uri, @$locations[0], "delete instance" );
     
    4345        proxy => "http://$hostport/nebulous",
    4446    );
    45     $neb->create( "foo" );
    46     $neb->replicate( "foo" );
     47    my $key = "foo";
     48    $neb->create($key);
     49    $neb->replicate($key);
    4750
    4851    my $uri1 = $neb->find_instances( "foo" )->[0];
    4952
    50     ok( $neb->delete_instance( $uri1 ), "delete instance" );
     53    ok( $neb->delete_instance($key, $uri1), "delete instance" );
    5154
    5255    my $uri2 = $neb->find_instances( "foo" )->[0];
     
    5457    isnt( $uri1, $uri2, "other instance remains" );
    5558
    56     ok( $neb->delete_instance( $uri2 ), "delete instance" );
     59    ok( $neb->delete_instance($key, $uri2), "delete instance" );
    5760
    5861    my $locations = $neb->find_instances( "foo" );
    5962
    60     is( $locations, undef, "no remaning instances" );
     63    is( $locations, undef, "no remaining instances" );
    6164}
     65
     66Test::Nebulous->setup;
    6267
    6368{
     
    6570        proxy => "http://$hostport/nebulous",
    6671    );
    67     my $uri = $neb->delete_instance( "file:/foo" );
     72    my $key = "foo";
     73    $neb->create($key);
     74    my $uri = $neb->delete_instance($key, "file:/foo" );
    6875
    6976    is( $uri, undef, "uri does not exist" );
     
    7683        proxy => "http://$hostport/nebulous",
    7784    );
    78     $neb->delete_instance();
     85    my $uri = $neb->delete_instance("foo", "file:/foo" );
    7986};
    80 like( $@, qr/1 was expected/, "no params" );
     87like( $@, qr/is valid object key/, "bad object key" );
    8188
    8289Test::Nebulous->setup;
     
    8693        proxy => "http://$hostport/nebulous",
    8794    );
    88     $neb->delete_instance( "foo", 2 );
     95    my $uri = $neb->delete_instance("foo");
    8996};
    90 like( $@, qr/1 was expected/, "too many params" );
     97like( $@, qr/2 were expected/, "missing second param" );
     98
     99Test::Nebulous->setup;
     100
     101eval {
     102    my $neb = Nebulous::Client->new(
     103        proxy => "http://$hostport/nebulous",
     104    );
     105    $neb->delete_instance();
     106};
     107like( $@, qr/2 were expected/, "no params" );
     108
     109Test::Nebulous->setup;
     110
     111eval {
     112    my $neb = Nebulous::Client->new(
     113        proxy => "http://$hostport/nebulous",
     114    );
     115    $neb->delete_instance("foo", 2, 3);
     116};
     117like( $@, qr/2 were expected/, "too many params" );
    91118
    92119Test::Nebulous->cleanup;
Note: See TracChangeset for help on using the changeset viewer.