Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
kjnilsson committed Feb 14, 2024
1 parent 1a71653 commit 9be20ed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/ra_aux.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ log_last_index_term(#{log := Log}) ->
log_fetch(Idx, #{log := Log0} = State)
when is_integer(Idx) ->
case ra_log:fetch(Idx, Log0) of
{undefined, Log} ->
{undefined, State#{log => Log}};
{{Idx, Term, {_Tag, Meta, Cmd, _ReplyMode}}, Log} ->
{{Term, Meta, Cmd}, State#{log => Log}}
{{Idx, Term, {'$usr', Meta, Cmd, _ReplyMode}}, Log} ->
{{Term, Meta, Cmd}, State#{log => Log}};
{_, Log} ->
%% we only allow usr commands to be read
{undefined, State#{log => Log}}
end.

%% TODO: refine this? also included in overview
-spec log_stats(ra_aux:state()) -> ra_log:overview().
log_stats(#{log := Log}) ->
ra_log:overview(Log).

16 changes: 14 additions & 2 deletions src/ra_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,25 @@
counter => counters:counters_ref(),
initial_access_pattern => sequential | random}.


-type overview() ::
#{type := ra_log,
last_index := ra_index(),
first_index := ra_index(),
last_written_index_term := ra_idxterm(),
num_segments := non_neg_integer(),
open_segments => non_neg_integer(),
snapshot_index => undefined | ra_index(),
cache_size => non_neg_integer()}.

-export_type([state/0,
ra_log_init_args/0,
ra_meta_key/0,
segment_ref/0,
event/0,
event_body/0,
effect/0
effect/0,
overview/0
]).

pre_init(#{uid := UId,
Expand Down Expand Up @@ -785,7 +797,7 @@ exists({Idx, Term}, Log0) ->
{false, Log}
end.

-spec overview(state()) -> map().
-spec overview(state()) -> overview().
overview(#?MODULE{last_index = LastIndex,
first_index = FirstIndex,
last_written_index_term = LWIT,
Expand Down
5 changes: 4 additions & 1 deletion test/ra_machine_int_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,16 @@ aux_command_v1_and_v2(Config) ->
(_RaftState, cast, eval, AuxState, Opaque) ->
%% replaces aux state
{no_reply, AuxState, Opaque};
(_RaftState, cast, NewState, _AuxState, Opaque) ->
(_RaftState, cast, NewState, _AuxState, Opaque0) ->
{Idx, _} = ra_aux:log_last_index_term(Opaque0),
{{_Term, _Meta, apple}, Opaque} = ra_aux:log_fetch(Idx, Opaque0),
%% replaces aux state
{no_reply, NewState, Opaque}

end),
ok = start_cluster(ClusterName, {module, Mod, #{}}, Cluster),
{ok, _, Leader} = ra:members(ServerId1),
{ok, _, _} = ra:process_command(Leader, apple),
ok = ra:cast_aux_command(Leader, banana),
{leader, banana} = ra:aux_command(Leader, emit),
[ServerId2, ServerId3] = Cluster -- [Leader],
Expand Down

0 comments on commit 9be20ed

Please sign in to comment.