Packages
spawn
2.0.0-RC4
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/manager.ex
defmodule Spawn.Cluster.StateHandoff.Manager do
@moduledoc """
This handles state handoff in a cluster.
This module monitors node up and down events as well as node terminate events and triggers `Spawn.Cluster.StateHandoff.ControllerBehaviour` implementations to handle these events.
"""
use GenServer
require Logger
def child_spec(id, opts \\ []) do
%{
id: id,
start: {__MODULE__, :start_link, [opts]},
restart: :permanent
}
end
defmodule State do
defstruct data: nil, controller: nil, timer: nil
end
@impl true
def init(opts) do
controller =
Application.get_env(
:spawn,
:state_handoff_controller_adapter,
Spawn.Cluster.StateHandoff.Controllers.CrdtController
)
do_init(opts)
case controller.handle_init(opts) do
{initial_state, {evt, delay} = _scheduler} ->
timer = Process.send_after(self(), {:timer, evt}, delay)
{:ok, %State{controller: controller, data: initial_state, timer: timer},
{:continue, :after_init}}
initial_state ->
{:ok, %State{controller: controller, data: initial_state}, {:continue, :after_init}}
end
end
@impl true
def handle_continue(:after_init, %State{controller: controller, data: data} = state) do
new_data = controller.handle_after_init(data)
{:noreply, %State{state | data: new_data}}
end
@impl true
def terminate(_reason, %State{controller: controller, data: data} = state) do
Logger.debug("Calling StateHandoff Manager terminate for controller #{inspect(controller)}")
node = Node.self()
new_data = controller.handle_terminate(node, data)
{:ok, %State{state | data: new_data}}
end
@impl true
def handle_call({:set, actor_id, host}, from, state) do
new_data = state.controller.set(actor_id, Node.self(), host, state.data)
{:reply, from, %State{state | data: new_data}}
end
@impl true
def handle_call({:get_actor_hosts_by_actor_id, actor_id}, _from, state) do
{_new_data, hosts} = state.controller.get_by_id(actor_id, state.data)
{:reply, hosts, state}
end
def handle_call({:clean, node}, _from, state) do
new_data = state.controller.clean(node, state.data)
{:reply, new_data, %State{state | data: new_data}}
end
@impl true
def handle_info(
{:timer, event},
%State{controller: controller, data: data, timer: timer} = state
) do
if !is_nil(timer) do
Process.cancel_timer(timer)
end
case controller.handle_timer(event, data) do
{new_data, {evt, delay} = _timer} ->
new_timer = Process.send_after(self(), {:timer, evt}, delay)
{:noreply, %State{state | data: new_data, timer: new_timer}}
new_data ->
{:noreply, %State{state | data: new_data}}
end
end
def handle_info({:nodeup, node, node_type}, %State{controller: controller, data: data} = state) do
Logger.debug("Received :nodeup event from #{inspect(node)}")
new_data = controller.handle_nodeup_event(node, node_type, data)
{:noreply, %State{state | data: new_data}}
end
def handle_info(
{:nodedown, node, node_type},
%State{controller: controller, data: data} = state
) do
Logger.debug("Received :nodedown event from #{inspect(node)}")
new_data = controller.handle_nodedown_event(node, node_type, data)
{:noreply, %State{state | data: new_data}}
end
def handle_info(event, state) do
Logger.debug("Received handle_info event #{inspect(event)}")
{:noreply, state}
end
# Client API
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end
@doc """
Store a actor and entity in the lookup store
"""
def set(actor_id, host), do: GenServer.call(__MODULE__, {:set, actor_id, host}, :infinity)
@doc """
Pickup the stored entity data for a actor
"""
def get(actor_id),
do: GenServer.call(__MODULE__, {:get_actor_hosts_by_actor_id, actor_id}, :infinity)
@doc """
Cluster HostActor cleanup
"""
def clean(node) do
Logger.debug("Received cleanup action from Node #{inspect(node)}")
GenServer.call(__MODULE__, {:clean, node})
Logger.debug("Hosts cleaned for node #{inspect(node)}")
end
# Private functions
defp do_init(_config) do
Process.flag(:trap_exit, true)
Process.flag(:message_queue_data, :off_heap)
:net_kernel.monitor_nodes(true, node_type: :visible)
end
end