void AgencyCallback::refetchAndUpdate(bool needToAcquireMutex) { if (!_needsValue) { // no need to pass any value to the callback if (needToAcquireMutex) { CONDITION_LOCKER(locker, _cv); executeEmpty(); } else { executeEmpty(); } return; } AgencyCommResult result = _agency.getValues(key); if (!result.successful()) { LOG(ERR) << "Callback getValues to agency was not successful: " << result.errorCode() << " " << result.errorMessage(); return; } std::vector<std::string> kv = basics::StringUtils::split(AgencyComm::prefixPath() + key,'/'); kv.erase(std::remove(kv.begin(), kv.end(), ""), kv.end()); std::shared_ptr<VPackBuilder> newData = std::make_shared<VPackBuilder>(); newData->add(result.slice()[0].get(kv)); if (needToAcquireMutex) { CONDITION_LOCKER(locker, _cv); checkValue(newData); } else { checkValue(newData); } }
ServerState::RoleEnum ServerState::checkServersList (std::string const& id) { // fetch value at Plan/DBServers // we need to do this to determine the server's role const std::string key = "Plan/DBServers"; AgencyComm comm; AgencyCommResult result; { AgencyCommLocker locker("Plan", "READ"); if (locker.successful()) { result = comm.getValues(key, true); } } if (! result.successful()) { const std::string endpoints = AgencyComm::getEndpointsString(); LOG_TRACE("Could not fetch configuration from agency endpoints (%s): " "got status code %d, message: %s, key: %s", endpoints.c_str(), result._statusCode, result.errorMessage().c_str(), key.c_str()); return ServerState::ROLE_UNDEFINED; } ServerState::RoleEnum role = ServerState::ROLE_UNDEFINED; // check if we can find ourselves in the list returned by the agency result.parse("Plan/DBServers/", false); std::map<std::string, AgencyCommResultEntry>::const_iterator it = result._values.find(id); if (it != result._values.end()) { // we are in the list. this means we are a primary server role = ServerState::ROLE_PRIMARY; } else { // check if we are a secondary... it = result._values.begin(); while (it != result._values.end()) { const std::string name = triagens::basics::JsonHelper::getStringValue((*it).second._json, ""); if (name == id) { role = ServerState::ROLE_SECONDARY; _idOfPrimary = it->first; break; } ++it; } } return role; }
int ServerState::lookupLocalInfoToId (std::string const& localInfo, std::string& id) { // fetch value at Plan/DBServers // we need to do this to determine the server's role const std::string key = "Target/MapLocalToID"; int count = 0; while (++count <= 600) { AgencyComm comm; AgencyCommResult result; { AgencyCommLocker locker("Target", "READ"); if (locker.successful()) { result = comm.getValues(key, true); } } if (! result.successful()) { const std::string endpoints = AgencyComm::getEndpointsString(); LOG_DEBUG("Could not fetch configuration from agency endpoints (%s): " "got status code %d, message: %s, key: %s", endpoints.c_str(), result._statusCode, result.errorMessage().c_str(), key.c_str()); } else { result.parse("Target/MapLocalToID/", false); std::map<std::string, AgencyCommResultEntry>::const_iterator it = result._values.find(localInfo); if (it != result._values.end()) { TRI_json_t const* json = it->second._json; Json j(TRI_UNKNOWN_MEM_ZONE, json, Json::NOFREE); id = triagens::basics::JsonHelper::getStringValue(json, "ID", ""); if (id.empty()) { LOG_ERROR("ID not set!"); return TRI_ERROR_CLUSTER_COULD_NOT_DETERMINE_ID; } std::string description = triagens::basics::JsonHelper::getStringValue(json, "Description", ""); if (! description.empty()) { setDescription(description); } return TRI_ERROR_NO_ERROR; } } sleep(1); }; return TRI_ERROR_CLUSTER_COULD_NOT_DETERMINE_ID; }
ServerState::RoleEnum ServerState::checkCoordinatorsList (std::string const& id) { // fetch value at Plan/Coordinators // we need to do this to determine the server's role const std::string key = "Plan/Coordinators"; AgencyComm comm; AgencyCommResult result; { AgencyCommLocker locker("Plan", "READ"); if (locker.successful()) { result = comm.getValues(key, true); } } if (! result.successful()) { const std::string endpoints = AgencyComm::getEndpointsString(); LOG_TRACE("Could not fetch configuration from agency endpoints (%s): " "got status code %d, message: %s, key: %s", endpoints.c_str(), result._statusCode, result.errorMessage().c_str(), key.c_str()); return ServerState::ROLE_UNDEFINED; } if (! result.parse("Plan/Coordinators/", false)) { LOG_TRACE("Got an invalid JSON response for Plan/Coordinators"); return ServerState::ROLE_UNDEFINED; } // check if we can find ourselves in the list returned by the agency std::map<std::string, AgencyCommResultEntry>::const_iterator it = result._values.find(id); if (it != result._values.end()) { // we are in the list. this means we are a primary server return ServerState::ROLE_COORDINATOR; } return ServerState::ROLE_UNDEFINED; }