From c4f6cbab20b0b1d08755073d93365e5bd00dc755 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 6 Nov 2018 11:57:09 +0100 Subject: Shard deletion ; update README and TODO --- shard/lib/manager.ex | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'shard/lib/manager.ex') diff --git a/shard/lib/manager.ex b/shard/lib/manager.ex index c178e2f..c3897a3 100644 --- a/shard/lib/manager.ex +++ b/shard/lib/manager.ex @@ -82,6 +82,27 @@ defmodule Shard.Manager do {:reply, pid, state} end + def handle_call({:delete, shard_id}, _from, state) do + case :dets.lookup(@shard_db, shard_id) do + [] -> + {:reply, {:error, :not_found}, state} + [{^shard_id, manifest, {:cached, _}, _}] -> + pid = case :ets.lookup(:shard_procs, {shard_id, nil}) do + [] -> + {:ok, pid} = apply(Shard.Manifest.module(manifest), :start_link, [manifest]) + pid + [{{^shard_id, nil}, pid}] -> + :ets.delete(:shard_procs, {shard_id, nil}) + pid + end + GenServer.call(pid, :delete_shard) + :dets.delete(@shard_db, shard_id) + {:reply, :ok, state} + [{^shard_id, _, _, _}] -> + {:reply, {:error, :pinned}, state} + end + end + def handle_cast({:dispatch_to, shard_id, path, pid}, state) do :ets.insert(:shard_procs, { {shard_id, path}, pid }) state = Map.put(state, pid, {shard_id, path}) @@ -105,6 +126,7 @@ defmodule Shard.Manager do case :dets.lookup(@shard_db, shard_id) do [{^shard_id, manifest, why_have_it, _old_state}] -> :dets.insert(@shard_db, {shard_id, manifest, why_have_it, shst}) + :dets.sync(@shard_db) end {:noreply, state} end @@ -169,7 +191,7 @@ defmodule Shard.Manager do for [id] <- shards do case :ets.lookup(:shard_procs, {id, nil}) do [{{^id, nil}, pid}] -> - GenServer.cast(pid, :delete_shard) + GenServer.call(pid, :delete_shard) _ -> nil end :dets.delete(@shard_db, id) @@ -365,6 +387,13 @@ defmodule Shard.Manager do end end + @doc""" + Delete a shard + """ + def delete(shard_id) do + GenServer.call(__MODULE__, {:delete, shard_id}) + end + @doc""" Return the list of all shards. Returns a list of tuples: -- cgit v1.2.3