File Coverage

blib/lib/Algorithm/Paxos/Role/Acceptor.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Algorithm::Paxos::Role::Acceptor;
2             {
3             $Algorithm::Paxos::Role::Acceptor::VERSION = '0.001';
4             }
5 1     1   6983 use Moose::Role;
  0            
  0            
6             use namespace::autoclean;
7              
8             # ABSTRACT: An Acceptor role for the Paxos algorithm
9              
10             use Algorithm::Paxos::Exception;
11              
12             has [qw(last_prepared_id last_accepted_id)] => (
13             isa => 'Str',
14             is => 'rw',
15             default => 0
16             );
17              
18             has learners => (
19             isa => 'ArrayRef',
20             writer => '_set_learners',
21             traits => ['Array'],
22             default => sub { [] },
23             handles => { learners => 'elements', }
24             );
25              
26             sub _latest_proposal {
27             my $self = shift;
28             my ($learner) = $self->learners;
29             $learner->latest_proposal;
30             }
31              
32             sub prepare {
33             my ( $self, $id ) = @_;
34             my $last = $self->last_accepted_id;
35             throw("Prepared id does not exceed lastest prepared id.") if $id < $last;
36             $self->last_prepared_id($id);
37             return 0 unless $self->proposal_count;
38             return $self->last_accepted_id;
39             }
40              
41             sub accept {
42             my ( $self, $id, $value ) = @_;
43             my $last = $self->last_prepared_id;
44             throw("Proposal id exceeds lastest prepared id.")
45             if $id < $last;
46             $_->learn( $id => $value ) for $self->learners;
47             return ( $id, $value );
48             }
49              
50             1;
51              
52              
53             =pod
54              
55             =head1 NAME
56              
57             Algorithm::Paxos::Role::Acceptor - An Acceptor role for the Paxos algorithm
58              
59             =head1 VERSION
60              
61             version 0.001
62              
63             =head1 SYNOPSIS
64              
65             package MyApp::PaxosBasic;
66             use Moose;
67            
68             with qw(Algorithm::Paxos::Role::Acceptor);
69            
70             1;
71             __END__
72              
73             =head1 DESCRIPTION
74              
75             From L<Wikipedia|http://en.wikipedia.org/wiki/Paxos_algorithm>
76              
77             The Acceptors act as the fault-tolerant "memory" of the protocol. Acceptors
78             are collected into groups called Quorums. Any message sent to an Acceptor must
79             be sent to a Quorum of Acceptors. Any message received from an Acceptor is
80             ignored unless a copy is received from each Acceptor in a Quorum.
81              
82             =head1 METHODS
83              
84             =head2 last_prepared_id ( ) : $id
85              
86             Internal method used by the algorithm. Returns the last id for a prepared
87             proposal.
88              
89             =head2 last_accepted_id ( ) : $id
90              
91             Internal method used by the algorithm. Returns the last id for an accepted
92             proposal.
93              
94             =head2 learners ( ) : @learners
95              
96             Returns a list of learners.
97              
98             =head2 prepare ( $id ) : $id
99              
100             One of the two required methods for an Acceptor. When a proposal is made, the
101             first step is to ask acceptors to prepare. If the proposed ID is too low
102             (meaning another proposal is already in process) an exception will be thrown.
103             If the proposal is new the ID returned is 0. If there is a pending proposal
104             the ID for that proposal is returned.
105              
106             =head2 accept ( $id, $value ) : $id, $value
107              
108             One of two required methods for an Acceptor. After a quorum is reached a
109             proposal is then accepted and submitted to the learners. If all the learners
110             return clean the proposal id and value are returned. If the ID for the
111             proposal exceeds the allowed value (ie we're trying to accept an ID that is
112             lower than a prepared ID) we throw an exception.
113              
114             =head1 AUTHOR
115              
116             Chris Prather <chris@prather.org>
117              
118             =head1 COPYRIGHT AND LICENSE
119              
120             This software is copyright (c) 2012 by Chris Prather.
121              
122             This is free software; you can redistribute it and/or modify it under
123             the same terms as the Perl 5 programming language system itself.
124              
125             =cut
126              
127              
128             __END__
129