Merge pull request #9252 from Icinga/bugfix/icingadb-state-history

Icinga DB: don't write state history for ack/downtime/host problem changes
This commit is contained in:
Julian Brost 2022-02-23 15:43:55 +01:00 committed by GitHub
commit ca37dbb9c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 70 deletions

View File

@ -84,14 +84,6 @@ void IcingaDB::ConfigStaticInitialize()
AcknowledgementClearedHandler(checkable, removedBy, changeTime);
});
Checkable::OnAcknowledgementSet.connect([](const Checkable::Ptr& checkable, const String& author, const String& comment, AcknowledgementType type, bool, bool persistent, double changeTime, double expiry, const MessageOrigin::Ptr&) {
IcingaDB::StateChangeHandler(checkable);
});
/* triggered when acknowledged host/service goes back to ok and when the acknowledgement gets deleted */
Checkable::OnAcknowledgementCleared.connect([](const Checkable::Ptr& checkable, const String&, double, const MessageOrigin::Ptr&) {
IcingaDB::StateChangeHandler(checkable);
});
/* triggered on create, update and delete objects */
ConfigObject::OnActiveChanged.connect([](const ConfigObject::Ptr& object, const Value&) {
IcingaDB::VersionChangedHandler(object);
@ -127,7 +119,7 @@ void IcingaDB::ConfigStaticInitialize()
});
Service::OnHostProblemChanged.connect([](const Service::Ptr& service, const CheckResult::Ptr&, const MessageOrigin::Ptr&) {
IcingaDB::StateChangeHandler(service);
IcingaDB::HostProblemChangedHandler(service);
});
Notification::OnUsersRawChangedWithOldValue.connect([](const Notification::Ptr& notification, const Value& oldValues, const Value& newValues) {
@ -1091,7 +1083,23 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
}
}
void IcingaDB::UpdateState(const Checkable::Ptr& checkable)
/**
* Update the state information of a checkable in Redis.
*
* What is updated exactly depends on the mode parameter:
* - Volatile: Update the volatile state information stored in icinga:host:state or icinga:service:state as well as
* the corresponding checksum stored in icinga:checksum:host:state or icinga:checksum:service:state.
* - RuntimeOnly: Write a runtime update to the icinga:runtime:state stream. It is up to the caller to ensure that
* identical volatile state information was already written before to avoid inconsistencies. This mode is only
* useful to upgrade a previous Volatile to a Full operation, otherwise Full should be used.
* - Full: Perform an update of all state information in Redis, that is updating the volatile information and sending
* a corresponding runtime update so that this state update gets written through to the persistent database by a
* running icingadb process.
*
* @param checkable State of this checkable is updated in Redis
* @param mode Mode of operation (StateUpdate::Volatile, StateUpdate::RuntimeOnly, or StateUpdate::Full)
*/
void IcingaDB::UpdateState(const Checkable::Ptr& checkable, StateUpdate mode)
{
if (!m_Rcon || !m_Rcon->IsConnected())
return;
@ -1101,9 +1109,34 @@ void IcingaDB::UpdateState(const Checkable::Ptr& checkable)
Dictionary::Ptr stateAttrs = SerializeState(checkable);
m_Rcon->FireAndForgetQuery({"HSET", m_PrefixConfigObject + objectType + ":state", objectKey, JsonEncode(stateAttrs)}, Prio::RuntimeStateSync);
m_Rcon->FireAndForgetQuery({"HSET", m_PrefixConfigCheckSum + objectType + ":state", objectKey, JsonEncode(new Dictionary({{"checksum", HashValue(stateAttrs)}}))}, Prio::RuntimeStateSync);
String redisStateKey = m_PrefixConfigObject + objectType + ":state";
String redisChecksumKey = m_PrefixConfigCheckSum + objectType + ":state";
String checksum = HashValue(stateAttrs);
if (mode & StateUpdate::Volatile) {
m_Rcon->FireAndForgetQueries({
{"HSET", redisStateKey, objectKey, JsonEncode(stateAttrs)},
{"HSET", redisChecksumKey, objectKey, JsonEncode(new Dictionary({{"checksum", checksum}}))},
}, Prio::RuntimeStateSync);
}
if (mode & StateUpdate::RuntimeOnly) {
ObjectLock olock(stateAttrs);
std::vector<String> streamadd({
"XADD", "icinga:runtime:state", "MAXLEN", "~", "1000000", "*",
"runtime_type", "upsert",
"redis_key", redisStateKey,
"checksum", checksum,
});
for (const Dictionary::Pair& kv : stateAttrs) {
streamadd.emplace_back(kv.first);
streamadd.emplace_back(IcingaToStreamValue(kv.second));
}
m_Rcon->FireAndForgetQuery(std::move(streamadd), Prio::RuntimeStateStream);
}
}
// Used to update a single object, used for runtime updates
@ -1120,16 +1153,7 @@ void IcingaDB::SendConfigUpdate(const ConfigObject::Ptr& object, bool runtimeUpd
CreateConfigUpdate(object, typeName, hMSets, runtimeUpdates, runtimeUpdate);
Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
if (checkable) {
String objectKey = GetObjectIdentifier(object);
Dictionary::Ptr state = SerializeState(checkable);
String checksum = HashValue(state);
m_Rcon->FireAndForgetQuery({"HSET", m_PrefixConfigObject + typeName + ":state", objectKey, JsonEncode(state)}, Prio::RuntimeStateSync);
m_Rcon->FireAndForgetQuery({"HSET", m_PrefixConfigCheckSum + typeName + ":state", objectKey, JsonEncode(new Dictionary({{"checksum", checksum}}))}, Prio::RuntimeStateSync);
if (runtimeUpdate) {
SendStatusUpdate(checkable);
}
UpdateState(checkable, runtimeUpdate ? StateUpdate::Full : StateUpdate::Volatile);
}
std::vector<std::vector<String> > transaction = {{"MULTI"}};
@ -1593,31 +1617,6 @@ unsigned short GetPreviousState(const Checkable::Ptr& checkable, const Service::
}
}
void IcingaDB::SendStatusUpdate(const Checkable::Ptr& checkable)
{
if (!m_Rcon || !m_Rcon->IsConnected())
return;
Host::Ptr host;
Service::Ptr service;
Dictionary::Ptr objectAttrs = SerializeState(checkable);
std::vector<String> streamadd({"XADD", "icinga:runtime:state", "MAXLEN", "~", "1000000", "*"});
ObjectLock olock(objectAttrs);
tie(host, service) = GetHostService(checkable);
objectAttrs->Set("checksum", HashValue(objectAttrs));
objectAttrs->Set("redis_key", service ? "icinga:service:state" : "icinga:host:state");
objectAttrs->Set("runtime_type", "upsert");
for (const Dictionary::Pair& kv : objectAttrs) {
streamadd.emplace_back(kv.first);
streamadd.emplace_back(IcingaToStreamValue(kv.second));
}
m_Rcon->FireAndForgetQuery(std::move(streamadd), Prio::RuntimeStateStream);
}
void IcingaDB::SendStateChange(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type)
{
if (!m_Rcon || !m_Rcon->IsConnected())
@ -1635,7 +1634,7 @@ void IcingaDB::SendStateChange(const ConfigObject::Ptr& object, const CheckResul
tie(host, service) = GetHostService(checkable);
SendStatusUpdate(checkable);
UpdateState(checkable, StateUpdate::RuntimeOnly);
int hard_state;
if (!cr) {
@ -1793,6 +1792,9 @@ void IcingaDB::SendStartedDowntime(const Downtime::Ptr& downtime)
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
/* Update checkable state as in_downtime may have changed. */
UpdateState(checkable, StateUpdate::Full);
std::vector<String> xAdd ({
"XADD", "icinga:history:stream:downtime", "*",
"downtime_id", GetObjectIdentifier(downtime),
@ -1878,6 +1880,9 @@ void IcingaDB::SendRemovedDowntime(const Downtime::Ptr& downtime)
if (downtime->GetTriggerTime() == 0)
return;
/* Update checkable state as in_downtime may have changed. */
UpdateState(checkable, StateUpdate::Full);
std::vector<String> xAdd ({
"XADD", "icinga:history:stream:downtime", "*",
"downtime_id", GetObjectIdentifier(downtime),
@ -2002,8 +2007,7 @@ void IcingaDB::SendAddedComment(const Comment::Ptr& comment)
}
m_Rcon->FireAndForgetQuery(std::move(xAdd), Prio::History);
UpdateState(checkable);
SendStatusUpdate(checkable);
UpdateState(checkable, StateUpdate::Full);
}
void IcingaDB::SendRemovedComment(const Comment::Ptr& comment)
@ -2071,8 +2075,7 @@ void IcingaDB::SendRemovedComment(const Comment::Ptr& comment)
}
m_Rcon->FireAndForgetQuery(std::move(xAdd), Prio::History);
UpdateState(checkable);
SendStatusUpdate(checkable);
UpdateState(checkable, StateUpdate::Full);
}
void IcingaDB::SendFlappingChange(const Checkable::Ptr& checkable, double changeTime, double flappingLastChange)
@ -2175,6 +2178,9 @@ void IcingaDB::SendAcknowledgementSet(const Checkable::Ptr& checkable, const Str
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
/* Update checkable state as is_acknowledged may have changed. */
UpdateState(checkable, StateUpdate::Full);
std::vector<String> xAdd ({
"XADD", "icinga:history:stream:acknowledgement", "*",
"environment_id", m_EnvironmentId,
@ -2229,6 +2235,9 @@ void IcingaDB::SendAcknowledgementCleared(const Checkable::Ptr& checkable, const
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
/* Update checkable state as is_acknowledged may have changed. */
UpdateState(checkable, StateUpdate::Full);
std::vector<String> xAdd ({
"XADD", "icinga:history:stream:acknowledgement", "*",
"environment_id", m_EnvironmentId,
@ -2582,15 +2591,6 @@ IcingaDB::UpdateObjectAttrs(const ConfigObject::Ptr& object, int fieldType,
//m_Rcon->FireAndForgetQuery({"HSET", keyPrefix + typeName, GetObjectIdentifier(object), JsonEncode(attrs)});
}
void IcingaDB::StateChangeHandler(const ConfigObject::Ptr& object)
{
auto checkable (dynamic_pointer_cast<Checkable>(object));
if (checkable) {
IcingaDB::StateChangeHandler(object, checkable->GetLastCheckResult(), checkable->GetStateType());
}
}
void IcingaDB::StateChangeHandler(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type)
{
for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
@ -2620,8 +2620,6 @@ void IcingaDB::VersionChangedHandler(const ConfigObject::Ptr& object)
void IcingaDB::DowntimeStartedHandler(const Downtime::Ptr& downtime)
{
StateChangeHandler(downtime->GetCheckable());
for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
rw->SendStartedDowntime(downtime);
}
@ -2629,8 +2627,6 @@ void IcingaDB::DowntimeStartedHandler(const Downtime::Ptr& downtime)
void IcingaDB::DowntimeRemovedHandler(const Downtime::Ptr& downtime)
{
StateChangeHandler(downtime->GetCheckable());
for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
rw->SendRemovedDowntime(downtime);
}
@ -2677,7 +2673,7 @@ void IcingaDB::FlappingChangeHandler(const Checkable::Ptr& checkable, double cha
void IcingaDB::NewCheckResultHandler(const Checkable::Ptr& checkable)
{
for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
rw->UpdateState(checkable);
rw->UpdateState(checkable, StateUpdate::Volatile);
rw->SendNextUpdate(checkable);
}
}
@ -2685,11 +2681,18 @@ void IcingaDB::NewCheckResultHandler(const Checkable::Ptr& checkable)
void IcingaDB::NextCheckChangedHandler(const Checkable::Ptr& checkable)
{
for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
rw->UpdateState(checkable);
rw->UpdateState(checkable, StateUpdate::Volatile);
rw->SendNextUpdate(checkable);
}
}
void IcingaDB::HostProblemChangedHandler(const Service::Ptr& service) {
for (auto& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
/* Host state changes affect is_handled and severity of services. */
rw->UpdateState(service, StateUpdate::Full);
}
}
void IcingaDB::AcknowledgementSetHandler(const Checkable::Ptr& checkable, const String& author, const String& comment, AcknowledgementType type, bool persistent, double changeTime, double expiry)
{
auto rws (ConfigType::GetObjectsByType<IcingaDB>());

View File

@ -57,6 +57,13 @@ private:
std::mutex m_Mutex;
};
enum StateUpdate
{
Volatile = 1ull << 0,
RuntimeOnly = 1ull << 1,
Full = Volatile | RuntimeOnly,
};
void OnConnectedHandler();
void PublishStatsTimerHandler();
@ -70,12 +77,11 @@ private:
std::vector<String> GetTypeDumpSignalKeys(const Type::Ptr& type);
void InsertObjectDependencies(const ConfigObject::Ptr& object, const String typeName, std::map<String, std::vector<String>>& hMSets,
std::vector<Dictionary::Ptr>& runtimeUpdates, bool runtimeUpdate);
void UpdateState(const Checkable::Ptr& checkable);
void UpdateState(const Checkable::Ptr& checkable, StateUpdate mode);
void SendConfigUpdate(const ConfigObject::Ptr& object, bool runtimeUpdate);
void CreateConfigUpdate(const ConfigObject::Ptr& object, const String type, std::map<String, std::vector<String>>& hMSets,
std::vector<Dictionary::Ptr>& runtimeUpdates, bool runtimeUpdate);
void SendConfigDelete(const ConfigObject::Ptr& object);
void SendStatusUpdate(const Checkable::Ptr& checkable);
void SendStateChange(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type);
void AddObjectDataToRuntimeUpdates(std::vector<Dictionary::Ptr>& runtimeUpdates, const String& objectKey,
const String& redisKey, const Dictionary::Ptr& data);
@ -130,7 +136,6 @@ private:
static String GetLowerCaseTypeNameDB(const ConfigObject::Ptr& obj);
static bool PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr& attributes, Dictionary::Ptr& checkSums);
static void StateChangeHandler(const ConfigObject::Ptr& object);
static void StateChangeHandler(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type);
static void VersionChangedHandler(const ConfigObject::Ptr& object);
static void DowntimeStartedHandler(const Downtime::Ptr& downtime);
@ -146,6 +151,7 @@ private:
static void FlappingChangeHandler(const Checkable::Ptr& checkable, double changeTime);
static void NewCheckResultHandler(const Checkable::Ptr& checkable);
static void NextCheckChangedHandler(const Checkable::Ptr& checkable);
static void HostProblemChangedHandler(const Service::Ptr& service);
static void AcknowledgementSetHandler(const Checkable::Ptr& checkable, const String& author, const String& comment, AcknowledgementType type, bool persistent, double changeTime, double expiry);
static void AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const String& removedBy, double changeTime);
static void NotificationUsersChangedHandler(const Notification::Ptr& notification, const Array::Ptr& oldValues, const Array::Ptr& newValues);