Skip to content

Commit

Permalink
Merge pull request #510 from rabbitmq/ra-counters-add-num_segments
Browse files Browse the repository at this point in the history
Add num_segments to Ra counters
  • Loading branch information
kjnilsson authored Feb 24, 2025
2 parents d781bdc + 0d9b6b9 commit a454217
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/ra.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@
-define(C_RA_SVR_METRIC_TERM, ?C_RA_SRV_RESERVED + 7).
-define(C_RA_SVR_METRIC_CHECKPOINT_INDEX, ?C_RA_SRV_RESERVED + 8).
-define(C_RA_SVR_METRIC_EFFECTIVE_MACHINE_VERSION, ?C_RA_SRV_RESERVED + 9).
-define(C_RA_SVR_METRIC_NUM_SEGMENTS, ?C_RA_SRV_RESERVED + 10).

-define(RA_SRV_METRICS_COUNTER_FIELDS,
[
Expand All @@ -393,7 +394,9 @@
{checkpoint_index, ?C_RA_SVR_METRIC_CHECKPOINT_INDEX, counter,
"The current checkpoint index."},
{effective_machine_version, ?C_RA_SVR_METRIC_EFFECTIVE_MACHINE_VERSION,
gauge, "The current effective version number of the machine."}
gauge, "The current effective version number of the machine."},
{num_segments, ?C_RA_SVR_METRIC_NUM_SEGMENTS,
gauge, "The number of non-empty segment files."}
]).

-define(RA_COUNTER_FIELDS,
Expand Down
7 changes: 5 additions & 2 deletions src/ra_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ init(#{uid := UId,
LastIdx = State0#?MODULE.last_index,
put_counter(Cfg, ?C_RA_SVR_METRIC_LAST_INDEX, LastIdx),
put_counter(Cfg, ?C_RA_SVR_METRIC_LAST_WRITTEN_INDEX, LastIdx),
put_counter(Cfg, ?C_RA_SVR_METRIC_NUM_SEGMENTS, ra_log_reader:segment_ref_count(Reader)),
case ra_snapshot:latest_checkpoint(SnapshotState) of
undefined ->
ok;
Expand Down Expand Up @@ -679,12 +680,13 @@ handle_event({written, _Term, {FromIdx, _}} = Evt,
State#?MODULE{last_written_index_term = {Expected, Term}})
end;
handle_event({segments, TidRanges, NewSegs},
#?MODULE{cfg = #cfg{uid = UId, names = Names},
#?MODULE{cfg = #cfg{uid = UId, names = Names} = Cfg,
reader = Reader0,
mem_table = Mt0,
readers = Readers
} = State0) ->
Reader = ra_log_reader:update_segments(NewSegs, Reader0),
put_counter(Cfg, ?C_RA_SVR_METRIC_NUM_SEGMENTS, ra_log_reader:segment_ref_count(Reader)),
%% the tid ranges arrive in the reverse order they were written
%% (new -> old) so we need to foldr here to process the oldest first
Mt = lists:foldr(
Expand Down Expand Up @@ -1161,7 +1163,7 @@ log_update_effects(Pids, ReplyPid, #?MODULE{first_index = Idx,
%% the Idx argument
delete_segments(SnapIdx, #?MODULE{cfg = #cfg{log_id = LogId,
segment_writer = SegWriter,
uid = UId},
uid = UId} = Cfg,
readers = Readers,
reader = Reader0} = State0) ->
case ra_log_reader:update_first_index(SnapIdx + 1, Reader0) of
Expand All @@ -1178,6 +1180,7 @@ delete_segments(SnapIdx, #?MODULE{cfg = #cfg{log_id = LogId,
NumActive = ra_log_reader:segment_ref_count(Reader),
?DEBUG("~ts: ~b obsolete segments at ~b - remaining: ~b, pivot ~0p",
[LogId, length(Obsolete), SnapIdx, NumActive, Pivot]),
put_counter(Cfg, ?C_RA_SVR_METRIC_NUM_SEGMENTS, NumActive),
State = State0#?MODULE{reader = Reader},
{State, log_update_effects(Readers, Pid, State)}
end.
Expand Down

0 comments on commit a454217

Please sign in to comment.