IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 14, 2008, 12:25:00 PM (18 years ago)
Author:
jhoblitt
Message:

cleanup database error handling: make sure queries are ->finished before showing an expect, remove DBI->rollback from the error paths of methods that don't insert data
improve getxattr_object() error handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous-Server/lib/Nebulous/Server.pm

    r17650 r17680  
    11# Copyright (c) 2004-2008  Joshua Hoblitt
    22#
    3 # $Id: Server.pm,v 1.67 2008-05-13 03:55:08 jhoblitt Exp $
     3# $Id: Server.pm,v 1.68 2008-05-14 22:25:00 jhoblitt Exp $
    44
    55package Nebulous::Server;
     
    291291        # if we affected more then one row something very bad has happened.
    292292        unless ($rows == 1) {
     293            $query->finish;
    293294            $log->logdie("affected row count is $rows instead of 1");
    294295        }
     
    361362            unless ( $rows > 0 ) {
    362363                $query->finish;
    363                 $db->rollback;
    364                 $log->debug("rollback");
    365364                $log->logdie( "storage object does not exist" );
    366365            }
     
    444443    my $read_lock;
    445444    my $write_lock;
    446     my $query;
    447445
    448446    eval {
    449447        {
    450448            # this will set update locks
    451             $query = $db->prepare_cached( $sql->get_object_locks );
     449            my $query = $db->prepare_cached( $sql->get_object_locks );
    452450            my $rows = $query->execute( $key );
    453451            unless ( $rows == 1 ) {
     452                $query->finish;
    454453                $log->logdie( "storage object does not exist" );
    455454            }
     
    546545    my $read_lock;
    547546    my $write_lock;
    548     my $query;
    549547
    550548    eval {
    551549        {
    552550            # this will set update locks
    553             $query = $db->prepare_cached( $sql->get_object_locks );
     551            my $query = $db->prepare_cached( $sql->get_object_locks );
    554552            my $rows = $query->execute($key);
    555553            unless ($rows == 1) {
     554                $query->finish;
    556555                $log->logdie("storage object does not exist");
    557556            }
     
    667666        # name, value, ext_id
    668667        my $rows = $query->execute($name, $value, $key);
     668        $query->finish;
    669669
    670670        # if we affected more then one row something very bad has happened.
     
    678678            }
    679679        }
     680
     681        $db->commit;
     682        $log->debug("commit");
    680683    };
    681684    if ($@) {
     
    685688    }
    686689
    687     eval {
     690    $log->debug("leaving");
     691
     692    return 1;
     693}
     694
     695
     696sub getxattr_object
     697{
     698    my $self = shift;
     699
     700    my ($key, $name) = validate_pos(@_,
     701        {
     702            type        => SCALAR,
     703            callbacks   => {
     704                'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
     705            },
     706        },
     707        {
     708            type        => SCALAR,
     709        },
     710    );
     711
     712    my $log = $self->log;
     713    my $sql = $self->sql;
     714    my $db  =$self->db;
     715
     716    $log->debug("entered - @_");
     717
     718    # ignore volume
     719    $key = parse_neb_key($key);
     720
     721    my $value;
     722    eval {
     723        my $query = $db->prepare_cached( $sql->get_object_xattr );
     724        # ext_id, name
     725        my $rows = $query->execute($key, $name);
     726
     727        # no rows returned means that the xattr does not exist
     728        if ($rows == 0) {
     729            $query->finish;
     730            $log->logdie( "xattr $key:$name does not exist" );
     731        }
     732        # if we go more then one row bad something very bad has happened.
     733        unless ($rows == 1) {
     734            $query->finish;
     735            $log->logdie( "affected row count is $rows instead of 1" );
     736        }
     737
     738        my $row = $query->fetchrow_hashref;
     739        # XXX: DBI bug? ->finish is needed here even though $query is going out
     740        # of scope
     741        $query->finish;
     742        $value = $row->{ 'value' };
     743    };
     744    $log->logdie("database error: $@") if $@;
     745
     746    $log->debug("leaving");
     747
     748    return $value;
     749}
     750
     751
     752sub listxattr_object
     753{
     754    my $self = shift;
     755
     756    my ($key) = validate_pos(@_,
     757        {
     758            type        => SCALAR,
     759            callbacks   => {
     760                'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
     761            },
     762        },
     763    );
     764
     765    my $log = $self->log;
     766    my $sql = $self->sql;
     767    my $db  =$self->db;
     768
     769    $log->debug("entered - @_");
     770
     771    # ignore volume
     772    $key = parse_neb_key($key);
     773
     774    my @xattrs;
     775    eval {
     776        my $query = $db->prepare_cached( $sql->list_object_xattr );
     777        # ext_id
     778        my $rows = $query->execute($key);
     779
     780        while (my $row = $query->fetchrow_hashref) {
     781            push @xattrs, $row->{ 'name' };
     782        }
     783    };
     784    $log->logdie("database error: $@") if $@;
     785
     786    $log->debug("leaving");
     787
     788    return \@xattrs;
     789}
     790
     791
     792sub removexattr_object
     793{
     794    my $self = shift;
     795
     796    my ($key, $name) = validate_pos(@_,
     797        {
     798            type        => SCALAR,
     799            callbacks   => {
     800                'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
     801            },
     802        },
     803        {
     804            type        => SCALAR,
     805        },
     806    );
     807
     808    my $log = $self->log;
     809    my $sql = $self->sql;
     810    my $db  =$self->db;
     811
     812    $log->debug("entered - @_");
     813
     814    # ignore volume
     815    $key = parse_neb_key($key);
     816
     817    eval {
     818        my $query = $db->prepare_cached( $sql->remove_object_xattr );
     819        # ext_id, name
     820        my $rows = $query->execute($key, $name);
     821        $query->finish;
     822
     823        # if we affected more then one row something very bad has happened.
     824        unless ($rows == 1) {
     825            $log->logdie( "affected row count is $rows instead of 1" );
     826        }
     827
    688828        $db->commit;
    689829        $log->debug("commit");
     
    701841
    702842
    703 sub getxattr_object
    704 {
    705     my $self = shift;
    706 
    707     my ($key, $name) = validate_pos(@_,
    708         {
    709             type        => SCALAR,
    710             callbacks   => {
    711                 'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
    712             },
    713         },
    714         {
    715             type        => SCALAR,
    716         },
    717     );
    718 
    719     my $log = $self->log;
    720     my $sql = $self->sql;
    721     my $db  =$self->db;
    722 
    723     $log->debug("entered - @_");
    724 
    725     # ignore volume
    726     $key = parse_neb_key($key);
    727 
    728     my $query;
    729     eval {
    730         $query = $db->prepare_cached( $sql->get_object_xattr );
    731         # ext_id, name
    732         my $rows = $query->execute($key, $name);
    733 
    734         # if we affected more then one row something very bad has happened.
    735         unless ($rows == 1) {
    736             $log->logdie( "affected row count is $rows instead of 1" );
    737         }
    738     };
    739     if ($@) {
    740         $db->rollback;
    741         $log->debug("rollback");
    742         $log->logdie("database error: $@");
    743     }
    744 
    745     my $row = $query->fetchrow_hashref;
    746     my $value = $row->{ 'value' };
    747     $query->finish;
    748 
    749     $log->debug("leaving");
    750 
    751     return $value;
    752 }
    753 
    754 
    755 sub listxattr_object
    756 {
    757     my $self = shift;
    758 
    759     my ($key) = validate_pos(@_,
    760         {
    761             type        => SCALAR,
    762             callbacks   => {
    763                 'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
    764             },
    765         },
    766     );
    767 
    768     my $log = $self->log;
    769     my $sql = $self->sql;
    770     my $db  =$self->db;
    771 
    772     $log->debug("entered - @_");
    773 
    774     # ignore volume
    775     $key = parse_neb_key($key);
    776 
    777     my $query;
    778     eval {
    779         $query = $db->prepare_cached( $sql->list_object_xattr );
    780         # ext_id
    781         my $rows = $query->execute($key);
    782     };
    783     if ($@) {
    784         $db->rollback;
    785         $log->debug("rollback");
    786         $log->logdie("database error: $@");
    787     }
    788 
    789     my @xattrs;
    790     while (my $row = $query->fetchrow_hashref) {
    791         push @xattrs, $row->{ 'name' };
    792     }
    793 
    794     $log->debug("leaving");
    795 
    796     return \@xattrs;
    797 }
    798 
    799 
    800 sub removexattr_object
    801 {
    802     my $self = shift;
    803 
    804     my ($key, $name) = validate_pos(@_,
    805         {
    806             type        => SCALAR,
    807             callbacks   => {
    808                 'is valid object key' => sub { $self->_is_valid_object_key($_[0]) },
    809             },
    810         },
    811         {
    812             type        => SCALAR,
    813         },
    814     );
    815 
    816     my $log = $self->log;
    817     my $sql = $self->sql;
    818     my $db  =$self->db;
    819 
    820     $log->debug("entered - @_");
    821 
    822     # ignore volume
    823     $key = parse_neb_key($key);
    824 
    825     my $query;
    826     eval {
    827         $query = $db->prepare_cached( $sql->remove_object_xattr );
    828         # ext_id, name
    829         my $rows = $query->execute($key, $name);
    830 
    831         # if we affected more then one row something very bad has happened.
    832         unless ($rows == 1) {
    833             $log->logdie( "affected row count is $rows instead of 1" );
    834         }
    835     };
    836     if ($@) {
    837         $db->rollback;
    838         $log->debug("rollback");
    839         $log->logdie("database error: $@");
    840     }
    841 
    842     eval {
    843         $db->commit;
    844         $log->debug("commit");
    845     };
    846     if ($@) {
    847         $db->rollback;
    848         $log->debug("rollback");
    849         $log->logdie("database error: $@");
    850     }
    851 
    852     $log->debug("leaving");
    853 
    854     return 1;
    855 }
    856 
    857 
    858843sub find_objects
    859844{
     
    883868    }
    884869
    885     my $query;
    886     eval {
    887         $query = $db->prepare_cached( $sql->find_objects );
     870    my @keys;
     871    eval {
     872        my $query = $db->prepare_cached( $sql->find_objects );
    888873        $query->execute( $pattern );
    889     };
    890     $log->logdie( "database error: $@" ) if $@;
    891 
    892     my @keys;
    893 
    894     while ( my $row = $query->fetchrow_hashref ) {
    895         my $key = $row->{ 'ext_id' };
    896         push @keys, $key if $key;
    897     }
     874
     875        while ( my $row = $query->fetchrow_hashref ) {
     876            my $key = $row->{ 'ext_id' };
     877            push @keys, $key if $key;
     878        }
     879    };
     880    $log->logdie("database error: $@") if $@;
    898881
    899882    $log->debug( "no keys found" ) unless ( scalar @keys );
     
    945928    }
    946929
    947     my $query;
    948     eval {
     930    my @locations;
     931    eval {
     932        my $query;
    949933        if ($vol_name) {
    950934            $query = $db->prepare_cached( $sql->get_object_instances_by_vol_name );
     
    964948            }
    965949        }
     950
     951        while (my $row = $query->fetchrow_hashref) {
     952            my $instance = $row->{ 'uri' };
     953            push @locations, $instance if $instance;
     954        }
    966955    };
    967956    $log->logdie("database error: $@") if $@;
    968 
    969     my @locations;
    970 
    971     while (my $row = $query->fetchrow_hashref) {
    972         my $instance = $row->{ 'uri' };
    973         push @locations, $instance if $instance;
    974     }
    975957
    976958    # XXX remove this?
     
    11031085        $query->finish;
    11041086    };
    1105     if ( $@ ) {
    1106         $db->rollback;
    1107         $log->debug("rollback");
    1108         $log->logdie("database error: $@");
    1109     }
     1087    $log->logdie("database error: $@") if $@;
    11101088
    11111089    $log->debug("leaving");
     
    11991177        $query->finish;
    12001178    };
    1201     if ($@) {
    1202         $db->rollback;
    1203         $log->debug("rollback");
    1204         $log->logdie("database error: $@");
    1205     }
     1179    $log->logdie("database error: $@") if $@;
    12061180
    12071181    $log->logdie("failed to find a suitable volume" )
     
    12621236        $query->finish;
    12631237    };
    1264     if ($@) {
    1265         $db->rollback;
    1266         $log->debug("rollback");
    1267         $log->logdie( "database error: $@" );
    1268     }
     1238    $log->logdie("database error: $@") if $@;
    12691239
    12701240    if (defined $vol_id and defined $vol_path) {
Note: See TracChangeset for help on using the changeset viewer.