Algorithm-Paxos

 view release on metacpan or  search on metacpan

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

package Algorithm::Paxos::Role::Acceptor;
{
  $Algorithm::Paxos::Role::Acceptor::VERSION = '0.001';
}
use Moose::Role;
use namespace::autoclean;

# ABSTRACT: An Acceptor role for the Paxos algorithm

use Algorithm::Paxos::Exception;

has [qw(last_prepared_id last_accepted_id)] => (
    isa     => 'Str',
    is      => 'rw',
    default => 0
);

has learners => (
    isa     => 'ArrayRef',
    writer  => '_set_learners',
    traits  => ['Array'],
    default => sub { [] },
    handles => { learners => 'elements', }
);

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

=head1 NAME

Algorithm::Paxos::Role::Acceptor - An Acceptor role for the Paxos algorithm

=head1 VERSION

version 0.001

=head1 SYNOPSIS

    package MyApp::PaxosBasic;
    use Moose;



( run in 0.493 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )