From 2495a9ed1235b21d2d46fee297c757cb68fd8cfa Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 11 Dec 2024 16:57:50 +0100 Subject: [PATCH 1/2] Count still queued messages per JsonRpcConnection --- lib/remote/jsonrpcconnection.cpp | 3 +++ lib/remote/jsonrpcconnection.hpp | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/lib/remote/jsonrpcconnection.cpp b/lib/remote/jsonrpcconnection.cpp index 889d4452c65..cd98d6ad61c 100644 --- a/lib/remote/jsonrpcconnection.cpp +++ b/lib/remote/jsonrpcconnection.cpp @@ -154,6 +154,7 @@ void JsonRpcConnection::WriteOutgoingMessages(boost::asio::yield_context yc) } size_t bytesSent = JsonRpc::SendRawMessage(m_Stream, message, yc); + m_PendingOutgoingMessages.fetch_sub(1, std::memory_order_relaxed); if (m_Endpoint) { m_Endpoint->AddMessageSent(bytesSent); @@ -230,6 +231,7 @@ void JsonRpcConnection::SendRawMessage(const String& message) m_OutgoingMessagesQueue.emplace_back(message); m_OutgoingMessagesQueued.Set(); + m_PendingOutgoingMessages.fetch_add(1, std::memory_order_relaxed); }); } @@ -241,6 +243,7 @@ void JsonRpcConnection::SendMessageInternal(const Dictionary::Ptr& message) m_OutgoingMessagesQueue.emplace_back(JsonEncode(message)); m_OutgoingMessagesQueued.Set(); + m_PendingOutgoingMessages.fetch_add(1, std::memory_order_relaxed); } void JsonRpcConnection::Disconnect() diff --git a/lib/remote/jsonrpcconnection.hpp b/lib/remote/jsonrpcconnection.hpp index 826d3b46ac2..e63f1f11faf 100644 --- a/lib/remote/jsonrpcconnection.hpp +++ b/lib/remote/jsonrpcconnection.hpp @@ -54,6 +54,11 @@ class JsonRpcConnection final : public Object Shared::Ptr GetStream() const; ConnectionRole GetRole() const; + auto GetPendingOutgoingMessages() const noexcept + { + return m_PendingOutgoingMessages.load(std::memory_order_relaxed); + } + void Disconnect(); void SendMessage(const Dictionary::Ptr& request); @@ -76,6 +81,7 @@ class JsonRpcConnection final : public Object boost::asio::io_context::strand m_IoStrand; std::vector m_OutgoingMessagesQueue; AsioEvent m_OutgoingMessagesQueued; + Atomic m_PendingOutgoingMessages {0}; AsioEvent m_WriterDone; Atomic m_ShuttingDown; boost::asio::deadline_timer m_CheckLivenessTimer, m_HeartbeatTimer; From 731d3211abde6f0697bba4f148f7a3ddfaadda02 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 11 Dec 2024 17:21:08 +0100 Subject: [PATCH 2/2] Introduce Endpoint#pending_outgoing_messages for the API --- lib/methods/clusterzonechecktask.cpp | 3 +++ lib/methods/icingachecktask.cpp | 3 +++ lib/remote/endpoint.cpp | 12 ++++++++++++ lib/remote/endpoint.hpp | 1 + lib/remote/endpoint.ti | 4 ++++ 5 files changed, 23 insertions(+) diff --git a/lib/methods/clusterzonechecktask.cpp b/lib/methods/clusterzonechecktask.cpp index d7c99138c60..f6a020acdce 100644 --- a/lib/methods/clusterzonechecktask.cpp +++ b/lib/methods/clusterzonechecktask.cpp @@ -133,6 +133,7 @@ void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const Che double lastMessageSent = 0; double lastMessageReceived = 0; + uint_fast64_t pendingOutgoingMessages = 0; double messagesSentPerSecond = 0; double messagesReceivedPerSecond = 0; double bytesSentPerSecond = 0; @@ -156,6 +157,7 @@ void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const Che if (endpoint->GetLastMessageReceived() > lastMessageReceived) lastMessageReceived = endpoint->GetLastMessageReceived(); + pendingOutgoingMessages += endpoint->GetPendingOutgoingMessages(); messagesSentPerSecond += endpoint->GetMessagesSentPerSecond(); messagesReceivedPerSecond += endpoint->GetMessagesReceivedPerSecond(); bytesSentPerSecond += endpoint->GetBytesSentPerSecond(); @@ -207,6 +209,7 @@ void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const Che new PerfdataValue("slave_lag", zoneLag, false, "s", lagWarning, lagCritical), new PerfdataValue("last_messages_sent", lastMessageSent), new PerfdataValue("last_messages_received", lastMessageReceived), + new PerfdataValue("sum_pending_outgoing_messages", pendingOutgoingMessages), new PerfdataValue("sum_messages_sent_per_second", messagesSentPerSecond), new PerfdataValue("sum_messages_received_per_second", messagesReceivedPerSecond), new PerfdataValue("sum_bytes_sent_per_second", bytesSentPerSecond), diff --git a/lib/methods/icingachecktask.cpp b/lib/methods/icingachecktask.cpp index 3a8e9fbce93..53704e4fecf 100644 --- a/lib/methods/icingachecktask.cpp +++ b/lib/methods/icingachecktask.cpp @@ -123,6 +123,7 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes double lastMessageSent = 0; double lastMessageReceived = 0; + uint_fast64_t pendingOutgoingMessages = 0; double messagesSentPerSecond = 0; double messagesReceivedPerSecond = 0; double bytesSentPerSecond = 0; @@ -136,6 +137,7 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes if (endpoint->GetLastMessageReceived() > lastMessageReceived) lastMessageReceived = endpoint->GetLastMessageReceived(); + pendingOutgoingMessages += endpoint->GetPendingOutgoingMessages(); messagesSentPerSecond += endpoint->GetMessagesSentPerSecond(); messagesReceivedPerSecond += endpoint->GetMessagesReceivedPerSecond(); bytesSentPerSecond += endpoint->GetBytesSentPerSecond(); @@ -144,6 +146,7 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes perfdata->Add(new PerfdataValue("last_messages_sent", lastMessageSent)); perfdata->Add(new PerfdataValue("last_messages_received", lastMessageReceived)); + perfdata->Add(new PerfdataValue("sum_pending_outgoing_messages", pendingOutgoingMessages)); perfdata->Add(new PerfdataValue("sum_messages_sent_per_second", messagesSentPerSecond)); perfdata->Add(new PerfdataValue("sum_messages_received_per_second", messagesReceivedPerSecond)); perfdata->Add(new PerfdataValue("sum_bytes_sent_per_second", bytesSentPerSecond)); diff --git a/lib/remote/endpoint.cpp b/lib/remote/endpoint.cpp index e534fc17840..fe6e099577b 100644 --- a/lib/remote/endpoint.cpp +++ b/lib/remote/endpoint.cpp @@ -101,6 +101,18 @@ Endpoint::Ptr Endpoint::GetLocalEndpoint() return listener->GetLocalEndpoint(); } +uint_fast64_t Endpoint::GetPendingOutgoingMessages() const +{ + uint_fast64_t pending = 0; + std::unique_lock lock (m_ClientsLock); + + for (auto& client : m_Clients) { + pending += client->GetPendingOutgoingMessages(); + } + + return pending; +} + void Endpoint::AddMessageSent(int bytes) { double time = Utility::GetTime(); diff --git a/lib/remote/endpoint.hpp b/lib/remote/endpoint.hpp index d641c2c6b8f..942ea42898e 100644 --- a/lib/remote/endpoint.hpp +++ b/lib/remote/endpoint.hpp @@ -39,6 +39,7 @@ class Endpoint final : public ObjectImpl static Endpoint::Ptr GetLocalEndpoint(); void SetCachedZone(const intrusive_ptr& zone); + uint_fast64_t GetPendingOutgoingMessages() const override; void AddMessageSent(int bytes); void AddMessageReceived(int bytes); diff --git a/lib/remote/endpoint.ti b/lib/remote/endpoint.ti index 78551ecf0dd..1a7c19dee02 100644 --- a/lib/remote/endpoint.ti +++ b/lib/remote/endpoint.ti @@ -39,6 +39,10 @@ class Endpoint : ConfigObject Timestamp last_message_sent; Timestamp last_message_received; + [no_user_modify, no_storage] uint_fast64_t pending_outgoing_messages { + get; + }; + [no_user_modify, no_storage] double messages_sent_per_second { get; };