colibri

colibri

The simplest possible example of using :ets.give.away to save a table if the table owner’s process crashes.

Add genservers to the supervisor’s tree

{UsersEtsTableBackup, []},
{UsersEtsTable, []}

UsersEtsTableBackup - is needed only to create a table and transfer the table and the rights to use it to the working process and also gets the table back if the working process crashes

defmodule UsersEtsTableBackup do
use GenServer

######## Client API #########################################################

def give_me_ets(data), do: GenServer.call(__MODULE__, data)

######## Server callbacks ###################################################

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

def init(state) do
      with :undefined <- :ets.whereis(:users) do
         :ets.new(:users, [:set, :protected, :named_table, {:heir, self(), {}}])
      else
        _ -> UsersEtsTable.set_me_heir(:set_me_heir)
      end

    {:ok, state}
end
#############################################################################

def handle_call(:give_me_ets, {pid, _}, state) do
      :ets.give_away(:users, pid, {})
    {:reply, :ok, state}
end

def handle_call(_, _from, state) do
    {:reply, :ok, state}
end

#############################################################################

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

#############################################################################

def handle_info({:"ETS-TRANSFER", :users, _, {}}, state) do
    {:noreply, state}
end

def handle_info(_, state) do
    {:noreply, state}
end

#############################################################################

end

UsersEtsTable - work process, gets the table and rights from UsersEtsTableBackup and works with it

defmodule UsersEtsTable do
use GenServer

######## Client API #########################################################

def set_me_heir(data), do: GenServer.call(__MODULE__, data)

######## Server callbacks ###################################################

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

def init(state), do: {:ok, state, {:continue, :give_me_ets}}

#############################################################################

def handle_call(:set_me_heir, {pid, _}, state) do
      :ets.setopts(:users, {:heir, pid, {}})
    {:reply, :ok, state}
end

def handle_call(_, _from, state) do
    {:reply, :ok, state}
end

#############################################################################

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

#############################################################################

def handle_continue(:give_me_ets, state) do
    UsersEtsTableBackup.give_me_ets(:give_me_ets)
    {:noreply, state}
end

def handle_continue(_, state) do
    {:noreply, state}
end

#############################################################################

def handle_info({:"ETS-TRANSFER", :users, _, {}}, state) do
    {:noreply, state}
end

def handle_info(_, state) do
    {:noreply, state}
end

#############################################################################

end

Showing Posts 1 to 2

dimitarvp

dimitarvp

While I don’t see anything wrong on a quick glance, I question why do you need this code at all.

Using a supervised process in your application’s tree is the safest way to have a persistent owner of an ETS table. It does not need to do anything else; it should just be there owning the table(s) and should not react to any messages.

If such a process crashes then you have much bigger problems i.e. OTP itself has a bug which is extremely unlikely on that particular code path.

So your code, while not looking to be wrong, is to me unnecessary. Drop the paranoia. If you make one GenServer that initializes by creating ETS tables and registering itself as their owner and then does nothing else, while staying as a part of your application’s supervision tree, then you will have no issues with crashes. No need for backup owner, no need for heirs or giving ETS tables away.

colibri

colibri OP

If there is a function in a language, then someone needs it.
The process that creates the table does not have to be a genserver, this is just an example.
Processes in elixir are easy and cheap to use and data is always important.

Regards

P.S Paranoia is a keen sense of reality o_O

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews