Skip to content

Conversation

@MarcialRosales
Copy link

It looks as if the current_file field of the osiris_log record is not updated when the log is opened to read from it.
I have noticed it while implementing the stream browser. I wanted to show the segment file associated to a group of chunks and messages.

readers_counter_fun = CountersFun,
shared = Shared
},
current_file = File,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#osiris_log.current_file should be the relative path of the file in the #osiris_log.cfg#cfg.directory but init_data_reader_at/4 and open_offset_reader_at/4 are passed absolute paths to the segment file. These paths also happen to be binaries rather than lists since list_dir/1 converts the filenames to binaries and the paths passed to this function are derived from the index file names.

We need the field to always be a binary or always be a list so that this check works properly:

osiris/src/osiris_log.erl

Lines 2986 to 2987 in 82ef2fd

SegFile = make_file_name(NextChId, "segment"),
case SegFile == CurFile of

I think that always using binaries in #osiris_log.current_file would be nice just since binaries are more compact than list strings. I'm pretty sure we would only need to change make_file_name/2 to use iolist_to_binary/1 instead of lists:flatten/1:

osiris/src/osiris_log.erl

Lines 2713 to 2715 in 82ef2fd

make_file_name(N, Suff) ->
lists:flatten(
io_lib:format("~20..0B.~s", [N, Suff])).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not change the make_file_name function as you suggest. I just set current_file the same way it is done in the other parts of the code that uses filename:basename(SegmentFile).

replica_nodes => []},
{ok, #{leader_pid := Leader}} = osiris:start_cluster(Conf0),
{ok, Log0} = osiris:init_reader(Leader, next, {offset_reader_1, []}),
?assertNot(undefined =:= osiris_log:get_current_file(Log0)).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the dialyzer isn't happy about this check since get_current_file/1's spec says that it can never return undefined. Since we're opening this on an empty log we could check this instead:

Suggested change
?assertNot(undefined =:= osiris_log:get_current_file(Log0)).
?assertEqual(<<"00000000000000000000.segment">>,
osiris_log:get_current_file(Log0)).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @the-mikedavis for reviewing the PR!

Removed spaces
Used same function to extract the relative
path used in other parts of the code
Fixed the assertion as the function indeed
never reuturns undefined
@MarcialRosales MarcialRosales self-assigned this Nov 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants