Skip to content

Commit

Permalink
Merge pull request #157 from rabbitmq/recover-tracking-bugfix
Browse files Browse the repository at this point in the history
Use a new file handle to recover tracking.
  • Loading branch information
kjnilsson authored Mar 21, 2024
2 parents 7be9499 + 4c6bf2d commit 863a1c2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/osiris_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2727,14 +2727,22 @@ part(Len, [B | L]) when Len > 0 ->

-spec recover_tracking(state()) ->
osiris_tracking:state().
recover_tracking(#?MODULE{cfg = #cfg{tracking_config = TrkConfig},
fd = Fd}) ->
recover_tracking(#?MODULE{cfg = #cfg{directory = Dir,
tracking_config = TrkConfig},
current_file = File}) ->
%% we need to open a new file handle here as we cannot use the one that is
%% being used for appending to the segment as pread _may_ move the file
%% position on some systems (such as windows)
{ok, Fd} = open(filename:join(Dir, File), [read, raw, binary]),
_ = file:advise(Fd, 0, 0, random),
%% TODO: if the first chunk in the segment isn't a tracking snapshot and
%% there are prior segments we could scan at least two segments increasing
%% the chance of encountering a snapshot and thus ensure we don't miss any
%% tracking entries
Trk = osiris_tracking:init(undefined, TrkConfig),
recover_tracking(Fd, Trk, ?LOG_HEADER_SIZE).
Trk0 = osiris_tracking:init(undefined, TrkConfig),
Trk = recover_tracking(Fd, Trk0, ?LOG_HEADER_SIZE),
_ = file:close(Fd),
Trk.

recover_tracking(Fd, Trk0, Pos0) ->
case file:pread(Fd, Pos0, ?HEADER_SIZE_B) of
Expand Down

0 comments on commit 863a1c2

Please sign in to comment.