]> granicus.if.org Git - icinga2/commitdiff
Replace existing queries with ExecuteQuery() calls
authorGunnar Beutner <gunnar.beutner@icinga.com>
Wed, 22 Mar 2017 08:14:01 +0000 (09:14 +0100)
committerMichael Friedrich <michael.friedrich@icinga.com>
Wed, 29 Mar 2017 08:17:03 +0000 (10:17 +0200)
refs #4991

lib/redis/rediswriter-config.cpp
lib/redis/rediswriter.cpp

index a01af7a0222f49381cc37a6996dd43930f6e8367..da4563bf1cb9ed64e81b7098ae9ca946cf95df04 100644 (file)
@@ -61,25 +61,7 @@ void RedisWriter::UpdateAllConfigObjects(void)
        AssertOnWorkQueue();
 
        //TODO: "Publish" the config dump by adding another event, globally or by object
-       redisReply *reply1 = reinterpret_cast<redisReply *>(redisCommand(m_Context, "MULTI"));
-
-       if (!reply1) {
-               redisFree(m_Context);
-               m_Context = NULL;
-               return;
-       }
-
-       if (reply1->type == REDIS_REPLY_ERROR) {
-               Log(LogInformation, "RedisWriter")
-                   << "MULTI: " << reply1->str;
-       }
-
-       if (reply1->type == REDIS_REPLY_ERROR) {
-               freeReplyObject(reply1);
-               return;
-       }
-
-       freeReplyObject(reply1);
+       ExecuteQuery({ "MULTI" });
 
        for (const Type::Ptr& type : Type::GetAllTypes()) {
                if (!ConfigObject::TypeInstance->IsAssignableFrom(type))
@@ -88,25 +70,7 @@ void RedisWriter::UpdateAllConfigObjects(void)
                String typeName = type->GetName();
 
                /* replace into aka delete insert is faster than a full diff */
-               redisReply *reply2 = reinterpret_cast<redisReply *>(redisCommand(m_Context, "DEL icinga:config:%s icinga:config:%s:checksum, icinga:status:%s", typeName.CStr(), typeName.CStr(), typeName.CStr()));
-
-               if (!reply2) {
-                       redisFree(m_Context);
-                       m_Context = NULL;
-                       return;
-               }
-
-               if (reply2->type == REDIS_REPLY_ERROR) {
-                       Log(LogInformation, "RedisWriter")
-                           << "DEL icinga:config:" << typeName << " icinga:status:" << typeName << ": " << reply2->str;
-               }
-
-               if (reply2->type == REDIS_REPLY_ERROR) {
-                       freeReplyObject(reply2);
-                       return;
-               }
-
-               freeReplyObject(reply2);
+               ExecuteQuery({ "DEL", "icinga:config:" + typeName, "icinga:config:" + typeName + ":checksum", "icinga:status:" + typeName });
 
                /* fetch all objects and dump them */
                ConfigType *ctype = dynamic_cast<ConfigType *>(type.get());
@@ -118,46 +82,10 @@ void RedisWriter::UpdateAllConfigObjects(void)
                }
 
                /* publish config type dump finished */
-               redisReply *reply3 = reinterpret_cast<redisReply *>(redisCommand(m_Context, "PUBLISH icinga:config:dump %s", typeName.CStr()));
-
-               if (!reply3) {
-                       redisFree(m_Context);
-                       m_Context = NULL;
-                       return;
-               }
-
-               if (reply3->type == REDIS_REPLY_ERROR) {
-                       Log(LogInformation, "RedisWriter")
-                           << "PUBLISH icinga:dump:config:dump " << typeName << ": " << reply3->str;
-               }
-
-               if (reply3->type == REDIS_REPLY_ERROR) {
-                       freeReplyObject(reply3);
-                       return;
-               }
-
-               freeReplyObject(reply3);
+               ExecuteQuery({ "PUBLISH", "icinga:config:dump", typeName });
        }
 
-       redisReply *reply3 = reinterpret_cast<redisReply *>(redisCommand(m_Context, "EXEC"));
-
-       if (!reply3) {
-               redisFree(m_Context);
-               m_Context = NULL;
-               return;
-       }
-
-       if (reply3->type == REDIS_REPLY_ERROR) {
-               Log(LogInformation, "RedisWriter")
-                   << "EXEC: " << reply3->str;
-       }
-
-       if (reply3->type == REDIS_REPLY_ERROR) {
-               freeReplyObject(reply3);
-               return;
-       }
-
-       freeReplyObject(reply3);
+       ExecuteQuery({ "EXEC" });
 }
 
 void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, const String& typeName, bool runtimeUpdate)
