IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 24915


Ignore:
Timestamp:
Jul 23, 2009, 4:14:20 PM (17 years ago)
Author:
jhoblitt
Message:

add Nebulous::Server->prune_object() API

Location:
trunk/Nebulous-Server
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous-Server/Changes

    r24639 r24915  
    3535    - change Nebulous::Server->stat_object() to return both the total number of
    3636      instance and just those that are available
     37    - add Nebulous::Server->prune_object() API
    3738     
    38390.16
  • trunk/Nebulous-Server/MANIFEST

    r24358 r24915  
    4949t/17_server_swap_objects.t
    5050t/18_server_chmod_object.t
     51t/19_server_prune_object.t
    5152t/75_parse_neb_key.t
  • trunk/Nebulous-Server/lib/Nebulous/Server.pm

    r24637 r24915  
    844844
    845845
     846sub prune_object
     847{
     848    my $self = shift;
     849
     850    my $log = $self->log;
     851    $log->debug("entered - @_");
     852
     853    my ($key) = validate_pos(@_,
     854        {
     855            type        => SCALAR,
     856            callbacks   => {
     857                'is valid object key'
     858                    => sub { $self->_is_valid_object_key($_[0]) },
     859            },
     860        },
     861    );
     862
     863    my $sql = $self->sql;
     864
     865    eval {
     866        $key = parse_neb_key($key);
     867    };
     868    $log->logdie("$@") if $@;
     869
     870    my $db  = $self->db($key);
     871
     872    my $rows_removed = 0;
     873TRANS: while (1) {
     874        eval {
     875            # remove key from cache
     876            $self->cache->delete($key->path) if defined $self->cache;
     877
     878            my $so_id;
     879            {
     880                my $query = $db->prepare_cached( $sql->find_object_by_ext_id );
     881                $query->execute( $key->path );
     882                $so_id = $query->fetchrow_hashref->{'so_id'};
     883                $query->finish;
     884            }
     885
     886            # record the path of the innaccesible files for deferred
     887            # deletion
     888            my $rows_copied;
     889            {
     890                my $query = $db->prepare_cached( $sql->copy_dead_instances_to_deleted );
     891                $rows_copied = $query->execute( $so_id );
     892            }
     893
     894            # check to see if there is anything to be done
     895            unless ($rows_copied > 0) {
     896                $db->rollback;
     897                return;
     898            }
     899
     900            # In MySQL you can't select from a table your deleting rows from so
     901            # we first have to get a list of instances to be removed, and then
     902            # removed them.
     903            my $rows_found;
     904            {
     905                my $query = $db->prepare_cached( $sql->find_dead_instances_by_so_id );
     906                $rows_found = $query->execute( $so_id );
     907                foreach my $row ($query->fetchrow_hashref) {
     908                    # remove dead instances
     909                    my $query = $db->prepare_cached( $sql->delete_instance_by_ins_id);
     910                    $rows_removed += $query->execute( $row->{ins_id} );
     911                }
     912                $query->finish;
     913            }
     914
     915            # sanity check
     916            die("instances inaccessible ($rows_copied) != instances removed ($rows_removed)")
     917                unless $rows_copied == $rows_removed;
     918           
     919            $db->commit;
     920            $log->debug("commit");
     921        };
     922        if ($@) {
     923            $db->rollback;
     924            $log->debug("rollback");
     925            if ($@ =~ $trans_regex) {
     926                $log->warn("database error, retrying transaction: $@");
     927                sleep TRANS_RETRY_WAIT;
     928                redo TRANS;
     929            }
     930            $log->logdie("error: $@");
     931        }
     932        last;
     933    }
     934
     935    $log->debug("leaving");
     936
     937    return $rows_removed;
     938}
     939
     940
    846941sub lock_object
    847942{
     
    16741769                # remove key from cache
    16751770                $self->cache->delete($key->path) if defined $self->cache;
    1676 
    1677                 # record the path of the innaccesible files for deferred
    1678                 # deletion
    1679                 {
    1680                     my $query = $db->prepare_cached( $sql->copy_instances_to_deleted );
    1681                     $query->execute( $so_id );
    1682                 }
    1683                 # remove all instances... not strictly nessicary as the delete
    1684                 # from storage_object should cascade but the fkey was specified
    1685                 # without the cascade
    1686                 {
    1687                     my $query = $db->prepare_cached( $sql->delete_instance_by_so_id );
    1688                     $query->execute( $so_id );
     1771               
     1772                if ($total > $available) {
     1773                    $self->prune_object("$key");
    16891774                }
    16901775               
  • trunk/Nebulous-Server/lib/Nebulous/Server/SQL.pm

    r24643 r24915  
    183183        WHERE ins_id = ?
    184184    },
    185     delete_instance_by_so_id => qq{
    186         DELETE FROM instance
    187         WHERE so_id = ?
    188     },
    189185    get_object_from_uri   => qq{
    190186        SELECT so_id
     
    218214        GROUP BY so_id
    219215    },
    220     copy_instances_to_deleted => qq{
     216    copy_dead_instances_to_deleted => qq{
    221217        INSERT INTO deleted
    222218        SELECT vol_id, uri, NULL
    223219        FROM instance
     220        LEFT JOIN mountedvol
     221            USING(vol_id)
    224222        WHERE
    225223            so_id = ?
     224            AND (mountedvol.available IS NULL || mountedvol.available = 0)
     225    },
     226    find_dead_instances_by_so_id => qq{
     227        SELECT so_id, ins_id, vol_id, uri
     228        FROM instance
     229        LEFT JOIN mountedvol
     230            USING(vol_id)
     231        WHERE
     232            so_id = ?
     233            AND (mountedvol.available IS NULL || mountedvol.available = 0)
    226234    },
    227235    get_object_instances    => qq{
     
    329337    },
    330338    find_object_by_ext_id => qq{
    331         SELECT ext_id, ext_id_basename
     339        SELECT so_id, ext_id, ext_id_basename
    332340        FROM storage_object
    333341        WHERE ext_id = ?
Note: See TracChangeset for help on using the changeset viewer.