Skip to content

Commit 634dc20

Browse files
authored
Calculate disconnections in last hour (#11007)
1 parent d4eaec4 commit 634dc20

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

ydb/library/actors/interconnect/interconnect_tcp_proxy.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace NActors {
2929
Y_ABORT_UNLESS(!*DynamicPtr);
3030
*DynamicPtr = this;
3131
}
32+
NumDisconnects.fill(0);
3233
}
3334

3435
void TInterconnectProxyTCP::Bootstrap() {
@@ -942,4 +943,33 @@ namespace NActors {
942943
// TODO: unregister actor mon page
943944
TActor::PassAway();
944945
}
946+
947+
void TInterconnectProxyTCP::RegisterDisconnect() {
948+
const TMonotonic now = TActivationContext::Monotonic();
949+
ShiftDisconnectWindow(now);
950+
++NumDisconnectsInLastHour;
951+
++NumDisconnects[NumDisconnectsIndex];
952+
}
953+
954+
ui32 TInterconnectProxyTCP::GetDisconnectCountInLastHour() {
955+
ShiftDisconnectWindow(TMonotonic::Now());
956+
return NumDisconnectsInLastHour;
957+
}
958+
959+
void TInterconnectProxyTCP::ShiftDisconnectWindow(TMonotonic now) {
960+
const ui64 currentMinutes = now.Minutes();
961+
if (FirstDisconnectWindowMinutes) {
962+
const ui32 steps = currentMinutes - FirstDisconnectWindowMinutes;
963+
if (steps < NumDisconnectsSize) { // advance window by "steps" items, clearing them
964+
for (ui32 i = 0; i < steps; ++i) {
965+
NumDisconnectsInLastHour -= std::exchange(NumDisconnects[++NumDisconnectsIndex %= NumDisconnectsSize], 0);
966+
}
967+
} else { // window has been fully flushed
968+
NumDisconnects.fill(0);
969+
NumDisconnectsInLastHour = 0;
970+
}
971+
}
972+
FirstDisconnectWindowMinutes = currentMinutes;
973+
}
974+
945975
}

ydb/library/actors/interconnect/interconnect_tcp_proxy.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,19 @@ namespace NActors {
565565
void HandleTerminate();
566566

567567
void PassAway() override;
568+
569+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
570+
// Disconnection management
571+
572+
static constexpr size_t NumDisconnectsSize = 60;
573+
std::array<ui32, NumDisconnectsSize> NumDisconnects;
574+
size_t NumDisconnectsIndex = 0;
575+
ui32 NumDisconnectsInLastHour = 0;
576+
ui64 FirstDisconnectWindowMinutes = 0;
577+
578+
void RegisterDisconnect();
579+
ui32 GetDisconnectCountInLastHour();
580+
void ShiftDisconnectWindow(TMonotonic now);
568581
};
569582

570583
}

ydb/library/actors/interconnect/interconnect_tcp_session.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ namespace NActors {
542542
CloseOnIdleWatchdog.Disarm();
543543
LostConnectionWatchdog.Rearm(SelfId());
544544
Proxy->Metrics->SetConnected(0);
545+
Proxy->RegisterDisconnect();
545546
LOG_INFO(*TlsActivationContext, NActorsServices::INTERCONNECT_STATUS, "[%u] disconnected", Proxy->PeerNodeId);
546547
}
547548
if (XdcSocket) {

0 commit comments

Comments
 (0)