diff --git a/lib/icingadb/icingadb-objects.cpp b/lib/icingadb/icingadb-objects.cpp index 0e5135c58..aac4a3104 100644 --- a/lib/icingadb/icingadb-objects.cpp +++ b/lib/icingadb/icingadb-objects.cpp @@ -1091,7 +1091,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 +1117,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 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 @@ -1128,7 +1169,7 @@ void IcingaDB::SendConfigUpdate(const ConfigObject::Ptr& object, bool runtimeUpd m_Rcon->FireAndForgetQuery({"HSET", m_PrefixConfigCheckSum + typeName + ":state", objectKey, JsonEncode(new Dictionary({{"checksum", checksum}}))}, Prio::RuntimeStateSync); if (runtimeUpdate) { - SendStatusUpdate(checkable); + UpdateState(checkable, StateUpdate::RuntimeOnly); } } @@ -1593,31 +1634,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 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 +1651,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) { @@ -2002,8 +2018,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 +2086,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) @@ -2677,7 +2691,7 @@ void IcingaDB::FlappingChangeHandler(const Checkable::Ptr& checkable, double cha void IcingaDB::NewCheckResultHandler(const Checkable::Ptr& checkable) { for (auto& rw : ConfigType::GetObjectsByType()) { - rw->UpdateState(checkable); + rw->UpdateState(checkable, StateUpdate::Volatile); rw->SendNextUpdate(checkable); } } @@ -2685,7 +2699,7 @@ void IcingaDB::NewCheckResultHandler(const Checkable::Ptr& checkable) void IcingaDB::NextCheckChangedHandler(const Checkable::Ptr& checkable) { for (auto& rw : ConfigType::GetObjectsByType()) { - rw->UpdateState(checkable); + rw->UpdateState(checkable, StateUpdate::Volatile); rw->SendNextUpdate(checkable); } } diff --git a/lib/icingadb/icingadb.hpp b/lib/icingadb/icingadb.hpp index 7a392a2ad..f0b62047f 100644 --- a/lib/icingadb/icingadb.hpp +++ b/lib/icingadb/icingadb.hpp @@ -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 GetTypeDumpSignalKeys(const Type::Ptr& type); void InsertObjectDependencies(const ConfigObject::Ptr& object, const String typeName, std::map>& hMSets, std::vector& 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>& hMSets, std::vector& 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& runtimeUpdates, const String& objectKey, const String& redisKey, const Dictionary::Ptr& data);