Skip to content

Commit de97e30

Browse files
hsun324facebook-github-bot
authored andcommitted
Enforce that the state passed to RAFT server logs is a valid state
Summary: Use an inlined method to ensure type checking of the state argument in log messages issued by the RAFT server. Reviewed By: jaher Differential Revision: D79672783 fbshipit-source-id: 6699acb6ca6b149ad621a633895a068e6a5e34ed
1 parent 79327d8 commit de97e30

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/wa_raft_server.erl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
-module(wa_raft_server).
1313
-compile(warn_missing_spec_all).
1414
-behaviour(gen_statem).
15+
-compile({inline, [require_valid_state/1]}).
1516

1617
%%------------------------------------------------------------------------------
1718
%% RAFT Server - OTP Supervision
@@ -180,7 +181,7 @@
180181
-define(SERVER_LOG_PREFIX, "Server[~0p, term ~0p, ~0p] ").
181182
-define(SERVER_LOG_FORMAT(Format), ?SERVER_LOG_PREFIX Format).
182183

183-
-define(SERVER_LOG_ARGS(State, Data, Args), [(Data)#raft_state.name, (Data)#raft_state.current_term, State | Args]).
184+
-define(SERVER_LOG_ARGS(State, Data, Args), [(Data)#raft_state.name, (Data)#raft_state.current_term, require_valid_state(State) | Args]).
184185

185186
-define(SERVER_LOG_ERROR(Data, Format, Args), ?SERVER_LOG_ERROR(?FUNCTION_NAME, Data, Format, Args)).
186187
-define(SERVER_LOG_ERROR(State, Data, Format, Args), ?RAFT_LOG_ERROR(?SERVER_LOG_FORMAT(Format), ?SERVER_LOG_ARGS(State, Data, Args))).
@@ -594,6 +595,14 @@ bootstrap(Server, Position, Config, Data) ->
594595
notify_complete(Server) ->
595596
gen_statem:cast(Server, ?NOTIFY_COMPLETE_COMMAND()).
596597

598+
%%------------------------------------------------------------------------------
599+
%% RAFT Server - State Machine Implementation - Logging
600+
%%------------------------------------------------------------------------------
601+
602+
-spec require_valid_state(state()) -> state().
603+
require_valid_state(State) ->
604+
State.
605+
597606
%%------------------------------------------------------------------------------
598607
%% RAFT Server - State Machine Implementation - General Callbacks
599608
%%------------------------------------------------------------------------------
@@ -3036,8 +3045,9 @@ handle_heartbeat(
30363045
EntryCount = length(Entries),
30373046

30383047
?RAFT_GATHER({raft, State, 'heartbeat.size'}, EntryCount),
3039-
?SERVER_LOG_DEBUG(State, Data0, "considering appending ~0p log entries in range ~0p to ~0p to log ending at ~0p.",
3040-
[EntryCount, PrevLogIndex + 1, PrevLogIndex + EntryCount, wa_raft_log:last_index(View)]),
3048+
EntryCount =/= 0 andalso
3049+
?SERVER_LOG_DEBUG(State, Data0, "considering appending ~0p log entries in range ~0p to ~0p to log ending at ~0p.",
3050+
[EntryCount, PrevLogIndex + 1, PrevLogIndex + EntryCount, wa_raft_log:last_index(View)]),
30413051

30423052
case append_entries(State, PrevLogIndex, PrevLogTerm, Entries, EntryCount, Data0) of
30433053
{ok, Accepted, NewMatchIndex, Data1} ->

0 commit comments

Comments
 (0)