Skip to content

Commit b4b5c3f

Browse files
authored
chore(sync-service): Refactor ShapeStatus initialize (#3296)
Based on @alco 's feedback on #3293
1 parent 3a00202 commit b4b5c3f

File tree

5 files changed

+39
-34
lines changed

5 files changed

+39
-34
lines changed

packages/sync-service/lib/electric/shape_cache/shape_status.ex

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule Electric.ShapeCache.ShapeStatusBehaviour do
2020
@type stack_ref() :: atom() | stack_id() | [stack_id: stack_id()]
2121
end
2222

23-
@callback initialise(stack_ref(), Storage.t()) :: :ok | {:error, term()}
23+
@callback initialize_from_storage(stack_ref(), Storage.t()) :: :ok | {:error, term()}
2424
@callback terminate(stack_ref(), String.t()) :: :ok | {:error, term()}
2525
@callback list_shapes(stack_ref()) :: [{shape_handle(), Shape.t()}]
2626
@callback count_shapes(stack_ref()) :: non_neg_integer()
@@ -84,20 +84,27 @@ defmodule Electric.ShapeCache.ShapeStatus do
8484
@snapshot_started :snapshot_started
8585

8686
@impl true
87-
def initialise(stack_ref, storage \\ nil) do
88-
last_used_table = shape_last_used_table(stack_ref)
89-
:ets.new(last_used_table, [:named_table, :public, :ordered_set])
90-
87+
def initialize_from_storage(stack_ref, storage) do
88+
last_used_table = create_last_used_table(stack_ref)
9189
meta_table = shape_meta_table(stack_ref)
9290

93-
if storage do
94-
load_from_storage(stack_ref, storage)
95-
else
96-
:ets.new(meta_table, [:named_table, :public, :ordered_set])
97-
:ok
91+
case load_table_backup(meta_table, storage) do
92+
{:ok, ^meta_table, path} ->
93+
Logger.info("Loaded shape status from backup at #{path}")
94+
:ok
95+
96+
_ ->
97+
Logger.debug("No shape status backup loaded, creating new table #{meta_table}")
98+
create_meta_table(stack_ref)
99+
load(meta_table, last_used_table, storage)
98100
end
99101
end
100102

103+
def initialize_empty(stack_ref) do
104+
create_last_used_table(stack_ref)
105+
create_meta_table(stack_ref)
106+
end
107+
101108
@impl true
102109
def terminate(stack_ref, backup_dir) do
103110
meta_table = shape_meta_table(stack_ref)
@@ -366,6 +373,20 @@ defmodule Electric.ShapeCache.ShapeStatus do
366373
def shape_last_used_table(state) when is_map(state), do: state.shape_last_used_table
367374
end
368375

376+
defp create_last_used_table(stack_ref) do
377+
last_used_table = shape_last_used_table(stack_ref)
378+
379+
:ets.new(last_used_table, [:named_table, :public, :ordered_set])
380+
last_used_table
381+
end
382+
383+
defp create_meta_table(stack_ref) do
384+
meta_table = shape_meta_table(stack_ref)
385+
386+
:ets.new(meta_table, [:named_table, :public, :ordered_set])
387+
meta_table
388+
end
389+
369390
defp load(meta_table, last_used_table, storage) do
370391
with {:ok, shapes} <- Storage.get_all_stored_shapes(storage) do
371392
now = System.monotonic_time()
@@ -412,22 +433,6 @@ defmodule Electric.ShapeCache.ShapeStatus do
412433
end
413434
end
414435

415-
defp load_from_storage(stack_ref, storage) do
416-
last_used_table = shape_last_used_table(stack_ref)
417-
meta_table = shape_meta_table(stack_ref)
418-
419-
case load_table_backup(meta_table, storage) do
420-
{:ok, ^meta_table, path} ->
421-
Logger.info("Loaded shape status from backup at #{path}")
422-
:ok
423-
424-
_ ->
425-
Logger.debug("No shape status backup loaded, creating new table #{meta_table}")
426-
:ets.new(meta_table, [:named_table, :public, :ordered_set])
427-
load(meta_table, last_used_table, storage)
428-
end
429-
end
430-
431436
defp load_table_backup(meta_table, storage) do
432437
case backup_dir(storage) do
433438
nil ->

packages/sync-service/lib/electric/shape_cache/shape_status_owner.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ defmodule Electric.ShapeCache.ShapeStatusOwner do
4040
Logger.metadata(stack_id: stack_id)
4141
Electric.Telemetry.Sentry.set_tags_context(stack_id: stack_id)
4242

43-
:ok = ShapeStatus.initialise(stack_id, config.storage)
43+
:ok = ShapeStatus.initialize_from_storage(stack_id, config.storage)
4444

4545
Electric.LsnTracker.create_table(stack_id)
4646

packages/sync-service/test/electric/shape_cache/expiry_manager_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule Electric.ExpiryManagerTest do
1919
@max_shapes 10
2020

2121
setup %{stack_id: stack_id} do
22-
ShapeStatus.initialise(stack_id)
22+
ShapeStatus.initialize_empty(stack_id)
2323

2424
expiry_manager =
2525
start_supervised!(

packages/sync-service/test/electric/shape_cache/shape_status_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ defmodule Electric.ShapeCache.ShapeStatusTest do
5656

5757
state = shape_status_opts(opts)
5858

59-
:ok = ShapeStatus.initialise(state, state.storage)
59+
:ok = ShapeStatus.initialize_from_storage(state, state.storage)
6060

6161
shapes = Keyword.get(opts, :shapes, [])
6262

@@ -357,7 +357,7 @@ defmodule Electric.ShapeCache.ShapeStatusTest do
357357

358358
state = shape_status_opts(table: table)
359359

360-
assert :ok = ShapeStatus.initialise(state, state.storage)
360+
assert :ok = ShapeStatus.initialize_from_storage(state, state.storage)
361361
shape = shape!()
362362
assert {:ok, shape_handle} = ShapeStatus.add_shape(state, shape)
363363
assert [{^shape_handle, ^shape}] = ShapeStatus.list_shapes(state)
@@ -383,7 +383,7 @@ defmodule Electric.ShapeCache.ShapeStatusTest do
383383

384384
state2 = shape_status_opts(table: table)
385385

386-
assert :ok = ShapeStatus.initialise(state2, state2.storage)
386+
assert :ok = ShapeStatus.initialize_from_storage(state2, state2.storage)
387387
assert [{^shape_handle, ^shape}] = ShapeStatus.list_shapes(state2)
388388
# consuming backup directory should have removed it after load
389389
refute File.exists?(backup_file)
@@ -403,7 +403,7 @@ defmodule Electric.ShapeCache.ShapeStatusTest do
403403

404404
state = shape_status_opts(table: table)
405405

406-
assert :ok = ShapeStatus.initialise(state, state.storage)
406+
assert :ok = ShapeStatus.initialize_from_storage(state, state.storage)
407407
shape = shape!()
408408
assert {:ok, shape_handle} = ShapeStatus.add_shape(state, shape)
409409
assert [{^shape_handle, ^shape}] = ShapeStatus.list_shapes(state)
@@ -425,7 +425,7 @@ defmodule Electric.ShapeCache.ShapeStatusTest do
425425

426426
state2 = shape_status_opts(table: table)
427427

428-
assert :ok = ShapeStatus.initialise(state2, state2.storage)
428+
assert :ok = ShapeStatus.initialize_from_storage(state2, state2.storage)
429429
# Shape from backup should NOT be present after failed integrity
430430
assert [] == ShapeStatus.list_shapes(state2)
431431
refute File.exists?(backup_file)

packages/sync-service/test/electric/shapes/monitor_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule Electric.Shapes.MonitorTest do
2424
defp start_stack_status(ctx) do
2525
Repatch.patch(Electric.Shapes.Shape, :generate_id, fn _shape -> {"hash", @shape_handle} end)
2626

27-
:ok = Electric.ShapeCache.ShapeStatus.initialise(ctx.stack_id, ctx.storage)
27+
:ok = Electric.ShapeCache.ShapeStatus.initialize_from_storage(ctx.stack_id, ctx.storage)
2828

2929
parent = self()
3030

0 commit comments

Comments
 (0)