Skip to content

Commit

Permalink
ApiListener: Process cluster config updates sequentially
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Feb 13, 2024
1 parent 0c4bd97 commit e5b9bd2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/remote/apilistener-configsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/configtype.hpp"
#include "base/json.hpp"
#include "base/convert.hpp"
#include "base/defer.hpp"
#include "config/vmops.hpp"
#include <fstream>

Expand All @@ -20,6 +21,8 @@ INITIALIZE_ONCE([]() {
ConfigObject::OnVersionChanged.connect(&ApiListener::ConfigUpdateObjectHandler);
});

std::unique_ptr<ConfigSyncRule> ApiListener::m_ConfigSyncPolicy(new ConfigSyncRule());

void ApiListener::ConfigUpdateObjectHandler(const ConfigObject::Ptr& object, const Value& cookie)
{
ApiListener::Ptr listener = ApiListener::GetInstance();
Expand Down Expand Up @@ -104,6 +107,15 @@ Value ApiListener::ConfigUpdateObjectAPIHandler(const MessageOrigin::Ptr& origin
return Empty;
}

// Wait for the object name to become available for processing.
m_ConfigSyncPolicy->WaitForObject(ptype, objName);
// Add object name to the locked list to block all other threads that try to process a message affecting the same object.
m_ConfigSyncPolicy->BlockObject(ptype, objName);

Defer unlockAndNotifyOne([&ptype, &objName]{
m_ConfigSyncPolicy->UnBlockNotify(ptype, objName);
});

ConfigObject::Ptr object = ctype->GetObject(objName);

String config = params->Get("config");
Expand Down Expand Up @@ -258,6 +270,15 @@ Value ApiListener::ConfigDeleteObjectAPIHandler(const MessageOrigin::Ptr& origin
return Empty;
}

// Wait for the object name to become available for processing.
m_ConfigSyncPolicy->WaitForObject(ptype, objName);
// Add object name to the locked list to block all other threads that try to process a message affecting the same object.
m_ConfigSyncPolicy->BlockObject(ptype, objName);

Defer unblockAndNotifyOne([&ptype, &objName]{
m_ConfigSyncPolicy->UnBlockNotify(ptype, objName);
});

ConfigObject::Ptr object = ctype->GetObject(objName);

if (!object) {
Expand Down
3 changes: 3 additions & 0 deletions lib/remote/apilistener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ class ApiListener final : public ObjectImpl<ApiListener>
mutable std::mutex m_ActivePackageStagesLock;
std::map<String, String> m_ActivePackageStages;

/* update/delete object config sync */
static std::unique_ptr<ConfigSyncRule> m_ConfigSyncPolicy;

void UpdateActivePackageStagesCache();
};

Expand Down

0 comments on commit e5b9bd2

Please sign in to comment.