Icinga DB: Merge SendStatusUpdate into UpdateState

Previously, both funktions did related operations but had unclear and confusing
naming:
- UpdateState updated the icinga:{host,service}:state Redis keys.
- SendStatusUpdate sent a runtime update for the icinga:{host,service}:state.

This commit merges both functions into one with a new mode parameter. The
following modes are now supported:
- Volatile: Update the icinga:{host,service}:state Redis key.
- Full: Perform the volatile state update and in addition send a corresponding
  runtime update so that this state update gets written through to the
  persistent database by a running icingadb process.
- RuntimeOnly: Special mode for callers that can ensure that a volatile update
  for the current state was already performed but has to be upgraded to a full
  update.

refs #9063
This commit is contained in:
Julian Brost 2022-01-12 15:16:26 +01:00
parent 506b7d9a35
commit a6d6cb788e
2 changed files with 58 additions and 38 deletions

View File

@ -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<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
@ -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<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 +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)
@ -2072,8 +2087,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)
@ -2678,7 +2692,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);
}
}
@ -2686,7 +2700,7 @@ 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);
}
}

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);