Skip to content

Commit aef7775

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 aef7775

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/ra_server.erl

Lines changed: 3 additions & 0 deletions
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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,22 @@ do_init(#{id := Id,
302302
Key = ra_lib:ra_server_id_to_local_name(Id),
303303
true = ets:insert(ra_state, {Key, init, unknown}),
304304
process_flag(trap_exit, true),
305-
Config = #{counter := Counter,
306-
system_config := SysConf} = maps:merge(config_defaults(Id),
307-
Config0),
305+
Config1 = #{system_config := SysConf} = maps:merge(config_defaults(),
306+
Config0),
307+
Counter = case maps:find(counter, Config1) of
308+
{ok, C} ->
309+
C;
310+
error ->
311+
case ra_counters:fetch(Id) of
312+
undefined ->
313+
Label = maps:get(counter_label, Config1, Id),
314+
ra_counters:new(
315+
Id, {persistent_term, ?FIELDSPEC_KEY}, Label);
316+
C ->
317+
C
318+
end
319+
end,
320+
Config = maps:put(counter, Counter, Config1),
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)