5353
5454% maximum size of a segment in bytes
5555-define (DEFAULT_MAX_SEGMENT_SIZE_B , 500 * 1000 * 1000 ).
56+ % -define(MIN_SEGMENT_SIZE_B, 4_000_000).
5657% maximum number of chunks per segment
5758-define (DEFAULT_MAX_SEGMENT_SIZE_C , 256_000 ).
5859-define (INDEX_RECORD_SIZE_B , 29 ).
@@ -442,8 +443,8 @@ init(#{dir := Dir,
442443 epoch := Epoch } = Config ,
443444 WriterType ) ->
444445 % % scan directory for segments if in write mode
445- MaxSizeBytes =
446- maps : get ( max_segment_size_bytes , Config , ? DEFAULT_MAX_SEGMENT_SIZE_B ),
446+ MaxSizeBytes = maps : get ( max_segment_size_bytes , Config ,
447+ ? DEFAULT_MAX_SEGMENT_SIZE_B ),
447448 MaxSizeChunks = application :get_env (osiris , max_segment_size_chunks ,
448449 ? DEFAULT_MAX_SEGMENT_SIZE_C ),
449450 Retention = maps :get (retention , Config , []),
@@ -479,20 +480,21 @@ init(#{dir := Dir,
479480 counter_id = counter_id (Config ),
480481 shared = Shared },
481482 ok = maybe_fix_corrupted_files (Config ),
483+ DefaultNextOffset = case Config of
484+ #{initial_offset := IO }
485+ when WriterType == acceptor ->
486+ IO ;
487+ _ ->
488+ 0
489+ end ,
482490 case first_and_last_seginfos (Config ) of
483491 none ->
484- NextOffset = case Config of
485- #{initial_offset := IO }
486- when WriterType == acceptor ->
487- IO ;
488- _ ->
489- 0
490- end ,
491- osiris_log_shared :set_first_chunk_id (Shared , NextOffset - 1 ),
492+ osiris_log_shared :set_first_chunk_id (Shared , DefaultNextOffset - 1 ),
492493 open_new_segment (#? MODULE {cfg = Cfg ,
493494 mode =
494495 # write {type = WriterType ,
495- tail_info = {NextOffset , empty },
496+ tail_info = {DefaultNextOffset ,
497+ empty },
496498 current_epoch = Epoch }});
497499 {NumSegments ,
498500 # seg_info {first = # chunk_info {id = FstChId ,
@@ -555,11 +557,11 @@ init(#{dir := Dir,
555557 % % here too?
556558 {ok , _ } = file :position (SegFd , eof ),
557559 {ok , _ } = file :position (IdxFd , eof ),
558- osiris_log_shared :set_first_chunk_id (Shared , - 1 ),
560+ osiris_log_shared :set_first_chunk_id (Shared , DefaultNextOffset - 1 ),
559561 #? MODULE {cfg = Cfg ,
560562 mode =
561563 # write {type = WriterType ,
562- tail_info = {0 , empty },
564+ tail_info = {DefaultNextOffset , empty },
563565 current_epoch = Epoch },
564566 current_file = filename :basename (Filename ),
565567 fd = SegFd ,
@@ -847,16 +849,19 @@ truncate_to(_Name, _Range, [], IdxFiles) ->
847849 [];
848850truncate_to (Name , RemoteRange , [{E , ChId } | NextEOs ], IdxFiles ) ->
849851 case find_segment_for_offset (ChId , IdxFiles ) of
850- not_found ->
852+ Result when Result == not_found orelse
853+ element (1 , Result ) == end_of_log ->
854+ % % both not_found and end_of_log needs to be treated as not found
855+ % % as they are...
851856 case build_seg_info (lists :last (IdxFiles )) of
852857 {ok , # seg_info {last = # chunk_info {epoch = E ,
853858 id = LastChId ,
854859 num = Num }}}
855- when ChId > LastChId + Num ->
860+ when ChId > LastChId ->
856861 % % the last available local chunk id is smaller than the
857- % % sources last chunk id but is in the same epoch
862+ % % source's last chunk id but is in the same epoch
858863 % % check if there is any overlap
859- LastOffsLocal = LastChId + Num ,
864+ LastOffsLocal = LastChId + Num - 1 ,
860865 FstOffsetRemote = case RemoteRange of
861866 empty -> 0 ;
862867 {F , _ } -> F
@@ -878,8 +883,6 @@ truncate_to(Name, RemoteRange, [{E, ChId} | NextEOs], IdxFiles) ->
878883 % % TODO: what to do if error is returned from
879884 % % build_seg_info/1?
880885 end ;
881- {end_of_log , _Info } ->
882- IdxFiles ;
883886 {found , # seg_info {file = File , index = IdxFile }} ->
884887 ? DEBUG (" osiris_log: ~s on node ~s truncating to chunk "
885888 " id ~b in epoch ~b " ,
@@ -1502,18 +1505,21 @@ send_file(Sock,
15021505 true ->
15031506 % % this avoids any data sent in the Callback to be dispatched
15041507 % % in it's own TCP frame
1505- ok = setopts (Transport , Sock , [{nopush , true }]),
1506- _ = Callback (Header , ToSend ),
1507- case sendfile (Transport , Fd , Sock , Pos , ToSend ) of
1508+ case setopts (Transport , Sock , [{nopush , true }]) of
15081509 ok ->
1509- ok = setopts (Transport , Sock , [{nopush , false }]),
1510- {ok , _ } = file :position (Fd , NextFilePos ),
1511- {ok , State };
1512- Err ->
1513- % % reset the position to the start of the current
1514- % % chunk so that subsequent reads won't error
1515- {ok , _ } = file :position (Fd , Pos ),
1516- Err
1510+ _ = Callback (Header , ToSend ),
1511+ case sendfile (Transport , Fd , Sock , Pos , ToSend ) of
1512+ ok ->
1513+ ok = setopts (Transport , Sock , [{nopush , false }]),
1514+ {ok , _ } = file :position (Fd , NextFilePos ),
1515+ {ok , State };
1516+ Err ->
1517+ % % reset the position to the start of the current
1518+ % % chunk so that subsequent reads won't error
1519+ {ok , _ } = file :position (Fd , Pos ),
1520+ Err
1521+ end ;
1522+ Err -> Err
15171523 end ;
15181524 false ->
15191525 {ok , _ } = file :position (Fd , NextFilePos ),
@@ -1997,10 +2003,10 @@ last_epoch_offsets([IdxFile]) ->
19972003last_epoch_offsets ([FstIdxFile | _ ] = IdxFiles ) ->
19982004 F = fun () ->
19992005 {ok , FstFd } = open (FstIdxFile , [read , raw , binary ]),
2000- % % on linux this disables read-ahead so should only
2001- % % bring a single block into memory
2002- % % having the first block of index files in page cache
2003- % % should generally be a good thing
2006+ % % on linux this disables read-ahead so should only
2007+ % % bring a single block into memory
2008+ % % having the first block of index files in page cache
2009+ % % should generally be a good thing
20042010 _ = file :advise (FstFd , 0 , 0 , random ),
20052011 {ok , <<FstO :64 /unsigned ,
20062012 _FstTimestamp :64 /signed ,
@@ -2186,9 +2192,9 @@ max_segment_size_reached(
21862192 CurrentSizeChunks >= MaxSizeChunks .
21872193
21882194setopts (tcp , Sock , Opts ) ->
2189- ok = inet :setopts (Sock , Opts );
2195+ inet :setopts (Sock , Opts );
21902196setopts (ssl , Sock , Opts ) ->
2191- ok = ssl :setopts (Sock , Opts ).
2197+ ssl :setopts (Sock , Opts ).
21922198
21932199sendfile (_Transport , _Fd , _Sock , _Pos , 0 ) ->
21942200 ok ;
0 commit comments