icinga2/lib/remote/authority.cpp
Michael Friedrich 149f640fd8 Improve DB IDO HA failover behaviour
- Decrease Object Authority updates to 10s (was 30s)
- Decrease failover timeout to 30s (was 60s)
- Decrease cold startup (after (re)start) with no OA updates to 30s (was 60s)
- Immediately connect on Resume()
- Fix query priority which got broken with #6970
- Add more logging when a failover is in progress

```
[2019-03-29 16:13:53 +0100] information/IdoMysqlConnection: Last update by endpoint 'master1' was 8.33246s ago (< failover timeout of 30s). Retrying.

[2019-03-29 16:14:23 +0100] information/IdoMysqlConnection: Last update by endpoint 'master1' was 38.3288s ago. Taking over 'ido-mysql' in HA zone 'master'.
```

- Add more logging for reconnect and disconnect handling
- Add 'last_failover' attribute to IDO*Connection objects

refs #6970
2019-04-01 08:50:00 +02:00

80 lines
2.1 KiB
C++

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "remote/zone.hpp"
#include "remote/apilistener.hpp"
#include "base/configtype.hpp"
#include "base/utility.hpp"
#include "base/convert.hpp"
using namespace icinga;
void ApiListener::UpdateObjectAuthority()
{
ApiListener::Ptr instance = ApiListener::GetInstance();
if (!instance)
return;
Log(LogNotice, "ApiListener")
<< "Updating object authority for objects at endpoint '" << instance->GetIdentity() << "'.";
Zone::Ptr my_zone = Zone::GetLocalZone();
std::vector<Endpoint::Ptr> endpoints;
Endpoint::Ptr my_endpoint;
if (my_zone) {
my_endpoint = Endpoint::GetLocalEndpoint();
int num_total = 0;
for (const Endpoint::Ptr& endpoint : my_zone->GetEndpoints()) {
num_total++;
if (endpoint != my_endpoint && !endpoint->GetConnected())
continue;
endpoints.push_back(endpoint);
}
double mainTime = Application::GetMainTime();
/* 30 seconds cold startup, don't update any authority to give the secondary endpoint time to reconnect. */
if (num_total > 1 && endpoints.size() <= 1 && (mainTime == 0 || Utility::GetTime() - mainTime < 30))
return;
std::sort(endpoints.begin(), endpoints.end(),
[](const ConfigObject::Ptr& a, const ConfigObject::Ptr& b) {
return a->GetName() < b->GetName();
}
);
}
for (const Type::Ptr& type : Type::GetAllTypes()) {
auto *dtype = dynamic_cast<ConfigType *>(type.get());
if (!dtype)
continue;
for (const ConfigObject::Ptr& object : dtype->GetObjects()) {
if (!object->IsActive() || object->GetHAMode() != HARunOnce)
continue;
bool authority;
if (!my_zone)
authority = true;
else
authority = endpoints[Utility::SDBM(object->GetName()) % endpoints.size()] == my_endpoint;
#ifdef I2_DEBUG
// //Enable on demand, causes heavy logging on each run.
// Log(LogDebug, "ApiListener")
// << "Setting authority '" << Convert::ToString(authority) << "' for object '" << object->GetName() << "' of type '" << object->GetReflectionType()->GetName() << "'.";
#endif /* I2_DEBUG */
object->SetAuthority(authority);
}
}
}