From 179ff3670da05d9db93091347a8942aadf0b00f7 Mon Sep 17 00:00:00 2001 From: Karl Nilsson Date: Fri, 13 Dec 2024 13:05:32 +0000 Subject: [PATCH] tweak random advise --- src/ra_log_segment.erl | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/ra_log_segment.erl b/src/ra_log_segment.erl index 54d82a8d..f830b2fd 100644 --- a/src/ra_log_segment.erl +++ b/src/ra_log_segment.erl @@ -115,17 +115,23 @@ open(Filename, Options) -> end. process_file(true, Mode, Filename, Fd, Options) -> + AccessPattern = maps:get(access_pattern, Options, random), + if AccessPattern == random andalso + Mode == read -> + %% advise for random access for any data beyond our best guess + %% index size, readers could pass `max_count` in the config to + %% ensure this is correct + ConfMaxCount = maps:get(max_count, Options, ?SEGMENT_MAX_ENTRIES), + Offset = ?HEADER_SIZE + (ConfMaxCount * ?INDEX_RECORD_SIZE_V2), + ok = file:advise(Fd, Offset, 0, random); + true -> + ok + end, case read_header(Fd) of {ok, Version, MaxCount} -> MaxPending = maps:get(max_pending, Options, ?SEGMENT_MAX_PENDING), IndexRecordSize = index_record_size(Version), IndexSize = MaxCount * IndexRecordSize, - AccessPattern = maps:get(access_pattern, Options, random), - if AccessPattern == random -> - ok = file:advise(Fd, 0, 0, random); - true -> - ok - end, {NumIndexRecords, DataOffset, Range, Index} = recover_index(Fd, Version, MaxCount), IndexOffset = ?HEADER_SIZE + NumIndexRecords * IndexRecordSize,