Skip to content

Commit 5b82f90

Browse files
Merge pull request #202 from rabbitmq/md/pt-field-specs
Store `seshat:field_specs/0` in persistent terms
2 parents 27b34f2 + a868b5b commit 5b82f90

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

src/osiris_counters.erl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
-spec init() -> ok.
2121
init() ->
2222
_ = seshat:new_group(osiris),
23+
ok = osiris_writer:init_fields_spec(),
24+
ok = osiris_replica_reader:init_fields_spec(),
25+
ok = osiris_replica:init_fields_spec(),
2326
ok.
2427

25-
-spec new(name(), [{Name :: atom(), Position :: non_neg_integer(),
26-
Type :: atom(), Description :: term()}]) ->
27-
counters:counters_ref().
28+
-spec new(name(), seshat:fields_spec()) ->
29+
counters:counters_ref().
2830
new(Name, Fields) ->
2931
seshat:new(osiris, Name, Fields).
3032

src/osiris_log.erl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
evaluate_retention/2,
5454
directory/1,
5555
delete_directory/1,
56+
counter_fields/0,
5657
make_counter/1,
5758
generate_log/4]).
5859

@@ -360,7 +361,7 @@
360361
-type offset() :: osiris:offset().
361362
-type epoch() :: osiris:epoch().
362363
-type range() :: empty | {From :: offset(), To :: offset()}.
363-
-type counter_spec() :: {Tag :: term(), Fields :: [atom()]}.
364+
-type counter_spec() :: {Tag :: term(), Fields :: seshat:fields_spec()}.
364365
-type chunk_type() ::
365366
?CHNK_USER |
366367
?CHNK_TRK_DELTA |
@@ -2848,13 +2849,23 @@ validate_crc(ChunkId, Crc, IOData) ->
28482849
exit({crc_validation_failure, {chunk_id, ChunkId}})
28492850
end.
28502851

2852+
-spec counter_fields() -> seshat:fields_spec().
2853+
counter_fields() ->
2854+
?COUNTER_FIELDS.
2855+
28512856
-spec make_counter(osiris_log:config()) ->
28522857
counters:counters_ref().
28532858
make_counter(#{counter := Counter}) ->
28542859
Counter;
2855-
make_counter(#{counter_spec := {Name, Fields}}) ->
2860+
make_counter(#{counter_spec := {Name, Spec0}}) ->
2861+
Spec = case Spec0 of
2862+
{persistent_term, _Key} ->
2863+
Spec0;
2864+
_ when is_list(Spec0) ->
2865+
?COUNTER_FIELDS ++ Spec0
2866+
end,
28562867
%% create a registered counter
2857-
osiris_counters:new(Name, ?COUNTER_FIELDS ++ Fields);
2868+
osiris_counters:new(Name, Spec);
28582869
make_counter(_) ->
28592870
%% if no spec is provided we create a local counter only
28602871
counters:new(?C_NUM_LOG_FIELDS, []).

src/osiris_replica.erl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
terminate/2,
3434
code_change/3,
3535
format_status/1]).
36+
%% for internal use
37+
-export([init_fields_spec/0]).
3638

3739
%% holds static or rarely changing fields
3840
-record(cfg,
@@ -79,6 +81,7 @@
7981
{packets, ?C_PACKETS, counter, "Number of packets"},
8082
{readers, ?C_READERS, counter, "Number of readers"},
8183
{epoch, ?C_EPOCH, counter, "Current epoch"}]).
84+
-define(FIELDSPEC_KEY, osiris_replica_seshat_fields_spec).
8285

8386
-define(DEFAULT_ONE_TIME_TOKEN_TIMEOUT, 30000).
8487
-define(TOKEN_SIZE, 32).
@@ -179,12 +182,12 @@ handle_continue(#{name := Name0,
179182
Transport = application:get_env(osiris, replication_transport, tcp),
180183
Self = self(),
181184
CntName = {?MODULE, ExtRef},
185+
CntSpec = {CntName, {persistent_term, ?FIELDSPEC_KEY}},
182186

183187
Dir = osiris_log:directory(Config),
184188
Log = osiris_log:init_acceptor(LeaderRange, LeaderEpochOffs,
185189
Config#{dir => Dir,
186-
counter_spec =>
187-
{CntName, ?ADD_COUNTER_FIELDS}}),
190+
counter_spec => CntSpec}),
188191
CntRef = osiris_log:counters_ref(Log),
189192
{NextOffset, LastChunk} = TailInfo = osiris_log:tail_info(Log),
190193

