You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by mp...@apache.org on 2017/06/13 22:34:33 UTC
[4/4] mesos git commit: Introduced method `Resources::allocatableTo`
to aggregate reservations.
Introduced method `Resources::allocatableTo` to aggregate reservations.
A reservation is allocatable to 'role' iff:
- it is reserved to ancestor of 'role' in hierarchy, OR
- it is reserved to 'role' itself, OR
- it is unreserved.
This is a helper function for reservation delegate, where reserved
resources can be 'delegated' downwards in the hierarchy.
Review: https://reviews.apache.org/r/58508/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/01d37127
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/01d37127
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/01d37127
Branch: refs/heads/master
Commit: 01d37127f8b002429d7960c0c2fd8abf3659f878
Parents: beb685b
Author: Jay Guo <gu...@gmail.com>
Authored: Tue Jun 13 12:20:09 2017 -0700
Committer: Michael Park <mc...@gmail.com>
Committed: Tue Jun 13 15:34:17 2017 -0700
----------------------------------------------------------------------
include/mesos/resources.hpp | 13 +++++++++++++
include/mesos/v1/resources.hpp | 13 +++++++++++++
src/common/resources.cpp | 22 ++++++++++++++++++++++
src/tests/resources_tests.cpp | 29 +++++++++++++++++++++++++++++
src/v1/resources.cpp | 22 ++++++++++++++++++++++
5 files changed, 99 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/01d37127/include/mesos/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp
index 451b0ce..2b55847 100644
--- a/include/mesos/resources.hpp
+++ b/include/mesos/resources.hpp
@@ -285,6 +285,15 @@ public:
const Resource& resource,
const Option<std::string>& role = None());
+ // Tests if the given Resource object is allocatable to the given role.
+ // A resource object is allocatable to 'role' if:
+ // * it is reserved to an ancestor of that role in the hierarchy, OR
+ // * it is reserved to 'role' itself, OR
+ // * it is unreserved.
+ static bool isAllocatableTo(
+ const Resource& resource,
+ const std::string& role);
+
// Tests if the given Resource object is unreserved.
static bool isUnreserved(const Resource& resource);
@@ -380,6 +389,10 @@ public:
// and will be ignored.
Resources reserved(const Option<std::string>& role = None()) const;
+ // Returns resources allocatable to role. See `isAllocatableTo` for the
+ // definition of 'allocatableTo'.
+ Resources allocatableTo(const std::string& role) const;
+
// Returns the unreserved resources.
Resources unreserved() const;
http://git-wip-us.apache.org/repos/asf/mesos/blob/01d37127/include/mesos/v1/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/v1/resources.hpp b/include/mesos/v1/resources.hpp
index c790927..7c6e856 100644
--- a/include/mesos/v1/resources.hpp
+++ b/include/mesos/v1/resources.hpp
@@ -285,6 +285,15 @@ public:
const Resource& resource,
const Option<std::string>& role = None());
+ // Tests if the given Resource object is allocatable to the given role.
+ // A resource object is allocatable to 'role' if:
+ // * it is reserved to an ancestor of that role in the hierarchy, OR
+ // * it is reserved to 'role' itself, OR
+ // * it is unreserved.
+ static bool isAllocatableTo(
+ const Resource& resource,
+ const std::string& role);
+
// Tests if the given Resource object is unreserved.
static bool isUnreserved(const Resource& resource);
@@ -380,6 +389,10 @@ public:
// and will be ignored.
Resources reserved(const Option<std::string>& role = None()) const;
+ // Returns resources allocatable to role. See `isAllocatableTo` for the
+ // definition of 'allocatableTo'.
+ Resources allocatableTo(const std::string& role) const;
+
// Returns the unreserved resources.
Resources unreserved() const;
http://git-wip-us.apache.org/repos/asf/mesos/blob/01d37127/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index f89f1aa..7b3fae1 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -913,6 +913,22 @@ bool Resources::isReserved(
}
+bool Resources::isAllocatableTo(
+ const Resource& resource,
+ const std::string& role)
+{
+ if (isUnreserved(resource)) {
+ return true;
+ }
+
+ const vector<string> path = strings::tokenize(role, "/");
+ const vector<string> prefix = strings::tokenize(resource.role(), "/");
+
+ return path.size() >= prefix.size() &&
+ std::equal(prefix.begin(), prefix.end(), path.begin());
+}
+
+
bool Resources::isUnreserved(const Resource& resource)
{
return resource.role() == "*" && !resource.has_reservation();
@@ -1161,6 +1177,12 @@ Resources Resources::reserved(const Option<string>& role) const
}
+Resources Resources::allocatableTo(const string& role) const
+{
+ return filter(lambda::bind(isAllocatableTo, lambda::_1, role));
+}
+
+
Resources Resources::unreserved() const
{
return filter(isUnreserved);
http://git-wip-us.apache.org/repos/asf/mesos/blob/01d37127/src/tests/resources_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp
index 71c7544..b591386 100644
--- a/src/tests/resources_tests.cpp
+++ b/src/tests/resources_tests.cpp
@@ -1529,6 +1529,34 @@ TEST(ResourcesTest, Reservations)
}
+// This test verifies that we can get all resources allocatable to a role,
+// including reservations of itself, its ancestors, and unreserved resoruces.
+TEST(ResourcesTest, HierarchicalReservations)
+{
+ Resources unreserved = Resources::parse(
+ "cpus:1;mem:2;disk:4").get();
+ Resources grandfather = Resources::parse(
+ "cpus(a):2;mem(a):4;disk(a):8").get();
+ Resources father = Resources::parse(
+ "cpus(a/bx):4;mem(a/bx):8;disk(a/bx):16").get();
+ Resources uncle = Resources::parse(
+ "cpus(a/b):4;mem(a/b):8;disk(a/b):16").get();
+ Resources child = Resources::parse(
+ "cpus(a/bx/c):8;mem(a/bx/c):16;disk(a/bx/c):32").get();
+
+ Resources family = unreserved + grandfather + father + uncle + child;
+
+ EXPECT_EQ(unreserved, family.allocatableTo("*"));
+
+ EXPECT_EQ(grandfather + unreserved, family.allocatableTo("a"));
+
+ EXPECT_EQ(grandfather + father + unreserved, family.allocatableTo("a/bx"));
+
+ EXPECT_EQ(grandfather + father + child + unreserved,
+ family.allocatableTo("a/bx/c"));
+}
+
+
TEST(ResourceProviderIDTest, Addition)
{
ResourceProviderID resourceProviderId;
@@ -2349,6 +2377,7 @@ TEST(DiskResourcesTest, SourceContains)
EXPECT_FALSE(r8.contains(r7 + r7));
}
+
TEST(DiskResourcesTest, FilterPersistentVolumes)
{
Resources resources = Resources::parse("cpus:1;mem:512;disk:1000").get();
http://git-wip-us.apache.org/repos/asf/mesos/blob/01d37127/src/v1/resources.cpp
----------------------------------------------------------------------
diff --git a/src/v1/resources.cpp b/src/v1/resources.cpp
index 24240ee..236fe17 100644
--- a/src/v1/resources.cpp
+++ b/src/v1/resources.cpp
@@ -915,6 +915,22 @@ bool Resources::isReserved(
}
+bool Resources::isAllocatableTo(
+ const Resource& resource,
+ const std::string& role)
+{
+ if (isUnreserved(resource)) {
+ return true;
+ }
+
+ const vector<string> path = strings::tokenize(role, "/");
+ const vector<string> prefix = strings::tokenize(resource.role(), "/");
+
+ return path.size() >= prefix.size() &&
+ std::equal(prefix.begin(), prefix.end(), path.begin());
+}
+
+
bool Resources::isUnreserved(const Resource& resource)
{
return resource.role() == "*" && !resource.has_reservation();
@@ -1163,6 +1179,12 @@ Resources Resources::reserved(const Option<string>& role) const
}
+Resources Resources::allocatableTo(const string& role) const
+{
+ return filter(lambda::bind(isAllocatableTo, lambda::_1, role));
+}
+
+
Resources Resources::unreserved() const
{
return filter(isUnreserved);