Skip to content

Commit 79a7026

Browse files
hsun324facebook-github-bot
authored andcommitted
Cache queue counters and table names
Summary: Cache queue counters and table names in a handle record to avoid repeated lookups against the RAFT partition's options persistent term. This also simplifies the interface to the commit and read queues. Reviewed By: jaher Differential Revision: D75234464 Privacy Context Container: L1141030 fbshipit-source-id: b615030cf09cddb3d756bf888688aacb21bd4114
1 parent f00adb6 commit 79a7026

File tree

5 files changed

+248
-210
lines changed

5 files changed

+248
-210
lines changed

include/wa_raft.hrl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@
365365

366366
% Queue options
367367
queue_name :: atom(),
368-
queue_counters :: counters:counters_ref(),
368+
queue_counters :: atomics:atomics_ref(),
369369
queue_commits :: atom(),
370370
queue_reads :: atom(),
371371

@@ -408,6 +408,8 @@
408408

409409
%% Current view into this RAFT replica's log state
410410
log_view :: wa_raft_log:view(),
411+
%% Current queue handle
412+
queues :: wa_raft_queue:queues(),
411413

412414
%% Active module for distribution of RPCs
413415
distribution_module :: module(),

src/wa_raft_acceptor.erl

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@
9494
-record(state, {
9595
% Acceptor service name
9696
name :: atom(),
97-
% Table name
98-
table :: wa_raft:table(),
99-
% Partition number
100-
partition :: wa_raft:partition(),
10197
% Server service name
102-
server :: atom()
98+
server :: atom(),
99+
% Queues handle
100+
queues :: wa_raft_queue:queues()
103101
}).
104102

105103
%%-------------------------------------------------------------------
@@ -190,17 +188,16 @@ registered_name(Table, Partition) ->
190188
%%-------------------------------------------------------------------
191189

192190
-spec init(Options :: #raft_options{}) -> {ok, #state{}}.
193-
init(#raft_options{table = Table, partition = Partition, acceptor_name = Name, server_name = Server}) ->
191+
init(#raft_options{table = Table, partition = Partition, acceptor_name = Name, server_name = Server} = Options) ->
194192
process_flag(trap_exit, true),
195193

196194
?LOG_NOTICE("Acceptor[~0p] starting for partition ~0p/~0p",
197195
[Name, Table, Partition], #{domain => [whatsapp, wa_raft]}),
198196

199197
{ok, #state{
200198
name = Name,
201-
table = Table,
202-
partition = Partition,
203-
server = Server
199+
server = Server,
200+
queues = wa_raft_queue:queues(Options)
204201
}}.
205202

206203
-spec handle_call(read_request(), gen_server:from(), #state{}) -> {reply, read_result(), #state{}} | {noreply, #state{}};
@@ -242,12 +239,12 @@ terminate(Reason, #state{name = Name}) ->
242239

243240
%% Enqueue a commit.
244241
-spec commit_impl(From :: gen_server:from(), Request :: op(), State :: #state{}) -> continue | commit_error().
245-
commit_impl(From, {Key, _} = Op, #state{table = Table, partition = Partition, server = Server, name = Name}) ->
242+
commit_impl(From, {Key, _} = Op, #state{name = Name, server = Server, queues = Queues}) ->
246243
StartT = os:timestamp(),
247244
try
248245
?LOG_DEBUG("Acceptor[~0p] starts to handle commit of ~0P from ~0p.",
249246
[Name, Op, 30, From], #{domain => [whatsapp, wa_raft]}),
250-
case wa_raft_queue:commit(Table, Partition, Key, From) of
247+
case wa_raft_queue:commit(Queues, Key, From) of
251248
duplicate ->
252249
?LOG_WARNING("Acceptor[~0p] is rejecting commit request from ~0p because it has duplicate key ~0p.",
253250
[Name, From, Key], #{domain => [whatsapp, wa_raft]}),
@@ -273,12 +270,12 @@ commit_impl(From, {Key, _} = Op, #state{table = Table, partition = Partition, se
273270

274271
%% Enqueue a strongly-consistent read.
275272
-spec read_impl(gen_server:from(), command(), #state{}) -> continue | read_error().
276-
read_impl(From, Command, #state{name = Name, table = Table, partition = Partition, server = Server}) ->
273+
read_impl(From, Command, #state{name = Name, server = Server, queues = Queues}) ->
277274
StartT = os:timestamp(),
278275
?LOG_DEBUG("Acceptor[~p] starts to handle read of ~0P from ~0p.",
279276
[Name, Command, 100, From], #{domain => [whatsapp, wa_raft]}),
280277
try
281-
case wa_raft_queue:reserve_read(Table, Partition) of
278+
case wa_raft_queue:reserve_read(Queues) of
282279
read_queue_full ->
283280
?RAFT_COUNT('raft.acceptor.strong_read.error.read_queue_full'),
284281
?LOG_WARNING("Acceptor[~0p] is rejecting read request from ~0p because the read queue is full.",

0 commit comments

Comments
 (0)