@@ -789,3 +792,7 @@ listen(tcp, Port, Options) ->
789792
gen_tcp:listen(Port, Options);
790793
listen(ssl, Port, Options) ->
791794
ssl:listen(Port, Options).
795+
796+
init_fields_spec() ->
797+
persistent_term:put(?FIELDSPEC_KEY,
798+
?ADD_COUNTER_FIELDS ++ osiris_log:counter_fields()).

src/osiris_replica_reader.erl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
handle_info/2,
2828
terminate/2,
2929
code_change/3]).
30-
-export([formatter/1]).
30+
-export([init_fields_spec/0,
31+
formatter/1]).
3132

3233
-record(state,
3334
{log :: osiris_log:state(),
@@ -48,6 +49,7 @@
4849
{offset_listeners, ?C_OFFSET_LISTENERS, counter, "Number of offset listeners"}
4950
]
5051
).
52+
-define(FIELDSPEC_KEY, osiris_replica_reader_seshat_fields_spec).
5153

5254
%%%===================================================================
5355
%%% API functions
@@ -122,7 +124,7 @@ init(#{hosts := Hosts,
122124
?INFO_(Name, "replica reader successfully connected to host ~0p port ~b",
123125
[Host, Port]),
124126
CntId = {?MODULE, ExtRef, Host, Port},
125-
CntSpec = {CntId, ?COUNTER_FIELDS},
127+
CntSpec = {CntId, {persistent_term, ?FIELDSPEC_KEY}},
126128
Config = #{counter_spec => CntSpec, transport => Transport},
127129
%% send token to replica to complete connection setup
128130
ok = send(Transport, Sock, Token),
@@ -433,3 +435,6 @@ maybe_add_sni_option(H) when is_list(H) ->
433435
maybe_add_sni_option(_) ->
434436
[].
435437

438+
init_fields_spec() ->
439+
persistent_term:put(?FIELDSPEC_KEY,
440+
?COUNTER_FIELDS ++ osiris_log:counter_fields()).

src/osiris_writer.erl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
read_tracking/1,
2727
query_writers/2,
2828
query_replication_state/1,
29+
init_fields_spec/0,
2930
init/1,
3031
handle_continue/2,
3132
handle_batch/2,
@@ -49,6 +50,7 @@
4950
{epoch, ?C_EPOCH, counter, "Current epoch"}
5051
]
5152
).
53+
-define(FIELDSPEC_KEY, osiris_writer_seshat_fields_spec).
5254

5355
%% primary osiris process
5456
%% batch writes incoming data
@@ -179,6 +181,10 @@ query_writers(Pid, QueryFun) ->
179181
query_replication_state(Pid) when is_pid(Pid) ->
180182
gen_batch_server:call(Pid, query_replication_state).
181183

184+
init_fields_spec() ->
185+
persistent_term:put(?FIELDSPEC_KEY,
186+
osiris_log:counter_fields() ++ ?ADD_COUNTER_FIELDS).
187+
182188
-spec init(osiris:config()) ->
183189
{ok, undefined, {continue, osiris:config()}}.
184190
init(#{name := Name0,
@@ -188,10 +194,11 @@ init(#{name := Name0,
188194
Shared = osiris_log_shared:new(),
189195
Dir = osiris_log:directory(Config0),
190196
CntName = {?MODULE, ExtRef},
197+
CntSpec = {CntName, {persistent_term, ?FIELDSPEC_KEY}},
191198
Config = Config0#{name => Name,
192199
dir => Dir,
193200
shared => Shared,
194-
counter_spec => {CntName, ?ADD_COUNTER_FIELDS}},
201+
counter_spec => CntSpec},
195202
CntRef = osiris_log:make_counter(Config),
196203
{ok, undefined, {continue, Config#{counter => CntRef}}}.
197204

0 commit comments

Comments
 (0)