mesos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mp...@apache.org
Subject [07/21] mesos git commit: Introduced a utility function `Resources::reservationRole` [7/20].
Date Tue, 20 Jun 2017 09:46:43 GMT
Introduced a utility function `Resources::reservationRole` [7/20].

Review: https://reviews.apache.org/r/60015


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/7877b46f
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/7877b46f
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/7877b46f

Branch: refs/heads/master
Commit: 7877b46f74730b61daf5179121139b40df26c2a6
Parents: 76b0f4e
Author: Michael Park <mpark@apache.org>
Authored: Sun Jun 18 21:15:14 2017 -0700
Committer: Michael Park <mpark@apache.org>
Committed: Tue Jun 20 02:30:33 2017 -0700

----------------------------------------------------------------------
 include/mesos/resources.hpp    |  4 ++++
 include/mesos/v1/resources.hpp |  4 ++++
 src/common/resources.cpp       | 34 +++++++++++++++++++++-------------
 src/v1/resources.cpp           | 34 +++++++++++++++++++++-------------
 4 files changed, 50 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/7877b46f/include/mesos/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp
index b6264e2..513b4c3 100644
--- a/include/mesos/resources.hpp
+++ b/include/mesos/resources.hpp
@@ -306,6 +306,10 @@ public:
   // Tests if the given Resource object has refined reservations.
   static bool hasRefinedReservations(const Resource& resource);
 
+  // Returns the role to which the given Resource object is reserved for.
+  // This must be called only when the resource is reserved!
+  static const std::string& reservationRole(const Resource& resource);
+
   // Returns the summed up Resources given a hashmap<Key, Resources>.
   //
   // NOTE: While scalar resources such as "cpus" sum correctly,

http://git-wip-us.apache.org/repos/asf/mesos/blob/7877b46f/include/mesos/v1/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/v1/resources.hpp b/include/mesos/v1/resources.hpp
index b7e390c..eb7d70c 100644
--- a/include/mesos/v1/resources.hpp
+++ b/include/mesos/v1/resources.hpp
@@ -306,6 +306,10 @@ public:
   // Tests if the given Resource object has refined reservations.
   static bool hasRefinedReservations(const Resource& resource);
 
+  // Returns the role to which the given Resource object is reserved for.
+  // This must be called only when the resource is reserved!
+  static const std::string& reservationRole(const Resource& resource);
+
   // Returns the summed up Resources given a hashmap<Key, Resources>.
   //
   // NOTE: While scalar resources such as "cpus" sum correctly,

http://git-wip-us.apache.org/repos/asf/mesos/blob/7877b46f/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index d1b9a9e..1dd058f 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -1080,11 +1080,8 @@ bool Resources::isReserved(
   CHECK(!resource.has_role());
   CHECK(!resource.has_reservation());
 
-  if (role.isSome()) {
-    return !isUnreserved(resource) && role.get() == resource.role();
-  } else {
-    return !isUnreserved(resource);
-  }
+  return !isUnreserved(resource) &&
+         (role.isNone() || role.get() == reservationRole(resource));
 }
 
 
@@ -1096,8 +1093,8 @@ bool Resources::isAllocatableTo(
   CHECK(!resource.has_reservation());
 
   return isUnreserved(resource) ||
-         role == resource.role() ||
-         roles::isStrictSubroleOf(role, resource.role());
+         role == reservationRole(resource) ||
+         roles::isStrictSubroleOf(role, reservationRole(resource));
 }
 
 
@@ -1146,6 +1143,13 @@ bool Resources::hasRefinedReservations(const Resource& resource)
   return resource.reservations_size() > 1;
 }
 
+
+const string& Resources::reservationRole(const Resource& resource)
+{
+  CHECK_GE(resource.reservations_size(), 0);
+  return resource.reservations().rbegin()->role();
+}
+
 /////////////////////////////////////////////////
 // Public member functions.
 /////////////////////////////////////////////////