@@ -180,26 +108,7 @@ void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, const String
 
        String objectName = object->GetName();
 
-       redisReply *reply1 = reinterpret_cast<redisReply *>(redisCommand(m_Context, "HSET icinga:config:%s %s %s", typeName.CStr(), objectName.CStr(), jsonBody.CStr()));
-
-       if (!reply1) {
-               redisFree(m_Context);
-               m_Context = NULL;
-               return;
-       }
-
-       if (reply1->type == REDIS_REPLY_ERROR) {
-               Log(LogInformation, "RedisWriter")
-                   << "HSET icinga:config:" << typeName << " " << objectName << " " << jsonBody << ": " << reply1->str;
-       }
-
-       if (reply1->type == REDIS_REPLY_ERROR) {
-               freeReplyObject(reply1);
-               return;
-       }
-
-       freeReplyObject(reply1);
-
+       ExecuteQuery({ "HSET", "icinga:config:" + typeName, objectName, jsonBody });
 
        /* check sums */
        /* hset icinga:config:Host:checksums localhost { "name_checksum": "...", "properties_checksum": "...", "groups_checksum": "...", "vars_checksum": null } */
@@ -227,25 +136,7 @@ void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, const String
 
        String checkSumBody = JsonEncode(checkSum);
 
-       redisReply *reply2 = reinterpret_cast<redisReply *>(redisCommand(m_Context, "HSET icinga:config:%s:checksum %s %s", typeName.CStr(), objectName.CStr(), checkSumBody.CStr()));
-
-       if (!reply2) {
-               redisFree(m_Context);
-               m_Context = NULL;
-               return;
-       }
-
-       if (reply2->type == REDIS_REPLY_ERROR) {
-               Log(LogInformation, "RedisWriter")
-                   << "HSET icinga:config:" << typeName << " " << objectName << " " << jsonBody << ": " << reply2->str;
-       }
-
-       if (reply2->type == REDIS_REPLY_ERROR) {
-               freeReplyObject(reply2);
-               return;
-       }
-
-       freeReplyObject(reply2);
+       ExecuteQuery({ "HSET", "icinga:config:" + typeName + ":checksum", objectName, checkSumBody });
 
        /* publish runtime updated objects immediately */
        if (!runtimeUpdate)
@@ -257,25 +148,7 @@ void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, const String
        PUBLISH "icinga:config:delete" "Host:__name"
        */
 
-       redisReply *reply3 = reinterpret_cast<redisReply *>(redisCommand(m_Context, "PUBLISH icinga:config:update %s:%s!%s", typeName.CStr(), objectName.CStr(), checkSumBody.CStr()));
-
-       if (!reply3) {
-               redisFree(m_Context);
-               m_Context = NULL;
-               return;
-       }
-
-       if (reply3->type == REDIS_REPLY_ERROR) {
-               Log(LogInformation, "RedisWriter")
-                   << "PUBLISH icinga:config:update " << typeName << ":" << objectName << "!" << checkSumBody << ": " << reply3->str;
-       }
-
-       if (reply3->type == REDIS_REPLY_ERROR) {
-               freeReplyObject(reply3);
-               return;
-       }
-
-       freeReplyObject(reply3);
+       ExecuteQuery({ "PUBLISH", "icinga:config:update", typeName + ":" + objectName + "!" + checkSumBody });
 }
 
 void RedisWriter::SendConfigDelete(const ConfigObject::Ptr& object, const String& typeName)
index 28a6173df35eaa2fc30507ffd3ac7ab076870e23..8fe916fb1eadbc8a291eafcffe1c528c9670d6bc 100644 (file)
@@ -97,41 +97,13 @@ void RedisWriter::TryToReconnect(void)
 
        String password = GetPassword();
 
-       if (!password.IsEmpty()) {
-               redisReply *reply = reinterpret_cast<redisReply *>(redisCommand(m_Context, "AUTH %s", password.CStr()));
-
-               if (!reply) {
-                       redisFree(m_Context);
-                       m_Context = NULL;
-                       return;
-               }
-
-               if (reply->type == REDIS_REPLY_STATUS || reply->type == REDIS_REPLY_ERROR) {
-                       Log(LogInformation, "RedisWriter")
-                           << "AUTH: " << reply->str;
-               }
-
-               freeReplyObject(reply);
-       }
+       if (!password.IsEmpty())
+               ExecuteQuery({ "AUTH", password });
 
        int dbIndex = GetDbIndex();
 
