Skip to content

Commit 507e1ec

Browse files
hsun324facebook-github-bot
authored andcommitted
Introduce header with standard logging options
Summary: Introduce a header that defines logging macros using standard options. Reviewed By: jaher Differential Revision: D77457750 fbshipit-source-id: 9322ffea114dcb5efc4b5339cd8b934c2bd33f81
1 parent 7e543d8 commit 507e1ec

14 files changed

+392
-355
lines changed

include/wa_raft_logger.hrl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-include_lib("kernel/include/logger.hrl").
2+
3+
-define(RAFT_LOG_OPTS, #{domain => [whatsapp, wa_raft]}).
4+
5+
-define(RAFT_LOG_ERROR(Message), ?LOG_ERROR(Message, ?RAFT_LOG_OPTS)).
6+
-define(RAFT_LOG_ERROR(Format, Args), ?LOG_ERROR(Format, Args, ?RAFT_LOG_OPTS)).
7+
8+
-define(RAFT_LOG_WARNING(Message), ?LOG_WARNING(Message, ?RAFT_LOG_OPTS)).
9+
-define(RAFT_LOG_WARNING(Format, Args), ?LOG_WARNING(Format, Args, ?RAFT_LOG_OPTS)).
10+
11+
-define(RAFT_LOG_NOTICE(Message), ?LOG_NOTICE(Message, ?RAFT_LOG_OPTS)).
12+
-define(RAFT_LOG_NOTICE(Format, Args), ?LOG_NOTICE(Format, Args, ?RAFT_LOG_OPTS)).
13+
14+
-define(RAFT_LOG_INFO(Message), ?LOG_INFO(Message, ?RAFT_LOG_OPTS)).
15+
-define(RAFT_LOG_INFO(Format, Args), ?LOG_INFO(Format, Args, ?RAFT_LOG_OPTS)).
16+
17+
-define(RAFT_LOG_DEBUG(Message), ?LOG_DEBUG(Message, ?RAFT_LOG_OPTS)).
18+
-define(RAFT_LOG_DEBUG(Format, Args), ?LOG_DEBUG(Format, Args, ?RAFT_LOG_OPTS)).
19+
20+
-define(RAFT_LOG(Level, Message), ?LOG(Level, Message, ?RAFT_LOG_OPTS)).
21+
-define(RAFT_LOG(Level, Format, Args), ?LOG(Level, Format, Args, ?RAFT_LOG_OPTS)).

src/wa_raft_acceptor.erl

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
commit_result/0
5858
]).
5959

60-
-include_lib("kernel/include/logger.hrl").
6160
-include_lib("wa_raft/include/wa_raft.hrl").
61+
-include_lib("wa_raft/include/wa_raft_logger.hrl").
6262

