]> granicus.if.org Git - icinga2/commitdiff
Make sure comment/downtime legacy IDs are always unique (even in a cluster context).
authorGunnar Beutner <gunnar.beutner@netways.de>
Wed, 30 Jan 2013 12:02:20 +0000 (13:02 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Wed, 30 Jan 2013 12:02:20 +0000 (13:02 +0100)
lib/icinga/commentprocessor.cpp
lib/icinga/commentprocessor.h
lib/icinga/downtimeprocessor.cpp
lib/icinga/downtimeprocessor.h
lib/icinga/host.cpp
lib/icinga/service.cpp

index 92a9b2946ba1728754e6bb202f56cb20bbd035dc..d0485f0a12f55b0908d0e0fd2e36d8d00ef5fa66 100644 (file)
@@ -47,10 +47,6 @@ String CommentProcessor::AddComment(const DynamicObject::Ptr& owner,
        if (!comments)
                comments = boost::make_shared<Dictionary>();
 
-       int legacy_id = m_NextCommentID;
-       m_NextCommentID++;
-       comment->Set("legacy_id", legacy_id);
-
        String id = Utility::NewUUID();
        comments->Set(id, comment);
        owner->Set("comments", comments);
@@ -80,7 +76,12 @@ void CommentProcessor::RemoveComment(const String& id)
 
 String CommentProcessor::GetIDFromLegacyID(int id)
 {
-       return Convert::ToString(id);
+       map<int, String>::iterator it = m_LegacyCommentCache.find(id);
+
+       if (it == m_LegacyCommentCache.end())
+               throw_exception(invalid_argument("Invalid legacy comment ID specified."));
+
+       return it->second;
 }
 
 DynamicObject::Ptr CommentProcessor::GetOwnerByCommentID(const String& id)
@@ -124,15 +125,29 @@ void CommentProcessor::AddCommentsToCache(const DynamicObject::Ptr& owner)
        String id;
        Dictionary::Ptr comment;
        BOOST_FOREACH(tie(id, comment), comments) {
-               if (comment->Contains("legacy_id")) {
-                       int legacy_id = comment->Get("legacy_id");
+               int legacy_id;
+
+               if (!comment->Contains("legacy_id")) {
+                       legacy_id = m_NextCommentID;
+                       m_NextCommentID++;
+                       comment->Set("legacy_id", legacy_id);
+               } else {
+                       legacy_id = comment->Get("legacy_id");
+               }
+
+               if (legacy_id >= m_NextCommentID)
+                       m_NextCommentID = legacy_id + 1;
 
-                       m_LegacyCommentCache[legacy_id] = id;
+               if (m_LegacyCommentCache.find(legacy_id) != m_LegacyCommentCache.end()) {
+                       /* The legacy_id is already in use by another comment;
+                        * this shouldn't usually happen - assign it a new ID */
 
-                       if (legacy_id > m_NextCommentID)
-                               m_NextCommentID = legacy_id;
+                       legacy_id = m_NextCommentID;
+                       m_NextCommentID++;
+                       comment->Set("legacy_id", legacy_id);
                }
 
+               m_LegacyCommentCache[legacy_id] = id;
                m_CommentCache[id] = owner;
        }
 }
index 744e3e1a0d5558277569ce7a785a4a7df5b1c9d3..32ad573638d7d03c4eb698620497626bd65def04 100644 (file)
@@ -53,6 +53,7 @@ public:
        static Dictionary::Ptr GetCommentByID(const String& id);
 
        static void InvalidateCommentCache(void);
+       static void ValidateCommentCache(void);
 
 private:
        static int m_NextCommentID;
@@ -64,7 +65,6 @@ private:
        CommentProcessor(void);
 
        static void AddCommentsToCache(const DynamicObject::Ptr& owner);
-       static void ValidateCommentCache(void);
 };
 
 }
