Skip to content

Commit 6a46c18

Browse files
committed
issue-97, cache: move reconfiguration logic to cache creation
Cache creation will be executed at startup and result in no-op if cache already exists. If cache appears to be old (< 0.6.0), it will be updated. Signed-off-by: Matteo Cafasso <[email protected]>
1 parent 3c88165 commit 6a46c18

File tree

1 file changed

+26
-22
lines changed
  • lib/rabbitmq_message_deduplication

1 file changed

+26
-22
lines changed

lib/rabbitmq_message_deduplication/cache.ex

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ defmodule RabbitMQMessageDeduplication.Cache do
2727
Create a new cache with the given name and options.
2828
2929
A distributed cache is replicated across multiple nodes.
30+
3031
"""
3132
@spec create(atom, boolean, list) :: :ok | { :error, any }
3233
def create(cache, distributed, options) do
@@ -162,7 +163,6 @@ defmodule RabbitMQMessageDeduplication.Cache do
162163
end
163164
def change_option(_, option, _), do: {:error, {:invalid, option}}
164165

165-
166166
## Utility functions
167167

168168
# Mnesia cache table creation.
@@ -180,24 +180,12 @@ defmodule RabbitMQMessageDeduplication.Cache do
180180
{:ttl, Keyword.get(options, :ttl)}]}]
181181

182182
case Mnesia.create_table(cache, options) do
183-
{:atomic, :ok} -> wait_for_cache(cache)
184-
{:aborted, {:already_exists, _}} -> maybe_reconfigure(cache, distributed)
185-
{:aborted, {:already_exists, _, _}} -> maybe_reconfigure(cache, distributed)
186-
error -> error
187-
end
188-
end
189-
190-
# Old caches created prior to v0.6.0 need to be reconfigured.
191-
defp maybe_reconfigure(cache, distributed) do
192-
if cache_property(cache, :distributed) == nil do
193-
cache_property(cache, :distributed, distributed)
194-
cache_property(cache, :size, cache_property(cache, :limit))
195-
cache_property(cache, :ttl, cache_property(cache, :default_ttl))
196-
197-
Mnesia.delete_table_property(cache, :limit)
198-
Mnesia.delete_table_property(cache, :default_ttl)
199-
200-
wait_for_cache(cache)
183+
{:atomic, :ok} ->
184+
wait_for_cache(cache)
185+
{:aborted, reason} when elem(reason, 0) == :already_exists ->
186+
maybe_reconfigure(cache, distributed)
187+
error ->
188+
error
201189
end
202190
end
203191

@@ -248,7 +236,7 @@ defmodule RabbitMQMessageDeduplication.Cache do
248236

249237
# Retrieve the given property from the Mnesia user_properties field
250238
defp cache_property(cache, property) do
251-
Mnesia.table_info(cache, :user_properties) |> Keyword.get(property)
239+
cache |> Mnesia.table_info(:user_properties) |> Keyword.get(property)
252240
end
253241

254242
# Set the given Mnesia user_properties field
@@ -265,8 +253,10 @@ defmodule RabbitMQMessageDeduplication.Cache do
265253

266254
for node <- cache_replicas(cache_nodes) do
267255
case Mnesia.add_table_copy(cache, node, storage_type) do
268-
{:atomic, :ok} -> wait_for_cache(cache)
269-
{:aborted, {:already_exists, ^cache, _}} -> wait_for_cache(cache)
256+
{:atomic, :ok} ->
257+
wait_for_cache(cache)
258+
{:aborted, reason} when elem(reason, 0) == :already_exists ->
259+
maybe_reconfigure(cache, true)
270260
end
271261
end
272262
end
@@ -287,4 +277,18 @@ defmodule RabbitMQMessageDeduplication.Cache do
287277
nodes -> {:ram_nodes, nodes}
288278
end
289279
end
280+
281+
# Caches created prior to v0.6.0 need to be reconfigured.
282+
defp maybe_reconfigure(cache, distributed) do
283+
if cache_property(cache, :distributed) == nil do
284+
cache_property(cache, :distributed, distributed)
285+
cache_property(cache, :size, cache_property(cache, :limit))
286+
cache_property(cache, :ttl, cache_property(cache, :default_ttl))
287+
288+
Mnesia.delete_table_property(cache, :limit)
289+
Mnesia.delete_table_property(cache, :default_ttl)
290+
end
291+
292+
wait_for_cache(cache)
293+
end
290294
end

0 commit comments

Comments
 (0)