@@ -310,6 +310,7 @@ class TTopicSession : public TActorBootstrapped<TTopicSession> {
310
310
void SendStatisticToRowDispatcher ();
311
311
void SendSessionError (TActorId readActorId, TStatus status);
312
312
bool CheckNewClient (NFq::TEvRowDispatcher::TEvStartSession::TPtr& ev);
313
+ void RestartSessionIfOldestClient (const TClientsInfo& info);
313
314
314
315
private:
315
316
@@ -750,10 +751,12 @@ void TTopicSession::Handle(NFq::TEvRowDispatcher::TEvStopSession::TPtr& ev) {
750
751
751
752
auto it = Clients.find (ev->Sender );
752
753
if (it == Clients.end ()) {
753
- LOG_ROW_DISPATCHER_DEBUG ( " Wrong ClientSettings " );
754
+ LOG_ROW_DISPATCHER_WARN ( " Ignore TEvStopSession from " << ev-> Sender << " , no client " );
754
755
return ;
755
756
}
756
757
auto & info = *it->second ;
758
+ RestartSessionIfOldestClient (info);
759
+
757
760
UnreadBytes -= info.UnreadBytes ;
758
761
Metrics.UnreadBytes ->Sub (info.UnreadBytes );
759
762
if (const auto formatIt = FormatHandlers.find (info.HandlerSettings ); formatIt != FormatHandlers.end ()) {
@@ -769,6 +772,41 @@ void TTopicSession::Handle(NFq::TEvRowDispatcher::TEvStopSession::TPtr& ev) {
769
772
SubscribeOnNextEvent ();
770
773
}
771
774
775
+ void TTopicSession::RestartSessionIfOldestClient (const TClientsInfo& info) {
776
+ // if we read historical data (because of this client), then we restart the session.
777
+
778
+ if (!ReadSession || !info.NextMessageOffset ) {
779
+ return ;
780
+ }
781
+ TMaybe<ui64> minMessageOffset;
782
+ for (auto & [readActorId, clientPtr] : Clients) {
783
+ if (info.ReadActorId == readActorId || !clientPtr->NextMessageOffset ) {
784
+ continue ;
785
+ }
786
+ if (!minMessageOffset) {
787
+ minMessageOffset = clientPtr->NextMessageOffset ;
788
+ continue ;
789
+ }
790
+ minMessageOffset = std::min (minMessageOffset, clientPtr->NextMessageOffset );
791
+ }
792
+ if (!minMessageOffset) {
793
+ return ;
794
+ }
795
+
796
+ if (info.NextMessageOffset >= minMessageOffset) {
797
+ return ;
798
+ }
799
+ LOG_ROW_DISPATCHER_INFO (" Client (on StopSession) has less offset (" << info.NextMessageOffset << " ) than others clients (" << minMessageOffset << " ), stop (restart) topic session" );
800
+ Metrics.RestartSessionByOffsets ->Inc ();
801
+ ++RestartSessionByOffsets;
802
+ info.RestartSessionByOffsetsByQuery ->Inc ();
803
+ StopReadSession ();
804
+
805
+ if (!ReadSession) {
806
+ Schedule (TDuration::Seconds (Config.GetTimeoutBeforeStartSessionSec ()), new NFq::TEvPrivate::TEvCreateSession ());
807
+ }
808
+ }
809
+
772
810
void TTopicSession::FatalError (TStatus status) {
773
811
LOG_ROW_DISPATCHER_ERROR (" FatalError: " << status.GetErrorMessage ());
774
812
0 commit comments