fix(dsrepl): verify if shards already allocated first

This commit is contained in:
Andrew Mayorov 2024-05-16 18:56:54 +02:00
parent 0119728d45
commit 5157e61418
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
1 changed files with 7 additions and 7 deletions

View File

@ -497,13 +497,6 @@ open_db_trans(DB, CreateOpts) ->
-spec allocate_shards_trans(emqx_ds:db()) -> [emqx_ds_replication_layer:shard_id()].
allocate_shards_trans(DB) ->
Opts = #{n_shards := NShards, n_sites := NSites} = db_config_trans(DB),
Nodes = mnesia:match_object(?NODE_TAB, ?NODE_PAT(), read),
case length(Nodes) of
N when N >= NSites ->
ok;
_ ->
mnesia:abort({insufficient_sites_online, NSites, Nodes})
end,
case mnesia:match_object(?SHARD_TAB, ?SHARD_PAT({DB, '_'}), write) of
[] ->
ok;
@ -511,6 +504,13 @@ allocate_shards_trans(DB) ->
ShardsAllocated = [Shard || #?SHARD_TAB{shard = {_DB, Shard}} <- Records],
mnesia:abort({shards_already_allocated, ShardsAllocated})
end,
Nodes = mnesia:match_object(?NODE_TAB, ?NODE_PAT(), read),
case length(Nodes) of
N when N >= NSites ->
ok;
_ ->
mnesia:abort({insufficient_sites_online, NSites, Nodes})
end,
Shards = gen_shards(NShards),
Sites = [S || #?NODE_TAB{site = S} <- Nodes],
Allocation = compute_allocation(Shards, Sites, Opts),