diff --git a/apps/emqx_durable_storage/src/emqx_ds_replication_layer_meta.erl b/apps/emqx_durable_storage/src/emqx_ds_replication_layer_meta.erl index 481a47ca1..fa53ecced 100644 --- a/apps/emqx_durable_storage/src/emqx_ds_replication_layer_meta.erl +++ b/apps/emqx_durable_storage/src/emqx_ds_replication_layer_meta.erl @@ -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),