Skip to content

Commit

Permalink
Filled in proposer.rb, added empty superclass of all Paxos roles
Browse files Browse the repository at this point in the history
  • Loading branch information
robhanlon22 committed Dec 10, 2009
1 parent bfb9298 commit b0550a5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
3 changes: 2 additions & 1 deletion paxos_replica.rb
@@ -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
2 changes: 2 additions & 0 deletions paxos_role.rb
@@ -0,0 +1,2 @@
class PaxosRole
end
53 changes: 52 additions & 1 deletion proposer.rb
@@ -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

0 comments on commit b0550a5

Please sign in to comment.