2929-export ([
3030 default_name /2 ,
3131 default_counters /0 ,
32- default_commit_queue_name /2 ,
3332 default_read_queue_name /2 ,
3433 registered_name /2
3534]).
3635
3736% % PENDING COMMIT QUEUE API
3837-export ([
39- commit /3 ,
40- fulfill_commit /3 ,
41- fulfill_incomplete_commit /3 ,
42- fulfill_all_commits /2
38+ commit_started /1 ,
39+ commit_cancelled /3 ,
40+ commit_completed /3
4341]).
4442
4543% % PENDING READ API
108106-record (queues , {
109107 application :: atom (),
110108 counters :: atomics :atomics_ref (),
111- commits :: atom (),
112109 reads :: atom ()
113110}).
114111-opaque queues () :: # queues {}.
@@ -122,7 +119,6 @@ queues(Options) ->
122119 # queues {
123120 application = Options # raft_options .application ,
124121 counters = Options # raft_options .queue_counters ,
125- commits = Options # raft_options .queue_commits ,
126122 reads = Options # raft_options .queue_reads
127123 }.
128124
@@ -204,12 +200,6 @@ default_name(Table, Partition) ->
204200default_counters () ->
205201 atomics :new (? RAFT_NUMBER_OF_QUEUE_SIZE_COUNTERS , []).
206202
207- % % Get the default name for the RAFT commit queue ETS table associated with the
208- % % provided RAFT partition.
209- -spec default_commit_queue_name (Table :: wa_raft :table (), Partition :: wa_raft :partition ()) -> Name :: atom ().
210- default_commit_queue_name (Table , Partition ) ->
211- binary_to_atom (<<" raft_commit_queue_" , (atom_to_binary (Table ))/bytes , " _" , (integer_to_binary (Partition ))/bytes >>).
212-
213203% % Get the default name for the RAFT read queue ETS table associated with the
214204% % provided RAFT partition.
215205-spec default_read_queue_name (Table :: wa_raft :table (), Partition :: wa_raft :partition ()) -> Name :: atom ().
@@ -229,9 +219,8 @@ registered_name(Table, Partition) ->
229219% % PENDING COMMIT QUEUE API
230220% %-------------------------------------------------------------------
231221
232- -spec commit (Queues :: queues (), Key :: wa_raft_acceptor :key (), From :: gen_server :from ()) ->
233- ok | apply_queue_full | commit_queue_full | duplicate .
234- commit (# queues {counters = Counters , commits = Commits } = Queues , Reference , From ) ->
222+ -spec commit_started (Queues :: queues ()) -> ok | apply_queue_full | commit_queue_full .
223+ commit_started (# queues {counters = Counters } = Queues ) ->
235224 case commit_queue_full (Queues ) of
236225 true ->
237226 commit_queue_full ;
@@ -240,47 +229,24 @@ commit(#queues{counters = Counters, commits = Commits} = Queues, Reference, From
240229 true ->
241230 apply_queue_full ;
242231 false ->
243- case ets :insert_new (Commits , {Reference , From }) of
244- true ->
245- PendingCommits = atomics :add_get (Counters , ? RAFT_COMMIT_QUEUE_SIZE_COUNTER , 1 ),
246- ? RAFT_GATHER ('raft.acceptor.commit.request.pending' , PendingCommits ),
247- ok ;
248- false ->
249- duplicate
250- end
232+ PendingCommits = atomics :add_get (Counters , ? RAFT_COMMIT_QUEUE_SIZE_COUNTER , 1 ),
233+ ? RAFT_GATHER ('raft.acceptor.commit.request.pending' , PendingCommits ),
234+ ok
251235 end
252236 end .
253237
254- % Fulfill a pending commit with the result of the application of the command contained
255- % within the commit.
256- -spec fulfill_commit (Queues :: queues (), term (), dynamic ()) -> ok | not_found .
257- fulfill_commit (# queues {counters = Counters , commits = Commits }, Reference , Reply ) ->
258- case ets :take (Commits , Reference ) of
259- [{Reference , From }] ->
260- atomics :sub (Counters , ? RAFT_COMMIT_QUEUE_SIZE_COUNTER , 1 ),
261- gen_server :reply (From , Reply );
262- [] ->
263- not_found
264- end .
265238
266- % Fulfill a pending commit with an error that indicates that the commit was not completed.
267- -spec fulfill_incomplete_commit (Queues :: queues (), term (), wa_raft_acceptor :commit_error ()) -> ok | not_found .
268- fulfill_incomplete_commit (Queues , Reference , Error ) ->
269- fulfill_commit (Queues , Reference , Error ).
239+ -spec commit_cancelled (Queues :: queues (), From :: gen_server :from (), Reason :: wa_raft_acceptor :commit_error () | undefined ) -> ok .
240+ commit_cancelled (# queues {counters = Counters }, From , Reason ) ->
241+ atomics :sub (Counters , ? RAFT_COMMIT_QUEUE_SIZE_COUNTER , 1 ),
242+ Reason =/= undefined andalso gen_server :reply (From , Reason ),
243+ ok .
270244
271- % Fulfill a pending commit with an error that indicates that the commit was not completed.
272- -spec fulfill_all_commits (Queues :: queues (), wa_raft_acceptor :commit_error ()) -> ok .
273- fulfill_all_commits (# queues {counters = Counters , commits = Commits }, Reply ) ->
274- lists :foreach (
275- fun ({Reference , _ }) ->
276- case ets :take (Commits , Reference ) of
277- [{Reference , From }] ->
278- atomics :sub (Counters , ? RAFT_COMMIT_QUEUE_SIZE_COUNTER , 1 ),
279- gen_server :reply (From , Reply );
280- [] ->
281- ok
282- end
283- end , ets :tab2list (Commits )).
245+ -spec commit_completed (Queues :: queues (), From :: gen_server :from (), Reply :: term ()) -> ok .
246+ commit_completed (# queues {counters = Counters }, From , Reply ) ->
247+ atomics :sub (Counters , ? RAFT_COMMIT_QUEUE_SIZE_COUNTER , 1 ),
248+ gen_server :reply (From , Reply ),
249+ ok .
284250
285251% %-------------------------------------------------------------------
286252% % PENDING READ QUEUE API
@@ -398,23 +364,21 @@ init(
398364 partition = Partition ,
399365 queue_name = Name ,
400366 queue_counters = Counters ,
401- queue_commits = CommitsName ,
402367 queue_reads = ReadsName
403368 }
404369) ->
405370 process_flag (trap_exit , true ),
406371
407- ? LOG_NOTICE (" Queue[~p ] starting for partition ~0p /~0p with read queue ~0p and commit queue ~0p " ,
408- [Name , Table , Partition , ReadsName , CommitsName ], #{domain => [whatsapp , wa_raft ]}),
372+ ? LOG_NOTICE (" Queue[~p ] starting for partition ~0p /~0p with read queue ~0p " ,
373+ [Name , Table , Partition , ReadsName ], #{domain => [whatsapp , wa_raft ]}),
409374
410375 % The queue process is the first process in the supervision for a single
411376 % RAFT partition. The supervisor is configured to restart all processes if
412377 % even a single process fails. Since the queue process is starting up, all
413378 % queues tracked should be empty so reset all counters.
414379 [atomics :put (Counters , Index , 0 ) || Index <- lists :seq (1 , ? RAFT_NUMBER_OF_QUEUE_SIZE_COUNTERS )],
415380
416- % Create ETS tables for pending commits and reads.
417- CommitsName = ets :new (CommitsName , [set | ? RAFT_QUEUE_TABLE_OPTIONS ]),
381+ % Create ETS table for pending reads.
418382 ReadsName = ets :new (ReadsName , [ordered_set | ? RAFT_QUEUE_TABLE_OPTIONS ]),
419383
420384 {ok , # state {name = Name }}.
0 commit comments