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.
/tmp/
*.bream

View File

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