-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For OTP<=25.1 use polling instead of monitor (#120)
- Loading branch information
Showing
5 changed files
with
193 additions
and
82 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,49 +1,125 @@ | ||
defmodule Finitomata.Distributed.GroupMonitor do | ||
@moduledoc false | ||
use GenServer | ||
major = System.otp_release() | ||
|
||
alias Finitomata.Distributed.Supervisor, as: Sup | ||
|
||
def start_link(id) do | ||
GenServer.start_link(__MODULE__, id, name: sup_name(id)) | ||
otp_version = | ||
try do | ||
{:ok, contents} = File.read(Path.join([:code.root_dir(), "releases", major, "OTP_VERSION"])) | ||
String.split(contents, "\n", trim: true) | ||
else | ||
[full] -> full | ||
_ -> major | ||
catch | ||
:error, _ -> major | ||
end | ||
|> String.split(".") | ||
|> case do | ||
[major] -> [major, 0, 0] | ||
[major, minor] -> [major, minor, 0] | ||
[major, minor, patch | _] -> [major, minor, patch] | ||
end | ||
|> Enum.join(".") | ||
|
||
def count(id), do: GenServer.call(sup_name(id), :count) | ||
if Version.compare(otp_version, "25.1.0") == :lt do | ||
defmodule Finitomata.Distributed.GroupMonitor do | ||
@moduledoc false | ||
use GenServer | ||
|
||
defp sup_name(id), do: Module.concat(id, "GroupMonitor") | ||
alias Finitomata.Distributed.Supervisor, as: Sup | ||
|
||
@impl GenServer | ||
def init(id) do | ||
{_reference, _pids} = | ||
id | ||
|> Sup.group() | ||
|> :pg.monitor() | ||
@update_interval Application.compile_env(:finitomata, :pg_update_interval, 500) | ||
|
||
{:ok, 0} | ||
end | ||
def start_link(id) do | ||
GenServer.start_link(__MODULE__, id, name: sup_name(id)) | ||
end | ||
|
||
@impl GenServer | ||
def handle_info({ref, :join, group, pids}, counter) do | ||
{:noreply, | ||
Enum.reduce(pids, counter, fn pid, counter -> | ||
id = Sup.ungroup(group) | ||
name = GenServer.call(pid, :name) | ||
Sup.put(id, name, %{node: :erlang.node(pid), pid: pid, ref: ref}) | ||
counter + 1 | ||
end)} | ||
end | ||
def count(id), do: GenServer.call(sup_name(id), :count) | ||
def members(id), do: id |> Sup.group() |> :pg.get_members() | ||
|
||
defp sup_name(id), do: Module.concat(id, "GroupMonitor") | ||
|
||
@impl GenServer | ||
def init(id) do | ||
Process.send_after(self(), {:update, id}, @update_interval) | ||
|
||
{:ok, {0, MapSet.new([])}} | ||
end | ||
|
||
@impl GenServer | ||
def handle_info({:update, id}, {count, members}) do | ||
updated_members = id |> Sup.group() |> :pg.get_members() |> MapSet.new() | ||
|
||
joined = MapSet.difference(updated_members, members) | ||
handle_join(id, joined) | ||
|
||
left = MapSet.difference(members, updated_members) | ||
handle_leave(id, left) | ||
|
||
Process.send_after(self(), {:update, id}, @update_interval) | ||
{:noreply, {count, updated_members}} | ||
end | ||
|
||
@impl GenServer | ||
def handle_info({_ref, :leave, group, pids}, counter) do | ||
deleted = | ||
group | ||
|> Sup.ungroup() | ||
|> Sup.delete_by_pids(pids) | ||
|> map_size() | ||
defp handle_join(id, pids) do | ||
for pid <- pids do | ||
name = GenServer.call(pid, :name) | ||
Sup.put(id, name, %{node: :erlang.node(pid), pid: pid, ref: make_ref()}) | ||
end | ||
end | ||
|
||
{:noreply, counter - deleted} | ||
defp handle_leave(id, pids) do | ||
Sup.delete_by_pids(id, pids) | ||
end | ||
|
||
@impl GenServer | ||
def handle_call(:count, _from, {count, members}), do: {:reply, count, {count, members}} | ||
end | ||
else | ||
defmodule Finitomata.Distributed.GroupMonitor do | ||
@moduledoc false | ||
use GenServer | ||
|
||
alias Finitomata.Distributed.Supervisor, as: Sup | ||
|
||
def start_link(id) do | ||
GenServer.start_link(__MODULE__, id, name: sup_name(id)) | ||
end | ||
|
||
def count(id), do: GenServer.call(sup_name(id), :count) | ||
def members(id), do: id |> Sup.group() |> :pg.get_members() | ||
|
||
defp sup_name(id), do: Module.concat(id, "GroupMonitor") | ||
|
||
@impl GenServer | ||
def init(id) do | ||
{_reference, _pids} = | ||
id | ||
|> Sup.group() | ||
|> :pg.monitor() | ||
|
||
@impl GenServer | ||
def handle_call(:count, _from, state), do: {:reply, state, state} | ||
{:ok, 0} | ||
end | ||
|
||
@impl GenServer | ||
def handle_info({ref, :join, group, pids}, count) do | ||
{:noreply, | ||
Enum.reduce(pids, count, fn pid, counter -> | ||
id = Sup.ungroup(group) | ||
name = GenServer.call(pid, :name) | ||
Sup.put(id, name, %{node: :erlang.node(pid), pid: pid, ref: ref}) | ||
counter + 1 | ||
end)} | ||
end | ||
|
||
@impl GenServer | ||
def handle_info({_ref, :leave, group, pids}, count) do | ||
deleted = | ||
group | ||
|> Sup.ungroup() | ||
|> Sup.delete_by_pids(pids) | ||
|> map_size() | ||
|
||
{:noreply, count - deleted} | ||
end | ||
|
||
@impl GenServer | ||
def handle_call(:count, _from, count), do: {:reply, count, count} | ||
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
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
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
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,52 +1,54 @@ | ||
defmodule Infinitomata.Test do | ||
use ExUnit.Case, async: true | ||
|
||
@moduletag :distributed | ||
|
||
setup do | ||
{_peers, _nodes} = Enfiladex.start_peers(3) | ||
# Enfiladex.block_call_everywhere(Infinitomata, :start_link, [InfiniTest]) | ||
Infinitomata.start_link(InfiniTest) | ||
# on_exit(fn -> Enfiladex.stop_peers(peers) end) | ||
Enfiladex.call_everywhere(Infinitomata, :start_link, [InfiniTest]) | ||
Process.sleep(1000) | ||
:ok | ||
end | ||
|
||
test "kinda stress-test instances (distributed)" do | ||
for i <- 1..100 do | ||
assert match?( | ||
{:ok, pid} when is_pid(pid), | ||
Infinitomata.start_fsm(InfiniTest, "FSM_ST_#{i}", Finitomata.Test.Log, %{ | ||
instance: i | ||
}) | ||
) | ||
|
||
assert :ok = Infinitomata.transition(InfiniTest, "FSM_ST_#{i}", :accept) | ||
assert :ok = Infinitomata.transition(InfiniTest, "FSM_ST_#{i}", :__end__) | ||
if Finitomata.MixProject.lib?(:enfiladex) do | ||
defmodule Infinitomata.Test do | ||
use ExUnit.Case, async: true | ||
|
||
@moduletag :distributed | ||
|
||
setup do | ||
{_peers, _nodes} = Enfiladex.start_peers(3) | ||
# Enfiladex.block_call_everywhere(Infinitomata, :start_link, [InfiniTest]) | ||
Infinitomata.start_link(InfiniTest) | ||
# on_exit(fn -> Enfiladex.stop_peers(peers) end) | ||
Enfiladex.call_everywhere(Infinitomata, :start_link, [InfiniTest]) | ||
Process.sleep(1000) | ||
:ok | ||
end | ||
end | ||
|
||
test "many instances (distributed)" do | ||
for i <- 1..10 do | ||
Infinitomata.start_fsm(InfiniTest, "FSM_#{i}", Finitomata.Test.Log, %{instance: i}) | ||
test "kinda stress-test instances (distributed)" do | ||
for i <- 1..100 do | ||
assert match?( | ||
{:ok, pid} when is_pid(pid), | ||
Infinitomata.start_fsm(InfiniTest, "FSM_ST_#{i}", Finitomata.Test.Log, %{ | ||
instance: i | ||
}) | ||
) | ||
|
||
assert :ok = Infinitomata.transition(InfiniTest, "FSM_ST_#{i}", :accept) | ||
assert :ok = Infinitomata.transition(InfiniTest, "FSM_ST_#{i}", :__end__) | ||
end | ||
end | ||
|
||
assert Infinitomata.count(InfiniTest) == 10 | ||
test "many instances (distributed)" do | ||
for i <- 1..10 do | ||
Infinitomata.start_fsm(InfiniTest, "FSM_#{i}", Finitomata.Test.Log, %{instance: i}) | ||
end | ||
|
||
for i <- 1..10 do | ||
Infinitomata.transition(InfiniTest, "FSM_#{i}", :accept) | ||
end | ||
assert Infinitomata.count(InfiniTest) == 10 | ||
|
||
assert %{"FSM_1" => %{}} = Infinitomata.all(InfiniTest) | ||
for i <- 1..10 do | ||
Infinitomata.transition(InfiniTest, "FSM_#{i}", :accept) | ||
end | ||
|
||
for i <- 1..10 do | ||
Infinitomata.transition(InfiniTest, "FSM_#{i}", :__end__) | ||
end | ||
assert %{"FSM_1" => %{}} = Infinitomata.all(InfiniTest) | ||
|
||
for i <- 1..10 do | ||
Infinitomata.transition(InfiniTest, "FSM_#{i}", :__end__) | ||
end | ||
|
||
Process.sleep(1_000) | ||
Process.sleep(1_000) | ||
|
||
assert Infinitomata.count(InfiniTest) == 0 | ||
assert Infinitomata.all(InfiniTest) == %{} | ||
assert Infinitomata.count(InfiniTest) == 0 | ||
assert Infinitomata.all(InfiniTest) == %{} | ||
end | ||
end | ||
end |