Dome

Dome

How to correctly handoff process state using bitwalker/swarm

Hi everybody, this is my first time here so sorry if I make some mistakes.

I’m working on a little project for my thesis. During my study on Elixir I found the swarm library and it suites perfectly on what I need to do. The problem is that I have some troubles using it:

  1. Process state handoff: I have a GenServer with a very simple struct and I need to distribute this worker around my cluster. When the node on which the worker is running goes down, Swarm resume the process on a new node but I cannot manage to resume the state. I’ve followed the worker example provided by the original author on github but it seems to not work. Here’s my code:
defmodule MyApp.Worker do
  use GenServer

  defstruct elements: %{}

  def start_link(opts \\ []) do
    GenServer.start_link(__MODULE__, opts, name: __MODULE__)
  end

  def init(opts) when opts == [] do
    state = %EventStore.Worker{}
    {:ok, state}
  end

  def init(opts) do
    state = opts
    {:ok, state}
  end

  def handle_call({:swarm, :begin_handoff}, _from, state) do
    {:reply, {:resume, state}, state}
  end

  def handle_call({:swarm, :end_handoff, state}, _from, state) do
    {:noreply, state}
  end

  def handle_cast({:swarm, :end_handoff, state}, state) do
    {:noreply, state}
  end

  def handle_cast({:swarm, :resolve_conflict, _delay}, state) do
    {:noreply, state}
  end

  def handle_info({:swarm, :die}, state) do
    {:stop, :shutdown, state}
  end

end

More details. I’ve used Swarm.register_name(workername, workername, :start_link, [opts]) for starting my GenServer and I’ve tried to kill the node via double CTRL+C and via :observer.start too. I really don’t know what is the problem since I’ve also looked at the Swarm tracker.ex file and saw that there’s a call with :begin_endoff which I (think) correctly handle in my GenServer. Indeed if I do the same call manually via shell, my worker handle it.

  1. Same worker on more nodes: if I have a cluster of 3 or more nodes and I would like to run the same process on more nodes at the same time, how can I do? Because after the first registration using the same Swarm function mentioned above, Swarm warns me that there’s already that registered process.

Most Liked

tristan

tristan

Rebar3 Core Team

Exactly. If you want to protect against losing state when losing nodes you need to either replicate it or simply store it in external storage. In Erleans (based on Microsoft Orleans) the latter is the default, https://github.com/erleans/erleans/. The state for a grain is saved to a storage provider and will be reloaded when reactivated.

Erleans also has stateless grains which may be a solution to your second question about the same process on multiple nodes, but not positive.

SophisticaSean

SophisticaSean

I’ve been running Elixir in prod for 3 years now and it is exceedingly rare for a healthy node to explode. The 99.99 percent most likely cause for a node shutdown is a deploy. During a deploy you can give your old nodes some time to gracefully shutdown and utilize the Swarm handoff use case.

Elixir/erlang is very resilient when it comes to crashing and unhandled errors. That being said, I am a firm believer of writing code that can rebuild it’s state in the worse-case scenario. So Swarm is a nice to have but not the only line of defense when it comes to rebuilding whatever state we had stuffed in Swarm.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement