Skip to content

Commit e09f258

Browse files
Few more dread cache counters (#12317)
1 parent 7d3acec commit e09f258

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

ydb/core/persqueue/dread_cache_service/caching_service.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class TPQDirectReadCacheService : public TActorBootstrapped<TPQDirectReadCacheSe
4545

4646
Become(&TThis::StateWork);
4747
Y_UNUSED(ctx);
48-
//Y_ABORT_UNLESS(Counters);
4948
}
5049

5150
STRICT_STFUNC(StateWork,
@@ -66,6 +65,7 @@ class TPQDirectReadCacheService : public TActorBootstrapped<TPQDirectReadCacheSe
6665
const auto& ctx = ActorContext();
6766
auto key = MakeSessionKey(ev->Get());
6867
PQ_CPROXY_LOG_D("client session connected with id '" << key.SessionId << "'");
68+
ChangeCounterValue("CreateClientSessionRate", 1, false, true);
6969
auto sessionIter = ServerSessions.find(key);
7070
if (sessionIter.IsEnd()) {
7171
PQ_CPROXY_LOG_D("unknown session id '" << key.SessionId << "', close session");
@@ -88,7 +88,11 @@ class TPQDirectReadCacheService : public TActorBootstrapped<TPQDirectReadCacheSe
8888
// Without this response, the client might have to wait until there are topic messages to send.
8989
ctx.Send(sender, new TEvPQProxy::TEvDirectReadDataSessionConnectedResponse(key.PartitionSessionId, ev->Get()->Generation));
9090

91+
if (!sessionIter->second.Client.Defined()) {
92+
ChangeCounterValue("ActiveClientSessions", 1, false);
93+
} // else Its probably a misbehavior by client (or proxy) but we can handle it anyway
9194
sessionIter->second.Client = TCacheClientContext{sender, startingReadId};
95+
9296
AssignByProxy[sender].insert(key.PartitionSessionId);
9397
while(SendNextReadToClient(sessionIter)) {
9498
// Empty
@@ -105,6 +109,7 @@ class TPQDirectReadCacheService : public TActorBootstrapped<TPQDirectReadCacheSe
105109
Ydb::PersQueue::ErrorCode::ErrorCode::OK, "", ev->Sender
106110
);
107111
}
112+
AssignByProxy.erase(assignIter);
108113
}
109114

110115
void HandleRegister(TEvPQ::TEvRegisterDirectReadSession::TPtr& ev) {
@@ -149,6 +154,7 @@ class TPQDirectReadCacheService : public TActorBootstrapped<TPQDirectReadCacheSe
149154
}
150155
ChangeCounterValue("StagedReadDataSize", ins.first->second->ByteSize(), false);
151156
ChangeCounterValue("StagedReadsCount", 1, false);
157+
ChangeCounterValue("StagedReadsRate", 1, false, true);
152158
PQ_CPROXY_LOG_D("staged direct read id " << ev->Get()->ReadKey.ReadId << " for session: " << sessionKey.SessionId);
153159
}
154160

@@ -178,6 +184,7 @@ class TPQDirectReadCacheService : public TActorBootstrapped<TPQDirectReadCacheSe
178184
if (inserted) {
179185
ChangeCounterValue("PublishedReadDataSize", stagedIter->second->ByteSize(), false);
180186
ChangeCounterValue("PublishedReadsCount", 1, false);
187+
ChangeCounterValue("PublishedReadsRate", 1, false, true);
181188
}
182189
ChangeCounterValue("StagedReadDataSize", -stagedIter->second->ByteSize(), false);
183190
ChangeCounterValue("StagedReadsCount", -1, false);
@@ -201,19 +208,25 @@ class TPQDirectReadCacheService : public TActorBootstrapped<TPQDirectReadCacheSe
201208
if (iter->second.Generation != generation) { // Stale generation in event, ignore it
202209
return;
203210
}
211+
bool didForget = false;
204212
auto readIter = iter->second.Reads.find(ev->Get()->ReadKey.ReadId);
205213
if (readIter != iter->second.Reads.end()) {
206214
ChangeCounterValue("PublishedReadDataSize", -readIter->second->ByteSize(), false);
207215
ChangeCounterValue("PublishedReadsCount", -1, false);
216+
didForget = true;
208217

209218
iter->second.Reads.erase(readIter);
210219
}
211220
auto stagedIter = iter->second.StagedReads.find(ev->Get()->ReadKey.ReadId);
212221
if (stagedIter != iter->second.StagedReads.end()) {
213222
ChangeCounterValue("StagedReadDataSize", -stagedIter->second->ByteSize(), false);
214223
ChangeCounterValue("StagedReadsCount", -1, false);
224+
didForget = true;
215225
iter->second.StagedReads.erase(stagedIter);
216226
}
227+
if (didForget) {
228+
ChangeCounterValue("ForgetReadsRate", 1, false, true);
229+
}
217230
iter->second.StagedReads.erase(ev->Get()->ReadKey.ReadId);
218231
}
219232