@@ -1356,7 +1360,7 @@ hashmap<string, Resources> Resources::reservations() const
 
   foreach (const Resource_& resource_, resources) {
     if (isReserved(resource_.resource)) {
-      result[resource_.resource.role()].add(resource_);
+      result[reservationRole(resource_.resource)].add(resource_);
     }
   }
 
@@ -1935,11 +1939,15 @@ Option<Resources> Resources::find(const Resource& target)
const
   Resources remaining = Resources(target).flatten();
 
   // First look in the target role, then unreserved, then any remaining role.
-  vector<lambda::function<bool(const Resource&)>> predicates = {
-    lambda::bind(isReserved, lambda::_1, target.role()),
-    isUnreserved,
-    [](const Resource&) { return true; }
-  };
+  vector<lambda::function<bool(const Resource&)>> predicates;
+
+  if (isReserved(target)) {
+    predicates.push_back(
+        lambda::bind(isReserved, lambda::_1, reservationRole(target)));
+  }
+
+  predicates.push_back(isUnreserved);
+  predicates.push_back([](const Resource&) { return true; });
 
   foreach (const auto& predicate, predicates) {
     foreach (const Resource_& resource_, total.filter(predicate)) {

http://git-wip-us.apache.org/repos/asf/mesos/blob/7877b46f/src/v1/resources.cpp
----------------------------------------------------------------------
diff --git a/src/v1/resources.cpp b/src/v1/resources.cpp
index a853f80..a9b0233 100644
--- a/src/v1/resources.cpp
+++ b/src/v1/resources.cpp
@@ -1111,11 +1111,8 @@ bool Resources::isReserved(
   CHECK(!resource.has_role());
   CHECK(!resource.has_reservation());
 
-  if (role.isSome()) {
-    return !isUnreserved(resource) && role.get() == resource.role();
-  } else {
-    return !isUnreserved(resource);
-  }
+  return !isUnreserved(resource) &&
+         (role.isNone() || role.get() == reservationRole(resource));
 }
 
 
@@ -1127,8 +1124,8 @@ bool Resources::isAllocatableTo(
   CHECK(!resource.has_reservation());
 
   return isUnreserved(resource) ||
-         role == resource.role() ||
-         roles::isStrictSubroleOf(role, resource.role());
+         role == reservationRole(resource) ||
+         roles::isStrictSubroleOf(role, reservationRole(resource));
 }
 
 
@@ -1177,6 +1174,13 @@ bool Resources::hasRefinedReservations(const Resource& resource)
   return resource.reservations_size() > 1;
 }
 
+
+const string& Resources::reservationRole(const Resource& resource)
+{
+  CHECK_GE(resource.reservations_size(), 0);
+  return resource.reservations().rbegin()->role();
+}
+
 /////////////////////////////////////////////////
 // Public member functions.
 /////////////////////////////////////////////////
@@ -1387,7 +1391,7 @@ hashmap<string, Resources> Resources::reservations() const
 
   foreach (const Resource_& resource_, resources) {
     if (isReserved(resource_.resource)) {
-      result[resource_.resource.role()].add(resource_);
+      result[reservationRole(resource_.resource)].add(resource_);
     }
   }
 
@@ -1966,11 +1970,15 @@ Option<Resources> Resources::find(const Resource& target)
const
   Resources remaining = Resources(target).flatten();
 
   // First look in the target role, then unreserved, then any remaining role.
-  vector<lambda::function<bool(const Resource&)>> predicates = {
-    lambda::bind(isReserved, lambda::_1, target.role()),
-    isUnreserved,
-    [](const Resource&) { return true; }
-  };
+  vector<lambda::function<bool(const Resource&)>> predicates;
+
+  if (isReserved(target)) {
+    predicates.push_back(
+        lambda::bind(isReserved, lambda::_1, reservationRole(target)));
+  }
+
+  predicates.push_back(isUnreserved);
+  predicates.push_back([](const Resource&) { return true; });
 
   foreach (const auto& predicate, predicates) {
     foreach (const Resource_& resource_, total.filter(predicate)) {


Mime
View raw message