Repository: mesos Updated Branches: refs/heads/1.4.x e7b117e0d -> 652eeb83f
Refactored `struct Slave` in the allocator for better performance. This patch refactors the `struct Slave` in the allocator. In particular, it addresses the slowness of computing agents' available resources. Instead of calculating them every time on the fly, this patch "denormalizes" the agent available resources by updating and persisting the field each time an agent's allocated or total resources change. Review: https://reviews.apache.org/r/67561/ Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/d5827547 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/d5827547 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/d5827547 Branch: refs/heads/1.4.x Commit: d5827547b3a83b79f4373199158bc40c0ae379d9 Parents: e7b117e Author: Meng Zhu <[email protected]> Authored: Thu Jun 21 09:09:36 2018 -0700 Committer: Greg Mann <[email protected]> Committed: Tue Jul 3 07:59:29 2018 -0700 ---------------------------------------------------------------------- src/master/allocator/mesos/hierarchical.cpp | 71 ++++++++-------- src/master/allocator/mesos/hierarchical.hpp | 103 +++++++++++++++++------ 2 files changed, 112 insertions(+), 62 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/d5827547/src/master/allocator/mesos/hierarchical.cpp ---------------------------------------------------------------------- diff --git a/src/master/allocator/mesos/hierarchical.cpp b/src/master/allocator/mesos/hierarchical.cpp index 33b7495..cd80791 100644 --- a/src/master/allocator/mesos/hierarchical.cpp +++ b/src/master/allocator/mesos/hierarchical.cpp @@ -500,16 +500,16 @@ void HierarchicalAllocatorProcess::addSlave( CHECK(!slaves.contains(slaveId)); CHECK(!paused || expectedAgentCount.isSome()); - slaves[slaveId] = Slave(); + slaves.insert({slaveId, + Slave( + slaveInfo.hostname(), + protobuf::slave::Capabilities(capabilities), + true, + total, + Resources::sum(used))}); Slave& slave = slaves.at(slaveId); - slave.total = total; - slave.allocated = Resources::sum(used); - slave.activated = true; - slave.hostname = slaveInfo.hostname(); - slave.capabilities = protobuf::slave::Capabilities(capabilities); - if (slaveInfo.has_domain()) { slave.domain = slaveInfo.domain(); } @@ -570,8 +570,8 @@ void HierarchicalAllocatorProcess::addSlave( } LOG(INFO) << "Added agent " << slaveId << " (" << slave.hostname << ")" - << " with " << slave.total - << " (allocated: " << slave.allocated << ")"; + << " with " << slave.getTotal() + << " (allocated: " << slave.getAllocated() << ")"; allocate(slaveId); } @@ -589,12 +589,13 @@ void HierarchicalAllocatorProcess::removeSlave( // all the resources. Fixing this would require more information // than what we currently track in the allocator. - roleSorter->remove(slaveId, slaves.at(slaveId).total); + roleSorter->remove(slaveId, slaves.at(slaveId).getTotal()); // See comment at `quotaRoleSorter` declaration regarding non-revocable. - quotaRoleSorter->remove(slaveId, slaves.at(slaveId).total.nonRevocable()); + quotaRoleSorter->remove( + slaveId, slaves.at(slaveId).getTotal().nonRevocable()); - untrackReservations(slaves.at(slaveId).total.reservations()); + untrackReservations(slaves.at(slaveId).getTotal().reservations()); slaves.erase(slaveId); allocationCandidates.erase(slaveId); @@ -807,8 +808,8 @@ void HierarchicalAllocatorProcess::updateAllocation( } // Update the per-slave allocation. - slave.allocated -= offeredResources; - slave.allocated += updatedOfferedResources; + slave.unallocate(offeredResources); + slave.allocate(updatedOfferedResources); // Update the allocation in the framework sorter. frameworkSorter->update( @@ -848,7 +849,7 @@ void HierarchicalAllocatorProcess::updateAllocation( protobuf::stripAllocationInfo(&operation); } - Try<Resources> updatedTotal = slave.total.apply(strippedOperations); + Try<Resources> updatedTotal = slave.getTotal().apply(strippedOperations); CHECK_SOME(updatedTotal); updateSlaveTotal(slaveId, updatedTotal.get()); @@ -898,13 +899,13 @@ Future<Nothing> HierarchicalAllocatorProcess::updateAvailable( // \___/ \___/ // // where A = allocate, R = reserve, U = updateAvailable - Try<Resources> updatedAvailable = slave.available().apply(operations); + Try<Resources> updatedAvailable = slave.getAvailable().apply(operations); if (updatedAvailable.isError()) { return Failure(updatedAvailable.error()); } // Update the total resources. - Try<Resources> updatedTotal = slave.total.apply(operations); + Try<Resources> updatedTotal = slave.getTotal().apply(operations); CHECK_SOME(updatedTotal); // Update the total resources in the allocator and role and quota sorters. @@ -1116,14 +1117,14 @@ void HierarchicalAllocatorProcess::recoverResources( if (slaves.contains(slaveId)) { Slave& slave = slaves.at(slaveId); - CHECK(slave.allocated.contains(resources)) - << slave.allocated << " does not contain " << resources; + CHECK(slave.getAllocated().contains(resources)) + << slave.getAllocated() << " does not contain " << resources; - slave.allocated -= resources; + slave.unallocate(resources); VLOG(1) << "Recovered " << resources - << " (total: " << slave.total - << ", allocated: " << slave.allocated << ")" + << " (total: " << slave.getTotal() + << ", allocated: " << slave.getAllocated() << ")" << " on agent " << slaveId << " from framework " << frameworkId; } @@ -1629,7 +1630,7 @@ void HierarchicalAllocatorProcess::__allocate() // NOTE: `totalScalarQuantities` omits dynamic reservation, // persistent volume info, and allocation info. We additionally // remove the static reservations here via `toUnreserved()`. - availableHeadroom -= slave.available().revocable() + availableHeadroom -= slave.getAvailable().revocable() .createStrippedScalarQuantity().toUnreserved(); } @@ -1715,7 +1716,7 @@ void HierarchicalAllocatorProcess::__allocate() // See MESOS-5634. if (filterGpuResources && !framework.capabilities.gpuResources && - slave.total.gpus().getOrElse(0) > 0) { + slave.getTotal().gpus().getOrElse(0) > 0) { continue; } @@ -1731,12 +1732,12 @@ void HierarchicalAllocatorProcess::__allocate() // Since shared resources are offerable even when they are in use, we // make one copy of the shared resources available regardless of the // past allocations. - Resources available = slave.available().nonShared(); + Resources available = slave.getAvailable().nonShared(); // Offer a shared resource only if it has not been offered in // this offer cycle to a framework. if (framework.capabilities.sharedResources) { - available += slave.total.shared(); + available += slave.getTotal().shared(); if (offeredSharedResources.contains(slaveId)) { available -= offeredSharedResources[slaveId]; } @@ -1927,7 +1928,7 @@ void HierarchicalAllocatorProcess::__allocate() // multiple copies of the same shared resources. const Resources newShared = resources.shared() .filter([this, &slaveId](const Resources& resource) { - return !slaves.at(slaveId).allocated.contains(resource); + return !slaves.at(slaveId).getAllocated().contains(resource); }); // We remove the static reservation metadata here via `toUnreserved()`. @@ -1935,7 +1936,7 @@ void HierarchicalAllocatorProcess::__allocate() (resources.reserved(role).nonShared() + newShared) .createStrippedScalarQuantity().toUnreserved(); - slave.allocated += resources; + slave.allocate(resources); trackAllocatedResources(slaveId, frameworkId, resources); } @@ -1976,7 +1977,7 @@ void HierarchicalAllocatorProcess::__allocate() // See MESOS-5634. if (filterGpuResources && !framework.capabilities.gpuResources && - slave.total.gpus().getOrElse(0) > 0) { + slave.getTotal().gpus().getOrElse(0) > 0) { continue; } @@ -1992,12 +1993,12 @@ void HierarchicalAllocatorProcess::__allocate() // Since shared resources are offerable even when they are in use, we // make one copy of the shared resources available regardless of the // past allocations. - Resources available = slave.available().nonShared(); + Resources available = slave.getAvailable().nonShared(); // Offer a shared resource only if it has not been offered in // this offer cycle to a framework. if (framework.capabilities.sharedResources) { - available += slave.total.shared(); + available += slave.getTotal().shared(); if (offeredSharedResources.contains(slaveId)) { available -= offeredSharedResources[slaveId]; } @@ -2091,7 +2092,7 @@ void HierarchicalAllocatorProcess::__allocate() headroomToAllocate.createStrippedScalarQuantity(); } - slave.allocated += resources; + slave.allocate(resources); trackAllocatedResources(slaveId, frameworkId, resources); } @@ -2412,7 +2413,7 @@ double HierarchicalAllocatorProcess::_resources_offered_or_allocated( foreachvalue (const Slave& slave, slaves) { Option<Value::Scalar> value = - slave.allocated.get<Value::Scalar>(resource); + slave.getAllocated().get<Value::Scalar>(resource); if (value.isSome()) { offered_or_allocated += value->value(); @@ -2588,13 +2589,13 @@ bool HierarchicalAllocatorProcess::updateSlaveTotal( Slave& slave = slaves.at(slaveId); - const Resources oldTotal = slave.total; + const Resources oldTotal = slave.getTotal(); if (oldTotal == total) { return false; } - slave.total = total; + slave.updateTotal(total); hashmap<std::string, Resources> oldReservations = oldTotal.reservations(); hashmap<std::string, Resources> newReservations = total.reservations(); http://git-wip-us.apache.org/repos/asf/mesos/blob/d5827547/src/master/allocator/mesos/hierarchical.hpp ---------------------------------------------------------------------- diff --git a/src/master/allocator/mesos/hierarchical.hpp b/src/master/allocator/mesos/hierarchical.hpp index 4f22c8b..348d631 100644 --- a/src/master/allocator/mesos/hierarchical.hpp +++ b/src/master/allocator/mesos/hierarchical.hpp @@ -345,47 +345,61 @@ protected: hashmap<FrameworkID, Framework> frameworks; - struct Slave + class Slave { - // Total amount of regular *and* oversubscribed resources. - Resources total; - - // Regular *and* oversubscribed resources that are allocated. - // - // NOTE: We maintain multiple copies of each shared resource allocated - // to a slave, where the number of copies represents the number of times - // this shared resource has been allocated to (and has not been recovered - // from) a specific framework. - // - // NOTE: We keep track of slave's allocated resources despite - // having that information in sorters. This is because the - // information in sorters is not accurate if some framework - // hasn't reregistered. See MESOS-2919 for details. - Resources allocated; - - // We track the total and allocated resources on the slave, the - // available resources are computed as follows: - // - // available = total - allocated - // - // Note that it's possible for the slave to be over-allocated! - // In this case, allocated > total. - Resources available() const + public: + Slave( + const std::string& _hostname, + const protobuf::slave::Capabilities& _capabilities, + bool _activated, + const Resources& _total, + const Resources& _allocated) + : hostname(_hostname), + capabilities(_capabilities), + activated(_activated), + total(_total), + allocated(_allocated) { // In order to subtract from the total, // we strip the allocation information. Resources allocated_ = allocated; allocated_.unallocate(); - return total - allocated_; + available = total - allocated_; + } + + Resources getTotal() const { return total; } + + Resources getAllocated() const { return allocated; } + + Resources getAvailable() const { return available; } + + void updateTotal(const Resources& newTotal) { + total = newTotal; + + updateAvailable(); + } + + void allocate(const Resources& toAllocate) + { + allocated += toAllocate; + + updateAvailable(); } - bool activated; // Whether to offer resources. + void unallocate(const Resources& toUnallocate) + { + allocated -= toUnallocate; + + updateAvailable(); + } std::string hostname; protobuf::slave::Capabilities capabilities; + bool activated; // Whether to offer resources. + Option<DomainInfo> domain; // Represents a scheduled unavailability due to maintenance for a specific @@ -423,6 +437,41 @@ protected: // a given point in time, for an optional duration. This information is used // to send out `InverseOffers`. Option<Maintenance> maintenance; + + private: + void updateAvailable() { + // In order to subtract from the total, + // we strip the allocation information. + Resources allocated_ = allocated; + allocated_.unallocate(); + + available = total - allocated_; + } + + // Total amount of regular *and* oversubscribed resources. + Resources total; + + // Regular *and* oversubscribed resources that are allocated. + // + // NOTE: We maintain multiple copies of each shared resource allocated + // to a slave, where the number of copies represents the number of times + // this shared resource has been allocated to (and has not been recovered + // from) a specific framework. + // + // NOTE: We keep track of the slave's allocated resources despite + // having that information in sorters. This is because the + // information in sorters is not accurate if some framework + // hasn't reregistered. See MESOS-2919 for details. + Resources allocated; + + // We track the total and allocated resources on the slave, the + // available resources are computed as follows: + // + // available = total - allocated + // + // Note that it's possible for the slave to be over-allocated! + // In this case, allocated > total. + Resources available; }; hashmap<SlaveID, Slave> slaves;
