Algorithm-Paxos

 view release on metacpan or  search on metacpan

lib/Algorithm/Paxos/Exception.pm  view on Meta::CPAN

package Algorithm::Paxos::Exception;
{
  $Algorithm::Paxos::Exception::VERSION = '0.001';
}
use Moose;

# ABSTRACT: Simple Sugar for Throwable::Error

use Sub::Exporter::Util ();
use Sub::Exporter -setup =>
    { exports => [ throw => Sub::Exporter::Util::curry_method('throw'), ], };

extends qw(Throwable::Error);

sub throw {
    my $class = shift;
    return $class->new(@_);
}

1;


=pod

=head1 NAME

lib/Algorithm/Paxos/Exception.pm  view on Meta::CPAN


=head1 VERSION

version 0.001

=head1 SYNOPSIS

    use Algorithm::Paxos::Exception;
    ...
    
    throw "Something failed";

=head1 DESCRIPTION

This is a very thin sugar wrapper around L<Throwable::Error>.

=head1 FUNCTIONS

=head2 throw ( $message )

Throw a new exception 

=head1 SEE ALSO

=over 4

=item *

L<Throwable>

lib/Algorithm/Paxos/Role/Acceptor.pm  view on Meta::CPAN


sub _latest_proposal {
    my $self = shift;
    my ($learner) = $self->learners;
    $learner->latest_proposal;
}

sub prepare {
    my ( $self, $id ) = @_;
    my $last = $self->last_accepted_id;
    throw("Prepared id does not exceed lastest prepared id.") if $id < $last;
    $self->last_prepared_id($id);
    return 0 unless $self->proposal_count;
    return $self->last_accepted_id;
}

sub accept {
    my ( $self, $id, $value ) = @_;
    my $last = $self->last_prepared_id;
    throw("Proposal id exceeds lastest prepared id.")
        if $id < $last;
    $_->learn( $id => $value ) for $self->learners;
    return ( $id, $value );
}

1;


=pod

lib/Algorithm/Paxos/Role/Acceptor.pm  view on Meta::CPAN

proposal.

=head2 learners ( ) : @learners

Returns a list of learners.

=head2 prepare ( $id ) : $id 

One of the two required methods for an Acceptor. When a proposal is made, the
first step is to ask acceptors to prepare. If the proposed ID is too low
(meaning another proposal is already in process) an exception will be thrown.
If the proposal is new the ID returned is 0. If there is a pending proposal
the ID for that proposal is returned.

=head2 accept ( $id, $value ) : $id, $value

One of two required methods for an Acceptor. After a quorum is reached a
proposal is then accepted and submitted to the learners. If all the learners
return clean the proposal id and value are returned. If the ID for the
proposal exceeds the allowed value (ie we're trying to accept an ID that is
lower than a prepared ID) we throw an exception.

=head1 AUTHOR

Chris Prather <chris@prather.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Chris Prather.

This is free software; you can redistribute it and/or modify it under

lib/Algorithm/Paxos/Role/Proposer.pm  view on Meta::CPAN

        try { $self->prepare($n) }
        catch { warn $_; undef }
    } $self->acceptors;

    if ( $self->is_quorum(@replies) ) {
        my $v = $self->highest_proposal_id(@replies);
        $v ||= $value;
        $_->accept( $n, $v ) for $self->acceptors;
        return $n;
    }
    throw("Proposal failed to reach quorum");
}

1;


=pod

=head1 NAME

Algorithm::Paxos::Role::Proposer - A Proposer role for the Paxos algorithm

lib/Algorithm/Paxos/Role/Proposer.pm  view on Meta::CPAN


=head2 new_proposal_id ( ) : $id

Generates a new proposal id. The default implementation is an increasing
integer (literally C<$i++>).

=head2 prospose ( $value ) : $id

Propose is the main interface between clients and the Paxos cluster/node.
Propose takes a single value (the proposal) and returns the ID that is
assigned to that proposal. If the proposal fails an exception is thrown.

=head1 AUTHOR

Chris Prather <chris@prather.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Chris Prather.

This is free software; you can redistribute it and/or modify it under



( run in 0.391 second using v1.01-cache-2.11-cpan-496ff517765 )