Skip to content

Commit 058e768

Browse files
authored
Merge pull request #171 from rabbitmq/unexpected-files-recovery-bug
Ignore unexpected files in the log directory.
2 parents c5cad7b + 85ac519 commit 058e768

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/osiris_log.erl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,10 @@ orphaned_segments([<<Name:20/binary, ".index">>,
18291829
%% when we find a matching pair we can return
18301830
Acc;
18311831
orphaned_segments([<<_:20/binary, ".segment">> = Dangler | Rem], Acc) ->
1832-
orphaned_segments(Rem, [Dangler | Acc]).
1832+
orphaned_segments(Rem, [Dangler | Acc]);
1833+
orphaned_segments([_Unexpected | Rem], Acc) ->
1834+
%% just ignore unexpected files
1835+
orphaned_segments(Rem, Acc).
18331836

18341837
first_and_last_seginfos(#{index_files := IdxFiles}) ->
18351838
first_and_last_seginfos0(IdxFiles);
@@ -2038,15 +2041,15 @@ overview(Dir) ->
20382041
{Range, EpochOffsets}
20392042
end.
20402043

2041-
index_files_with_segment([<<_:20/binary, ".segment">> | Rem], Dir, Acc) ->
2042-
%% orphaned segment file, ignore
2043-
index_files_with_segment(Rem, Dir, Acc);
2044+
index_files_with_segment([], _, Acc) ->
2045+
lists:reverse(Acc);
20442046
index_files_with_segment([<<Name:20/binary, ".index">> = I,
20452047
<<Name:20/binary, ".segment">>
20462048
| Rem], Dir, Acc) ->
20472049
index_files_with_segment(Rem, Dir, [filename:join(Dir, I) | Acc]);
2048-
index_files_with_segment(_, _, Acc) ->
2049-
lists:reverse(Acc).
2050+
index_files_with_segment([_OrphanedOrUnexpected | Rem], Dir, Acc) ->
2051+
%% orphaned segment file or unexpected file, ignore
2052+
index_files_with_segment(Rem, Dir, Acc).
20502053

20512054

20522055

test/osiris_log_SUITE.erl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ all_tests() ->
8989
small_chunk_overview,
9090
overview,
9191
init_partial_writes,
92+
init_with_unexpected_file,
9293
overview_with_missing_segment,
93-
overview_with_missing_segment_at_start
94+
overview_with_missing_index_at_start
9495
].
9596

9697
groups() ->
@@ -1791,6 +1792,23 @@ run_scenario(Config, NumChunks, MsgSize, Scenario) ->
17911792

17921793
ok.
17931794

1795+
init_with_unexpected_file(Config) ->
1796+
Data = crypto:strong_rand_bytes(1500),
1797+
EpochChunks =
1798+
[begin {1, [Data || _ <- lists:seq(1, 50)]} end
1799+
|| _ <- lists:seq(1, 20)],
1800+
Dir = ?config(dir, Config),
1801+
Log = seed_log(Dir, EpochChunks, Config),
1802+
osiris_log:close(Log),
1803+
Segments = filelib:wildcard(filename:join(Dir, "*.segment")),
1804+
Indexes = filelib:wildcard(filename:join(Dir, "*.index")),
1805+
?assertEqual(2, length(Segments)),
1806+
?assertEqual(2, length(Indexes)),
1807+
ok = file:write_file(filename:join(Dir, ".nfs000000000000000000"), <<"bananas">>),
1808+
_ = osiris_log:init(?config(osiris_conf, Config)),
1809+
?assertEqual({{0,999},[{1,950}]}, osiris_log:overview(Dir)),
1810+
ok.
1811+
17941812
overview_with_missing_segment(Config) ->
17951813
Data = crypto:strong_rand_bytes(1500),
17961814
EpochChunks =
@@ -1821,7 +1839,7 @@ overview_with_missing_segment(Config) ->
18211839
filename:join(?config(dir, Config), "*.index")))),
18221840
ok.
18231841

1824-
overview_with_missing_segment_at_start(Config) ->
1842+
overview_with_missing_index_at_start(Config) ->
18251843
Data = crypto:strong_rand_bytes(1500),
18261844
EpochChunks =
18271845
[begin {1, [Data || _ <- lists:seq(1, 50)]} end

0 commit comments

Comments
 (0)