Skip to content

Add num_segments to Ra counters #510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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