Browse Source

Improve monitoring

alexvru 1 year ago
parent
commit
17a85ebbec

+ 8 - 15
library/cpp/monlib/service/pages/index_mon_page.cpp

@@ -28,9 +28,8 @@ void TIndexMonPage::Output(IMonHttpRequest& request) {
         TGuard<TMutex> g(Mtx);
         TStringBuf pathTmp = request.GetPathInfo();
         for (;;) {
-            TPagesByPath::iterator i = PagesByPath.find(pathTmp);
-            if (i != PagesByPath.end()) {
-                found = i->second;
+            if (TPagesByPath::iterator i = PagesByPath.find(pathTmp); i != PagesByPath.end()) {
+                found = *i->second;
                 pathInfo = request.GetPathInfo().substr(pathTmp.size());
                 Y_VERIFY(pathInfo.empty() || pathInfo.StartsWith('/'));
                 break;
@@ -67,18 +66,12 @@ void TIndexMonPage::OutputIndex(IOutputStream& out, bool pathEndsWithSlash) {
 
 void TIndexMonPage::Register(TMonPagePtr page) {
     TGuard<TMutex> g(Mtx);
-    auto insres = PagesByPath.insert(std::make_pair("/" + page->GetPath(), page));
-    if (insres.second) {
-        // new unique page just inserted, update Pages
-        Pages.push_back(page);
+    if (auto [it, inserted] = PagesByPath.try_emplace("/" + page->GetPath()); inserted) {
+        // new unique page just inserted, insert it to the end
+        it->second = Pages.insert(Pages.end(), page);
     } else {
         // a page with the given path is already present, replace it with the new page
-
-        // find old page, sorry for O(n)
-        auto it = std::find(Pages.begin(), Pages.end(), insres.first->second);
-        *it = page;
-        // this already present, replace it
-        insres.first->second = page;
+        *it->second = page;
     }
     page->Parent = this;
 }
@@ -101,7 +94,7 @@ IMonPage* TIndexMonPage::FindPage(const TString& relativePath) {
     if (i == PagesByPath.end()) {
         return nullptr;
     } else {
-        return i->second.Get();
+        return i->second->Get();
     }
 }
 
@@ -171,7 +164,7 @@ void TIndexMonPage::OutputBody(IMonHttpRequest& req) {
 
 void TIndexMonPage::SortPages() {
     TGuard<TMutex> g(Mtx);
-    std::sort(Pages.begin(), Pages.end(), [](const TMonPagePtr& a, const TMonPagePtr& b) {
+    Pages.sort([](const TMonPagePtr& a, const TMonPagePtr& b) {
         return AsciiCompareIgnoreCase(a->GetTitle(), b->GetTitle()) < 0;
     });
 }

+ 5 - 3
library/cpp/monlib/service/pages/index_mon_page.h

@@ -2,12 +2,14 @@
 
 #include "mon_page.h"
 
+#include <list>
+
 namespace NMonitoring {
     struct TIndexMonPage: public IMonPage {
         TMutex Mtx;
-        typedef TVector<TMonPagePtr> TPages;
-        TPages Pages;
-        typedef THashMap<TString, TMonPagePtr> TPagesByPath;
+        using TPages = std::list<TMonPagePtr>;
+        TPages Pages; // a list of pages to maintain specific order
+        using TPagesByPath = THashMap<TString, TPages::iterator>;
         TPagesByPath PagesByPath;
 
         TIndexMonPage(const TString& path, const TString& title)

+ 9 - 3
ydb/core/mon/async_http_mon.cpp

@@ -394,6 +394,7 @@ public:
     STATEFN(StateWork) {
         switch (ev->GetTypeRewrite()) {
             hFunc(NHttp::TEvHttpProxy::TEvHttpIncomingRequest, Handle);
+            cFunc(TEvents::TSystem::Poison, PassAway);
         }
     }
 
@@ -458,6 +459,7 @@ public:
     STATEFN(StateWork) {
         switch (ev->GetTypeRewrite()) {
             hFunc(NHttp::TEvHttpProxy::TEvHttpIncomingRequest, Handle);
+            cFunc(TEvents::TSystem::Poison, PassAway);
         }
     }
 
@@ -660,6 +662,7 @@ public:
         switch (ev->GetTypeRewrite()) {
             hFunc(NHttp::TEvHttpProxy::TEvHttpIncomingRequest, Handle);
             hFunc(TEvMon::TEvMonitoringRequest, Handle);
+            cFunc(TEvents::TSystem::Poison, PassAway);
         }
     }
 
@@ -729,7 +732,7 @@ void TAsyncHttpMon::Stop() {
     IndexMonPage->ClearPages(); // it's required to avoid loop-reference
     if (ActorSystem) {
         TGuard<TMutex> g(Mutex);
-        for (const TActorId& actorId : ActorServices) {
+        for (const auto& [path, actorId] : ActorServices) {
             ActorSystem->Send(actorId, new TEvents::TEvPoisonPill);
         }
         ActorSystem->Send(NodeProxyServiceActorId, new TEvents::TEvPoisonPill);
@@ -752,12 +755,15 @@ NMonitoring::TIndexMonPage* TAsyncHttpMon::RegisterIndexPage(const TString& path
 void TAsyncHttpMon::RegisterActorMonPage(const TActorMonPageInfo& pageInfo) {
     if (ActorSystem) {
         TActorMonPage* actorMonPage = static_cast<TActorMonPage*>(pageInfo.Page.Get());
-        auto actorId = ActorSystem->Register(
+        auto& actorId = ActorServices[pageInfo.Path];
+        if (actorId) {
+            ActorSystem->Send(new IEventHandle(TEvents::TSystem::Poison, 0, actorId, {}, nullptr, 0));
+        }
+        actorId = ActorSystem->Register(
             new THttpMonServiceLegacyActor(actorMonPage),
             TMailboxType::ReadAsFilled,
             ActorSystem->AppData<NKikimr::TAppData>()->UserPoolId);
         ActorSystem->Send(HttpProxyActorId, new NHttp::TEvHttpProxy::TEvRegisterHandler(pageInfo.Path, actorId));
-        ActorServices.push_back(actorId);
     }
 }
 

+ 1 - 1
ydb/core/mon/async_http_mon.h

@@ -41,7 +41,7 @@ protected:
 
     TMutex Mutex;
     std::vector<TActorMonPageInfo> ActorMonPages;
-    std::vector<TActorId> ActorServices;
+    THashMap<TString, TActorId> ActorServices;
 
     void RegisterActorMonPage(const TActorMonPageInfo& pageInfo);
 };