You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by mz...@apache.org on 2019/06/15 01:12:08 UTC

[mesos] branch master updated (2030964 -> ceb1120)

This is an automated email from the ASF dual-hosted git repository.

mzhu pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git.


    from 2030964  Replaced a set of single '*' role in SCRIPT_TEST ACL with ANY.
     new f05f061  Added a wrapper struct for quota guarantees and limits.
     new 7579844  Refactored quota overcommit check.
     new 495162e  Refactored allocator with the new quota wrapper struct.
     new 4bdbd8e  Added `Metrics::updateQuota` for quota metrics.
     new 4703b23  Added `updateQuota` method to the allocator.
     new ceb1120  Used the new quota struct for the allocator recover call.

The 6 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 include/mesos/allocator/allocator.hpp       |  20 ++++-
 include/mesos/quota/quota.hpp               |  23 ++++++
 src/master/allocator/mesos/allocator.hpp    |  27 +++++-
 src/master/allocator/mesos/hierarchical.cpp |  71 ++++++++--------
 src/master/allocator/mesos/hierarchical.hpp |  25 ++----
 src/master/allocator/mesos/metrics.cpp      |  59 ++++++++++++++
 src/master/allocator/mesos/metrics.hpp      |   3 +
 src/master/constants.hpp                    |   4 +
 src/master/master.cpp                       |   8 +-
 src/master/quota.cpp                        |  50 ++++++++++++
 src/master/quota_handler.cpp                | 106 ++++++++++++------------
 src/tests/allocator.hpp                     |  17 +++-
 src/tests/master_quota_tests.cpp            | 122 ++++++++++++++--------------
 13 files changed, 363 insertions(+), 172 deletions(-)


[mesos] 02/06: Refactored quota overcommit check.

Posted by mz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mzhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 75798445f932f1f163a502e2325e76cf33450836
Author: Meng Zhu <mz...@mesosphere.io>
AuthorDate: Tue Jun 4 10:48:51 2019 -0700

    Refactored quota overcommit check.
    
    This refactor makes the `QuotaTree` to use the new
    quota wrapper struct.
    
    Also refactor the check to reflect that it is currently
    only checking guarantees.
    
    Review: https://reviews.apache.org/r/70800
---
 src/master/quota_handler.cpp | 102 +++++++++++++++++++++++--------------------
 1 file changed, 54 insertions(+), 48 deletions(-)

diff --git a/src/master/quota_handler.cpp b/src/master/quota_handler.cpp
index 21176dd..b51e663 100644
--- a/src/master/quota_handler.cpp
+++ b/src/master/quota_handler.cpp
@@ -25,6 +25,8 @@
 
 #include <mesos/quota/quota.hpp>
 
+#include <mesos/resource_quantities.hpp>
+
 #include <process/collect.hpp>
 #include <process/defer.hpp>
 #include <process/future.hpp>
