Finished leader elector

This commit is contained in:
Andre Henriques 2024-01-02 23:40:03 +00:00
parent f9b734fa12
commit 06c7b1f368
2 changed files with 5 additions and 13 deletions

2
.gitignore vendored
View File

@ -24,3 +24,5 @@ distributed_system_coursework-*.tar
# Temporary files, for example, from tests. # Temporary files, for example, from tests.
/tmp/ /tmp/
*.bream

View File

@ -7,10 +7,6 @@ defmodule EventualLeaderElector do
String.to_atom(Atom.to_string(name) <> "_ele") String.to_atom(Atom.to_string(name) <> "_ele")
end end
def getOriginalName(name) do
String.to_atom(String.replace(Atom.to_string(name), "_ele", ""))
end
def start(name, processes) do def start(name, processes) do
new_name = getEleName(name) new_name = getEleName(name)
pid = spawn(EventualLeaderElector, :init, [new_name, name, processes]) pid = spawn(EventualLeaderElector, :init, [new_name, name, processes])
@ -27,7 +23,6 @@ defmodule EventualLeaderElector do
name: name, name: name,
parent: parent, parent: parent,
processes: processes, processes: processes,
mp_processes: MapSet.new(processes),
timeout: 1000, timeout: 1000,
heard_back: MapSet.new(), heard_back: MapSet.new(),
seq: 0, seq: 0,
@ -49,7 +44,7 @@ defmodule EventualLeaderElector do
run( run(
receive do receive do
{:heartbeat_request, name, seq} -> {:heartbeat_request, name, seq} ->
Utils.unicast({:heartbeat, state.name, seq}, name) Utils.unicast({:heartbeat, state.parent, seq}, name)
state state
{:heartbeat, name, seq} -> {:heartbeat, name, seq} ->
@ -60,16 +55,11 @@ defmodule EventualLeaderElector do
end end
{:timeout} -> {:timeout} ->
active = MapSet.intersection(state.mp_processes, state.heard_back)
state = %{state | heard_back: MapSet.new()}
state = state =
if MapSet.size(active) == 0 do if MapSet.size(state.heard_back) == 0 do
state state
else else
to_trust = Enum.at(Enum.sort(MapSet.to_list(active)), 0) to_trust = Enum.at(Enum.sort(MapSet.to_list(state.heard_back)), 0)
to_trust = getOriginalName(to_trust)
if state.last_trust != to_trust do if state.last_trust != to_trust do
Utils.unicast({:ele_trust, to_trust}, state.parent) Utils.unicast({:ele_trust, to_trust}, state.parent)