]> granicus.if.org Git - icinga2/commitdiff
Only activate HARunOnce objects once there's a cluster connection
authorGunnar Beutner <gunnar.beutner@netways.de>
Wed, 11 May 2016 10:50:08 +0000 (12:50 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 12 May 2016 09:11:02 +0000 (11:11 +0200)
fixes #11765

lib/base/application.cpp
lib/base/application.hpp
lib/base/configobject.cpp
lib/cli/daemoncommand.cpp
lib/remote/apilistener.hpp
lib/remote/authority.cpp

index b0cdc1286dc59f8d051e2cf51e4b440f6d2eacf1..c9e2c7c334d54f9fc8109a92de3073418f9ce299 100644 (file)
@@ -60,6 +60,7 @@ static bool l_InExceptionHandler = false;
 int Application::m_ArgC;
 char **Application::m_ArgV;
 double Application::m_StartTime;
+double Application::m_MainTime;
 bool Application::m_ScriptDebuggerEnabled = false;
 
 /**
@@ -885,6 +886,8 @@ int Application::Run(void)
                return EXIT_FAILURE;
        }
 
+       SetMainTime(Utility::GetTime());
+
        return Main();
 }
 
@@ -1364,6 +1367,16 @@ void Application::SetStartTime(double ts)
        m_StartTime = ts;
 }
 
+double Application::GetMainTime(void)
+{
+       return m_MainTime;
+}
+
+void Application::SetMainTime(double ts)
+{
+       m_MainTime = ts;
+}
+
 bool Application::GetScriptDebuggerEnabled(void)
 {
        return m_ScriptDebuggerEnabled;
index d030eb2a7cf6b52bca83bb41558610cd96c08436..2174d3e03dede0f9fcfa8328ea56c2c7006ae98a 100644 (file)
@@ -134,6 +134,9 @@ public:
        static double GetStartTime(void);
        static void SetStartTime(double ts);
 
+       static double GetMainTime(void);
+       static void SetMainTime(double ts);
+
        static bool GetScriptDebuggerEnabled(void);
        static void SetScriptDebuggerEnabled(bool enabled);
 
@@ -167,6 +170,7 @@ private:
        static bool m_Debugging; /**< Whether debugging is enabled. */
        static LogSeverity m_DebuggingSeverity; /**< Whether debugging severity is set. */
        static double m_StartTime;
+       static double m_MainTime;
        static bool m_ScriptDebuggerEnabled;
 
 #ifndef _WIN32
index cfb099480dd39d137f0e8182812ea6eee17162e3..1cb6e3f3c5403ee72aef3ab4aec56b9a7268945c 100644 (file)
@@ -396,7 +396,8 @@ void ConfigObject::Activate(bool runtimeCreated)
                SetActive(true, true);
        }
 
-       SetAuthority(true);
+       if (GetHAMode() == HARunEverywhere)
+               SetAuthority(true);
 
        NotifyActive();
 }
index f650af3a4c91cb0dcb23e654cfa5cdcde3974265..be32b22d63b37548c745a159055735e6b8433752 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "cli/daemoncommand.hpp"
 #include "cli/daemonutility.hpp"
+#include "remote/apilistener.hpp"
 #include "config/configcompiler.hpp"
 #include "config/configcompilercontext.hpp"
 #include "config/configitembuilder.hpp"
@@ -302,5 +303,8 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::strin
        sigaction(SIGHUP, &sa, NULL);
 #endif /* _WIN32 */
 
+       /* update object authority */
+       ApiListener::UpdateObjectAuthorityAsync();
+
        return Application::GetInstance()->Run();
 }
index 4c860f89bbbf615e95db3ee11618790f4cedf226..d4cb5e540574160001cb85551da0c3ad0167c21e 100644 (file)
@@ -97,6 +97,9 @@ public:
        static Value ConfigDeleteObjectAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
        
        static Value HelloAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
+
+       static void UpdateObjectAuthorityAsync(void);
+
 protected:
        virtual void OnConfigLoaded(void) override;
        virtual void OnAllConfigLoaded(void) override;
index 37a244ee40b2ccb45e99acada2b8747b320cb6f9..cb81fa92745bce6c45e322d218ff1219479bd0a2 100644 (file)
@@ -36,42 +36,60 @@ static bool ObjectNameLessComparer(const ConfigObject::Ptr& a, const ConfigObjec
 
 static void AuthorityTimerHandler(void)
 {
-       ApiListener::Ptr listener = ApiListener::GetInstance();
+       Zone::Ptr my_zone = Zone::GetLocalZone();
 
-       if (!listener || !listener->IsActive())
-               return;
+       std::vector<Endpoint::Ptr> endpoints;
+       Endpoint::Ptr my_endpoint;
 
-       Zone::Ptr my_zone = Zone::GetLocalZone();
-       if (!my_zone)
-               return;
+       if (my_zone) {
+               my_endpoint = Endpoint::GetLocalEndpoint();
 
-       Endpoint::Ptr my_endpoint = Endpoint::GetLocalEndpoint();
+               int num_total = 0;
 
-       std::vector<Endpoint::Ptr> endpoints;
-       BOOST_FOREACH(const Endpoint::Ptr& endpoint, my_zone->GetEndpoints()) {
-               if (!endpoint->GetConnected() && endpoint != my_endpoint)
-                       continue;
+               BOOST_FOREACH(const Endpoint::Ptr& endpoint, my_zone->GetEndpoints()) {
+                       num_total++;
 
-               endpoints.push_back(endpoint);
-       }
+                       if (endpoint != my_endpoint && !endpoint->GetConnected())
+                               continue;
+
+                       endpoints.push_back(endpoint);
+               }
+
+               double mainTime = Application::GetMainTime();
 
-       std::sort(endpoints.begin(), endpoints.end(), ObjectNameLessComparer);
+               if (num_total > 1 && endpoints.size() <= 1 && (mainTime == 0 || Utility::GetTime() - mainTime < 60))
+                       return;
+
+               std::sort(endpoints.begin(), endpoints.end(), ObjectNameLessComparer);
+       }
 
        BOOST_FOREACH(const ConfigType::Ptr& type, ConfigType::GetTypes()) {
                BOOST_FOREACH(const ConfigObject::Ptr& object, type->GetObjects()) {
-                       Endpoint::Ptr endpoint = endpoints[Utility::SDBM(object->GetName()) % endpoints.size()];
+                       if (object->GetHAMode() != HARunOnce)
+                               continue;
+
+                       bool authority;
+
+                       if (!my_zone)
+                               authority = true;
+                       else
+                               authority = endpoints[Utility::SDBM(object->GetName()) % endpoints.size()] == my_endpoint;
 
-                       if (object->GetHAMode() == HARunOnce)
-                               object->SetAuthority(endpoint == my_endpoint);
+                       object->SetAuthority(authority);
                }
        }
 }
 
+void ApiListener::UpdateObjectAuthorityAsync(void)
+{
+       l_AuthorityTimer->Reschedule(0);
+}
+
 static void StaticInitialize(void)
 {
        l_AuthorityTimer = new Timer();
        l_AuthorityTimer->OnTimerExpired.connect(boost::bind(&AuthorityTimerHandler));
-       l_AuthorityTimer->SetInterval(30);
+       l_AuthorityTimer->SetInterval(15);
        l_AuthorityTimer->Start();
 }