Paxos/Role/Acceptor.pm

filter: Statementstmt 89.6% 26 / 29 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 100% 2 / 2 totaltotal 86.3% 38 / 44 CC 5 SCAR 7.8
CC
5
Cov
86%
accept
3.0
prepare
3.0
_latest_proposal
2.0
CRAP 2.2
« Paxos/Role/Learner.pm ↑ Summary Paxos/Role/Proposer.pm »
1 package Algorithm::Paxos::Role::Acceptor;
2 {
3 1 $Algorithm::Paxos::Role::Acceptor::VERSION = '0.001';
4 }
5 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: An Acceptor role for the Paxos algorithm
9  
10 1 ▶use Algorithm::Paxos::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 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 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 1 handles => { learners => 'elements', }
24 );
25  
26 0 ▶sub _latest_proposal {
Subroutine _latest_proposal: not called CC=1 SCAR=6.9
27 0 my $self = shift;
28 0 my ($learner) = $self->learners;
29 0 $learner->latest_proposal;
30 }
31  
32 9 ▶sub prepare {
Subroutine prepare: called CC=3 SCAR=11.0
Pod documented
33 9 my ( $self, $id ) = @_;
34 9 my $last = $self->last_accepted_id;
35 9 ▶ throw("Prepared id does not exceed lastest prepared id.") if $id < $last;
Branch: 1 branch
BranchTrueFalseTotal
if $id < $last 0 9 9
36 9 $self->last_prepared_id($id);
37 9 ▶ return 0 unless $self->proposal_count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->proposal_count 3 6 9
38 6 return $self->last_accepted_id;
39 }
40  
41 9 ▶sub accept {
Subroutine accept: called CC=3 SCAR=11.1
Pod documented
42 9 my ( $self, $id, $value ) = @_;
43 9 my $last = $self->last_prepared_id;
44 9 ▶ throw("Proposal id exceeds lastest prepared id.")
Branch: 1 branch
BranchTrueFalseTotal
if $id < $last 0 9 9
45 if $id < $last;
46 9 $_->learn( $id => $value ) for $self->learners;
47 9 return ( $id, $value );
48 }
49  
50 1 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__
« Paxos/Role/Learner.pm ↑ Summary Paxos/Role/Proposer.pm »