Skip to content

Commit a454217

Browse files
authored
Merge pull request #510 from rabbitmq/ra-counters-add-num_segments
Add num_segments to Ra counters
2 parents d781bdc + 0d9b6b9 commit a454217

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/ra.hrl

+4-1
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@
374374
-define(C_RA_SVR_METRIC_TERM, ?C_RA_SRV_RESERVED + 7).
375375
-define(C_RA_SVR_METRIC_CHECKPOINT_INDEX, ?C_RA_SRV_RESERVED + 8).
376376
-define(C_RA_SVR_METRIC_EFFECTIVE_MACHINE_VERSION, ?C_RA_SRV_RESERVED + 9).
377+
-define(C_RA_SVR_METRIC_NUM_SEGMENTS, ?C_RA_SRV_RESERVED + 10).
377378

378379
-define(RA_SRV_METRICS_COUNTER_FIELDS,
379380
[
@@ -393,7 +394,9 @@
393394
{checkpoint_index, ?C_RA_SVR_METRIC_CHECKPOINT_INDEX, counter,
394395
"The current checkpoint index."},
395396
{effective_machine_version, ?C_RA_SVR_METRIC_EFFECTIVE_MACHINE_VERSION,
396-
gauge, "The current effective version number of the machine."}
397+
gauge, "The current effective version number of the machine."},
398+
{num_segments, ?C_RA_SVR_METRIC_NUM_SEGMENTS,
399+
gauge, "The number of non-empty segment files."}
397400
]).
398401

399402
-define(RA_COUNTER_FIELDS,

src/ra_log.erl

+5-2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ init(#{uid := UId,
259259
LastIdx = State0#?MODULE.last_index,
260260
put_counter(Cfg, ?C_RA_SVR_METRIC_LAST_INDEX, LastIdx),
261261
put_counter(Cfg, ?C_RA_SVR_METRIC_LAST_WRITTEN_INDEX, LastIdx),
262+
put_counter(Cfg, ?C_RA_SVR_METRIC_NUM_SEGMENTS, ra_log_reader:segment_ref_count(Reader)),
262263
case ra_snapshot:latest_checkpoint(SnapshotState) of
263264
undefined ->
264265
ok;
@@ -679,12 +680,13 @@ handle_event({written, _Term, {FromIdx, _}} = Evt,
679680
State#?MODULE{last_written_index_term = {Expected, Term}})
680681
end;
681682
handle_event({segments, TidRanges, NewSegs},
682-
#?MODULE{cfg = #cfg{uid = UId, names = Names},
683+
#?MODULE{cfg = #cfg{uid = UId, names = Names} = Cfg,
683684
reader = Reader0,
684685
mem_table = Mt0,
685686
readers = Readers
686687
} = State0) ->
687688
Reader = ra_log_reader:update_segments(NewSegs, Reader0),
689+
put_counter(Cfg, ?C_RA_SVR_METRIC_NUM_SEGMENTS, ra_log_reader:segment_ref_count(Reader)),
688690
%% the tid ranges arrive in the reverse order they were written
689691
%% (new -> old) so we need to foldr here to process the oldest first
690692
Mt = lists:foldr(
@@ -1161,7 +1163,7 @@ log_update_effects(Pids, ReplyPid, #?MODULE{first_index = Idx,
11611163
%% the Idx argument
11621164
delete_segments(SnapIdx, #?MODULE{cfg = #cfg{log_id = LogId,
11631165
segment_writer = SegWriter,
1164-
uid = UId},
1166+
uid = UId} = Cfg,
11651167
readers = Readers,
11661168
reader = Reader0} = State0) ->
11671169
case ra_log_reader:update_first_index(SnapIdx + 1, Reader0) of
@@ -1178,6 +1180,7 @@ delete_segments(SnapIdx, #?MODULE{cfg = #cfg{log_id = LogId,
11781180
NumActive = ra_log_reader:segment_ref_count(Reader),
11791181
?DEBUG("~ts: ~b obsolete segments at ~b - remaining: ~b, pivot ~0p",
11801182
[LogId, length(Obsolete), SnapIdx, NumActive, Pivot]),
1183+
put_counter(Cfg, ?C_RA_SVR_METRIC_NUM_SEGMENTS, NumActive),
11811184
State = State0#?MODULE{reader = Reader},
11821185
{State, log_update_effects(Readers, Pid, State)}
11831186
end.

0 commit comments

Comments
 (0)