Skip to content

Commit 55a4a9c

Browse files
committed
Add counter_label field to Ra server config
This new field sets the `seshat:label()` when creating a new counter for a server (via `ra_counters:new/3` and in turn `seshat:new/4`). This also deprecates the `counter` field. Having the creator of a Ra server pass a `counters:counter_ref()` is unergonomic because the caller must know the `seshat:fields_spec()` with which the counter should be created. Also in this change we remove the call to `ra_counters:new/2` from the `ra_server_proc:config_defaults()` helper. By calling that function when creating a default, the server process registered a counter (causing an insertion into an ETS table in seshat) for the server even if the caller specified a `counter` in the config.
1 parent c087c3b commit 55a4a9c

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/ra_server.erl

+3
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,17 @@
206206
await_condition_timeout => non_neg_integer(),
207207
max_pipeline_count => non_neg_integer(),
208208
ra_event_formatter => {module(), atom(), [term()]},
209+
%% Deprecated in favor of counter_label:
209210
counter => counters:counters_ref(),
211+
counter_label => seshat:label(),
210212
membership => ra_membership(),
211213
system_config => ra_system:config(),
212214
has_changed => boolean()
213215
}.
214216

215217
-type mutable_config() :: #{cluster_name => ra_cluster_name(),
216218
metrics_key => term(),
219+
counter_label => seshat:label(),
217220
broadcast_time => non_neg_integer(), % ms
218221
tick_timeout => non_neg_integer(), % ms
219222
install_snap_rpc_timeout => non_neg_integer(), % ms

src/ra_server_proc.erl

+15-10
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,21 @@ do_init(#{id := Id,
303303
true = ets:insert(ra_state, {Key, init, unknown}),
304304
process_flag(trap_exit, true),
305305
Config = #{counter := Counter,
306-
system_config := SysConf} = maps:merge(config_defaults(Id),
306+
system_config := SysConf} = maps:merge(config_defaults(),
307307
Config0),
308+
Counter = case maps:find(counter, Config) of
309+
{ok, C} ->
310+
C;
311+
error ->
312+
case ra_counters:fetch(Id) of
313+
undefined ->
314+
Label = maps:get(counter_label, Config, Id),
315+
ra_counters:new(
316+
Id, {persistent_term, ?FIELDSPEC_KEY}, Label);
317+
C ->
318+
C
319+
end
320+
end,
308321
MsgQData = maps:get(message_queue_data, SysConf, off_heap),
309322
MinBinVheapSize = maps:get(server_min_bin_vheap_size, SysConf,
310323
?MIN_BIN_VHEAP_SIZE),
@@ -1709,20 +1722,12 @@ gen_statem_safe_call(ServerId, Msg, Timeout) ->
17091722
do_state_query(QueryName, #state{server_state = State}) ->
17101723
ra_server:state_query(QueryName, State).
17111724

1712-
config_defaults(ServerId) ->
1713-
Counter = case ra_counters:fetch(ServerId) of
1714-
undefined ->
1715-
ra_counters:new(ServerId,
1716-
{persistent_term, ?FIELDSPEC_KEY});
1717-
C ->
1718-
C
1719-
end,
1725+
config_defaults() ->
17201726
#{broadcast_time => ?DEFAULT_BROADCAST_TIME,
17211727
tick_timeout => ?TICK_INTERVAL_MS,
17221728
install_snap_rpc_timeout => ?INSTALL_SNAP_RPC_TIMEOUT,
17231729
await_condition_timeout => ?DEFAULT_AWAIT_CONDITION_TIMEOUT,
17241730
initial_members => [],
1725-
counter => Counter,
17261731
system_config => ra_system:default_config()
17271732
}.
17281733

0 commit comments

Comments
 (0)