index 08fdeb1c82f317de38a1c1371bc6852f1d303928..a62931f01411d7f4fb6fb2768c9dbfe236649a0a 100644 (file)
@@ -52,10 +52,6 @@ String DowntimeProcessor::AddDowntime(const DynamicObject::Ptr& owner,
        if (!downtimes)
                downtimes = boost::make_shared<Dictionary>();
 
-       int legacy_id = m_NextDowntimeID;
-       m_NextDowntimeID++;
-       downtime->Set("legacy_id", legacy_id);
-
        String id = Utility::NewUUID();
        downtimes->Set(id, downtime);
        owner->Set("downtimes", downtimes);
@@ -77,7 +73,12 @@ void DowntimeProcessor::RemoveDowntime(const String& id)
 
 String DowntimeProcessor::GetIDFromLegacyID(int id)
 {
-       return Convert::ToString(id);
+       map<int, String>::iterator it = m_LegacyDowntimeCache.find(id);
+
+       if (it == m_LegacyDowntimeCache.end())
+               throw_exception(invalid_argument("Invalid legacy downtime ID specified."));
+
+       return it->second;
 }
 
 DynamicObject::Ptr DowntimeProcessor::GetOwnerByDowntimeID(const String& id)
@@ -140,15 +141,28 @@ void DowntimeProcessor::AddDowntimesToCache(const DynamicObject::Ptr& owner)
        String id;
        Dictionary::Ptr downtime;
        BOOST_FOREACH(tie(id, downtime), downtimes) {
-               if (downtime->Contains("legacy_id")) {
-                       int legacy_id = downtime->Get("legacy_id");
+               int legacy_id;
+
+               if (!downtime->Contains("legacy_id")) {
+                       legacy_id = m_NextDowntimeID;
+                       m_NextDowntimeID++;
+                       downtime->Set("legacy_id", legacy_id);
+               } else {
+                       legacy_id = downtime->Get("legacy_id");
+               }
 
-                       m_LegacyDowntimeCache[legacy_id] = id;
+               if (legacy_id >= m_NextDowntimeID)
+                       m_NextDowntimeID = legacy_id + 1;
 
-                       if (legacy_id > m_NextDowntimeID)
-                               m_NextDowntimeID = legacy_id;
+               if (m_LegacyDowntimeCache.find(legacy_id) != m_LegacyDowntimeCache.end()) {
+                       /* The legacy_id is already in use by another downtime;
+                        * this shouldn't usually happen - assign it a new ID. */
+                       legacy_id = m_NextDowntimeID;
+                       m_NextDowntimeID++;
+                       downtime->Set("legacy_id", legacy_id);
                }
 
+               m_LegacyDowntimeCache[legacy_id] = id;
                m_DowntimeCache[id] = owner;
        }
 }
index 9c2090d579fe754a5b9d1d1603a7317bd6075366..6612a63ba38a999b28c3a77d5fb8c6a68f680409 100644 (file)
@@ -47,6 +47,7 @@ public:
        static bool IsDowntimeActive(const Dictionary::Ptr& downtime);
 
        static void InvalidateDowntimeCache(void);
+       static void ValidateDowntimeCache(void);
 
 private:
        static int m_NextDowntimeID;
@@ -58,7 +59,6 @@ private:
        DowntimeProcessor(void);
 
        static void AddDowntimesToCache(const DynamicObject::Ptr& owner);
-       static void ValidateDowntimeCache(void);
 };
 
 }
index 6bf67d07ff97018837f3f994ecd07e943f056c82..ec0a1daeb33551da4897211d531dc8e96e2e7b06 100644 (file)
@@ -120,11 +120,15 @@ Dictionary::Ptr Host::GetMacros(void) const
 
 Dictionary::Ptr Host::GetDowntimes(void) const
 {
+       DowntimeProcessor::ValidateDowntimeCache();
+
        return Get("downtimes");
 }
 
 Dictionary::Ptr Host::GetComments(void) const
 {
+       CommentProcessor::ValidateCommentCache();
+
        return Get("comments");
 }
 
index e7815f4cc6ddfbd422a34eba46862c5b813903a3..b9848bbd0495b524ce3ca3d3b1e704afe5681ce0 100644 (file)
@@ -120,11 +120,15 @@ Dictionary::Ptr Service::GetMacros(void) const
 
 Dictionary::Ptr Service::GetDowntimes(void) const
 {
+       DowntimeProcessor::ValidateDowntimeCache();
+
        return Get("downtimes");
 }
 
 Dictionary::Ptr Service::GetComments(void) const
 {
+       CommentProcessor::ValidateCommentCache();
+
        return Get("comments");
 }