Added more tests and updated the presentation
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
39
lib/paxos.ex
39
lib/paxos.ex
@@ -1,3 +1,12 @@
|
||||
#
|
||||
#
|
||||
# Possible actions
|
||||
# :kill_before_decision
|
||||
# :increase_ballot_number - this makes it so that it does not propose but jump simply increases the number of the current ballot
|
||||
# this is usefull when forcing a nack
|
||||
#
|
||||
|
||||
|
||||
defmodule Paxos do
|
||||
def start(name, processes) do
|
||||
IO.puts("Starting paxos for #{name}")
|
||||
@@ -73,6 +82,16 @@ defmodule Paxos do
|
||||
send(pid_to_inform, {:decision, inst, state.decided[inst]})
|
||||
state
|
||||
|
||||
action == :increase_ballot_number ->
|
||||
state = has_or_create(state, inst)
|
||||
|
||||
IO.puts("#{state.name} - Got request to increase ballot number for inst #{inst}")
|
||||
|
||||
# Inform the pid with timeout right way
|
||||
send(pid_to_inform, {:timeout, inst});
|
||||
|
||||
set_instmap(state, inst, fn map -> %{map| ballot: map.ballot + 1} end)
|
||||
|
||||
not Map.has_key?(state.instmap, inst) ->
|
||||
EagerReliableBroadcast.broadcast(state.name, {:other_propose, inst, value})
|
||||
state = has_or_create(state, inst, value, pid_to_inform, action)
|
||||
@@ -94,6 +113,7 @@ defmodule Paxos do
|
||||
)
|
||||
|
||||
true ->
|
||||
EagerReliableBroadcast.broadcast(state.name, {:other_propose, inst, value})
|
||||
prepare(state, inst)
|
||||
end
|
||||
|
||||
@@ -101,7 +121,7 @@ defmodule Paxos do
|
||||
state = %{state | other_values: Map.put(state.other_values, inst, value)}
|
||||
|
||||
cond do
|
||||
Map.has_key?(state.decided, inst) ->
|
||||
has_finished(state, inst) ->
|
||||
EagerReliableBroadcast.broadcast(
|
||||
state.name,
|
||||
{:decide, inst, state.decided[inst]}
|
||||
@@ -170,7 +190,7 @@ defmodule Paxos do
|
||||
send(state.instmap[inst].pid_to_inform, {:abort, inst})
|
||||
end
|
||||
|
||||
EagerReliableBroadcast.broadcast(state.name, {:abort, inst, ballot, new_ballot})
|
||||
EagerReliableBroadcast.broadcast(state.name, {:abort, inst, ballot})
|
||||
|
||||
set_instmap(state, inst, fn map -> %{
|
||||
map | has_sent_accept: false,
|
||||
@@ -187,16 +207,15 @@ defmodule Paxos do
|
||||
has_finished(state, inst) ->
|
||||
state
|
||||
|
||||
state.instmap[inst].ballot == ballot ->
|
||||
true ->
|
||||
IO.puts("#{state.name} - got information to send abort")
|
||||
|
||||
if Map.has_key?(state.instmap, inst) and state.instmap[inst].pid_to_inform != nil do
|
||||
send(state.instmap[inst].pid_to_inform, {:abort, inst})
|
||||
end
|
||||
|
||||
state
|
||||
|
||||
true ->
|
||||
state
|
||||
|
||||
end
|
||||
|
||||
{:prepared, inst, ballot, accepted_ballot, accepted_value} ->
|
||||
@@ -218,7 +237,7 @@ defmodule Paxos do
|
||||
prepared(state, inst)
|
||||
|
||||
ballot > state.instmap[inst].ballot ->
|
||||
IO.puts("Probably recieved this before preare came to self sending with delay")
|
||||
IO.puts("#{state.name} - Probably recieved this before preare came to self sending with delay")
|
||||
Process.send_after(self(), {:prepared, inst, ballot, accepted_ballot, accepted_value}, 100)
|
||||
state
|
||||
|
||||
@@ -253,7 +272,7 @@ defmodule Paxos do
|
||||
end
|
||||
|
||||
{:accepted, inst, ballot} ->
|
||||
IO.puts("#{state.name} accepted #{inspect(inst)} #{inspect(ballot)}")
|
||||
IO.puts("#{state.name} - accepted #{inspect(inst)} #{inspect(ballot)}")
|
||||
|
||||
cond do
|
||||
has_finished(state, inst) ->
|
||||
@@ -289,7 +308,7 @@ defmodule Paxos do
|
||||
state
|
||||
|
||||
{:rb_deliver, _, {:decide, inst, value}} ->
|
||||
IO.puts("#{state.name} decided #{inspect(inst)} #{inspect(value)}")
|
||||
IO.puts("#{state.name} - decided #{inspect(inst)} #{inspect(value)}")
|
||||
|
||||
if has_finished(state, inst) do
|
||||
state
|
||||
@@ -333,7 +352,7 @@ defmodule Paxos do
|
||||
|
||||
true ->
|
||||
ballot = state.instmap[inst].ballot + 1
|
||||
IO.puts("#{state.name} sending all prepare #{inst} #{ballot}")
|
||||
IO.puts("#{state.name} - sending all prepare #{inst} #{ballot}")
|
||||
EagerReliableBroadcast.broadcast(state.name, {:prepare, state.name, inst, ballot})
|
||||
|
||||
set_instmap(state, inst, fn map -> %{
|
||||
|
||||
Reference in New Issue
Block a user