Merge pull request #7699 from Icinga/bugfix/icingadb-hash-arrays-7691

Get rid of IcingaDB#CalculateCheckSum*()
This commit is contained in:
Noah Hilverling 2019-12-06 16:37:24 +01:00 committed by GitHub
commit 5e23414197
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 62 deletions

View File

@ -368,7 +368,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
{
String objectKey = GetObjectIdentifier(object);
CustomVarObject::Ptr customVarObject = dynamic_pointer_cast<CustomVarObject>(object);
String envId = CalculateCheckSumString(GetEnvironment());
String envId = SHA1(GetEnvironment());
auto* configUpdates (runtimeUpdate ? &publishes["icinga:config:update"] : nullptr);
if (customVarObject) {
@ -390,7 +390,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
configUpdates->emplace_back("customvar:" + kv.first);
}
String id = CalculateCheckSumArray(new Array({envId, kv.first, objectKey}));
String id = HashValue(new Array({envId, kv.first, objectKey}));
typeCvs.emplace_back(id);
typeCvs.emplace_back(JsonEncode(new Dictionary({{"object_id", objectKey}, {"environment_id", envId}, {"customvar_id", kv.first}})));
@ -410,7 +410,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
String iconImage = checkable->GetIconImage();
if (!actionUrl.IsEmpty()) {
auto& actionUrls (hMSets[m_PrefixConfigObject + "action_url"]);
actionUrls.emplace_back(CalculateCheckSumArray(new Array({envId, actionUrl})));
actionUrls.emplace_back(HashValue(new Array({envId, actionUrl})));
actionUrls.emplace_back(JsonEncode(new Dictionary({{"environment_id", envId}, {"action_url", actionUrl}})));
if (configUpdates) {
@ -419,7 +419,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
}
if (!notesUrl.IsEmpty()) {
auto& notesUrls (hMSets[m_PrefixConfigObject + "notes_url"]);
notesUrls.emplace_back(CalculateCheckSumArray(new Array({envId, notesUrl})));
notesUrls.emplace_back(HashValue(new Array({envId, notesUrl})));
notesUrls.emplace_back(JsonEncode(new Dictionary({{"environment_id", envId}, {"notes_url", notesUrl}})));
if (configUpdates) {
@ -428,7 +428,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
}
if (!iconImage.IsEmpty()) {
auto& iconImages (hMSets[m_PrefixConfigObject + "icon_image"]);
iconImages.emplace_back(CalculateCheckSumArray(new Array({envId, iconImage})));
iconImages.emplace_back(HashValue(new Array({envId, iconImage})));
iconImages.emplace_back(JsonEncode(new Dictionary({{"environment_id", envId}, {"icon_image", iconImage}})));
if (configUpdates) {
@ -460,7 +460,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
for (auto& group : groups) {
String groupId = GetObjectIdentifier((*getGroup)(group));
String id = CalculateCheckSumArray(new Array({envId, groupId, objectKey}));
String id = HashValue(new Array({envId, groupId, objectKey}));
members.emplace_back(id);
members.emplace_back(JsonEncode(new Dictionary({{"object_id", objectKey}, {"environment_id", envId}, {"group_id", groupId}})));
@ -487,10 +487,10 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
rangeIds->Reserve(ranges->GetLength());
for (auto& kv : ranges) {
String rangeId = CalculateCheckSumArray(new Array({envId, kv.first, kv.second}));
String rangeId = HashValue(new Array({envId, kv.first, kv.second}));
rangeIds->Add(rangeId);
String id = CalculateCheckSumArray(new Array({envId, rangeId, objectKey}));
String id = HashValue(new Array({envId, rangeId, objectKey}));
typeRanges.emplace_back(id);
typeRanges.emplace_back(JsonEncode(new Dictionary({{"environment_id", envId}, {"timeperiod_id", objectKey}, {"range_key", kv.first}, {"range_value", kv.second}})));
@ -518,7 +518,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
String includeId = GetObjectIdentifier((*getInclude)(include.Get<String>()));
includeChecksums->Add(includeId);
String id = CalculateCheckSumArray(new Array({envId, includeId, objectKey}));
String id = HashValue(new Array({envId, includeId, objectKey}));
includs.emplace_back(id);
includs.emplace_back(JsonEncode(new Dictionary({{"environment_id", envId}, {"timeperiod_id", objectKey}, {"include_id", includeId}})));
@ -546,7 +546,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
String excludeId = GetObjectIdentifier((*getExclude)(exclude.Get<String>()));
excludeChecksums->Add(excludeId);
String id = CalculateCheckSumArray(new Array({envId, excludeId, objectKey}));
String id = HashValue(new Array({envId, excludeId, objectKey}));
excluds.emplace_back(id);
excluds.emplace_back(JsonEncode(new Dictionary({{"environment_id", envId}, {"timeperiod_id", objectKey}, {"exclude_id", excludeId}})));
@ -570,7 +570,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
for (auto& parent : parentsRaw) {
String parentId = GetObjectIdentifier(parent);
String id = CalculateCheckSumArray(new Array({envId, parentId, objectKey}));
String id = HashValue(new Array({envId, parentId, objectKey}));
parnts.emplace_back(id);
parnts.emplace_back(JsonEncode(new Dictionary({{"zone_id", objectKey}, {"environment_id", envId}, {"parent_id", parentId}})));
@ -603,7 +603,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
for (auto& group : groups) {
String groupId = GetObjectIdentifier((*getGroup)(group));
String id = CalculateCheckSumArray(new Array({envId, groupId, objectKey}));
String id = HashValue(new Array({envId, groupId, objectKey}));
members.emplace_back(id);
members.emplace_back(JsonEncode(new Dictionary({{"user_id", objectKey}, {"environment_id", envId}, {"group_id", groupId}})));
@ -637,7 +637,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
for (auto& user : users) {
String userId = GetObjectIdentifier(user);
String id = CalculateCheckSumArray(new Array({envId, userId, objectKey}));
String id = HashValue(new Array({envId, userId, objectKey}));
usrs.emplace_back(id);
usrs.emplace_back(JsonEncode(new Dictionary({{"notification_id", objectKey}, {"environment_id", envId}, {"user_id", userId}})));
@ -659,7 +659,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
auto groupMembers = usergroup->GetMembers();
std::copy(groupMembers.begin(), groupMembers.end(), std::inserter(allUsers, allUsers.begin()));
String id = CalculateCheckSumArray(new Array({envId, "usergroup", usergroupId, objectKey}));
String id = HashValue(new Array({envId, "usergroup", usergroupId, objectKey}));
groups.emplace_back(id);
groups.emplace_back(JsonEncode(new Dictionary({{"notification_id", objectKey}, {"environment_id", envId}, {"usergroup_id", usergroupId}})));
@ -675,7 +675,7 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
for (auto& user : allUsers) {
String userId = GetObjectIdentifier(user);
String id = CalculateCheckSumArray(new Array({envId, "user", userId, objectKey}));
String id = HashValue(new Array({envId, "user", userId, objectKey}));
notificationRecipients.emplace_back(id);
notificationRecipients.emplace_back(JsonEncode(new Dictionary({{"notification_id", objectKey}, {"environment_id", envId}, {"user_id", userId}})));
@ -847,8 +847,8 @@ void IcingaDB::SendConfigUpdate(const ConfigObject::Ptr& object, bool runtimeUpd
// for IcingaDB.
bool IcingaDB::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr& attributes, Dictionary::Ptr& checksums)
{
attributes->Set("name_checksum", CalculateCheckSumString(object->GetName()));
attributes->Set("environment_id", CalculateCheckSumString(GetEnvironment()));
attributes->Set("name_checksum", SHA1(object->GetName()));
attributes->Set("environment_id", SHA1(GetEnvironment()));
attributes->Set("name", object->GetName());
Zone::Ptr ObjectsZone = static_pointer_cast<Zone>(object->GetZone());
@ -923,11 +923,11 @@ bool IcingaDB::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr& a
String notesUrl = checkable->GetNotesUrl();
String iconImage = checkable->GetIconImage();
if (!actionUrl.IsEmpty())
attributes->Set("action_url_id", CalculateCheckSumArray(new Array({CalculateCheckSumString(GetEnvironment()), actionUrl})));
attributes->Set("action_url_id", HashValue(new Array({SHA1(GetEnvironment()), actionUrl})));
if (!notesUrl.IsEmpty())
attributes->Set("notes_url_id", CalculateCheckSumArray(new Array({CalculateCheckSumString(GetEnvironment()), notesUrl})));
attributes->Set("notes_url_id", HashValue(new Array({SHA1(GetEnvironment()), notesUrl})));
if (!iconImage.IsEmpty())
attributes->Set("icon_image_id", CalculateCheckSumArray(new Array({CalculateCheckSumString(GetEnvironment()), iconImage})));
attributes->Set("icon_image_id", HashValue(new Array({SHA1(GetEnvironment()), iconImage})));
Host::Ptr host;
@ -1762,7 +1762,7 @@ Dictionary::Ptr IcingaDB::SerializeState(const Checkable::Ptr& checkable)
tie(host, service) = GetHostService(checkable);
attrs->Set("id", GetObjectIdentifier(checkable));;
attrs->Set("environment_id", CalculateCheckSumString(GetEnvironment()));
attrs->Set("environment_id", SHA1(GetEnvironment()));
attrs->Set("state_type", checkable->HasBeenChecked() ? checkable->GetStateType() : StateTypeHard);
// TODO: last_hard/soft_state should be "previous".

View File

@ -75,44 +75,8 @@ String IcingaDB::GetObjectIdentifier(const ConfigObject::Ptr& object)
return HashValue((Array::Ptr)new Array({GetEnvironment(), object->GetName()}));
}
String IcingaDB::CalculateCheckSumString(const String& str)
{
return SHA1(str);
}
String IcingaDB::CalculateCheckSumArray(const Array::Ptr& arr)
{
/* Ensure that checksums happen in a defined order. */
Array::Ptr tmpArr = arr->ShallowClone();
tmpArr->Sort();
return SHA1(PackObject(tmpArr));
}
String IcingaDB::CalculateCheckSumProperties(const ConfigObject::Ptr& object, const std::set<String>& propertiesBlacklist)
{
//TODO: consider precision of 6 for double values; use specific config fields for hashing?
return HashValue(object, propertiesBlacklist);
}
static const std::set<String> metadataWhitelist ({"package", "source_location", "templates"});
String IcingaDB::CalculateCheckSumMetadata(const ConfigObject::Ptr& object)
{
return HashValue(object, metadataWhitelist, true);
}
String IcingaDB::CalculateCheckSumVars(const CustomVarObject::Ptr& object)
{
Dictionary::Ptr vars = object->GetVars();
if (!vars)
return HashValue(Empty);
return HashValue(vars);
}
/**
* Prepare object's custom vars for being written to Redis
*

View File

@ -83,11 +83,6 @@ private:
static String GetObjectIdentifier(const ConfigObject::Ptr& object);
static String GetEnvironment();
static String CalculateCheckSumString(const String& str);
static String CalculateCheckSumArray(const Array::Ptr& arr);
static String CalculateCheckSumProperties(const ConfigObject::Ptr& object, const std::set<String>& propertiesBlacklist);
static String CalculateCheckSumMetadata(const ConfigObject::Ptr& object);
static String CalculateCheckSumVars(const CustomVarObject::Ptr& object);
static Dictionary::Ptr SerializeVars(const CustomVarObject::Ptr& object);
static String HashValue(const Value& value);