5050 reference :: term (),
5151 event_formatter :: undefined | mfa (),
5252 counter :: counters :counters_ref (),
53- token :: undefined | binary ()}).
53+ token :: undefined | binary (),
54+ committed_offset_calculate :: boolean ()}).
5455
5556-type parse_state () ::
5657 undefined |
7576-define (C_PACKETS , ? C_NUM_LOG_FIELDS + 3 ).
7677-define (C_READERS , ? C_NUM_LOG_FIELDS + 4 ).
7778-define (C_EPOCH , ? C_NUM_LOG_FIELDS + 5 ).
79+ -define (C_COMMITTED_CHUNK_ID , ? C_NUM_LOG_FIELDS + 6 ).
7880-define (ADD_COUNTER_FIELDS ,
7981 [{committed_offset , ? C_COMMITTED_OFFSET , counter , " Last committed offset" },
8082 {forced_gcs , ? C_FORCED_GCS , counter , " Number of garbage collection runs" },
8183 {packets , ? C_PACKETS , counter , " Number of packets" },
8284 {readers , ? C_READERS , counter , " Number of readers" },
83- {epoch , ? C_EPOCH , counter , " Current epoch" }]).
85+ {epoch , ? C_EPOCH , counter , " Current epoch" },
86+ {committed_chunk_id , ? C_COMMITTED_CHUNK_ID , counter , " Last committed chunk ID" }
87+ ]).
8488-define (FIELDSPEC_KEY , osiris_replica_seshat_fields_spec ).
8589
8690-define (DEFAULT_ONE_TIME_TOKEN_TIMEOUT , 30000 ).
@@ -194,10 +198,10 @@ handle_continue(#{name := Name0,
194198 case LastChunk of
195199 empty ->
196200 ok ;
197- { _ , LastChId , LastTs } ->
201+ _ ->
198202 % % need to ack last chunk back to leader so that it can
199203 % % re-discover the committed offset
200- osiris_writer :ack (LeaderPid , { LastChId , LastTs } )
204+ osiris_writer :ack (LeaderPid , ack_msg ( Config , TailInfo ) )
201205 end ,
202206 ? INFO_ (Name , " osiris replica starting in epoch ~b , next offset ~b , tail info ~w " ,
203207 [Epoch , NextOffset , TailInfo ]),
@@ -236,6 +240,10 @@ handle_continue(#{name := Name0,
236240 Acceptor = spawn_link (fun () -> accept (Name , Transport , LSock , Self ) end ),
237241 ? DEBUG_ (Name , " starting replica reader on node '~w '" , [Node ]),
238242
243+ CmttedOfstCalculate = committed_offset_calculate (Config ),
244+ Features0 = maps :get (features , Config , #{}),
245+ Features1 = Features0 #{committed_offset_calculate =>
246+ committed_offset_calculate (Config )},
239247 ReplicaReaderConf = #{hosts => IpsHosts ,
240248 port => Port ,
241249 transport => Transport ,
@@ -244,7 +252,8 @@ handle_continue(#{name := Name0,
244252 leader_pid => LeaderPid ,
245253 start_offset => TailInfo ,
246254 reference => ExtRef ,
247- connection_token => Token },
255+ connection_token => Token ,
256+ features => Features1 },
248257 case osiris_replica_reader :start (Node , ReplicaReaderConf ) of
249258 {ok , RRPid } ->
250259 true = link (RRPid ),
@@ -260,6 +269,7 @@ handle_continue(#{name := Name0,
260269 false ->
261270 infinity
262271 end ,
272+ counters :put (CntRef , ? C_COMMITTED_CHUNK_ID , - 1 ),
263273 counters :put (CntRef , ? C_COMMITTED_OFFSET , - 1 ),
264274 counters :put (CntRef , ? C_EPOCH , Epoch ),
265275 Shared = osiris_log :get_shared (Log ),
@@ -281,7 +291,8 @@ handle_continue(#{name := Name0,
281291 event_formatter = EvtFmt ,
282292 counter = CntRef ,
283293 token = Token ,
284- transport = Transport },
294+ transport = Transport ,
295+ committed_offset_calculate = CmttedOfstCalculate },
285296 log = Log ,
286297 parse_state = undefined }};
287298 {error , {connection_refused = R , _ }} ->
@@ -424,22 +435,26 @@ handle_call(Unknown, _From,
424435% % {stop, Reason, State}
425436% % @end
426437% %--------------------------------------------------------------------
427- handle_cast ({committed_offset , CommittedChId },
438+ handle_cast ({committed_offset , { CommittedChId , LastOffset } },
428439 #? MODULE {cfg = # cfg {counter = Cnt },
429440 log = Log ,
430441 committed_chunk_id = LastCommittedChId } =
431442 State ) ->
432443 case CommittedChId > LastCommittedChId of
433444 true ->
434445 % % notify offset listeners
435- counters :put (Cnt , ? C_COMMITTED_OFFSET , CommittedChId ),
446+ counters :put (Cnt , ? C_COMMITTED_CHUNK_ID , CommittedChId ),
447+ counters :put (Cnt , ? C_COMMITTED_OFFSET , LastOffset ),
436448 ok = osiris_log :set_committed_chunk_id (Log , CommittedChId ),
449+ ok = osiris_log :set_committed_offset (Log , LastOffset ),
437450 {noreply ,
438451 notify_offset_listeners (
439452 State #? MODULE {committed_chunk_id = CommittedChId })};
440453 false ->
441454 State
442455 end ;
456+ handle_cast ({committed_offset , CommittedChId }, State ) ->
457+ handle_cast ({committed_offset , {CommittedChId , - 1 }}, State );
443458handle_cast ({register_offset_listener , Pid , EvtFormatter , Offset },
444459 #? MODULE {cfg = # cfg {reference = Ref ,
445460 event_formatter = DefaultFmt },
@@ -572,7 +587,7 @@ handle_incoming_data(Socket, Bin,
572587 # cfg {socket = Socket ,
573588 leader_pid = LeaderPid ,
574589 transport = Transport ,
575- counter = Cnt },
590+ counter = Cnt } = Cfg ,
576591 parse_state = ParseState0 ,
577592 log = Log0 } =
578593 State0 ) ->
@@ -594,7 +609,8 @@ handle_incoming_data(Socket, Bin,
594609 undefined ->
595610 {noreply , State1 };
596611 _ ->
597- ok = osiris_writer :ack (LeaderPid , OffsetTimestamp ),
612+ TailInfo = osiris_log :tail_info (Log ),
613+ ok = osiris_writer :ack (LeaderPid , ack_msg (Cfg , TailInfo )),
598614 State = notify_offset_listeners (State1 ),
599615 {noreply , State }
600616 end .
@@ -738,7 +754,7 @@ notify_offset_listeners(#?MODULE{cfg = #cfg{reference = Ref,
738754 State #? MODULE {offset_listeners = L }.
739755
740756max_readable_chunk_id (Log ) ->
741- min (osiris_log :committed_offset (Log ), osiris_log :last_chunk_id (Log )).
757+ min (osiris_log :committed_chunk_id (Log ), osiris_log :last_chunk_id (Log )).
742758
743759% % INTERNAL
744760
@@ -796,3 +812,20 @@ listen(ssl, Port, Options) ->
796812init_fields_spec () ->
797813 persistent_term :put (? FIELDSPEC_KEY ,
798814 ? ADD_COUNTER_FIELDS ++ osiris_log :counter_fields ()).
815+
816+ ack_msg (Cfg , TailInfo ) ->
817+ case committed_offset_calculate (Cfg ) of
818+ true ->
819+ TailInfo ;
820+ false ->
821+ ? TAIL_INFO (TailChkId , TailTs ) = TailInfo ,
822+ {TailChkId , TailTs }
823+ end .
824+
825+ committed_offset_calculate (# cfg {committed_offset_calculate = On }) ->
826+ On ;
827+ committed_offset_calculate (#{features := #{committed_offset_calculate := On }})
828+ when is_boolean (On ) ->
829+ On ;
830+ committed_offset_calculate (_ ) ->
831+ false .
0 commit comments