Skip to content

Commit

Permalink
Non-zero instance_start_time
Browse files Browse the repository at this point in the history
Set instance_start_time to the creation time of the database to
restore the ability for the replicator to detect a db recreation event
during a replication. Without this, a replication can fail to notice
the db was deleted and recreated, write the checkpoint document for
the in-progress replication and keep going, whereas what should happen
is the replication should reset from sequence 0.
  • Loading branch information
rnewson committed Jan 17, 2022
1 parent 7954aca commit 4ebecda
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/chttpd/src/chttpd_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -554,12 +554,13 @@ db_req(
%% for missing databases that'd return error 404 from chttpd
%% get_security used to prefer shards on the same node over other nodes
fabric:get_security(DbName, [{user_ctx, Ctx}]),
#shard{suffix = Suffix} = hd(mem3:shards(DbName)),
send_json(
Req,
201,
{[
{ok, true},
{instance_start_time, <<"0">>}
{instance_start_time, Suffix}
]}
);
db_req(#httpd{path_parts = [_, <<"_ensure_full_commit">>]} = Req, _Db) ->
Expand Down
5 changes: 3 additions & 2 deletions src/fabric/src/fabric_db_info.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

go(DbName) ->
Shards = mem3:shards(DbName),
#shard{suffix = Suffix} = hd(mem3:shards(DbName)),
Workers = fabric_util:submit_jobs(Shards, get_db_info, []),
RexiMon = fabric_util:create_monitors(Shards),
Fun = fun handle_message/3,
Expand All @@ -28,7 +29,7 @@ go(DbName) ->
try
case fabric_util:recv(Workers, #shard.ref, Fun, Acc0) of
{ok, Acc} ->
{ok, Acc};
{ok, [{instance_start_time, Suffix} | Acc]};
{timeout, {WorkersDict, _, _}} ->
DefunctWorkers = fabric_util:remove_done_workers(
WorkersDict,
Expand Down Expand Up @@ -117,7 +118,7 @@ merge_results(Info) ->
(_K, _V, Acc) ->
Acc
end,
[{instance_start_time, <<"0">>}],
[],
Dict
).

Expand Down
2 changes: 2 additions & 0 deletions src/mem3/include/mem3.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
dbname :: binary() | 'undefined',
range :: [non_neg_integer() | '$1' | '$2'] | '_' | 'undefined',
ref :: reference() | '_' | 'undefined',
suffix :: binary(),
opts :: list() | 'undefined'
}).

Expand All @@ -33,6 +34,7 @@
range :: [non_neg_integer() | '$1' | '$2'] | '_',
ref :: reference() | 'undefined' | '_',
order :: non_neg_integer() | 'undefined' | '_',
suffix :: binary(),
opts :: list()
}).

Expand Down
3 changes: 3 additions & 0 deletions src/mem3/src/mem3_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ build_shards_by_node(DbName, DocProps) ->
dbname = DbName,
node = to_atom(Node),
range = [Beg, End],
suffix = list_to_binary(Suffix),
opts = get_shard_opts(DocProps)
},
Suffix
Expand Down Expand Up @@ -255,6 +256,7 @@ build_shards_by_range(DbName, DocProps) ->
node = to_atom(Node),
range = [Beg, End],
order = Order,
suffix = list_to_binary(Suffix),
opts = get_shard_opts(DocProps)
},
Suffix
Expand Down Expand Up @@ -370,6 +372,7 @@ downcast(#ordered_shard{} = S) ->
dbname = S#ordered_shard.dbname,
range = S#ordered_shard.range,
ref = S#ordered_shard.ref,
suffix = S#ordered_shard.suffix,
opts = S#ordered_shard.opts
};
downcast(Shards) when is_list(Shards) ->
Expand Down
16 changes: 8 additions & 8 deletions src/mem3/test/eunit/mem3_util_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,21 @@ build_shards_test() ->
ExpectedShards1 =
[
{shard, <<"shards/00000000-1fffffff/testdb1">>, '[email protected]', <<"testdb1">>,
[0, 536870911], undefined, []},
[0, 536870911], undefined, <<>>, []},
{shard, <<"shards/20000000-3fffffff/testdb1">>, '[email protected]', <<"testdb1">>,
[536870912, 1073741823], undefined, []},
[536870912, 1073741823], undefined, <<>>, []},
{shard, <<"shards/40000000-5fffffff/testdb1">>, '[email protected]', <<"testdb1">>,
[1073741824, 1610612735], undefined, []},
[1073741824, 1610612735], undefined, <<>>, []},
{shard, <<"shards/60000000-7fffffff/testdb1">>, '[email protected]', <<"testdb1">>,
[1610612736, 2147483647], undefined, []},
[1610612736, 2147483647], undefined, <<>>, []},
{shard, <<"shards/80000000-9fffffff/testdb1">>, '[email protected]', <<"testdb1">>,
[2147483648, 2684354559], undefined, []},
[2147483648, 2684354559], undefined, <<>>, []},
{shard, <<"shards/a0000000-bfffffff/testdb1">>, '[email protected]', <<"testdb1">>,
[2684354560, 3221225471], undefined, []},
[2684354560, 3221225471], undefined, <<>>, []},
{shard, <<"shards/c0000000-dfffffff/testdb1">>, '[email protected]', <<"testdb1">>,
[3221225472, 3758096383], undefined, []},
[3221225472, 3758096383], undefined, <<>>, []},
{shard, <<"shards/e0000000-ffffffff/testdb1">>, '[email protected]', <<"testdb1">>,
[3758096384, 4294967295], undefined, []}
[3758096384, 4294967295], undefined, <<>>, []}
],
?assertEqual(ExpectedShards1, Shards1),
ok.
Expand Down

0 comments on commit 4ebecda

Please sign in to comment.