* Execute the provided HMSET values and runtime updates in a single Redis transaction on the provided Redis connection. * * The HMSETs should just contain the necessary key value pairs to be set in Redis, i.e, without the HMSET command * itself. This function will then go through each of the map keys and prepend the HMSET command when transforming the * map into valid Redis queries. Likewise,
| 3227 | * @param runtimeUpdates A list of dictionaries to be sent to the icinga:runtime stream. |
| 3228 | */ |
| 3229 | void IcingaDB::ExecuteRedisTransaction(const RedisConnection::Ptr& rcon, |
| 3230 | std::map<RedisConnection::QueryArg, RedisConnection::Query>& hMSets, const std::vector<Dictionary::Ptr>& runtimeUpdates) |
| 3231 | { |
| 3232 | RedisConnection::Queries transaction{{"MULTI"}}; |
| 3233 | for (auto& [redisKey, query] : hMSets) { |
| 3234 | if (!query.empty()) { |
| 3235 | query.insert(query.begin(), {"HSET", redisKey}); |
| 3236 | transaction.emplace_back(std::move(query)); |
| 3237 | } |
| 3238 | } |
| 3239 | |
| 3240 | for (auto& attrs : runtimeUpdates) { |
| 3241 | RedisConnection::Query xAdd{"XADD", "icinga:runtime", "MAXLEN", "~", "1000000", "*"}; |
| 3242 | |
| 3243 | ObjectLock olock(attrs); |
| 3244 | for (auto& [key, value] : attrs) { |
| 3245 | if (auto streamVal(IcingaToStreamValue(value)); !streamVal.IsEmpty()) { |
| 3246 | xAdd.emplace_back(key); |
| 3247 | xAdd.emplace_back(std::move(streamVal)); |
| 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | transaction.emplace_back(std::move(xAdd)); |
| 3252 | } |
| 3253 | |
| 3254 | if (transaction.size() > 1) { |
| 3255 | transaction.emplace_back(RedisConnection::Query{"EXEC"}); |
| 3256 | if (!runtimeUpdates.empty()) { |
| 3257 | rcon->FireAndForgetQueries(std::move(transaction), {1}); |
| 3258 | } else { |
| 3259 | // This is likely triggered by the initial Redis config dump, so a) we don't need to record the number of |
| 3260 | // affected objects and b) we don't really know how many objects are going to be affected by this tx. |
| 3261 | rcon->FireAndForgetQueries(std::move(transaction)); |
| 3262 | } |
| 3263 | } |
| 3264 | } |