-       if (dbIndex != 0) {
-               redisReply *reply = reinterpret_cast<redisReply *>(redisCommand(m_Context, "SELECT %d", dbIndex));
-
-               if (!reply) {
-                       redisFree(m_Context);
-                       m_Context = NULL;
-                       return;
-               }
-
-               if (reply->type == REDIS_REPLY_STATUS || reply->type == REDIS_REPLY_ERROR) {
-                       Log(LogInformation, "RedisWriter")
-                           << "SELECT " << dbIndex << ": " << reply->str;
-               }
-
-               freeReplyObject(reply);
-       }
+       if (dbIndex != 0)
+               ExecuteQuery({ "SELECT", Convert::ToString(dbIndex) });
 
        /* Config dump */
        m_ConfigDumpInProgress = true;
@@ -159,22 +131,7 @@ void RedisWriter::UpdateSubscriptions(void)
        long long cursor = 0;
 
        do {
-               redisReply *reply = reinterpret_cast<redisReply *>(redisCommand(m_Context, "SCAN %lld MATCH icinga:subscription:* COUNT 1000", cursor));
-
-               if (!reply) {
-                       redisFree(m_Context);
-                       m_Context = NULL;
-                       return;
-               }
-
-               if (reply->type == REDIS_REPLY_STATUS || reply->type == REDIS_REPLY_ERROR) {
-                       Log(LogInformation, "RedisWriter")
-                           << "SCAN " << cursor << " MATCH icinga:subscription:* COUNT 1000: " << reply->str;
-
-                       freeReplyObject(reply);
-
-                       return;
-               }
+               boost::shared_ptr<redisReply> reply = ExecuteQuery({ "SCAN", Convert::ToString(cursor), "MATCH", "icinga:subscription:*" });
 
                VERIFY(reply->type == REDIS_REPLY_ARRAY);
                VERIFY(reply->elements % 2 == 0);
@@ -188,30 +145,10 @@ void RedisWriter::UpdateSubscriptions(void)
                        redisReply *keyReply = keysReply->element[i];
                        VERIFY(keyReply->type == REDIS_REPLY_STRING);
 
-                       redisReply *vreply = reinterpret_cast<redisReply *>(redisCommand(m_Context, "GET %s", keyReply->str));
-
-                       if (!vreply) {
-                               freeReplyObject(reply);
-                               redisFree(m_Context);
-                               m_Context = NULL;
-                               return;
-                       }
-
-                       if (vreply->type == REDIS_REPLY_STATUS || vreply->type == REDIS_REPLY_ERROR) {
-                               Log(LogInformation, "RedisWriter")
-                                   << "GET " << keyReply->str << ": " << vreply->str;
-
-                               freeReplyObject(vreply);
-
-                               continue;
-                       }
+                       boost::shared_ptr<redisReply> vreply = ExecuteQuery({ "GET", keyReply->str });
 
                        subscriptions[keyReply->str] = vreply->str;
-
-                       freeReplyObject(vreply);
                }
-
-               freeReplyObject(reply);
        } while (cursor != 0);
 
        m_Subscriptions.clear();
@@ -299,29 +236,7 @@ void RedisWriter::HandleEvent(const Dictionary::Ptr& event)
 
                String body = JsonEncode(event);
 
-               redisReply *reply = reinterpret_cast<redisReply *>(redisCommand(m_Context, "LPUSH icinga:event:%s %s", name.CStr(), body.CStr()));
-
-               if (!reply) {
-                       redisFree(m_Context);
-                       m_Context = NULL;
-                       return;
-               }
-
-               if (reply->type == REDIS_REPLY_STATUS || reply->type == REDIS_REPLY_ERROR) {
-                       Log(LogInformation, "RedisWriter")
-                           << "LPUSH icinga:event:" << kv.first << " " << body << ": " << reply->str;
-
-                       freeReplyObject(reply);
-
-                       continue;
-               }
-
-               if (reply->type == REDIS_REPLY_ERROR) {
-                       freeReplyObject(reply);
-                       return;
-               }
-
-               freeReplyObject(reply);
+               ExecuteQuery({ "LPUSH", "icinga:event:" + name, body });
        }
 }