@@ -234,13 +247,15 @@ class TPQDirectReadCacheService : public TActorBootstrapped<TPQDirectReadCacheSe
234247
if (!assignIter.IsEnd()) {
235248
assignIter->second.erase(sessionIter->first.PartitionSessionId);
236249
}
250+
if (sessionIter->second.Client.Defined()) {
251+
ChangeCounterValue("ActiveClientSessions", -1, false);
252+
}
237253
sessionIter->second.Client = Nothing();
238254
}
239255

240256
[[nodiscard]] bool DestroyServerSession(TSessionsMap::iterator sessionIter, ui64 generation) {
241257
if (sessionIter.IsEnd() || sessionIter->second.Generation > generation)
242258
return false;
243-
Cerr << "CahceProxy: DestroyServerSession with generation: " << generation << Endl;
244259
DestroyPartitionSession(sessionIter, Ydb::PersQueue::ErrorCode::READ_ERROR_NO_SESSION, "Closed by server");
245260
ServerSessions.erase(sessionIter);
246261
ChangeCounterValue("ActiveServerSessions", ServerSessions.size(), true);
@@ -311,6 +326,7 @@ class TPQDirectReadCacheService : public TActorBootstrapped<TPQDirectReadCacheSe
311326
return false;
312327
}
313328
auto result = SendData(sessionIter->first.PartitionSessionId, client, nextData->first, nextData->second);
329+
ChangeCounterValue("SendDataRate", 1, false, true);
314330
if (!result) {
315331
//ToDo: for discuss. Error in parsing partition response - shall we kill the entire session or just the partition session?
316332
DestroyClientSession(sessionIter, false, Ydb::PersQueue::ErrorCode::OK, "");
@@ -369,10 +385,10 @@ class TPQDirectReadCacheService : public TActorBootstrapped<TPQDirectReadCacheSe
369385
return true;
370386
}
371387

372-
void ChangeCounterValue(const TString& name, i64 value, bool isAbs) {
388+
void ChangeCounterValue(const TString& name, i64 value, bool isAbs, bool deriv = false) {
373389
if (!Counters)
374390
return;
375-
auto counter = Counters->GetCounter(name, false);
391+
auto counter = Counters->GetCounter(name, deriv);
376392
if (isAbs)
377393
counter->Set(value);
378394
else if (value >= 0)
@@ -510,9 +526,12 @@ class TPQDirectReadCacheService : public TActorBootstrapped<TPQDirectReadCacheSe
510526

511527

512528
IActor* CreatePQDReadCacheService(const ::NMonitoring::TDynamicCounterPtr& counters) {
513-
Y_VERIFY_DEBUG(counters);
514-
return new TPQDirectReadCacheService(
515-
GetServiceCounters(counters, "persqueue")->GetSubgroup("subsystem", "caching_service"));
529+
if (counters) {
530+
return new TPQDirectReadCacheService(
531+
GetServiceCounters(counters, "persqueue")->GetSubgroup("subsystem", "caching_service"));
532+
} else {
533+
return new TPQDirectReadCacheService(nullptr);
534+
}
516535
}
517536

518537
} // namespace NKikimr::NPQ

0 commit comments

Comments
 (0)