Packages
spawn
0.5.0-rc.10
2.0.0-RC9
2.0.0-RC8
2.0.0-RC7
2.0.0-RC6
2.0.0-RC5
2.0.0-RC4
2.0.0-RC3
2.0.0-RC2
2.0.0-RC14
2.0.0-RC13
2.0.0-RC12
2.0.0-RC11
2.0.0-RC10
2.0.0-RC1
1.4.3
1.4.2
1.4.1
1.4.0
1.3.3
1.3.2
1.3.1
1.3.0
1.2.1
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc3
1.0.0-rc16
1.0.0-rc1
1.0.0-rc.38
1.0.0-rc.37
1.0.0-rc.36
1.0.0-rc.35
1.0.0-rc.34
1.0.0-rc.33
1.0.0-rc.32
1.0.0-rc.31
1.0.0-rc.30
1.0.0-rc.29
1.0.0-rc.28
1.0.0-rc.27
1.0.0-rc.26
1.0.0-rc.25
1.0.0-rc.24
1.0.0-rc.23
1.0.0-rc.22
1.0.0-rc.21
1.0.0-rc.20
1.0.0-rc.19
1.0.0-rc.18
1.0.0-rc.17
1.0.0-rc.2
0.6.3
0.6.2
0.6.1
0.6.0
0.5.5
0.5.4
0.5.3
0.5.1
0.5.0
0.5.0-rc.13
0.5.0-rc.12
0.5.0-rc.11
0.5.0-rc.10
0.5.0-rc.9
0.5.0-rc.8
0.5.0-rc.7
0.5.0-rc.6
0.5.0-rc.5
0.5.0-rc.3
0.5.0-alpha.13
0.5.0-alpha.12
0.5.0-alpha.11
0.5.0-alpha.10
0.5.0-alpha.9
0.5.0-alpha.8
0.5.0-alpha.7
0.5.0-alpha.6
0.5.0-alpha.5
0.5.0-alpha.4
0.5.0-alpha.3
0.5.0-alpha.2
0.5.0-alpha.1
0.1.0
Spawn is the core lib for Spawn Actors System
Current section
Files
Jump to
Current section
Files
lib/spawn/cluster/state_handoff.ex
defmodule Spawn.Cluster.StateHandoff do
@moduledoc false
use GenServer
require Logger
@call_timeout 15_000
@default_sync_interval 5
@default_ship_interval 5
@default_ship_debounce 5
def child_spec(opts \\ []) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]}
}
end
@impl true
def init(opts) do
Process.flag(:message_queue_data, :off_heap)
:net_kernel.monitor_nodes(true, node_type: :visible)
{:ok, crdt_pid} =
DeltaCrdt.start_link(DeltaCrdt.AWLWWMap,
sync_interval: Keyword.get(opts, :sync_interval, @default_sync_interval),
ship_interval: Keyword.get(opts, :ship_interval, @default_ship_interval),
ship_debounce: Keyword.get(opts, :ship_debounce, @default_ship_debounce)
)
{:ok, crdt_pid}
end
@impl true
def handle_info({:nodeup, node, _node_type}, state) do
Logger.debug("Received :nodeup event from #{inspect(node)}")
{:noreply, state}
end
def handle_info({:nodedown, node, _node_type}, state) do
Logger.debug("Received :nodedown event from #{inspect(node)}")
{:noreply, state}
end
@impl true
def handle_call({:set_neighbours, other_node}, _from, this_crdt_pid) do
Logger.debug(
"Sending :set_neighbours to #{inspect(other_node)} with #{inspect(this_crdt_pid)}"
)
other_crdt_pid = GenServer.call(other_node, {:fulfill_set_neighbours, this_crdt_pid})
# add other_node's crdt_pid as a neighbour, we need to add both ways so changes in either
# are reflected across, otherwise it would be one way only
DeltaCrdt.set_neighbours(this_crdt_pid, [other_crdt_pid])
{:reply, :ok, this_crdt_pid}
end
def handle_call({:fulfill_set_neighbours, other_crdt_pid}, _from, this_crdt_pid) do
Logger.debug("Adding neighbour #{inspect(other_crdt_pid)} to this #{inspect(this_crdt_pid)}")
DeltaCrdt.set_neighbours(this_crdt_pid, [other_crdt_pid])
{:reply, this_crdt_pid, this_crdt_pid}
end
def handle_call({:handoff, actor, hosts}, _from, crdt_pid) do
DeltaCrdt.put(crdt_pid, actor, hosts)
{:reply, :ok, crdt_pid}
end
def handle_call({:get, actor}, _from, crdt_pid) do
hosts = DeltaCrdt.get(crdt_pid, actor)
{:reply, hosts, crdt_pid}
end
def handle_call(:get_all_invocations, _from, crdt_pid) do
schedules =
crdt_pid
|> DeltaCrdt.to_map()
|> Map.values()
|> List.flatten()
|> Enum.map(& &1.opts[:invocations])
|> List.flatten()
|> Enum.reject(&is_nil/1)
|> Enum.uniq()
{:reply, schedules, crdt_pid}
end
@impl true
def handle_call({:clean, node}, _from, crdt_pid) do
Logger.debug("Received cleanup action from Node #{inspect(node)}")
actors = DeltaCrdt.to_map(crdt_pid)
new_hosts =
actors
|> Enum.map(fn {key, hosts} ->
hosts_not_in_node = Enum.reject(hosts, &(&1.node == node))
{key, hosts_not_in_node}
end)
|> Map.new()
drop_operations = actors |> Map.keys() |> Enum.map(&{:remove, [&1]})
merge_operations = Enum.map(new_hosts, fn {key, value} -> {:add, [key, value]} end)
# this is calling the internals of DeltaCrdt GenServer function (to keep atomicity in check)
GenServer.call(crdt_pid, {:bulk_operation, drop_operations ++ merge_operations})
Logger.debug("Hosts cleaned for node #{inspect(node)}")
{:reply, :ok, crdt_pid}
end
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end
@doc """
Join this crdt with one on another node by adding it as a neighbour
"""
def join(other_node) do
Logger.debug("Joining StateHandoff at #{inspect(other_node)}")
GenServer.call(__MODULE__, {:set_neighbours, {__MODULE__, other_node}})
end
@doc """
Store a actor and entity in the handoff crdt
"""
def set(actor, hosts) do
GenServer.call(__MODULE__, {:handoff, actor, hosts})
end
@doc """
Pickup the stored entity data for a actor
"""
def get(actor) do
GenServer.call(__MODULE__, {:get, actor}, @call_timeout)
end
def get_all_invocations do
GenServer.call(__MODULE__, :get_all_invocations, @call_timeout)
end
@doc """
Cluster HostActor cleanup
"""
def clean(node) do
GenServer.call(__MODULE__, {:clean, node}, @call_timeout)
end
end