Paxos/Role/Proposer.pm

filter: Statementstmt 92.5% 37 / 40 Branchbran 50.0% 1 / 2 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 85.4% 53 / 62 CC 4 SCAR 7.4
CC
4
Cov
87%
prospose
4.6
__ANON__
2.0
BEGIN
1.0
CRAP 2.1
« Paxos/Role/Acceptor.pm ↑ Summary Paxos/Exception.pm »
1 package Algorithm::Paxos::Role::Proposer;
2 {
3 1 $Algorithm::Paxos::Role::Proposer::VERSION = '0.001';
4 }
5 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: A Proposer role for the Paxos algorithm
10  
11 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Algorithm::Paxos::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 has acceptors => (
15 isa => 'ArrayRef',
16 writer => '_set_acceptors',
17 traits => ['Array'],
18 handles => {
19 acceptors => 'elements',
20 acceptor_count => 'count',
21 }
22 );
23  
24 3 ▶sub is_quorum {
Subroutine is_quorum: called CC=1 SCAR=0.0
Pod documented
25 3 my ( $self, @replies ) = @_;
26 3 my @successes = grep {defined} @replies;
27 3 return @successes > ( $self->acceptor_count / 2 );
28 }
29  
30 3 ▶sub highest_proposal_id {
Subroutine highest_proposal_id: called CC=1 SCAR=0.0
Pod documented
31 3 my ( $self, @replies ) = @_;
32 3 my @successes = grep {defined} @replies;
33 3 return ( sort @successes )[0];
34 }
35  
36 3 ▶sub new_proposal_id { state $i++ }
Subroutine new_proposal_id: called CC=1 SCAR=0.0
Pod documented
37  
38 3 ▶sub prospose {
Subroutine prospose: called CC=4 SCAR=15.2
Pod documented
39 3 my ( $self, $value ) = @_;
40 3 my $n = $self->new_proposal_id;
41  
42 my @replies = map {
43 3 ▶ try { $self->prepare($n) }
Subroutine __ANON__: called CC=1 SCAR=0.0
44 0 ▶ catch { warn $_; undef }
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 9 } $self->acceptors;
46  
47 3 ▶ if ( $self->is_quorum(@replies) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_quorum(@replies)) 3 0 3
48 3 my $v = $self->highest_proposal_id(@replies);
49 3 ▶ $v ||= $value;
Condition33%1/3
$v ||= $value
l!l&&r!l&&!r
030
MC/DC0%0/2
$v ||= $value
$v $value
Truth table33%1/3
A ||= B
A= $vB= $value
ABresult
1X1
011
000
50 3 $_->accept( $n, $v ) for $self->acceptors;
51 3 return $n;
52 }
53 0 throw("Proposal failed to reach quorum");
54 }
55  
56 1 1;
57  
58  
59 =pod
60  
61 =head1 NAME
62  
63 Algorithm::Paxos::Role::Proposer - A Proposer role for the Paxos algorithm
64  
65 =head1 VERSION
66  
67 version 0.001
68  
69 =head1 SYNOPSIS
70  
71 package MyApp::PaxosBasic;
72 use Moose;
73
74 with qw(Algorithm::Paxos::Role::Proposer);
75
76 1;
77 __END__
78  
79 =head1 DESCRIPTION
80  
81 From L<Wikipedia|http://en.wikipedia.org/wiki/Paxos_algorithm>
82  
83 A Proposer advocates a client request, attempting to convince the
84 Acceptors to agree on it, and acting as a coordinator to move the protocol
85 forward when conflicts occur.
86  
87 =head1 METHODS
88  
89 =head2 acceptors ( ) : @acceptors
90  
91 Returns a list of the acceptors.
92  
93 =head2 acceptor_count ( ) : $count
94  
95 Returns count of the number of acceptors.
96  
97 =head2 is_quorum ( @replies ) : $bool
98  
99 Takes a list of IDs and sees if they meet a quorum.
100  
101 =head2 highest_proposal_id ( @replies ) : $id
102  
103 Takes a list of replies and returns the highest proposal id from the list.
104  
105 =head2 new_proposal_id ( ) : $id
106  
107 Generates a new proposal id. The default implementation is an increasing
108 integer (literally C<$i++>).
109  
110 =head2 prospose ( $value ) : $id
111  
112 Propose is the main interface between clients and the Paxos cluster/node.
113 Propose takes a single value (the proposal) and returns the ID that is
114 assigned to that proposal. If the proposal fails an exception is thrown.
115  
116 =head1 AUTHOR
117  
118 Chris Prather <chris@prather.org>
119  
120 =head1 COPYRIGHT AND LICENSE
121  
122 This software is copyright (c) 2012 by Chris Prather.
123  
124 This is free software; you can redistribute it and/or modify it under
125 the same terms as the Perl 5 programming language system itself.
126  
127 =cut
128  
129  
130 __END__
« Paxos/Role/Acceptor.pm ↑ Summary Paxos/Exception.pm »