Future<http::Response> Master::QuotaHandler::_set( const QuotaInfo& quotaInfo, bool forced) const { if (forced) { VLOG(1) << "Using force flag to override quota capacity heuristic check"; } else { // Validate whether a quota request can be satisfied. Option<Error> error = capacityHeuristic(quotaInfo); if (error.isSome()) { return Conflict( "Heuristic capacity check for set quota request failed: " + error.get().message); } } Quota quota = Quota{quotaInfo}; // Populate master's quota-related local state. We do this before updating // the registry in order to make sure that we are not already trying to // satisfy a request for this role (since this is a multi-phase event). // NOTE: We do not need to remove quota for the role if the registry update // fails because in this case the master fails as well. master->quotas[quotaInfo.role()] = quota; // Update the registry with the new quota and acknowledge the request. return master->registrar->apply(Owned<Operation>( new quota::UpdateQuota(quotaInfo))) .then(defer(master->self(), [=](bool result) -> Future<http::Response> { // See the top comment in "master/quota.hpp" for why this check is here. CHECK(result); master->allocator->setQuota(quotaInfo.role(), quota); // Rescind outstanding offers to facilitate satisfying the quota request. // NOTE: We set quota before we rescind to avoid a race. If we were to // rescind first, then recovered resources may get allocated again // before our call to `setQuota` was handled. // The consequence of setting quota first is that (in the hierarchical // allocator) it will trigger an allocation. This means the rescinded // offer resources will only be available to quota once another // allocation is invoked. // This can be resolved in the future with an explicit allocation call, // and this solution is preferred to having the race described earlier. rescindOffers(quotaInfo); return OK(); })); }
Future<http::Response> Master::QuotaHandler::set( const http::Request& request) const { VLOG(1) << "Setting quota from request: '" << request.body << "'"; // Authenticate and authorize the request. // TODO(alexr): Check Master::Http::authenticate() for an example. // Check that the request type is POST which is guaranteed by the master. CHECK_EQ("POST", request.method); // Validate request and extract JSON. Try<hashmap<string, string>> decode = http::query::decode(request.body); if (decode.isError()) { return BadRequest("Failed to decode set quota request query string ('" + request.body + "'): " + decode.error()); } hashmap<string, string> values = decode.get(); if (!values.contains("resources")) { return BadRequest("Failed to parse set quota request query string ('" + request.body + "'): Missing 'resources'"); } Try<google::protobuf::RepeatedPtrField<Resource>> resources = parseResources(values["resources"]); if (resources.isError()) { return BadRequest("Failed to parse set quota request query string ('" + request.body + "'): " + resources.error()); } // Create the `QuotaInfo` protobuf message from the request JSON. Try<QuotaInfo> create = createQuotaInfo(resources.get()); if (create.isError()) { return BadRequest("Failed to create QuotaInfo from set quota request " "query string '(" + request.body + "'): " + create.error()); } // Check that the `QuotaInfo` is a valid quota request. Try<Nothing> validate = quota::validation::quotaInfo(create.get()); if (validate.isError()) { return BadRequest("Failed to validate set quota request query string: ('" + request.body + "'): " + validate.error()); } // Check that the role is known by the master. // TODO(alexr): Once we are able to dynamically add roles, we should stop // checking whether the requested role is known to the master, because an // operator may set quota for a role that is about to be introduced. if (!master->roles.contains(create.get().role())) { return BadRequest("Failed to validate set quota request query string: ('" + request.body +"')': Unknown role: '" + create.get().role() + "'"); } // Check that we are not updating an existing quota. // TODO(joerg84): Update error message once quota update is in place. if (master->quotas.contains(create.get().role())) { return BadRequest("Failed to validate set quota request query string: ('" + request.body + "')': " "Can not set quota for a role that already has quota"); } const QuotaInfo& quotaInfo = create.get(); // Validate whether a quota request can be satisfied. Option<Error> error = capacityHeuristic(quotaInfo); if (error.isSome()) { return Conflict("Heuristic capacity check for set quota request failed: " + error.get().message); } // Populate master's quota-related local state. We do this before updating // the registry in order to make sure that we are not already trying to // satisfy a request for this role (since this is a multi-phase event). // NOTE: We do not need to remove quota for the role if the registry update // fails because in this case the master fails as well. master->quotas[quotaInfo.role()] = Quota{quotaInfo}; // Update the registry with the new quota and acknowledge the request. return master->registrar->apply(Owned<Operation>( new quota::UpdateQuota(quotaInfo))) .then(defer(master->self(), [=](bool result) -> Future<http::Response> { // See the top comment in "master/quota.hpp" for why this check is here. CHECK(result); master->allocator->setQuota(quotaInfo.role(), quotaInfo); return OK(); })); }