Skip to content

Commit 340eb54

Browse files
committed
Remove pending entries after snapshot installation.
Else we may end up with pending entries that never will be written and will later trigger a resend that will fail.
1 parent 5b3f178 commit 340eb54

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/ra_log.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ install_snapshot({SnapIdx, SnapTerm} = IdxTerm, MacMod, LiveIndexes,
11411141
#?MODULE{cfg = #cfg{uid = UId,
11421142
names = Names} = Cfg,
11431143
snapshot_state = SnapState0,
1144+
pending = Pend0,
11441145
mem_table = Mt0} = State0)
11451146
when is_atom(MacMod) ->
11461147
ok = incr_counter(Cfg, ?C_RA_LOG_SNAPSHOTS_INSTALLED, 1),
@@ -1160,9 +1161,10 @@ install_snapshot({SnapIdx, SnapTerm} = IdxTerm, MacMod, LiveIndexes,
11601161
I
11611162
end,
11621163
%% TODO: more mt entries could potentially be cleared up in the
1163-
%% mem table here
1164+
%% mem table here if we walked the live indexes
11641165
{Spec, Mt1} = ra_mt:set_first(SmallestLiveIndex, Mt0),
11651166
ok = exec_mem_table_delete(Names, UId, Spec),
1167+
Pend = ra_seq:floor(SmallestLiveIndex, Pend0),
11661168
%% always create a new mem table here as we could have written
11671169
%% sparese entries in the snapshot install
11681170
%% TODO: check an empty mt doesn't leak
@@ -1174,6 +1176,7 @@ install_snapshot({SnapIdx, SnapTerm} = IdxTerm, MacMod, LiveIndexes,
11741176
last_term = SnapTerm,
11751177
live_indexes = LiveIndexes,
11761178
mem_table = Mt,
1179+
pending = Pend,
11771180
last_written_index_term = IdxTerm},
11781181
{Reader, CompEffs} = ra_log_segments:schedule_compaction(minor, SnapIdx,
11791182
LiveIndexes,

test/ra_log_2_SUITE.erl

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ all_tests() ->
5858
wal_loses_writer_state,
5959
detect_lost_written_range,
6060
snapshot_installation,
61+
snapshot_installation_with_no_live_indexes_overtakes_written,
6162
snapshot_written_after_installation,
6263
oldcheckpoints_deleted_after_snapshot_install,
6364
append_after_snapshot_installation_with_live_indexes,
@@ -177,7 +178,6 @@ snapshot_before_written(Config) ->
177178
end),
178179
ok.
179180

180-
181181
handle_overwrite(Config) ->
182182
Log0 = ra_log_init(Config),
183183
{ok, Log1} = ra_log:write([{1, 1, "value"},
@@ -1625,6 +1625,38 @@ snapshot_installation(Config) ->
16251625
{[_, _], _} = ra_log_take(16, 17, Log),
16261626
ok.
16271627

1628+
snapshot_installation_with_no_live_indexes_overtakes_written(Config) ->
1629+
Log0 = ra_log_init(Config),
1630+
{0, 0} = ra_log:last_index_term(Log0),
1631+
Log1 = assert_log_events(write_n(1, 10, 2, Log0),
1632+
fun (L) -> {9, 2} == ra_log:last_written(L) end),
1633+
1634+
true = ra_log_wal_SUITE:suspend_process(whereis(ra_log_wal)),
1635+
Log2 = write_n(1, 10, 2, Log1),
1636+
1637+
%% create snapshot chunk
1638+
Meta = meta(15, 2, [?N1]),
1639+
Chunk = create_snapshot_chunk(Config, Meta, #{}),
1640+
SnapState0 = ra_log:snapshot_state(Log2),
1641+
{ok, SnapState1} = ra_snapshot:begin_accept(Meta, SnapState0),
1642+
Machine = {machine, ?MODULE, #{}},
1643+
{SnapState, _, [], AEffs} =
1644+
ra_snapshot:complete_accept(Chunk, 1, Machine, SnapState1),
1645+
run_effs(AEffs),
1646+
{ok, Log3, Effs4} = ra_log:install_snapshot({15, 2}, ?MODULE, [],
1647+
ra_log:set_snapshot_state(SnapState, Log2)),
1648+
1649+
run_effs(Effs4),
1650+
{15, _} = ra_log:last_index_term(Log3),
1651+
{15, _} = ra_log:last_written(Log3),
1652+
#{mem_table_range := undefined} = ra_log:overview(Log3),
1653+
1654+
true = erlang:resume_process(whereis(ra_log_wal)),
1655+
Log4 = write_n(16, 20, 2, Log3),
1656+
_ = assert_log_events(Log4,
1657+
fun (L) -> {19, 2} == ra_log:last_written(L) end),
1658+
ok.
1659+
16281660
append_after_snapshot_installation_with_live_indexes(Config) ->
16291661
logger:set_primary_config(level, all),
16301662
%% simulates scenario where a node becomes leader after receiving a

0 commit comments

Comments
 (0)