@@ -73,16 +75,19 @@ namespace mesos {
 namespace internal {
 namespace master {
 
-// Represents the tree of roles that have quota. The quota of a child
-// node is "contained" in the quota of its parent node. This has two
+// Represents the tree of roles that have quota. The quota guarantees of a child
+// node is "contained" in the guarantees of its parent node. This has two
 // implications:
 //
-//   (1) The quota of a parent must be greater than or equal to the
-//       sum of the quota of its children.
+//   (1) The quota guarantees of a parent must be greater than or equal to the
+//       sum of the quota guarantees of its children.
 //
 //   (2) When computing the total resources guaranteed by quota, we
 //       don't want to double-count resource guarantees between a
 //       parent role and its children.
+//
+// TODO(mzhu): The above check is only about guarantees. We should extend
+// the check to also cover limits.
 class QuotaTree
 {
 public:
@@ -90,11 +95,11 @@ public:
     : root(new Node(""))
   {
     foreachpair (const string& role, const Quota& quota, quotas) {
-      insert(role, quota);
+      insert(role, Quota2(quota.info));
     }
   }
 
-  void insert(const string& role, const Quota& quota)
+  void insert(const string& role, const Quota2& quota)
   {
     // Create the path from root->leaf in the tree. Any missing nodes
     // are created implicitly.
@@ -110,15 +115,18 @@ public:
       current = current->children.at(component).get();
     }
 
-    // Update `current` with the guaranteed quota resources for this
-    // role. A path in the tree should be associated with at most one
-    // quota guarantee, so the current guarantee should be empty.
-    CHECK(current->quota.info.guarantee().empty());
+    // Update `current` with the configured quota for this role.
+    // A path in the tree should be associated with at most one
+    // quota, so the current quota should be unset (default).
+    CHECK(current->quota == DEFAULT_QUOTA);
     current->quota = quota;
   }
 
-  // Check whether the tree satisfies the "parent >= sum(children)"
-  // constraint described above.
+  // Check whether the tree satisfies:
+  //
+  //   parent guarantees >= sum(children guarantees)
+  //
+  // TODO(mzhu): Add limit check.
   Option<Error> validate() const
   {
     // Don't check the root node because it does not have quota set.
@@ -132,17 +140,17 @@ public:
     return None();
   }
 
-  // Returns the total resources requested by all quotas in the
-  // tree. Since a role's quota must be greater than or equal to the
-  // sum of the quota of its children, we can just sum the quota of
-  // the top-level roles.
-  Resources total() const
+  // Returns the total guaranteed resource quantities requested by
+  // all quotas in the tree. Since a role's guarantees must be greater
+  // than or equal to the sum of the guarantees of its children, we can
+  // just sum the guarantees of the top-level roles.
+  ResourceQuantities totalGuarantees() const
   {
-    Resources result;
+    ResourceQuantities result;
 
     // Don't include the root node because it does not have quota set.
     foreachvalue (const unique_ptr<Node>& child, root->children) {
-      result += child->quota.info.guarantee();
+      result += child->quota.guarantees;
     }
 
     return result;
@@ -162,25 +170,24 @@ private:
         }
       }
 
-      Resources childResources;
+      ResourceQuantities childGuarantees;
       foreachvalue (const unique_ptr<Node>& child, children) {
-        childResources += child->quota.info.guarantee();
+        childGuarantees += child->quota.guarantees;
       }
 
-      Resources selfResources = quota.info.guarantee();
-
-      if (!selfResources.contains(childResources)) {
-        return Error("Invalid quota configuration. Parent role '" +
-                     name + "' with quota " + stringify(selfResources) +
-                     " does not contain the sum of its children's" +
-                     " resources (" + stringify(childResources) + ")");
+      // Check if self guarantees contains sum of children's guarantees.
+      if (!quota.guarantees.contains(childGuarantees)) {
+        return Error("Invalid quota configuration. Parent role '" + name +
+                     "' with guarantees (" + stringify(quota.guarantees) +
+                     ") does not contain the sum of its children's" +
+                     " guarantees (" + stringify(childGuarantees) + ")");
       }
 
       return None();
     }
 
     const string name;
-    Quota quota;
+    Quota2 quota;
     hashmap<string, unique_ptr<Node>> children;
   };
 
@@ -193,24 +200,23 @@ Option<Error> Master::QuotaHandler::overcommitCheck(
     const hashmap<string, Quota>& quotas,
     const QuotaInfo& request)
 {
-  ResourceQuantities totalQuota = ResourceQuantities::fromScalarResources(
-      [&]() {
-        QuotaTree quotaTree({});
-
-        foreachpair (const string& role, const Quota& quota, quotas) {
-          if (role != request.role()) {
-            quotaTree.insert(role, quota);
-          }
-        }
+  ResourceQuantities totalGuarantees = [&]() {
+    QuotaTree quotaTree({});
+
+    foreachpair (const string& role, const Quota& quota, quotas) {
+      if (role != request.role()) {
+        quotaTree.insert(role, Quota2(quota.info));
+      }
+    }
 
-        quotaTree.insert(request.role(), Quota{request});
+    quotaTree.insert(request.role(), Quota2{request});
 
-        // Hard CHECK since this is already validated earlier
-        // during request validation.
-        CHECK_NONE(quotaTree.validate());
+    // Hard CHECK since this is already validated earlier
+    // during request validation.
+    CHECK_NONE(quotaTree.validate());
 
-        return quotaTree.total();
-      }());
+    return quotaTree.totalGuarantees();
+  }();
 
   // Determine whether quota overcommits the cluster.
   ResourceQuantities capacity;
@@ -220,12 +226,12 @@ Option<Error> Master::QuotaHandler::overcommitCheck(
         agent.nonRevocable().scalars());
   }
 
-  if (!capacity.contains(totalQuota)) {
+  if (!capacity.contains(totalGuarantees)) {
     // TODO(bmahler): Specialize this message based on whether
     // this request leads to the overcommit vs the quota was
     // already overcommitted.
     return Error(
-        "Total quota guarantees '" + stringify(totalQuota) + "'"
+        "Total quota guarantees '" + stringify(totalGuarantees) + "'"
         " exceed cluster capacity '" + stringify(capacity) + "'");
   }
 
@@ -538,11 +544,11 @@ Future<http::Response> Master::QuotaHandler::_set(
 
     foreachpair (const string& role, const Quota& quota, master->quotas) {
       if (role != quotaInfo.role()) {
-        quotaTree.insert(role, quota);
+        quotaTree.insert(role, Quota2(quota.info));
       }
     }
 
-    quotaTree.insert(quotaInfo.role(), Quota{quotaInfo});
+    quotaTree.insert(quotaInfo.role(), Quota2{quotaInfo});
 
     Option<Error> error = quotaTree.validate();
     if (error.isSome()) {


[mesos] 03/06: Refactored allocator with the new quota wrapper struct.

Posted by mz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mzhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 495162eefa12900b3a74bfbb269851473df4cce9
Author: Meng Zhu <mz...@mesosphere.io>
AuthorDate: Wed Jun 5 14:04:53 2019 -0700

    Refactored allocator with the new quota wrapper struct.
    
    This patch also introduces a constant `DEFAULT_QUOTA`.
    By default, a role has no guarantees and no limits.
    
    Review: https://reviews.apache.org/r/70801
---
 src/master/allocator/mesos/hierarchical.cpp | 49 +++++++++--------------------
 src/master/allocator/mesos/hierarchical.hpp | 19 ++---------
 src/master/constants.hpp                    |  4 +++
 3 files changed, 22 insertions(+), 50 deletions(-)

diff --git a/src/master/allocator/mesos/hierarchical.cpp b/src/master/allocator/mesos/hierarchical.cpp
index ad0bcca..a7c237e 100644
--- a/src/master/allocator/mesos/hierarchical.cpp
+++ b/src/master/allocator/mesos/hierarchical.cpp
@@ -1410,21 +1410,12 @@ void HierarchicalAllocatorProcess::setQuota(
   // the role is not set. Setting quota differs from updating it because
   // the former moves the role to a different allocation group with a
   // dedicated sorter, while the later just updates the actual quota.
-  CHECK(getGuarantees(role).empty() && getLimits(role).empty());
+  CHECK(getQuota(role) == DEFAULT_QUOTA);
 
   // In the legacy `SetQuota` call, quota guarantee also acts as quota limits.
   //
   // Note, quota has been validated to have no duplicate resource names.
-  roles[role].quotaLimits = [&]() {
-    google::protobuf::Map<string, Value::Scalar> limits;
-    foreach (const Resource& r, quota.info.guarantee()) {
-      limits[r.name()] = r.scalar();
-    }
-    return ResourceLimits(limits);
-  }();
-
-  roles[role].quotaGuarantees =
-    ResourceQuantities::fromScalarResources(quota.info.guarantee());
+  roles[role].quota = Quota2(quota.info);
 
   metrics.setQuota(role, quota);
 
@@ -1447,16 +1438,16 @@ void HierarchicalAllocatorProcess::removeQuota(
   CHECK(initialized);
 
   // Do not allow removing quota if it is not set.
-  CHECK(!getGuarantees(role).empty() || !getLimits(role).empty());
+  // Note: it is impossible to set to default quota using the old `setQuota`.
+  CHECK(getQuota(role) != DEFAULT_QUOTA);
 
   Role& r = roles.at(role);
 
   // TODO(alexr): Print all quota info for the role.
-  LOG(INFO) << "Removed quota " << r.quotaGuarantees
+  LOG(INFO) << "Removed quota " << r.quota.guarantees
             << " for role '" << role << "'";
 
-  r.quotaGuarantees = ResourceQuantities();
-  r.quotaLimits = ResourceLimits();
+  r.quota = DEFAULT_QUOTA;
   if (r.isEmpty()) {
     roles.erase(role);
   }
@@ -1657,7 +1648,7 @@ void HierarchicalAllocatorProcess::__allocate()
   foreachpair (const string& role, const Role& r, roles) {
     // TODO(mzhu): Track all role consumed quota. We may want to expose
     // these as metrics.
-    if (!r.quotaGuarantees.empty() || !r.quotaLimits.empty()) {
+    if (r.quota != DEFAULT_QUOTA) {
       logHeadroomInfo = true;
       // Note, `reservationScalarQuantities` in `struct role`
       // is hierarchical aware, thus it also includes subrole reservations.
@@ -1674,8 +1665,7 @@ void HierarchicalAllocatorProcess::__allocate()
     const string& topLevelRole = strings::contains(role, "/") ?
       role.substr(0, role.find('/')) : role;
 
-    if (getGuarantees(topLevelRole).empty() &&
-        getLimits(topLevelRole).empty()) {
+    if (getQuota(topLevelRole) == DEFAULT_QUOTA) {
       continue;
     }
 
@@ -1701,7 +1691,7 @@ void HierarchicalAllocatorProcess::__allocate()
   ResourceQuantities requiredHeadroom;
   foreachpair (const string& role, const Role& r, roles) {
     requiredHeadroom +=
-      r.quotaGuarantees -
+      r.quota.guarantees -
       rolesConsumedQuota.get(role).getOrElse(ResourceQuantities());
   }
 
@@ -1788,8 +1778,8 @@ void HierarchicalAllocatorProcess::__allocate()
     Slave& slave = slaves.at(slaveId);
 
     foreach (const string& role, roleSorter->sort()) {
-      const ResourceQuantities& quotaGuarantees = getGuarantees(role);
-      const ResourceLimits& quotaLimits = getLimits(role);
+      const ResourceQuantities& quotaGuarantees = getQuota(role).guarantees;
+      const ResourceLimits& quotaLimits = getQuota(role).limits;
 
       // We only allocate to roles with non-default guarantees
       // in the first stage.
@@ -2039,7 +2029,7 @@ void HierarchicalAllocatorProcess::__allocate()
         break; // Nothing left on this agent.
       }
 
-      const ResourceLimits& quotaLimits = getLimits(role);
+      const ResourceLimits& quotaLimits = getQuota(role).limits;
 
       // NOTE: Suppressed frameworks are not included in the sort.
       CHECK_CONTAINS(frameworkSorters, role);
@@ -2137,7 +2127,7 @@ void HierarchicalAllocatorProcess::__allocate()
         ResourceQuantities increasedQuotaConsumption =
           ResourceQuantities::fromScalarResources(additionalScalarAllocation);
 
-        if (!getGuarantees(role).empty() || !getLimits(role).empty()) {
+        if (getQuota(role) != DEFAULT_QUOTA) {
           rolesConsumedQuota[role] += increasedQuotaConsumption;
           for (const string& ancestor : roles::ancestors(role)) {
             rolesConsumedQuota[ancestor] += increasedQuotaConsumption;
@@ -2557,18 +2547,9 @@ bool HierarchicalAllocatorProcess::isFrameworkTrackedUnderRole(
 }
 
 
-const ResourceQuantities& HierarchicalAllocatorProcess::getGuarantees(
-    const string& role) const
-{
-  return roles.contains(role) ? roles.at(role).quotaGuarantees
-                              : defaultQuotaGuarantees;
-}
-
-
-const ResourceLimits& HierarchicalAllocatorProcess::getLimits(
-    const string& role) const
+const Quota2& HierarchicalAllocatorProcess::getQuota(const string& role) const
 {
-  return roles.contains(role) ? roles.at(role).quotaLimits : defaultQuotaLimits;
+  return roles.contains(role) ? roles.at(role).quota : DEFAULT_QUOTA;
 }
 
 
diff --git a/src/master/allocator/mesos/hierarchical.hpp b/src/master/allocator/mesos/hierarchical.hpp
index 1a3329d..0a6482a 100644
--- a/src/master/allocator/mesos/hierarchical.hpp
+++ b/src/master/allocator/mesos/hierarchical.hpp
@@ -119,15 +119,13 @@ struct Role
 
   // Configured guaranteed resource quantities and resource limits for
   // this role. By default, a role has no guarantee and no limit.
-  ResourceQuantities quotaGuarantees;
-  ResourceLimits quotaLimits;
+  Quota2 quota;
 
   bool isEmpty() const
   {
     return frameworks.empty() &&
            reservationScalarQuantities.empty() &&
-           quotaGuarantees.empty() &&
-           quotaLimits.empty();
+           quota == DEFAULT_QUOTA;
   }
 };
 
@@ -619,23 +617,12 @@ protected:
   // Factory function for framework sorters.
   const std::function<Sorter*()> frameworkSorterFactory;
 
-  // By default, roles have empty quota guarantees and limits.
-  //
-  // We keep this in memory so that roles that are absent in the `roles` map
-  // could also keep their quota state in memory.
-  //
-  // TODO(mzhu): remove this once we have proper role life cycle management
-  // such that every role would have an entry in the `roles` map.
-  const ResourceQuantities defaultQuotaGuarantees;
-  const ResourceLimits defaultQuotaLimits;
-
 private:
   bool isFrameworkTrackedUnderRole(
       const FrameworkID& frameworkId,
       const std::string& role) const;
 
-  const ResourceQuantities& getGuarantees(const std::string& role) const;
-  const ResourceLimits& getLimits(const std::string& role) const;
+  const Quota2& getQuota(const std::string& role) const;
 
   void trackFrameworkUnderRole(
       const FrameworkID& frameworkId,
diff --git a/src/master/constants.hpp b/src/master/constants.hpp
index 8f729d1..8502240 100644
--- a/src/master/constants.hpp
+++ b/src/master/constants.hpp
@@ -20,6 +20,7 @@
 #include <stdint.h>
 
 #include <mesos/mesos.hpp>
+#include <mesos/quota/quota.hpp>
 
 #include <stout/bytes.hpp>
 #include <stout/duration.hpp>
@@ -172,6 +173,9 @@ const Version MINIMUM_AGENT_VERSION = Version(1, 0, 0);
 
 std::vector<MasterInfo::Capability> MASTER_CAPABILITIES();
 
+// A role's default quota: no guarantees and no limits.
+const Quota2 DEFAULT_QUOTA;
+
 } // namespace master {
 } // namespace internal {
 } // namespace mesos {


[mesos] 06/06: Used the new quota struct for the allocator recover call.

Posted by mz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mzhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit ceb1120e8c53771219363e0bf579a770b914a592
Author: Meng Zhu <mz...@mesosphere.io>
AuthorDate: Thu Jun 6 16:18:45 2019 -0700

    Used the new quota struct for the allocator recover call.
    
    Review: https://reviews.apache.org/r/70804
---
 include/mesos/allocator/allocator.hpp       | 2 +-
 src/master/allocator/mesos/allocator.hpp    | 6 +++---
 src/master/allocator/mesos/hierarchical.cpp | 6 +++---
 src/master/allocator/mesos/hierarchical.hpp | 2 +-
 src/master/master.cpp                       | 8 +++++++-
 src/tests/allocator.hpp                     | 2 +-
 6 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/include/mesos/allocator/allocator.hpp b/include/mesos/allocator/allocator.hpp
index a01775b..0aef003 100644
--- a/include/mesos/allocator/allocator.hpp
+++ b/include/mesos/allocator/allocator.hpp
@@ -141,7 +141,7 @@ public:
    */
   virtual void recover(
       const int expectedAgentCount,
-      const hashmap<std::string, Quota>& quotas) = 0;
+      const hashmap<std::string, Quota2>& quotas) = 0;
 
   /**
    * Adds a framework to the Mesos cluster. The allocator is invoked when
diff --git a/src/master/allocator/mesos/allocator.hpp b/src/master/allocator/mesos/allocator.hpp
index 3c04b4f..0ae7e36 100644
--- a/src/master/allocator/mesos/allocator.hpp
+++ b/src/master/allocator/mesos/allocator.hpp
@@ -59,7 +59,7 @@ public:
 
   void recover(
       const int expectedAgentCount,
-      const hashmap<std::string, Quota>& quotas) override;
+      const hashmap<std::string, Quota2>& quotas) override;
 
   void addFramework(
       const FrameworkID& frameworkId,
@@ -210,7 +210,7 @@ public:
 
   virtual void recover(
       const int expectedAgentCount,
-      const hashmap<std::string, Quota>& quotas) = 0;
+      const hashmap<std::string, Quota2>& quotas) = 0;
 
   virtual void addFramework(
       const FrameworkID& frameworkId,
@@ -380,7 +380,7 @@ inline void MesosAllocator<AllocatorProcess>::initialize(
 template <typename AllocatorProcess>
 inline void MesosAllocator<AllocatorProcess>::recover(
     const int expectedAgentCount,
-    const hashmap<std::string, Quota>& quotas)
+    const hashmap<std::string, Quota2>& quotas)
 {
   process::dispatch(
       process,
diff --git a/src/master/allocator/mesos/hierarchical.cpp b/src/master/allocator/mesos/hierarchical.cpp
index 50a4982..0d8b0ca 100644
--- a/src/master/allocator/mesos/hierarchical.cpp
+++ b/src/master/allocator/mesos/hierarchical.cpp
@@ -225,7 +225,7 @@ void HierarchicalAllocatorProcess::initialize(
 
 void HierarchicalAllocatorProcess::recover(
     const int _expectedAgentCount,
-    const hashmap<string, Quota>& quotas)
+    const hashmap<string, Quota2>& quotas)
 {
   // Recovery should start before actual allocation starts.
   CHECK(initialized);
@@ -249,8 +249,8 @@ void HierarchicalAllocatorProcess::recover(
     return;
   }
 
-  foreachpair (const string& role, const Quota& quota, quotas) {
-    setQuota(role, quota);
+  foreachpair (const string& role, const Quota2& quota, quotas) {
+    updateQuota(role, quota);
   }
 
   // TODO(alexr): Consider exposing these constants.
diff --git a/src/master/allocator/mesos/hierarchical.hpp b/src/master/allocator/mesos/hierarchical.hpp
index 93555f6..25472eb 100644
--- a/src/master/allocator/mesos/hierarchical.hpp
+++ b/src/master/allocator/mesos/hierarchical.hpp
@@ -323,7 +323,7 @@ public:
 
   void recover(
       const int _expectedAgentCount,
-      const hashmap<std::string, Quota>& quotas) override;
+      const hashmap<std::string, Quota2>& quotas) override;
 
   void addFramework(
       const FrameworkID& frameworkId,
diff --git a/src/master/master.cpp b/src/master/master.cpp
index 2b4e51d..8a238aa 100644
--- a/src/master/master.cpp
+++ b/src/master/master.cpp
@@ -1740,7 +1740,13 @@ Future<Nothing> Master::_recover(const Registry& registry)
   // allocations. An allocator may decide to hold off with allocation
   // until after it restores a view of the cluster state.
   int expectedAgentCount = registry.slaves().slaves().size();
-  allocator->recover(expectedAgentCount, quotas);
+
+  // TODO(mzhu): Remove this conversion once we replace `Quota` with `Quota2`.
+  hashmap<string, Quota2> quota2s;
+  foreachpair (const string& role, const Quota& quota, quotas) {
+    quota2s[role] = Quota2{quota.info};
+  }
+  allocator->recover(expectedAgentCount, quota2s);
 
   // TODO(alexr): Consider adding a sanity check: whether quotas are
   // satisfiable given all recovering agents reregister. We may want
diff --git a/src/tests/allocator.hpp b/src/tests/allocator.hpp
index bc6e3a9..765968d 100644
--- a/src/tests/allocator.hpp
+++ b/src/tests/allocator.hpp
@@ -422,7 +422,7 @@ public:
 
   MOCK_METHOD2(recover, void(
       const int expectedAgentCount,
-      const hashmap<std::string, Quota>&));
+      const hashmap<std::string, Quota2>&));
 
   MOCK_METHOD5(addFramework, void(
       const FrameworkID&,


[mesos] 04/06: Added `Metrics::updateQuota` for quota metrics.

Posted by mz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mzhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 4bdbd8e7da5063d55726b628b5e0d31c79650d3f
Author: Meng Zhu <mz...@mesosphere.io>
AuthorDate: Thu Jun 6 15:58:05 2019 -0700

    Added `Metrics::updateQuota` for quota metrics.
    
    This intends to replace the existing ``Metrics::setQuota` and
    `Metrics::remove` calls.
    
    Currently, it only tracks guarantees. Need to add limits metrics.
    
    Review: https://reviews.apache.org/r/70802
---
 src/master/allocator/mesos/metrics.cpp | 59 ++++++++++++++++++++++++++++++++++
 src/master/allocator/mesos/metrics.hpp |  3 ++
 2 files changed, 62 insertions(+)

diff --git a/src/master/allocator/mesos/metrics.cpp b/src/master/allocator/mesos/metrics.cpp
index 5533eb9..b61a39b 100644
--- a/src/master/allocator/mesos/metrics.cpp
+++ b/src/master/allocator/mesos/metrics.cpp
@@ -139,6 +139,7 @@ void Metrics::setQuota(const string& role, const Quota& quota)
     CHECK_EQ(Value::SCALAR, resource.type());
     double value = resource.scalar().value();
 
+    // TODO(mzhu): use PushGauge for guarantees.
     PullGauge guarantee = PullGauge(
         "allocator/mesos/quota"
         "/roles/" + role +
@@ -146,6 +147,8 @@ void Metrics::setQuota(const string& role, const Quota& quota)
         "/guarantee",
         process::defer([value]() { return value; }));
 
+    // TODO(mzhu): expose this for every role, including roles
+    // with default quota.
     PullGauge offered_or_allocated(
         "allocator/mesos/quota"
         "/roles/" + role +
@@ -186,6 +189,62 @@ void Metrics::removeQuota(const string& role)
 }
 
 
+// TODO(mzhu): This currently only updates quota guarantees.
+// Add metrics for quota limits as well.
+void Metrics::updateQuota(const string& role, const Quota2& quota)
+{
+  // This is the "remove" case where the role's quota
+  // is set to the default.
+  if (quota.guarantees == DEFAULT_QUOTA.guarantees) {
+    foreachvalue (const PullGauge& gauge, quota_allocated[role]) {
+      process::metrics::remove(gauge);
+    }
+
+    foreachvalue (const PullGauge& gauge, quota_guarantee[role]) {
+      process::metrics::remove(gauge);
+    }
+
+    quota_allocated.erase(role);
+    quota_guarantee.erase(role);
+
+    return;
+  }
+
+  hashmap<string, PullGauge> allocated;
+  hashmap<string, PullGauge> guarantees;
+
+  foreach (auto& quantity, quota.guarantees) {
+    double value = quantity.second.value();
+
+    PullGauge guarantee = PullGauge(
+        "allocator/mesos/quota"
+        "/roles/" + role +
+        "/resources/" + quantity.first +
+        "/guarantee",
+        process::defer([value]() { return value; }));
+
+    PullGauge offered_or_allocated(
+        "allocator/mesos/quota"
+        "/roles/" + role +
+        "/resources/" + quantity.first +
+        "/offered_or_allocated",
+        defer(allocator,
+              &HierarchicalAllocatorProcess::_quota_allocated,
+              role,
+              quantity.first));
+
+    guarantees.put(quantity.first, guarantee);
+    allocated.put(quantity.first, offered_or_allocated);
+
+    process::metrics::add(guarantee);
+    process::metrics::add(offered_or_allocated);
+  }
+
+  quota_allocated[role] = allocated;
+  quota_guarantee[role] = guarantees;
+}
+
+
 void Metrics::addRole(const string& role)
 {
   CHECK(!offer_filters_active.contains(role));
diff --git a/src/master/allocator/mesos/metrics.hpp b/src/master/allocator/mesos/metrics.hpp
index a26278a..a0953b0 100644
--- a/src/master/allocator/mesos/metrics.hpp
+++ b/src/master/allocator/mesos/metrics.hpp
@@ -48,9 +48,12 @@ struct Metrics
 
   ~Metrics();
 
+  // TODO(mzhu): Remove these in favor of `updateQuota`.
   void setQuota(const std::string& role, const Quota& quota);
   void removeQuota(const std::string& role);
 
+  void updateQuota(const std::string& role, const Quota2& quota);
+
   void addRole(const std::string& role);
   void removeRole(const std::string& role);
 


[mesos] 05/06: Added `updateQuota` method to the allocator.

Posted by mz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mzhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 4703b23143ee806ed5e68d9ff6eabe9600ffc9c9
Author: Meng Zhu <mz...@mesosphere.io>
AuthorDate: Wed Jun 5 16:44:00 2019 -0700

    Added `updateQuota` method to the allocator.
    
    This call updates a role's quota guarantees and limits.
    All roles have a default quota defined as `DEFAULT_QUOTA`.
    Currently, it is no guarantees and limits. Thus to "remove"
    a quota, one should simply update the quota to be
    `DEFAULT_QUOTA`.
    
    Master `setQuota` and `removeQuota` calls into the allocator
    are replaced with the `updateQuota`.
    
    `setQuota` and `removeQuota` calls are now only used in the tests.
    They will be removed once those tests are refactored.
    
    Also fixed affected tests.
    
    Review: https://reviews.apache.org/r/70803
---
 include/mesos/allocator/allocator.hpp       |  18 ++++
 src/master/allocator/mesos/allocator.hpp    |  21 +++++
 src/master/allocator/mesos/hierarchical.cpp |  16 ++++
 src/master/allocator/mesos/hierarchical.hpp |   4 +
 src/master/quota_handler.cpp                |   4 +-
 src/tests/allocator.hpp                     |  15 ++++
 src/tests/master_quota_tests.cpp            | 122 ++++++++++++++--------------
 7 files changed, 136 insertions(+), 64 deletions(-)

diff --git a/include/mesos/allocator/allocator.hpp b/include/mesos/allocator/allocator.hpp
index eafcdfd..a01775b 100644
--- a/include/mesos/allocator/allocator.hpp
+++ b/include/mesos/allocator/allocator.hpp
@@ -430,6 +430,9 @@ public:
    *
    * TODO(alexr): Consider adding an `updateQuota()` method which allows
    * updating existing quota.
+   *
+   * TODO(mzhu): Remove this call in favor of `updateQuota`.
+   *
    */
   virtual void setQuota(
       const std::string& role,
@@ -448,11 +451,26 @@ public:
    *
    * TODO(alexr): Consider returning a future which an allocator can fail in
    * order to report failure.
+   *
+   * TODO(mzhu): Remove this call in favor of `updateQuota`.
    */
   virtual void removeQuota(
       const std::string& role) = 0;
 
   /**
+   * Informs the allocator to update quota for the given role.
+   *
+   * It is up to the allocator implementation how to satisfy quota. An
+   * implementation may employ different strategies for roles with or
+   * without quota. All roles have a default quota defined as `DEFAULT_QUOTA`.
+   * Currently, it is no guarantees and no limits. Thus to "remove" a quota,
+   * one should simply update the quota to be `DEFAULT_QUOTA`.
+   */
+  virtual void updateQuota(
+      const std::string& role,
+      const Quota2& quota) = 0;
+
+  /**
    * Updates the weight associated with one or more roles. If a role
    * was previously configured to have a weight and that role is
    * omitted from this list, it keeps its old weight.
diff --git a/src/master/allocator/mesos/allocator.hpp b/src/master/allocator/mesos/allocator.hpp
index 2d83f38..3c04b4f 100644
--- a/src/master/allocator/mesos/allocator.hpp
+++ b/src/master/allocator/mesos/allocator.hpp
@@ -165,6 +165,10 @@ public:
   void removeQuota(
       const std::string& role) override;
 
+  void updateQuota(
+      const std::string& role,
+      const Quota2& quota) override;
+
   void updateWeights(
       const std::vector<WeightInfo>& weightInfos) override;
 
@@ -312,6 +316,10 @@ public:
   virtual void removeQuota(
       const std::string& role) = 0;
 
+  virtual void updateQuota(
+      const std::string& role,
+      const Quota2& quota) = 0;
+
   virtual void updateWeights(
       const std::vector<WeightInfo>& weightInfos) = 0;
 
@@ -701,6 +709,19 @@ inline void MesosAllocator<AllocatorProcess>::removeQuota(
 
 
 template <typename AllocatorProcess>
+inline void MesosAllocator<AllocatorProcess>::updateQuota(
+    const std::string& role,
+    const Quota2& quota)
+{
+  process::dispatch(
+      process,
+      &MesosAllocatorProcess::updateQuota,
+      role,
+      quota);
+}
+
+
+template <typename AllocatorProcess>
 inline void MesosAllocator<AllocatorProcess>::updateWeights(
     const std::vector<WeightInfo>& weightInfos)
 {
diff --git a/src/master/allocator/mesos/hierarchical.cpp b/src/master/allocator/mesos/hierarchical.cpp
index a7c237e..50a4982 100644
--- a/src/master/allocator/mesos/hierarchical.cpp
+++ b/src/master/allocator/mesos/hierarchical.cpp
@@ -1463,6 +1463,22 @@ void HierarchicalAllocatorProcess::removeQuota(
 }
 
 
+void HierarchicalAllocatorProcess::updateQuota(
+    const string& role,
+    const Quota2& quota)
+{
+  CHECK(initialized);
+
+  roles[role].quota = quota;
+
+  metrics.updateQuota(role, quota);
+
+  LOG(INFO) << "Updated quota for role '" << role << "', "
+            << " guarantees: " << quota.guarantees
+            << " limits: " << quota.limits;
+}
+
+
 void HierarchicalAllocatorProcess::updateWeights(
     const vector<WeightInfo>& weightInfos)
 {
diff --git a/src/master/allocator/mesos/hierarchical.hpp b/src/master/allocator/mesos/hierarchical.hpp
index 0a6482a..93555f6 100644
--- a/src/master/allocator/mesos/hierarchical.hpp
+++ b/src/master/allocator/mesos/hierarchical.hpp
@@ -429,6 +429,10 @@ public:
   void removeQuota(
       const std::string& role) override;
 
+  void updateQuota(
+      const std::string& role,
+      const Quota2& quota) override;
+
   void updateWeights(
       const std::vector<WeightInfo>& weightInfos) override;
 
diff --git a/src/master/quota_handler.cpp b/src/master/quota_handler.cpp
index b51e663..854189c 100644
--- a/src/master/quota_handler.cpp
+++ b/src/master/quota_handler.cpp
@@ -642,7 +642,7 @@ Future<http::Response> Master::QuotaHandler::__set(
       // See the top comment in "master/quota.hpp" for why this check is here.
       CHECK(result);
 
-      master->allocator->setQuota(quotaInfo.role(), quota);
+      master->allocator->updateQuota(quotaInfo.role(), Quota2{quota.info});
 
       // Rescind outstanding offers to facilitate satisfying the quota request.
       // NOTE: We set quota before we rescind to avoid a race. If we were to
@@ -764,7 +764,7 @@ Future<http::Response> Master::QuotaHandler::__remove(const string& role) const
       // See the top comment in "master/quota.hpp" for why this check is here.
       CHECK(result);
 
-      master->allocator->removeQuota(role);
+      master->allocator->updateQuota(role, DEFAULT_QUOTA);
 
       return OK();
     }));
diff --git a/src/tests/allocator.hpp b/src/tests/allocator.hpp
index 0718bef..bc6e3a9 100644
--- a/src/tests/allocator.hpp
+++ b/src/tests/allocator.hpp
@@ -212,6 +212,12 @@ ACTION_P(InvokeRemoveQuota, allocator)
 }
 
 
+ACTION_P(InvokeUpdateQuota, allocator)
+{
+  allocator->real->updateQuota(arg0, arg1);
+}
+
+
 ACTION_P(InvokeUpdateWeights, allocator)
 {
   allocator->real->updateWeights(arg0);
@@ -382,6 +388,11 @@ public:
     EXPECT_CALL(*this, removeQuota(_))
       .WillRepeatedly(DoDefault());
 
+    ON_CALL(*this, updateQuota(_, _))
+      .WillByDefault(InvokeUpdateQuota(this));
+    EXPECT_CALL(*this, updateQuota(_, _))
+      .WillRepeatedly(DoDefault());
+
     ON_CALL(*this, updateWeights(_))
       .WillByDefault(InvokeUpdateWeights(this));
     EXPECT_CALL(*this, updateWeights(_))
@@ -516,6 +527,10 @@ public:
   MOCK_METHOD1(removeQuota, void(
       const std::string&));
 
+  MOCK_METHOD2(updateQuota, void(
+      const std::string&,
+      const Quota2&));
+
   MOCK_METHOD1(updateWeights, void(
       const std::vector<WeightInfo>&));
 
diff --git a/src/tests/master_quota_tests.cpp b/src/tests/master_quota_tests.cpp
index 146b977..9b4cde1 100644
--- a/src/tests/master_quota_tests.cpp
+++ b/src/tests/master_quota_tests.cpp
@@ -39,6 +39,7 @@
 
 #include "common/resources_utils.hpp"
 
+#include "master/constants.hpp"
 #include "master/flags.hpp"
 #include "master/master.hpp"
 
@@ -487,17 +488,15 @@ TEST_F(MasterQuotaTest, RemoveSingleQuota)
     EXPECT_EQ(1, metrics.values[metricKey]);
 
     // Remove the previously requested quota.
-    Future<Nothing> receivedRemoveRequest;
-    EXPECT_CALL(allocator, removeQuota(Eq(ROLE1)))
-      .WillOnce(DoAll(InvokeRemoveQuota(&allocator),
-                      FutureSatisfy(&receivedRemoveRequest)));
+    Future<Nothing> receivedQuotaRequest;
+    EXPECT_CALL(allocator, updateQuota(Eq(ROLE1), Eq(master::DEFAULT_QUOTA)))
+      .WillOnce(DoAll(
+          InvokeUpdateQuota(&allocator), FutureSatisfy(&receivedQuotaRequest)));
 
     response = removeQuota("quota/" + ROLE1);
 
     AWAIT_EXPECT_RESPONSE_STATUS_EQ(OK().status, response);
-
-    // Ensure that the quota remove request has reached the allocator.
-    AWAIT_READY(receivedRemoveRequest);
+    AWAIT_READY(receivedQuotaRequest);
 
     // Ensure metrics update is finished.
     Clock::settle();
@@ -840,9 +839,9 @@ TEST_F(MasterQuotaTest, AvailableResourcesSingleAgent)
   EXPECT_TRUE(agentTotalResources->contains(quotaResources));
 
   // Send a quota request for the specified role.
-  Future<Quota> receivedQuotaRequest;
-  EXPECT_CALL(allocator, setQuota(Eq(ROLE1), _))
-    .WillOnce(DoAll(InvokeSetQuota(&allocator),
+  Future<Quota2> receivedQuotaRequest;
+  EXPECT_CALL(allocator, updateQuota(Eq(ROLE1), _))
+    .WillOnce(DoAll(InvokeUpdateQuota(&allocator),
                     FutureArg<1>(&receivedQuotaRequest)));
 
   Future<Response> response = process::http::post(
@@ -857,8 +856,9 @@ TEST_F(MasterQuotaTest, AvailableResourcesSingleAgent)
   // got lost in-between.
   AWAIT_READY(receivedQuotaRequest);
 
-  EXPECT_EQ(ROLE1, receivedQuotaRequest->info.role());
-  EXPECT_EQ(quotaResources, receivedQuotaRequest->info.guarantee());
+  EXPECT_EQ(
+      ResourceQuantities::fromScalarResources(quotaResources),
+      receivedQuotaRequest->guarantees);
 }
 
 
@@ -894,9 +894,9 @@ TEST_F(MasterQuotaTest, AvailableResourcesSingleReservedAgent)
   Resources quotaResources = Resources::parse("cpus:1;mem:512").get();
 
   // Send a quota request for the specified role.
-  Future<Quota> receivedQuotaRequest;
-  EXPECT_CALL(allocator, setQuota(Eq(ROLE1), _))
-    .WillOnce(DoAll(InvokeSetQuota(&allocator),
+  Future<Quota2> receivedQuotaRequest;
+  EXPECT_CALL(allocator, updateQuota(Eq(ROLE1), _))
+    .WillOnce(DoAll(InvokeUpdateQuota(&allocator),
                     FutureArg<1>(&receivedQuotaRequest)));
 
   Future<Response> response = process::http::post(
@@ -911,8 +911,9 @@ TEST_F(MasterQuotaTest, AvailableResourcesSingleReservedAgent)
   // got lost in-between.
   AWAIT_READY(receivedQuotaRequest);
 
-  EXPECT_EQ(ROLE1, receivedQuotaRequest->info.role());
-  EXPECT_EQ(quotaResources, receivedQuotaRequest->info.guarantee());
+  EXPECT_EQ(
+      ResourceQuantities::fromScalarResources(quotaResources),
+      receivedQuotaRequest->guarantees);
 }
 
 
@@ -960,9 +961,9 @@ TEST_F(MasterQuotaTest, AvailableResourcesSingleDisconnectedAgent)
   EXPECT_TRUE(agentTotalResources->contains(quotaResources));
 
   // Send a quota request for the specified role.
-  Future<Quota> receivedQuotaRequest;
-  EXPECT_CALL(allocator, setQuota(Eq(ROLE1), _))
-    .WillOnce(DoAll(InvokeSetQuota(&allocator),
+  Future<Quota2> receivedQuotaRequest;
+  EXPECT_CALL(allocator, updateQuota(Eq(ROLE1), _))
+    .WillOnce(DoAll(InvokeUpdateQuota(&allocator),
                     FutureArg<1>(&receivedQuotaRequest)));
 
   Future<Response> response = process::http::post(
@@ -977,8 +978,9 @@ TEST_F(MasterQuotaTest, AvailableResourcesSingleDisconnectedAgent)
   // got lost in-between.
   AWAIT_READY(receivedQuotaRequest);
 
-  EXPECT_EQ(ROLE1, receivedQuotaRequest->info.role());
-  EXPECT_EQ(quotaResources, receivedQuotaRequest->info.guarantee());
+  EXPECT_EQ(
+      ResourceQuantities::fromScalarResources(quotaResources),
+      receivedQuotaRequest->guarantees);
 }
 
 
@@ -1028,9 +1030,9 @@ TEST_F(MasterQuotaTest, AvailableResourcesMultipleAgents)
     });
 
   // Send a quota request for the specified role.
-  Future<Quota> receivedQuotaRequest;
-  EXPECT_CALL(allocator, setQuota(Eq(ROLE1), _))
-    .WillOnce(DoAll(InvokeSetQuota(&allocator),
+  Future<Quota2> receivedQuotaRequest;
+  EXPECT_CALL(allocator, updateQuota(Eq(ROLE1), _))
+    .WillOnce(DoAll(InvokeUpdateQuota(&allocator),
                     FutureArg<1>(&receivedQuotaRequest)));
 
   Future<Response> response = process::http::post(
@@ -1044,9 +1046,9 @@ TEST_F(MasterQuotaTest, AvailableResourcesMultipleAgents)
   // Quota request is granted and reached the allocator. Make sure nothing
   // got lost in-between.
   AWAIT_READY(receivedQuotaRequest);
-
-  EXPECT_EQ(ROLE1, receivedQuotaRequest->info.role());
-  EXPECT_EQ(quotaResources, receivedQuotaRequest->info.guarantee());
+  EXPECT_EQ(
+      ResourceQuantities::fromScalarResources(quotaResources),
+      receivedQuotaRequest->guarantees);
 }
 
 
@@ -1188,9 +1190,9 @@ TEST_F(MasterQuotaTest, AvailableResourcesAfterRescinding)
     .WillRepeatedly(Return());
 
   // Send a quota request for the specified role.
-  Future<Quota> receivedQuotaRequest;
-  EXPECT_CALL(allocator, setQuota(Eq(ROLE2), _))
-    .WillOnce(DoAll(InvokeSetQuota(&allocator),
+  Future<Quota2> receivedQuotaRequest;
+  EXPECT_CALL(allocator, updateQuota(Eq(ROLE2), _))
+    .WillOnce(DoAll(InvokeUpdateQuota(&allocator),
                     FutureArg<1>(&receivedQuotaRequest)));
 
   Future<Response> response = process::http::post(
@@ -1215,8 +1217,9 @@ TEST_F(MasterQuotaTest, AvailableResourcesAfterRescinding)
   // The quota request is granted and reached the allocator. Make sure nothing
   // got lost in-between.
   AWAIT_READY(receivedQuotaRequest);
-  EXPECT_EQ(ROLE2, receivedQuotaRequest->info.role());
-  EXPECT_EQ(quotaResources, receivedQuotaRequest->info.guarantee());
+  EXPECT_EQ(
+      ResourceQuantities::fromScalarResources(quotaResources),
+      receivedQuotaRequest->guarantees);
 
   // Ensure `RescindResourceOfferMessage`s are processed by `sched1`.
   AWAIT_READY(offerRescinded1);
@@ -1300,10 +1303,10 @@ TEST_F(MasterQuotaTest, RecoverQuotaEmptyCluster)
 
   // Delete quota.
   {
-    Future<Nothing> receivedRemoveRequest;
-    EXPECT_CALL(allocator, removeQuota(Eq(ROLE1)))
-      .WillOnce(DoAll(InvokeRemoveQuota(&allocator),
-                      FutureSatisfy(&receivedRemoveRequest)));
+    Future<Nothing> receivedQuotaRequest;
+    EXPECT_CALL(allocator, updateQuota(Eq(ROLE1), Eq(master::DEFAULT_QUOTA)))
+      .WillOnce(DoAll(
+          InvokeUpdateQuota(&allocator), FutureSatisfy(&receivedQuotaRequest)));
 
     Future<Response> response = process::http::requestDelete(
         master.get()->pid,
@@ -1311,7 +1314,7 @@ TEST_F(MasterQuotaTest, RecoverQuotaEmptyCluster)
         createBasicAuthHeaders(DEFAULT_CREDENTIAL));
 
     // Quota request succeeds and reaches the allocator.
-    AWAIT_READY(receivedRemoveRequest);
+    AWAIT_READY(receivedQuotaRequest);
     AWAIT_EXPECT_RESPONSE_STATUS_EQ(OK().status, response);
   }
 }
@@ -1345,9 +1348,9 @@ TEST_F(MasterQuotaTest, NoAuthenticationNoAuthorization)
   {
     Resources quotaResources = Resources::parse("cpus:1;mem:512").get();
 
-    Future<Quota> receivedSetRequest;
-    EXPECT_CALL(allocator, setQuota(Eq(ROLE1), _))
-      .WillOnce(DoAll(InvokeSetQuota(&allocator),
+    Future<Quota2> receivedSetRequest;
+    EXPECT_CALL(allocator, updateQuota(Eq(ROLE1), _))
+      .WillOnce(DoAll(InvokeUpdateQuota(&allocator),
                       FutureArg<1>(&receivedSetRequest)));
 
     // Send a set quota request with absent credentials.
@@ -1364,10 +1367,10 @@ TEST_F(MasterQuotaTest, NoAuthenticationNoAuthorization)
 
   // Check whether quota can be removed.
   {
-    Future<Nothing> receivedRemoveRequest;
-    EXPECT_CALL(allocator, removeQuota(Eq(ROLE1)))
-      .WillOnce(DoAll(InvokeRemoveQuota(&allocator),
-                      FutureSatisfy(&receivedRemoveRequest)));
+    Future<Nothing> receivedQuotaRequest;
+    EXPECT_CALL(allocator, updateQuota(Eq(ROLE1), Eq(master::DEFAULT_QUOTA)))
+      .WillOnce(DoAll(
+          InvokeUpdateQuota(&allocator), FutureSatisfy(&receivedQuotaRequest)));
 
     // Send a remove quota request with absent credentials.
     Future<Response> response = process::http::requestDelete(
@@ -1376,7 +1379,7 @@ TEST_F(MasterQuotaTest, NoAuthenticationNoAuthorization)
         None());
 
     // Quota request succeeds and reaches the allocator.
-    AWAIT_READY(receivedRemoveRequest);
+    AWAIT_READY(receivedQuotaRequest);
     AWAIT_EXPECT_RESPONSE_STATUS_EQ(OK().status, response);
   }
 }
@@ -1487,9 +1490,9 @@ TEST_F(MasterQuotaTest, AuthorizeGetUpdateQuotaRequests)
     // request below to override the capacity heuristic check.
     Resources quotaResources = Resources::parse("cpus:1;mem:512").get();
 
-    Future<Quota> quota;
-    EXPECT_CALL(allocator, setQuota(Eq(ROLE1), _))
-      .WillOnce(DoAll(InvokeSetQuota(&allocator),
+    Future<Quota2> quota;
+    EXPECT_CALL(allocator, updateQuota(Eq(ROLE1), _))
+      .WillOnce(DoAll(InvokeUpdateQuota(&allocator),
                       FutureArg<1>(&quota)));
 
     Future<Response> response = process::http::post(
@@ -1502,14 +1505,9 @@ TEST_F(MasterQuotaTest, AuthorizeGetUpdateQuotaRequests)
 
     AWAIT_READY(quota);
 
-    // Extract the principal from `DEFAULT_CREDENTIAL` because `EXPECT_EQ`
-    // does not compile if `DEFAULT_CREDENTIAL.principal()` is used as an
-    // argument.
-    const string principal = DEFAULT_CREDENTIAL.principal();
-
-    EXPECT_EQ(ROLE1, quota->info.role());
-    EXPECT_EQ(principal, quota->info.principal());
-    EXPECT_EQ(quotaResources, quota->info.guarantee());
+    EXPECT_EQ(
+        ResourceQuantities::fromScalarResources(quotaResources),
+        quota->guarantees);
   }
 
   // Try to get the previously requested quota using a principal that is
@@ -1582,10 +1580,10 @@ TEST_F(MasterQuotaTest, AuthorizeGetUpdateQuotaRequests)
 
   // Remove the previously requested quota using the default principal.
   {
-    Future<Nothing> receivedRemoveRequest;
-    EXPECT_CALL(allocator, removeQuota(Eq(ROLE1)))
-      .WillOnce(DoAll(InvokeRemoveQuota(&allocator),
-                      FutureSatisfy(&receivedRemoveRequest)));
+    Future<Nothing> receivedQuotaRequest;
+    EXPECT_CALL(allocator, updateQuota(Eq(ROLE1), Eq(master::DEFAULT_QUOTA)))
+      .WillOnce(DoAll(
+          InvokeUpdateQuota(&allocator), FutureSatisfy(&receivedQuotaRequest)));
 
     Future<Response> response = process::http::requestDelete(
         master.get()->pid,
@@ -1594,7 +1592,7 @@ TEST_F(MasterQuotaTest, AuthorizeGetUpdateQuotaRequests)
 
     AWAIT_EXPECT_RESPONSE_STATUS_EQ(OK().status, response);
 
-    AWAIT_READY(receivedRemoveRequest);
+    AWAIT_READY(receivedQuotaRequest);
   }
 }
 


[mesos] 01/06: Added a wrapper struct for quota guarantees and limits.

Posted by mz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mzhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit f05f0616841bd539a8b6abfc591f3c287ad998d9
Author: Meng Zhu <mz...@mesosphere.io>
AuthorDate: Tue Jun 4 17:34:52 2019 -0700

    Added a wrapper struct for quota guarantees and limits.
    
    This struct is temporarily named to `Quota2` to differentiate
    with the existing `Quota` struct. It will replace all `Quota`
    and rename to `Quota`.
    
    Review: https://reviews.apache.org/r/70799
---
 include/mesos/quota/quota.hpp | 23 ++++++++++++++++++++
 src/master/quota.cpp          | 50 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)

diff --git a/include/mesos/quota/quota.hpp b/include/mesos/quota/quota.hpp
index b028e12..edfd1c0 100644
--- a/include/mesos/quota/quota.hpp
+++ b/include/mesos/quota/quota.hpp
@@ -17,6 +17,8 @@
 #ifndef __MESOS_QUOTA_PROTO_HPP__
 #define __MESOS_QUOTA_PROTO_HPP__
 
+#include <mesos/resource_quantities.hpp>
+
 // ONLY USEFUL AFTER RUNNING PROTOC.
 #include <mesos/quota/quota.pb.h>
 
@@ -28,4 +30,25 @@ struct Quota
   mesos::quota::QuotaInfo info;
 };
 
+
+namespace mesos {
+
+// TODO(mzhu): replace `struct Quota` above.
+struct Quota2
+{
+  ResourceQuantities guarantees;
+  ResourceLimits limits;
+
+  Quota2() {};
+
+  Quota2(const mesos::quota::QuotaConfig& config);
+  Quota2(const mesos::quota::QuotaInfo& info);
+  Quota2(const mesos::quota::QuotaRequest& request);
+
+  bool operator==(const Quota2& quota) const;
+  bool operator!=(const Quota2& quota) const;
+};
+
+} // namespace mesos {
+
 #endif // __MESOS_QUOTA_PROTO_HPP__
diff --git a/src/master/quota.cpp b/src/master/quota.cpp
index 001c240..4f2a59c 100644
--- a/src/master/quota.cpp
+++ b/src/master/quota.cpp
@@ -19,6 +19,7 @@
 #include <string>
 
 #include <mesos/mesos.hpp>
+#include <mesos/quota/quota.hpp>
 #include <mesos/resource_quantities.hpp>
 #include <mesos/resources.hpp>
 #include <mesos/roles.hpp>
@@ -237,4 +238,53 @@ Option<Error> validate(const QuotaConfig& config)
 } // namespace quota {
 } // namespace master {
 } // namespace internal {
+
+Quota2::Quota2(const QuotaConfig& config)
+{
+  guarantees = ResourceQuantities(config.guarantees());
+  limits = ResourceLimits(config.limits());
+}
+
+
+Quota2::Quota2(const QuotaInfo& info)
+{
+  guarantees = ResourceQuantities::fromScalarResources(info.guarantee());
+
+  // For legacy `QuotaInfo`, guarantee also acts as limit.
+  limits = [&info]() {
+    google::protobuf::Map<string, Value::Scalar> limits;
+    foreach (const Resource& r, info.guarantee()) {
+      limits[r.name()] = r.scalar();
+    }
+    return ResourceLimits(limits);
+  }();
+}
+
+
+Quota2::Quota2(const QuotaRequest& request)
+{
+  guarantees = ResourceQuantities::fromScalarResources(request.guarantee());
+
+  // For legacy `QuotaInfo`, guarantee also acts as limit.
+  limits = [&request]() {
+    google::protobuf::Map<string, Value::Scalar> limits;
+    foreach (const Resource& r, request.guarantee()) {
+      limits[r.name()] = r.scalar();
+    }
+    return ResourceLimits(limits);
+  }();
+}
+
+
+bool Quota2::operator==(const Quota2& that) const
+{
+  return guarantees == that.guarantees && limits == that.limits;
+}
+
+
+bool Quota2::operator!=(const Quota2& that) const
+{
+  return guarantees != that.guarantees || limits != that.limits;
+}
+
 } // namespace mesos {