6363
-type command() :: noop_command() | noop_omitted_command() | config_command() | dynamic().
6464
-type noop_command() :: noop.
@@ -191,8 +191,7 @@ registered_name(Table, Partition) ->
191191
init(#raft_options{table = Table, partition = Partition, acceptor_name = Name, server_name = Server} = Options) ->
192192
process_flag(trap_exit, true),
193193

194-
?LOG_NOTICE("Acceptor[~0p] starting for partition ~0p/~0p",
195-
[Name, Table, Partition], #{domain => [whatsapp, wa_raft]}),
194+
?RAFT_LOG_NOTICE("Acceptor[~0p] starting for partition ~0p/~0p", [Name, Table, Partition]),
196195

197196
{ok, #state{
198197
name = Name,
@@ -213,8 +212,7 @@ handle_call({commit, Op}, From, State) ->
213212
{error, _} = Error -> {reply, Error, State}
214213
end;
215214
handle_call(Request, From, #state{name = Name} = State) ->
216-
?LOG_ERROR("Acceptor[~0p] received unexpected call ~0P from ~0p.",
217-
[Name, Request, 30, From], #{domain => [whatsapp, wa_raft]}),
215+
?RAFT_LOG_ERROR("Acceptor[~0p] received unexpected call ~0P from ~0p.", [Name, Request, 30, From]),
218216
{noreply, State}.
219217

220218
-spec handle_cast(commit_async_request(), #state{}) -> {noreply, #state{}}.
@@ -223,14 +221,12 @@ handle_cast({commit, From, Op}, State) ->
223221
Result =/= continue andalso gen_server:reply(From, Result),
224222
{noreply, State};
225223
handle_cast(Request, #state{name = Name} = State) ->
226-
?LOG_ERROR("Acceptor[~0p] received unexpected cast ~0P.",
227-
[Name, Request, 30], #{domain => [whatsapp, wa_raft]}),
224+
?RAFT_LOG_ERROR("Acceptor[~0p] received unexpected cast ~0P.", [Name, Request, 30]),
228225
{noreply, State}.
229226

230227
-spec terminate(Reason :: term(), State :: #state{}) -> ok.
231228
terminate(Reason, #state{name = Name}) ->
232-
?LOG_NOTICE("Acceptor[~0p] terminating with reason ~0P",
233-
[Name, Reason, 30], #{domain => [whatsapp, wa_raft]}),
229+
?RAFT_LOG_NOTICE("Acceptor[~0p] terminating with reason ~0P", [Name, Reason, 30]),
234230
ok.
235231

236232
%%-------------------------------------------------------------------
@@ -242,17 +238,20 @@ terminate(Reason, #state{name = Name}) ->
242238
commit_impl(From, {Key, _} = Op, #state{name = Name, server = Server, queues = Queues}) ->
243239
StartT = os:timestamp(),
244240
try
245-
?LOG_DEBUG("Acceptor[~0p] starts to handle commit of ~0P from ~0p.",
246-
[Name, Op, 30, From], #{domain => [whatsapp, wa_raft]}),
241+
?RAFT_LOG_DEBUG("Acceptor[~0p] starts to handle commit of ~0P from ~0p.", [Name, Op, 30, From]),
247242
case wa_raft_queue:commit_started(Queues) of
248243
commit_queue_full ->
249-
?LOG_WARNING("Acceptor[~0p] is rejecting commit request from ~0p because the commit queue is full.",
250-
[Name, From], #{domain => [whatsapp, wa_raft]}),
244+
?RAFT_LOG_WARNING(
245+
"Acceptor[~0p] is rejecting commit request from ~0p because the commit queue is full.",
246+
[Name, From]
247+
),
251248
?RAFT_COUNT('raft.acceptor.error.commit_queue_full'),
252249
{error, {commit_queue_full, Key}};
253250
apply_queue_full ->
254-
?LOG_WARNING("Acceptor[~0p] is rejecting commit request from ~0p because the apply queue is full.",
255-
[Name, From], #{domain => [whatsapp, wa_raft]}),
251+
?RAFT_LOG_WARNING(
252+
"Acceptor[~0p] is rejecting commit request from ~0p because the apply queue is full.",
253+
[Name, From]
254+
),
256255
?RAFT_COUNT('raft.acceptor.error.apply_queue_full'),
257256
{error, {apply_queue_full, Key}};
258257
ok ->
@@ -267,19 +266,22 @@ commit_impl(From, {Key, _} = Op, #state{name = Name, server = Server, queues = Q
267266
-spec read_impl(gen_server:from(), command(), #state{}) -> continue | read_error().
268267
read_impl(From, Command, #state{name = Name, server = Server, queues = Queues}) ->
269268
StartT = os:timestamp(),
270-
?LOG_DEBUG("Acceptor[~p] starts to handle read of ~0P from ~0p.",
271-
[Name, Command, 100, From], #{domain => [whatsapp, wa_raft]}),
269+
?RAFT_LOG_DEBUG("Acceptor[~p] starts to handle read of ~0P from ~0p.", [Name, Command, 100, From]),
272270
try
273271
case wa_raft_queue:reserve_read(Queues) of
274272
read_queue_full ->
275273
?RAFT_COUNT('raft.acceptor.strong_read.error.read_queue_full'),
276-
?LOG_WARNING("Acceptor[~0p] is rejecting read request from ~0p because the read queue is full.",
277-
[Name, From], #{domain => [whatsapp, wa_raft]}),
274+
?RAFT_LOG_WARNING(
275+
"Acceptor[~0p] is rejecting read request from ~0p because the read queue is full.",
276+
[Name, From]
277+
),
278278
{error, read_queue_full};
279279
apply_queue_full ->
280280
?RAFT_COUNT('raft.acceptor.strong_read.error.apply_queue_full'),
281-
?LOG_WARNING("Acceptor[~0p] is rejecting read request from ~0p because the apply queue is full.",
282-
[Name, From], #{domain => [whatsapp, wa_raft]}),
281+
?RAFT_LOG_WARNING(
282+
"Acceptor[~0p] is rejecting read request from ~0p because the apply queue is full.",
283+
[Name, From]
284+
),
283285
{error, apply_queue_full};
284286
ok ->
285287
wa_raft_server:read(Server, {From, Command}),

src/wa_raft_dist_transport.erl

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
terminate/2
2828
]).
2929

30-
-include_lib("kernel/include/logger.hrl").
3130
-include_lib("wa_raft/include/wa_raft.hrl").
31+
-include_lib("wa_raft/include/wa_raft_logger.hrl").
3232

3333
-record(sender_state, {
3434
}).
@@ -58,8 +58,7 @@ transport_init(_Node) ->
5858
{ok, NewState :: #sender_state{}} |
5959
{stop, Reason :: term(), NewState :: #sender_state{}}.
6060
transport_send(ID, FileID, State) ->
61-
?LOG_DEBUG("wa_raft_dist_transport starting to send file ~p/~p",
62-
[ID, FileID], #{domain => [whatsapp, wa_raft]}),
61+
?RAFT_LOG_DEBUG("wa_raft_dist_transport starting to send file ~p/~p", [ID, FileID]),
6362
case wa_raft_transport:transport_info(ID) of
6463
{ok, #{peer := Peer}} ->
6564
case wa_raft_transport:file_info(ID, FileID) of
@@ -78,8 +77,10 @@ transport_send(ID, FileID, State) ->
7877
prim_file:close(Fd)
7978
end;
8079
{error, Reason} ->
81-
?LOG_ERROR("wa_raft_dist_transport failed to open file ~p/~p (~s) due to ~p",
82-
[ID, FileID, File, Reason], #{domain => [whatsapp, wa_raft]}),
80+
?RAFT_LOG_ERROR(
81+
"wa_raft_dist_transport failed to open file ~p/~p (~s) due to ~p",
82+
[ID, FileID, File, Reason]
83+
),
8384
{stop, {failed_to_open_file, ID, FileID, Reason}, State}
8485
end;
8586
_ ->
@@ -121,16 +122,13 @@ transport_send_loop(ID, FileID, Fd, Offset, Peer, [RequestId | Chunks], ChunkSiz
121122
{reply, ok} ->
122123
transport_send_loop(ID, FileID, Fd, Offset, Peer, Chunks, ChunkSize, MaxInflight, State);
123124
{reply, {error, Reason}} ->
124-
?LOG_ERROR("wa_raft_dist_transport failed to send file ~p/~p due to receiver error ~p",
125-
[ID, FileID, Reason], #{domain => [whatsapp, wa_raft]}),
125+
?RAFT_LOG_ERROR("wa_raft_dist_transport failed to send file ~p/~p due to receiver error ~p", [ID, FileID, Reason]),
126126
{error, {receiver_error, ID, FileID, Reason}, State};
127127
timeout ->
128-
?LOG_ERROR("wa_raft_dist_transport timed out while sending file ~p/~p",
129-
[ID, FileID], #{domain => [whatsapp, wa_raft]}),
128+
?RAFT_LOG_ERROR("wa_raft_dist_transport timed out while sending file ~p/~p", [ID, FileID]),
130129
{error, {send_timed_out, ID, FileID}, State};
131130
{error, {Reason, _}} ->
132-
?LOG_ERROR("wa_raft_dist_transport failed to send file ~p/~p due to ~p",
133-
[ID, FileID, Reason], #{domain => [whatsapp, wa_raft]}),
131+
?RAFT_LOG_ERROR("wa_raft_dist_transport failed to send file ~p/~p due to ~p", [ID, FileID, Reason]),
134132
{error, {send_failed, ID, FileID, Reason}, State}
135133
end;
136134
transport_send_loop(ID, FileID, Fd, Offset, Peer, Chunks, ChunkSize, MaxInflight, State) when is_integer(Offset) ->
@@ -145,8 +143,7 @@ transport_send_loop(ID, FileID, Fd, Offset, Peer, Chunks, ChunkSize, MaxInflight
145143
eof ->
146144
transport_send_loop(ID, FileID, Fd, eof, Peer, Chunks, ChunkSize, MaxInflight, State);
147145
{error, Reason} ->
148-
?LOG_ERROR("wa_raft_dist_transport failed to read file ~p/~p due to ~p",
149-
[ID, FileID, Reason], #{domain => [whatsapp, wa_raft]}),
146+
?RAFT_LOG_ERROR("wa_raft_dist_transport failed to read file ~p/~p due to ~p", [ID, FileID, Reason]),
150147
{error, {read_failed, ID, FileID, Reason}, State}
151148
end.
152149

@@ -170,19 +167,22 @@ handle_call({chunk, ID, FileID, Offset, Data}, _From, #receiver_state{} = State0
170167

171168
{ok, State1};
172169
{error, Reason} ->
173-
?LOG_WARNING("wa_raft_dist_transport receiver failed to write file chunk ~p/~p @ ~p due to ~p",
174-
[ID, FileID, Offset, Reason], #{domain => [whatsapp, wa_raft]}),
170+
?RAFT_LOG_WARNING(
171+
"wa_raft_dist_transport receiver failed to write file chunk ~p/~p @ ~p due to ~p",
172+
[ID, FileID, Offset, Reason]
173+
),
175174
{{write_failed, Reason}, State1}
176175
end;
177176
{error, Reason, State1} ->
178-
?LOG_WARNING("wa_raft_dist_transport receiver failed to handle file chunk ~p/~p @ ~p due to open failing due to ~p",
179-
[ID, FileID, Offset, Reason], #{domain => [whatsapp, wa_raft]}),
177+
?RAFT_LOG_WARNING(
178+
"wa_raft_dist_transport receiver failed to handle file chunk ~p/~p @ ~p due to open failing due to ~p",
179+
[ID, FileID, Offset, Reason]
180+
),
180181
{{open_failed, Reason}, State1}
181182
end,
182183
{reply, Reply, NewState};
183184
handle_call(Request, From, #receiver_state{} = State) ->
184-
?LOG_NOTICE("wa_raft_dist_transport got unrecognized call ~p from ~p",
185-
[Request, From], #{domain => [whatsapp, wa_raft]}),
185+
?RAFT_LOG_NOTICE("wa_raft_dist_transport got unrecognized call ~p from ~p", [Request, From]),
186186
{noreply, State}.
187187

188188
-spec handle_cast(Request, State :: #receiver_state{}) -> {noreply, NewState :: #receiver_state{}}
@@ -194,19 +194,19 @@ handle_cast({complete, ID, FileID}, #receiver_state{} = State0) ->
194194
wa_raft_transport:complete(ID, FileID, ok),
195195
{noreply, State2};
196196
{error, Reason, State1} ->
197-
?LOG_WARNING("wa_raft_dist_transport receiver failed to handle file complete ~p/~p due to open failing due to ~p",
198-
[ID, FileID, Reason], #{domain => [whatsapp, wa_raft]}),
197+
?RAFT_LOG_WARNING(
198+
"wa_raft_dist_transport receiver failed to handle file complete ~p/~p due to open failing due to ~p",
199+
[ID, FileID, Reason]
200+
),
199201
{noreply, State1}
200202
end;
201203
handle_cast(Request, #receiver_state{} = State) ->
202-
?LOG_NOTICE("wa_raft_dist_transport got unrecognized cast ~p",
203-
[Request], #{domain => [whatsapp, wa_raft]}),
204+
?RAFT_LOG_NOTICE("wa_raft_dist_transport got unrecognized cast ~p", [Request]),
204205
{noreply, State}.
205206

206207
-spec terminate(Reason :: term(), State :: #receiver_state{}) -> term().
207208
terminate(Reason, #receiver_state{}) ->
208-
?LOG_NOTICE("wa_raft_dist_transport terminating due to ~p",
209-
[Reason], #{domain => [whatsapp, wa_raft]}),
209+
?RAFT_LOG_NOTICE("wa_raft_dist_transport terminating due to ~p", [Reason]),
210210
ok.
211211

212212
-spec open_file(ID :: wa_raft_transport:transport_id(), FileID :: wa_raft_transport:file_id(), State :: #receiver_state{}) ->
@@ -224,8 +224,10 @@ open_file(ID, FileID, #receiver_state{fds = Fds} = State0) ->
224224
State1 = State0#receiver_state{fds = Fds#{{ID, FileID} => Fd}},
225225
{ok, Fd, State1};
226226
{error, Reason} ->
227-
?LOG_WARNING("wa_raft_dist_transport receiver failed to open file ~p/~p (~p) due to ~p",
228-
[ID, FileID, File, Reason], #{domain => [whatsapp, wa_raft]}),
227+
?RAFT_LOG_WARNING(
228+
"wa_raft_dist_transport receiver failed to open file ~p/~p (~p) due to ~p",
229+
[ID, FileID, File, Reason]
230+
),
229231
{error, {open_failed, Reason}, State0}
230232
end;
231233
_ ->

src/wa_raft_durable_state.erl

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
-module(wa_raft_durable_state).
99
-compile(warn_missing_spec_all).
1010

11-
-include_lib("kernel/include/logger.hrl").
1211
-include_lib("wa_raft/include/wa_raft.hrl").
12+
-include_lib("wa_raft/include/wa_raft_logger.hrl").
1313

1414
-export([
1515
load/1,
@@ -34,8 +34,7 @@ load(#raft_state{name = Name, partition_path = PartitionPath} = State) ->
3434
fun ({Item, Validator, Updater, Default}, StateN) ->
3535
case proplists:lookup(Item, StateTerms) of
3636
none when Default =:= required ->
37-
?LOG_ERROR("~p read state file but cannot find ~p.",
38-
[Name, Item], #{domain => [whatsapp, wa_raft]}),
37+
?RAFT_LOG_ERROR("~p read state file but cannot find ~p.", [Name, Item]),
3938
throw({error, {missing, Item}});
4039
none ->
4140
Updater(Default, StateN);
@@ -44,8 +43,7 @@ load(#raft_state{name = Name, partition_path = PartitionPath} = State) ->
4443
true ->
4544
Updater(Value, StateN);
4645
false ->
47-
?LOG_ERROR("~p read state file but ~p has an invalid value `~p`.",
48-
[Name, Item, Value], #{domain => [whatsapp, wa_raft]}),
46+
?RAFT_LOG_ERROR("~p read state file but ~p has an invalid value `~p`.", [Name, Item, Value]),
4947
throw({error, {invalid, Item}})
5048
end
5149
end
@@ -54,18 +52,17 @@ load(#raft_state{name = Name, partition_path = PartitionPath} = State) ->
5452
throw:{error, Reason} -> {error, Reason}
5553
end;
5654
InvalidCRC ->
57-
?LOG_ERROR("~p read state file but CRCs did not match. (saved crc: ~p, computed crc: ~p)",
58-
[Name, InvalidCRC, CRC], #{domain => [whatsapp, wa_raft]}),
55+
?RAFT_LOG_ERROR("~p read state file but CRCs did not match. (saved crc: ~p, computed crc: ~p)", [Name, InvalidCRC, CRC]),
5956
{error, invalid_crc}
6057
end;
6158
{ok, _} ->
62-
?LOG_ERROR("~p read state file but no CRC was found", [Name], #{domain => [whatsapp, wa_raft]}),
59+
?RAFT_LOG_ERROR("~p read state file but no CRC was found", [Name]),
6360
{error, no_crc};
6461
{error, enoent} ->
65-
?LOG_NOTICE("~p is not loading non-existant state file.", [Name], #{domain => [whatsapp, wa_raft]}),
62+
?RAFT_LOG_NOTICE("~p is not loading non-existant state file.", [Name]),
6663
no_state;
6764
{error, Reason} ->
68-
?LOG_ERROR("~p could not read state file due to ~p.", [Name, Reason], #{domain => [whatsapp, wa_raft]}),
65+
?RAFT_LOG_ERROR("~p could not read state file due to ~p.", [Name, Reason]),
6966
{error, Reason}
7067
end.
7168

@@ -89,17 +86,17 @@ store(#raft_state{name = Name, partition_path = PartitionPath, current_term = Cu
8986
ok;
9087
{error, Reason} ->
9188
?RAFT_COUNT({'raft.server.persist_state.error.rename', Reason}),
92-
?LOG_ERROR("~p failed to rename temporary state file due to ~p.", [Name, Reason], #{domain => [whatsapp, wa_raft]}),
89+
?RAFT_LOG_ERROR("~p failed to rename temporary state file due to ~p.", [Name, Reason]),
9390
{error, {rename, Reason}}
9491
end;
9592
{error, Reason} ->
9693
?RAFT_COUNT({'raft.server.persist_state.error.write', Reason}),
97-
?LOG_ERROR("~p failed to write current state to temporary file due to ~p.", [Name, Reason], #{domain => [whatsapp, wa_raft]}),
94+
?RAFT_LOG_ERROR("~p failed to write current state to temporary file due to ~p.", [Name, Reason]),
9895
{error, {write, Reason}}
9996
end;
10097
{error, Reason} ->
10198
?RAFT_COUNT({'raft.server.persist_state.error.ensure_dir', Reason}),
102-
?LOG_ERROR("~p failed to ensure directory exists due to ~p.", [Name, Reason], #{domain => [whatsapp, wa_raft]}),
99+
?RAFT_LOG_ERROR("~p failed to ensure directory exists due to ~p.", [Name, Reason]),
103100
{error, {ensure_dir, Reason}}
104101
end.
105102

0 commit comments

Comments
 (0)