diff --git a/arangod/Agency/AgencyComm.cpp b/arangod/Agency/AgencyComm.cpp index 9f5d4122de..7d11477fa5 100644 --- a/arangod/Agency/AgencyComm.cpp +++ b/arangod/Agency/AgencyComm.cpp @@ -249,6 +249,17 @@ bool AgencyWriteTransaction::validate(AgencyCommResult const& result) const { result.slice().get("results").isArray()); } +std::string AgencyWriteTransaction::randomClientId() { + std::string uuid = to_string(boost::uuids::random_generator()()), id; + + auto ss = ServerState::instance(); + if (ss != nullptr && !ss->getId().empty()) { + return ss->getId() + ":" + uuid; + } + return uuid; +} + + // ----------------------------------------------------------------------------- // --SECTION-- AgencyTransientTransaction // ----------------------------------------------------------------------------- @@ -1186,13 +1197,13 @@ bool AgencyComm::ensureStructureInitialized() { LOG_TOPIC("17e16", TRACE, Logger::AGENCYCOMM) << "Agency is fresh. Needs initial structure."; - + if (tryInitializeStructure()) { LOG_TOPIC("4c5aa", TRACE, Logger::AGENCYCOMM) << "Successfully initialized agency"; break; } - + LOG_TOPIC("63f7b", WARN, Logger::AGENCYCOMM) << "Initializing agency failed. We'll try again soon"; // We should really have exclusive access, here, this is strange! @@ -1818,18 +1829,18 @@ bool AgencyComm::shouldInitializeStructure() { auto result = getValues("Plan"); if (!result.successful()) { // Not 200 - 299 - + if (result.httpCode() == 401) { // unauthorized LOG_TOPIC("32781", FATAL, Logger::STARTUP) << "Unauthorized. Wrong credentials."; FATAL_ERROR_EXIT(); } - + // Agency not ready yet LOG_TOPIC("36253", TRACE, Logger::AGENCYCOMM) << "waiting for agency to become ready"; continue; - + } else { // Sanity @@ -1858,9 +1869,9 @@ bool AgencyComm::shouldInitializeStructure() { } continue; } - + } - + std::this_thread::sleep_for(std::chrono::milliseconds(250)); } diff --git a/arangod/Agency/AgencyComm.h b/arangod/Agency/AgencyComm.h index 20a2efbb83..0774eb6879 100644 --- a/arangod/Agency/AgencyComm.h +++ b/arangod/Agency/AgencyComm.h @@ -357,23 +357,26 @@ class AgencyTransaction { struct AgencyWriteTransaction : public AgencyTransaction { public: + + static std::string randomClientId(); + explicit AgencyWriteTransaction(AgencyOperation const& operation) - : clientId(to_string(boost::uuids::random_generator()())) { + : clientId(randomClientId()) { operations.push_back(operation); } explicit AgencyWriteTransaction(std::vector const& _opers) - : operations(_opers), clientId(to_string(boost::uuids::random_generator()())) {} + : operations(_opers), clientId(randomClientId()) {} AgencyWriteTransaction(AgencyOperation const& operation, AgencyPrecondition const& precondition) - : clientId(to_string(boost::uuids::random_generator()())) { + : clientId(randomClientId()) { operations.push_back(operation); preconditions.push_back(precondition); } AgencyWriteTransaction(std::vector const& opers, AgencyPrecondition const& precondition) - : clientId(to_string(boost::uuids::random_generator()())) { + : clientId(randomClientId()) { std::copy(opers.begin(), opers.end(), std::back_inserter(operations)); preconditions.push_back(precondition); @@ -381,7 +384,7 @@ struct AgencyWriteTransaction : public AgencyTransaction { AgencyWriteTransaction(AgencyOperation const& operation, std::vector const& precs) - : clientId(to_string(boost::uuids::random_generator()())) { + : clientId(randomClientId()) { operations.push_back(operation); std::copy(precs.begin(), precs.end(), std::back_inserter(preconditions)); @@ -389,14 +392,14 @@ struct AgencyWriteTransaction : public AgencyTransaction { AgencyWriteTransaction(std::vector const& opers, std::vector const& precs) - : clientId(to_string(boost::uuids::random_generator()())) { + : clientId(randomClientId()) { std::copy(opers.begin(), opers.end(), std::back_inserter(operations)); std::copy(precs.begin(), precs.end(), std::back_inserter(preconditions)); } - AgencyWriteTransaction() = default; + AgencyWriteTransaction() : clientId(randomClientId()) {}; void toVelocyPack(arangodb::velocypack::Builder& builder) const override final; diff --git a/arangod/Cluster/ServerState.cpp b/arangod/Cluster/ServerState.cpp index 7ea05bb1cd..9d38c3297c 100644 --- a/arangod/Cluster/ServerState.cpp +++ b/arangod/Cluster/ServerState.cpp @@ -368,7 +368,7 @@ bool ServerState::integrateIntoCluster(ServerState::RoleEnum role, id = getPersistedId(); LOG_TOPIC("db3ce", DEBUG, Logger::CLUSTER) << "Restarting with persisted UUID " << id; } - _id = id; + setId(id); _myEndpoint = myEndpoint; _advertisedEndpoint = advEndpoint; TRI_ASSERT(!_myEndpoint.empty()); @@ -690,7 +690,7 @@ void ServerState::setRole(ServerState::RoleEnum role) { //////////////////////////////////////////////////////////////////////////////// std::string ServerState::getId() const { - READ_LOCKER(readLocker, _lock); + std::lock_guard guard(_idLock); return _id; } @@ -703,7 +703,7 @@ void ServerState::setId(std::string const& id) { return; } - WRITE_LOCKER(writeLocker, _lock); + std::lock_guard guard(_idLock); _id = id; } diff --git a/arangod/Cluster/ServerState.h b/arangod/Cluster/ServerState.h index f7f54c664e..fc371c5c6c 100644 --- a/arangod/Cluster/ServerState.h +++ b/arangod/Cluster/ServerState.h @@ -24,6 +24,8 @@ #ifndef ARANGOD_CLUSTER_SERVER_STATE_H #define ARANGOD_CLUSTER_SERVER_STATE_H 1 +#include + #include "Basics/Common.h" #include "Basics/ReadWriteSpinLock.h" #include "VocBase/voc-types.h" @@ -283,8 +285,10 @@ class ServerState { /// @brief r/w lock for state mutable arangodb::basics::ReadWriteSpinLock _lock; - /// @brief the server's id, can be set just once + /// @brief the server's id, can be set just once, use getId and setId, do not access directly std::string _id; + /// @brief lock for writing and reading server id + mutable std::mutex _idLock; /// @brief the server's short id, can be set just once std::atomic _shortId;