Skip to content

Commit 51a447f

Browse files
committed
Optimise snapshotting by moving the call to ra_machine:live_indexes
to the snapshot process _if_ the Ref returned from ra_snapshot:prepare/2 is the same term as the machine state.
1 parent d89abfc commit 51a447f

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/ra_log_wal.erl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,8 @@ dump_records(<<_:1/unsigned, 0:1/unsigned, _:22/unsigned,
757757
_EntryData:0/binary,
758758
_Rest/binary>>, Entries) ->
759759
Entries;
760-
dump_records(<<_:1/unsigned, 0:1/unsigned, Id2:22/unsigned,
761-
IdDataLen:16/unsigned, Id:IdDataLen/binary,
760+
dump_records(<<_:1/unsigned, 0:1/unsigned, _Id2:22/unsigned,
761+
IdDataLen:16/unsigned, _Id:IdDataLen/binary,
762762
Crc:32/integer,
763763
EntryDataLen:32/unsigned,
764764
Idx:64/unsigned, Term:64/unsigned,
@@ -767,19 +767,19 @@ dump_records(<<_:1/unsigned, 0:1/unsigned, Id2:22/unsigned,
767767
% TODO: recover writers info, i.e. last index seen
768768
case erlang:adler32(<<Idx:64/unsigned, Term:64/unsigned, EntryData/binary>>) of
769769
Crc ->
770-
dump_records(Rest, [{{Id, Id2}, {Idx, Term, binary_to_term(EntryData)}} | Entries]);
770+
dump_records(Rest, [{Idx, Term, binary_to_term(EntryData)} | Entries]);
771771
_ ->
772772
exit({crc_failed_for, Idx, EntryData})
773773
end;
774-
dump_records(<<_:1/unsigned, 1:1/unsigned, Id:22/unsigned,
774+
dump_records(<<_:1/unsigned, 1:1/unsigned, _Id:22/unsigned,
775775
Crc:32/integer,
776776
EntryDataLen:32/unsigned,
777777
Idx:64/unsigned, Term:64/unsigned,
778778
EntryData:EntryDataLen/binary,
779779
Rest/binary>>, Entries) ->
780780
case erlang:adler32(<<Idx:64/unsigned, Term:64/unsigned, EntryData/binary>>) of
781781
Crc ->
782-
dump_records(Rest, [{Id, {Idx, Term, binary_to_term(EntryData)}} | Entries]);
782+
dump_records(Rest, [{Idx, Term, binary_to_term(EntryData)} | Entries]);
783783
_ ->
784784
exit({crc_failed_for, Idx, EntryData})
785785
end;

src/ra_snapshot.erl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,33 +390,48 @@ begin_snapshot(#{index := Idx, term := Term} = Meta, MacMod, MacState, SnapKind,
390390
Sync = SnapKind =:= snapshot,
391391
%% create directory for this snapshot
392392
SnapDir = make_snapshot_dir(Dir, Idx, Term),
393-
%% TODO: really we'd like to run this in the ra worker as good potentially
394-
%% be quite large and a touch expensive to compute but also we don't want
395-
%% to close over both the MacState and the MacRef
396-
LiveIndexes = ra_seq:from_list(ra_machine:live_indexes(MacMod, MacState)),
397-
%% call prepare then write_snapshotS
398393
%% This needs to be called in the current process to "lock" potentially
399394
%% mutable machine state
400395
Ref = Mod:prepare(Meta, MacState),
396+
PostPrepareEqualsMacState = Ref == MacState,
397+
LiveIndexes0 = case PostPrepareEqualsMacState of
398+
false ->
399+
ra_seq:from_list(
400+
ra_machine:live_indexes(MacMod, MacState));
401+
true ->
402+
[]
403+
end,
401404
%% write the snapshot in a separate process
402405
Self = self(),
403406
IdxTerm = {Idx, Term},
404407
BgWorkFun = fun () ->
405408
ok = ra_lib:make_dir(SnapDir),
409+
%% if the Ref returned by ra_snapshot:prepare/2 is
410+
%% the same as the mac state then indexes can be
411+
%% calculated here
412+
LiveIndexes =
413+
case PostPrepareEqualsMacState of
414+
true ->
415+
ra_seq:from_list(
416+
ra_machine:live_indexes(MacMod, Ref));
417+
false ->
418+
LiveIndexes0
419+
end,
406420
case Mod:write(SnapDir, Meta, Ref, Sync) of
407421
ok -> ok;
408422
{ok, BytesWritten} ->
409423
counters_add(Counter, CounterIdx,
410424
BytesWritten),
411425
ok
412426
end,
413-
%% write the live indexes, if any
427+
414428
case LiveIndexes of
415429
[] -> ok;
416430
_ ->
417431
ok = write_indexes(SnapDir, LiveIndexes),
418432
ok
419433
end,
434+
420435
Self ! {ra_log_event,
421436
{snapshot_written, IdxTerm,
422437
LiveIndexes, SnapKind}},

0 commit comments

Comments
 (0)