@@ -489,7 +489,9 @@ struct TRawPartitionStreamEvent {
489489template <bool UseMigrationProtocol>
490490class TRawPartitionStreamEventQueue {
491491public:
492- TRawPartitionStreamEventQueue () = default ;
492+ TRawPartitionStreamEventQueue (TCallbackContextPtr<UseMigrationProtocol> cbContext)
493+ : CbContext(cbContext) {
494+ };
493495
494496 template <class ... Ts>
495497 TRawPartitionStreamEvent<UseMigrationProtocol>& emplace_back (Ts&&... event)
@@ -550,6 +552,7 @@ class TRawPartitionStreamEventQueue {
550552 std::deque<TRawPartitionStreamEvent<UseMigrationProtocol>>& queue);
551553
552554private:
555+ TCallbackContextPtr<UseMigrationProtocol> CbContext;
553556 std::deque<TRawPartitionStreamEvent<UseMigrationProtocol>> Ready;
554557 std::deque<TRawPartitionStreamEvent<UseMigrationProtocol>> NotReady;
555558};
@@ -583,6 +586,7 @@ class TPartitionStreamImpl : public TAPartitionStream<UseMigrationProtocol> {
583586 , AssignId(assignId)
584587 , FirstNotReadOffset(readOffset)
585588 , CbContext(std::move(cbContext))
589+ , EventsQueue(CbContext)
586590 {
587591 TAPartitionStream<true >::PartitionStreamId = partitionStreamId;
588592 TAPartitionStream<true >::TopicPath = std::move (topicPath);
@@ -603,6 +607,7 @@ class TPartitionStreamImpl : public TAPartitionStream<UseMigrationProtocol> {
603607 , AssignId(static_cast <ui64>(assignId))
604608 , FirstNotReadOffset(static_cast <ui64>(readOffset))
605609 , CbContext(std::move(cbContext))
610+ , EventsQueue(CbContext)
606611 {
607612 TAPartitionStream<false >::PartitionSessionId = partitionStreamId;
608613 TAPartitionStream<false >::TopicPath = std::move (topicPath);
@@ -625,6 +630,7 @@ class TPartitionStreamImpl : public TAPartitionStream<UseMigrationProtocol> {
625630
626631 void ConfirmCreate (TMaybe<ui64> readOffset, TMaybe<ui64> commitOffset);
627632 void ConfirmDestroy ();
633+ void ConfirmEnd (const std::vector<ui32>& childIds);
628634
629635 void StopReading () /* override*/ ;
630636 void ResumeReading () /* override*/ ;
@@ -764,6 +770,7 @@ class TPartitionStreamImpl : public TAPartitionStream<UseMigrationProtocol> {
764770 TMutex Lock;
765771};
766772
773+
767774template <bool UseMigrationProtocol>
768775class TReadSessionEventsQueue : public TBaseSessionEventsQueue <TAReadSessionSettings<UseMigrationProtocol>,
769776 typename TAReadSessionEvent<UseMigrationProtocol>::TEvent,
@@ -823,7 +830,8 @@ class TReadSessionEventsQueue: public TBaseSessionEventsQueue<TAReadSessionSetti
823830 }
824831
825832 bool TryApplyCallbackToEventImpl (typename TParent::TEvent& event,
826- TDeferredActions<UseMigrationProtocol>& deferred);
833+ TDeferredActions<UseMigrationProtocol>& deferred,
834+ TCallbackContextPtr<UseMigrationProtocol>& cbContext);
827835 bool HasDataEventCallback () const ;
828836 void ApplyCallbackToEventImpl (TADataReceivedEvent<UseMigrationProtocol>& event,
829837 TUserRetrievedEventsInfoAccumulator<UseMigrationProtocol>&& eventsInfo,
@@ -859,13 +867,19 @@ class TReadSessionEventsQueue: public TBaseSessionEventsQueue<TAReadSessionSetti
859867
860868 void ClearAllEvents ();
861869
870+ void SetCallbackContext (TCallbackContextPtr<UseMigrationProtocol>& ctx) {
871+ CbContext = ctx;
872+ }
873+
862874private:
863875 struct THandlersVisitor : public TParent ::TBaseHandlersVisitor {
864876 THandlersVisitor (const TAReadSessionSettings<UseMigrationProtocol>& settings,
865877 typename TParent::TEvent& event,
866- TDeferredActions<UseMigrationProtocol>& deferred)
878+ TDeferredActions<UseMigrationProtocol>& deferred,
879+ TCallbackContextPtr<UseMigrationProtocol>& cbContext)
867880 : TParent::TBaseHandlersVisitor(settings, event)
868- , Deferred(deferred) {
881+ , Deferred(deferred)
882+ , CbContext(cbContext) {
869883 }
870884
871885#define DECLARE_HANDLER (type, handler, answer ) \
@@ -880,7 +894,7 @@ class TReadSessionEventsQueue: public TBaseSessionEventsQueue<TAReadSessionSetti
880894 } \
881895 /* */
882896
883- #define DECLARE_TEMPLATE_HANDLER (type_true, type_false, handler_true, handler_false, answer ) \
897+ #define DECLARE_TEMPLATE_HANDLER (type_true, type_false, handler_true, handler_false ) \
884898 bool operator ()(std::conditional_t <UseMigrationProtocol, type_true, type_false>&) { \
885899 if (this ->template PushHandler <std::conditional_t <UseMigrationProtocol, type_true, type_false>>( \
886900 std::move (TParent::TBaseHandlersVisitor::Event), \
@@ -892,7 +906,7 @@ class TReadSessionEventsQueue: public TBaseSessionEventsQueue<TAReadSessionSetti
892906 } \
893907 }(), \
894908 this ->Settings .EventHandlers_ .CommonHandler_ )) { \
895- return answer; \
909+ return true ; \
896910 } \
897911 return false ; \
898912 } \
@@ -901,50 +915,85 @@ class TReadSessionEventsQueue: public TBaseSessionEventsQueue<TAReadSessionSetti
901915 DECLARE_TEMPLATE_HANDLER (typename TAReadSessionEvent<true >::TDataReceivedEvent,
902916 typename TAReadSessionEvent<false >::TDataReceivedEvent,
903917 DataReceivedHandler_,
904- DataReceivedHandler_,
905- true );
918+ DataReceivedHandler_);
906919 DECLARE_TEMPLATE_HANDLER (typename TAReadSessionEvent<true >::TCommitAcknowledgementEvent,
907920 typename TAReadSessionEvent<false >::TCommitOffsetAcknowledgementEvent,
908921 CommitAcknowledgementHandler_,
909- CommitOffsetAcknowledgementHandler_,
910- true );
922+ CommitOffsetAcknowledgementHandler_);
911923 DECLARE_TEMPLATE_HANDLER (typename TAReadSessionEvent<true >::TCreatePartitionStreamEvent,
912924 typename TAReadSessionEvent<false >::TStartPartitionSessionEvent,
913925 CreatePartitionStreamHandler_,
914- StartPartitionSessionHandler_,
915- true );
926+ StartPartitionSessionHandler_);
916927 DECLARE_TEMPLATE_HANDLER (typename TAReadSessionEvent<true >::TDestroyPartitionStreamEvent,
917928 typename TAReadSessionEvent<false >::TStopPartitionSessionEvent,
918929 DestroyPartitionStreamHandler_,
919- StopPartitionSessionHandler_,
920- true );
930+ StopPartitionSessionHandler_);
921931 DECLARE_TEMPLATE_HANDLER (typename TAReadSessionEvent<true >::TPartitionStreamStatusEvent,
922932 typename TAReadSessionEvent<false >::TPartitionSessionStatusEvent,
923933 PartitionStreamStatusHandler_,
924- PartitionSessionStatusHandler_,
925- true );
926- DECLARE_TEMPLATE_HANDLER (typename TAReadSessionEvent<true >::TPartitionStreamClosedEvent,
927- typename TAReadSessionEvent<false >::TPartitionSessionClosedEvent,
928- PartitionStreamClosedHandler_,
929- PartitionSessionClosedHandler_,
930- true );
934+ PartitionSessionStatusHandler_);
935+ // DECLARE_TEMPLATE_HANDLER(typename TAReadSessionEvent<true>::TPartitionStreamClosedEvent,
936+ // typename TAReadSessionEvent<false>::TPartitionSessionClosedEvent,
937+ // PartitionStreamClosedHandler_,
938+ // PartitionSessionClosedHandler_);
931939 DECLARE_HANDLER (TASessionClosedEvent<UseMigrationProtocol>, SessionClosedHandler_, false ); // Not applied
932940
933941#undef DECLARE_HANDLER
934942#undef DECLARE_TEMPLATE_HANDLER
935943
944+ bool operator ()(std::conditional_t <UseMigrationProtocol, typename TAReadSessionEvent<true >::TPartitionStreamClosedEvent, typename TAReadSessionEvent<false >::TPartitionSessionClosedEvent>&) {
945+ auto specific = [this ]() {
946+ if constexpr (UseMigrationProtocol) {
947+ return this ->Settings .EventHandlers_ .PartitionStreamClosedHandler_ ;
948+ } else {
949+ return this ->Settings .EventHandlers_ .PartitionSessionClosedHandler_ ;
950+ }
951+ }();
952+
953+ if (!specific && !this ->Settings .EventHandlers_ .CommonHandler_ ) {
954+ return false ;
955+ }
956+
957+ this ->template PushCommonHandler <>(
958+ std::move (TParent::TBaseHandlersVisitor::Event),
959+ [specific = specific,
960+ common = this ->Settings .EventHandlers_ .CommonHandler_ ,
961+ cbContext = CbContext](auto & event) {
962+ auto & e = std::get<std::conditional_t <UseMigrationProtocol, typename TAReadSessionEvent<true >::TPartitionStreamClosedEvent, typename TAReadSessionEvent<false >::TPartitionSessionClosedEvent>>(event);
963+ if (specific) {
964+ specific (e);
965+ } else if (common) {
966+ common (event);
967+ }
968+ if constexpr (!UseMigrationProtocol) {
969+ if (auto session = cbContext->LockShared ()) {
970+ session->UnregisterPartition (e.GetPartitionSession ()->GetPartitionId (), e.GetPartitionSession ()->GetPartitionSessionId ());
971+ }
972+ }
973+ });
974+
975+ return true ;
976+ }
977+
936978 template <bool E = !UseMigrationProtocol>
937979 constexpr std::enable_if_t <E, bool >
938980 operator ()(typename TAReadSessionEvent<false >::TEndPartitionSessionEvent&) {
939- if (this ->template PushHandler <typename TAReadSessionEvent<false >::TEndPartitionSessionEvent>(
940- std::move (TParent::TBaseHandlersVisitor::Event),
941- [this ](){
942- return this ->Settings .EventHandlers_ .EndPartitionSessionHandler_ ;
943- }(),
944- this ->Settings .EventHandlers_ .CommonHandler_ )) {
981+ if (!this ->Settings .EventHandlers_ .EndPartitionSessionHandler_ && !this ->Settings .EventHandlers_ .CommonHandler_ ) {
945982 return false ;
946983 }
947- return false ;
984+ this ->template PushCommonHandler <>(
985+ std::move (TParent::TBaseHandlersVisitor::Event),
986+ [specific = this ->Settings .EventHandlers_ .EndPartitionSessionHandler_ ,
987+ common = this ->Settings .EventHandlers_ .CommonHandler_ ,
988+ cbContext = CbContext](TReadSessionEvent::TEvent& event) {
989+ auto & e = std::get<TReadSessionEvent::TEndPartitionSessionEvent>(event);
990+ if (specific) {
991+ specific (e);
992+ } else if (common) {
993+ common (event);
994+ }
995+ });
996+ return true ;
948997 }
949998
950999 bool Visit () {
@@ -956,6 +1005,7 @@ class TReadSessionEventsQueue: public TBaseSessionEventsQueue<TAReadSessionSetti
9561005 }
9571006
9581007 TDeferredActions<UseMigrationProtocol>& Deferred;
1008+ TCallbackContextPtr<UseMigrationProtocol> CbContext;
9591009 };
9601010
9611011 TADataReceivedEvent<UseMigrationProtocol>
@@ -964,12 +1014,13 @@ class TReadSessionEventsQueue: public TBaseSessionEventsQueue<TAReadSessionSetti
9641014 TUserRetrievedEventsInfoAccumulator<UseMigrationProtocol>& accumulator); // Assumes that we're under lock.
9651015
9661016 bool ApplyHandler (TReadSessionEventInfo<UseMigrationProtocol>& eventInfo, TDeferredActions<UseMigrationProtocol>& deferred) {
967- THandlersVisitor visitor (this ->Settings , eventInfo.GetEvent (), deferred);
1017+ THandlersVisitor visitor (this ->Settings , eventInfo.GetEvent (), deferred, CbContext );
9681018 return visitor.Visit ();
9691019 }
9701020
9711021private:
9721022 bool HasEventCallbacks;
1023+ TCallbackContextPtr<UseMigrationProtocol> CbContext;
9731024};
9741025
9751026} // namespace NYdb::NTopic
@@ -1045,6 +1096,7 @@ class TSingleClusterReadSessionImpl : public TEnableSelfContext<TSingleClusterRe
10451096 void Start ();
10461097 void ConfirmPartitionStreamCreate (const TPartitionStreamImpl<UseMigrationProtocol>* partitionStream, TMaybe<ui64> readOffset, TMaybe<ui64> commitOffset);
10471098 void ConfirmPartitionStreamDestroy (TPartitionStreamImpl<UseMigrationProtocol>* partitionStream);
1099+ void ConfirmPartitionStreamEnd (TPartitionStreamImpl<UseMigrationProtocol>* partitionStream, const std::vector<ui32>& childIds);
10481100 void RequestPartitionStreamStatus (const TPartitionStreamImpl<UseMigrationProtocol>* partitionStream);
10491101 void Commit (const TPartitionStreamImpl<UseMigrationProtocol>* partitionStream, ui64 startOffset, ui64 endOffset);
10501102
@@ -1092,6 +1144,16 @@ class TSingleClusterReadSessionImpl : public TEnableSelfContext<TSingleClusterRe
10921144 return Log;
10931145 }
10941146
1147+ void RegisterParentPartition (ui32 partitionId, ui32 parentPartitionId, ui64 parentPartitionSessionId);
1148+ void UnregisterPartition (ui32 partitionId, ui64 partitionSessionId);
1149+ std::vector<ui64> GetParentPartitionSessions (ui32 partitionId, ui64 partitionSessionId);
1150+ bool AllParentSessionsHasBeenRead (ui32 partitionId, ui64 partitionSessionId);
1151+
1152+ void SetSelfContext (TPtr ptr) {
1153+ TEnableSelfContext<TSingleClusterReadSessionImpl<UseMigrationProtocol>>::SetSelfContext (std::move (ptr));
1154+ EventsQueue->SetCallbackContext (TEnableSelfContext<TSingleClusterReadSessionImpl<UseMigrationProtocol>>::SelfContext);
1155+ }
1156+
10951157private:
10961158 void BreakConnectionAndReconnectImpl (TPlainStatus&& status, TDeferredActions<UseMigrationProtocol>& deferred);
10971159
@@ -1273,6 +1335,14 @@ class TSingleClusterReadSessionImpl : public TEnableSelfContext<TSingleClusterRe
12731335 std::atomic<int > DecompressionTasksInflight = 0 ;
12741336 i64 ReadSizeBudget;
12751337 i64 ReadSizeServerDelta = 0 ;
1338+
1339+ struct TParentInfo {
1340+ ui32 PartitionId;
1341+ ui64 PartitionSessionId;
1342+ };
1343+
1344+ std::unordered_map<ui32, std::vector<TParentInfo>> HierarchyData;
1345+ std::unordered_set<ui64> ReadingFinishedData;
12761346};
12771347
12781348} // namespace NYdb::NTopic
0 commit comments