Skip to content

Commit 9c5e1f8

Browse files
the-mikedavismkuratczyk
authored andcommitted
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 aab0f38 commit 9c5e1f8

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/ra_server.erl

+3
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@
229229
await_condition_timeout => non_neg_integer(),
230230
max_pipeline_count => non_neg_integer(),
231231
ra_event_formatter => {module(), atom(), [term()]},
232+
%% Deprecated in favor of counter_label:
232233
counter => counters:counters_ref(),
234+
counter_label => seshat:label(),
233235
membership => ra_membership(),
234236
system_config => ra_system:config(),
235237
has_changed => boolean()
@@ -258,6 +260,7 @@
258260

259261
-type mutable_config() :: #{cluster_name => ra_cluster_name(),
260262
metrics_key => term(),
263+
counter_label => seshat:label(),
261264
broadcast_time => non_neg_integer(), % ms
262265
tick_timeout => non_neg_integer(), % ms
263266
install_snap_rpc_timeout => non_neg_integer(), % ms

src/ra_server_proc.erl

+17-12
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,22 @@ do_init(#{id := Id,
315315
Key = ra_lib:ra_server_id_to_local_name(Id),
316316
true = ets:insert(ra_state, {Key, init, unknown}),
317317
process_flag(trap_exit, true),
318-
Config = #{counter := Counter,
319-
system_config := #{names := Names} = SysConf} = maps:merge(config_defaults(Id),
320-
Config0),
318+
Config1 = #{system_config := SysConf} = maps:merge(config_defaults(),
319+
Config0),
320+
Counter = case maps:find(counter, Config1) of
321+
{ok, C} ->
322+
C;
323+
error ->
324+
case ra_counters:fetch(Id) of
325+
undefined ->
326+
Label = maps:get(counter_label, Config1, Id),
327+
ra_counters:new(
328+
Id, {persistent_term, ?FIELDSPEC_KEY}, Label);
329+
C ->
330+
C
331+
end
332+
end,
333+
Config = maps:put(counter, Counter, Config1),
321334
MsgQData = maps:get(message_queue_data, SysConf, off_heap),
322335
MinBinVheapSize = maps:get(server_min_bin_vheap_size, SysConf,
323336
?MIN_BIN_VHEAP_SIZE),
@@ -1768,20 +1781,12 @@ gen_statem_safe_call(ServerId, Msg, Timeout) ->
17681781
do_state_query(QueryName, #state{server_state = State}) ->
17691782
ra_server:state_query(QueryName, State).
17701783

1771-
config_defaults(ServerId) ->
1772-
Counter = case ra_counters:fetch(ServerId) of
1773-
undefined ->
1774-
ra_counters:new(ServerId,
1775-
{persistent_term, ?FIELDSPEC_KEY});
1776-
C ->
1777-
C
1778-
end,
1784+
config_defaults() ->
17791785
#{broadcast_time => ?DEFAULT_BROADCAST_TIME,
17801786
tick_timeout => ?TICK_INTERVAL_MS,
17811787
install_snap_rpc_timeout => ?INSTALL_SNAP_RPC_TIMEOUT,
17821788
await_condition_timeout => ?DEFAULT_AWAIT_CONDITION_TIMEOUT,
17831789
initial_members => [],
1784-
counter => Counter,
17851790
system_config => ra_system:default_config()
17861791
}.
17871792

0 commit comments

Comments
 (0)