Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filled in proposer.rb, added empty superclass of all Paxos roles
- Loading branch information
1 parent
bfb9298
commit b0550a5
Showing
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
class PaxosReplica | ||
def initialize | ||
def initialize(supervisor) | ||
@proposer, @acceptor, @learner = Proposer.new, Acceptor.new, Learner.new | ||
supervisor.add_replica(self) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class PaxosRole | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,53 @@ | ||
class Proposer | ||
class Proposer < PaxosRole | ||
def initialize(supervisor) | ||
@supervisor = supervisor | ||
@propose_mutex = Mutex.new | ||
@propose_thread = nil | ||
@n = 0 | ||
end | ||
|
||
protected | ||
def propose(value) | ||
kill_thread | ||
|
||
@propose_thread = Thread.new do | ||
loop do | ||
@propose_mutex.synchronize do | ||
replicas = @supervisor.replicas | ||
|
||
responses = replicas.inject([]) do |memo, replica| | ||
response = replica.paxos_instance.acceptor.prepare(@n) | ||
memo << response if response | ||
end | ||
|
||
next if responses.size < (replicas.size / 2.0).ceil | ||
|
||
number = responses.inject(nil) do |memo, response| | ||
if response.highest_accepted | ||
unless memo and response.highest_accepted <= memo | ||
memo = response.highest_accepted | ||
end | ||
end | ||
end | ||
|
||
number = @n unless number | ||
|
||
responses.each do |response| | ||
response.acceptor.request_accept(Proposal.new(number, value, self)) | ||
end | ||
end | ||
|
||
@n += 1 | ||
end | ||
end | ||
end | ||
|
||
def value_learned! | ||
@propose_thread.kill if @propose_thread | ||
end | ||
|
||
alias_method :value_learned!, :kill_thread | ||
|
||
private | ||
Proposal = Struct.new :number, :value, :proposer | ||
end |