You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2016/12/17 20:07:28 UTC
mesos git commit: Added helper function to get per-role resource
allocations.
Repository: mesos
Updated Branches:
refs/heads/master 8919d84c3 -> 50f9abcb5
Added helper function to get per-role resource allocations.
Review: https://reviews.apache.org/r/54830/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/50f9abcb
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/50f9abcb
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/50f9abcb
Branch: refs/heads/master
Commit: 50f9abcb518fbcf5810ecc0efe17dba8cc5d1a86
Parents: 8919d84
Author: Guangya Liu <gy...@gmail.com>
Authored: Sat Dec 17 14:02:13 2016 -0600
Committer: Benjamin Mahler <bm...@apache.org>
Committed: Sat Dec 17 14:07:03 2016 -0600
----------------------------------------------------------------------
include/mesos/resources.hpp | 4 ++++
include/mesos/v1/resources.hpp | 4 ++++
src/common/resources.cpp | 16 ++++++++++++++++
src/tests/resources_tests.cpp | 36 ++++++++++++++++++++++++++++++++++++
src/v1/resources.cpp | 16 ++++++++++++++++
5 files changed, 76 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/50f9abcb/include/mesos/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp
index f569c93..c2e6d66 100644
--- a/include/mesos/resources.hpp
+++ b/include/mesos/resources.hpp
@@ -374,6 +374,10 @@ public:
// Returns the non-shared resources.
Resources nonShared() const;
+ // Returns the per-role allocations within these resource objects.
+ // This must be called only when the resources are allocated!
+ hashmap<std::string, Resources> allocations() const;
+
// Returns a Resources object with the same amount of each resource
// type as these Resources, but with all Resource objects marked as
// the specified (role, reservation) pair. This is used to cross
http://git-wip-us.apache.org/repos/asf/mesos/blob/50f9abcb/include/mesos/v1/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/v1/resources.hpp b/include/mesos/v1/resources.hpp
index f60ab79..98f025b 100644
--- a/include/mesos/v1/resources.hpp
+++ b/include/mesos/v1/resources.hpp
@@ -374,6 +374,10 @@ public:
// Returns the non-shared resources.
Resources nonShared() const;
+ // Returns the per-role allocations within these resource objects.
+ // This must be called only when the resources are allocated!
+ hashmap<std::string, Resources> allocations() const;
+
// Returns a Resources object with the same amount of each resource
// type as these Resources, but with all Resource objects marked as
// the specified (role, reservation) pair. This is used to cross
http://git-wip-us.apache.org/repos/asf/mesos/blob/50f9abcb/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index 4bb9bef..16f6de6 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -1073,6 +1073,22 @@ Resources Resources::nonShared() const
}
+hashmap<string, Resources> Resources::allocations() const
+{
+ hashmap<string, Resources> result;
+
+ foreach (const Resource_& resource_, resources) {
+ // We require that this is called only when
+ // the resources are allocated.
+ CHECK(resource_.resource.has_allocation_info());
+ CHECK(resource_.resource.allocation_info().has_role());
+ result[resource_.resource.allocation_info().role()].add(resource_);
+ }
+
+ return result;
+}
+
+
Try<Resources> Resources::flatten(
const string& role,
const Option<Resource::ReservationInfo>& reservation) const
http://git-wip-us.apache.org/repos/asf/mesos/blob/50f9abcb/src/tests/resources_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp
index d6eb778..c0ff977 100644
--- a/src/tests/resources_tests.cpp
+++ b/src/tests/resources_tests.cpp
@@ -2753,6 +2753,42 @@ TEST(SharedResourcesTest, Filter)
}
+// Helper for creating an allocated resource, indicating resource
+// that has been allocated to a framework.
+//
+// TODO(bmahler): Inroduce `Resources::allocate` to remove this helper.
+static Resource createAllocatedResource(
+ const string& name,
+ const string& value,
+ const string& role)
+{
+ Resource resource = Resources::parse(name, value, role).get();
+ resource.mutable_allocation_info()->set_role(role);
+ return resource;
+}
+
+
+TEST(AllocatedResourcesTest, Allocations)
+{
+ Resources cpus1 = createAllocatedResource("cpus", "1", "*");
+ Resources cpus2 = createAllocatedResource("cpus", "2", "role2");
+ Resources cpus3 = createAllocatedResource("cpus", "3", "role3");
+
+ Resources mem1 = createAllocatedResource("mem", "1024", "*");
+ Resources mem2 = createAllocatedResource("mem", "2048", "role2");
+ Resources mem3 = createAllocatedResource("mem", "3096", "role3");
+
+ Resources resources = cpus1 + cpus2 + cpus3 + mem1 + mem2 + mem3;
+
+ hashmap<string, Resources> allocations = resources.allocations();
+
+ EXPECT_EQ(3u, allocations.size());
+ EXPECT_EQ(cpus1 + mem1, allocations["*"]);
+ EXPECT_EQ(cpus2 + mem2, allocations["role2"]);
+ EXPECT_EQ(cpus3 + mem3, allocations["role3"]);
+}
+
+
struct ScalarArithmeticParameter
{
Resources resources;
http://git-wip-us.apache.org/repos/asf/mesos/blob/50f9abcb/src/v1/resources.cpp
----------------------------------------------------------------------
diff --git a/src/v1/resources.cpp b/src/v1/resources.cpp
index 46cc00f..a1adf1b 100644
--- a/src/v1/resources.cpp
+++ b/src/v1/resources.cpp
@@ -1073,6 +1073,22 @@ Resources Resources::nonShared() const
}
+hashmap<string, Resources> Resources::allocations() const
+{
+ hashmap<string, Resources> result;
+
+ foreach (const Resource_& resource_, resources) {
+ // We require that this is called only when
+ // the resources are allocated.
+ CHECK(resource_.resource.has_allocation_info());
+ CHECK(resource_.resource.allocation_info().has_role());
+ result[resource_.resource.allocation_info().role()].add(resource_);
+ }
+
+ return result;
+}
+
+
Try<Resources> Resources::flatten(
const string& role,
const Option<Resource::ReservationInfo>& reservation) const