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 2017/01/30 01:18:42 UTC
[1/3] mesos git commit: Added helpers to allocate / unallocate
Resources.
Repository: mesos
Updated Branches:
refs/heads/master e8460826f -> 9df201ff2
Added helpers to allocate / unallocate Resources.
Review: https://reviews.apache.org/r/54836
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/9aa1d619
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/9aa1d619
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/9aa1d619
Branch: refs/heads/master
Commit: 9aa1d619038538b09dacbc2d78b3ef641c5f8c03
Parents: e846082
Author: Benjamin Mahler <bm...@apache.org>
Authored: Sat Dec 17 13:54:39 2016 -0600
Committer: Benjamin Mahler <bm...@apache.org>
Committed: Sun Jan 29 17:12:26 2017 -0800
----------------------------------------------------------------------
include/mesos/resources.hpp | 8 ++++
include/mesos/v1/resources.hpp | 8 ++++
src/common/resources.cpp | 18 +++++++++
src/tests/resources_tests.cpp | 77 ++++++++++++++++++++++---------------
src/v1/resources.cpp | 18 +++++++++
5 files changed, 98 insertions(+), 31 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/9aa1d619/include/mesos/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp
index e391ce0..f8c3e8c 100644
--- a/include/mesos/resources.hpp
+++ b/include/mesos/resources.hpp
@@ -345,6 +345,14 @@ public:
size_t size() const { return resources.size(); }
+ // Allocates the resources to the given role (by setting the
+ // `AllocationInfo.role`). Any existing allocation will be
+ // over-written.
+ void allocate(const std::string& role);
+
+ // Unallocates the resources.
+ void unallocate();
+
// Checks if this Resources is a superset of the given Resources.
bool contains(const Resources& that) const;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9aa1d619/include/mesos/v1/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/v1/resources.hpp b/include/mesos/v1/resources.hpp
index 98f025b..e8b586a 100644
--- a/include/mesos/v1/resources.hpp
+++ b/include/mesos/v1/resources.hpp
@@ -344,6 +344,14 @@ public:
// - If the resource is not in the Resources object, the count is 0.
size_t count(const Resource& that) const;
+ // Allocates the resources to the given role (by setting the
+ // `AllocationInfo.role`). Any existing allocation will be
+ // over-written.
+ void allocate(const std::string& role);
+
+ // Unallocates the resources.
+ void unallocate();
+
// Filter resources based on the given predicate.
Resources filter(
const lambda::function<bool(const Resource&)>& predicate) const;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9aa1d619/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index a83c74e..54486cf 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -1067,6 +1067,24 @@ size_t Resources::count(const Resource& that) const
}
+void Resources::allocate(const string& role)
+{
+ foreach (Resource_& resource_, resources) {
+ resource_.resource.mutable_allocation_info()->set_role(role);
+ }
+}
+
+
+void Resources::unallocate()
+{
+ foreach (Resource_& resource_, resources) {
+ if (resource_.resource.has_allocation_info()) {
+ resource_.resource.clear_allocation_info();
+ }
+ }
+}
+
+
Resources Resources::filter(
const lambda::function<bool(const Resource&)>& predicate) const
{
http://git-wip-us.apache.org/repos/asf/mesos/blob/9aa1d619/src/tests/resources_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp
index 8dfb1be..0ed8f6b 100644
--- a/src/tests/resources_tests.cpp
+++ b/src/tests/resources_tests.cpp
@@ -2753,25 +2753,13 @@ 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, Equality)
{
- Resources cpus1 = createAllocatedResource("cpus", "1", "*");
- Resources cpus2 = createAllocatedResource("cpus", "1", "role1");
+ Resources cpus1 = Resources::parse("cpus", "1", "*").get();
+ Resources cpus2 = Resources::parse("cpus", "1", "*").get();
+
+ cpus1.allocate("role1");
+ cpus2.allocate("role2");
EXPECT_EQ(cpus1, cpus1);
EXPECT_NE(cpus1, cpus2);
@@ -2780,8 +2768,11 @@ TEST(AllocatedResourcesTest, Equality)
TEST(AllocatedResourcesTest, Contains)
{
- Resources cpus1 = createAllocatedResource("cpus", "1", "*");
- Resources cpus2 = createAllocatedResource("cpus", "1", "role1");
+ Resources cpus1 = Resources::parse("cpus", "1", "*").get();
+ Resources cpus2 = Resources::parse("cpus", "1", "*").get();
+
+ cpus1.allocate("role1");
+ cpus2.allocate("role2");
EXPECT_TRUE((cpus1 + cpus2).contains(cpus1));
EXPECT_TRUE((cpus1 + cpus2).contains(cpus2));
@@ -2790,8 +2781,11 @@ TEST(AllocatedResourcesTest, Contains)
TEST(AllocatedResourcesTest, Addition)
{
- Resources cpus1 = createAllocatedResource("cpus", "1", "*");
- Resources cpus2 = createAllocatedResource("cpus", "1", "role1");
+ Resources cpus1 = Resources::parse("cpus", "1", "*").get();
+ Resources cpus2 = Resources::parse("cpus", "1", "*").get();
+
+ cpus1.allocate("role1");
+ cpus2.allocate("role2");
EXPECT_EQ(2u, (cpus1 + cpus2).size());
EXPECT_SOME_EQ(2.0, (cpus1 + cpus2).cpus());
@@ -2800,8 +2794,11 @@ TEST(AllocatedResourcesTest, Addition)
TEST(AllocatedResourcesTest, Subtraction)
{
- Resources cpus1 = createAllocatedResource("cpus", "1", "*");
- Resources cpus2 = createAllocatedResource("cpus", "1", "role1");
+ Resources cpus1 = Resources::parse("cpus", "1", "*").get();
+ Resources cpus2 = Resources::parse("cpus", "1", "*").get();
+
+ cpus1.allocate("role1");
+ cpus2.allocate("role2");
EXPECT_TRUE((cpus1 - cpus1).empty());
EXPECT_TRUE((cpus2 - cpus2).empty());
@@ -2813,13 +2810,25 @@ TEST(AllocatedResourcesTest, Subtraction)
TEST(AllocatedResourcesTest, Allocations)
{
- Resources cpus1 = createAllocatedResource("cpus", "1", "*");
- Resources cpus2 = createAllocatedResource("cpus", "2", "role2");
- Resources cpus3 = createAllocatedResource("cpus", "3", "role3");
+ // Unreserved resources can be allocated to any role (including *).
+ Resources cpus1 = Resources::parse("cpus", "1", "*").get();
+ Resources mem1 = Resources::parse("mem", "1024", "*").get();
+
+ cpus1.allocate("*");
+ mem1.allocate("*");
+
+ Resources cpus2 = Resources::parse("cpus", "2", "*").get();
+ Resources mem2 = Resources::parse("mem", "2048", "*").get();
- Resources mem1 = createAllocatedResource("mem", "1024", "*");
- Resources mem2 = createAllocatedResource("mem", "2048", "role2");
- Resources mem3 = createAllocatedResource("mem", "3096", "role3");
+ cpus2.allocate("role1");
+ mem2.allocate("role1");
+
+ // Reserved resources are allocated to the reserved role.
+ Resources cpus3 = Resources::parse("cpus", "3", "role2").get();
+ Resources mem3 = Resources::parse("mem", "3096", "role2").get();
+
+ cpus3.allocate("role2");
+ mem3.allocate("role2");
Resources resources = cpus1 + cpus2 + cpus3 + mem1 + mem2 + mem3;
@@ -2827,8 +2836,14 @@ TEST(AllocatedResourcesTest, Allocations)
EXPECT_EQ(3u, allocations.size());
EXPECT_EQ(cpus1 + mem1, allocations["*"]);
- EXPECT_EQ(cpus2 + mem2, allocations["role2"]);
- EXPECT_EQ(cpus3 + mem3, allocations["role3"]);
+ EXPECT_EQ(cpus2 + mem2, allocations["role1"]);
+ EXPECT_EQ(cpus3 + mem3, allocations["role2"]);
+
+ // Test unallocation.
+ cpus1.unallocate();
+
+ Resource r = *cpus1.begin();
+ EXPECT_FALSE(r.has_allocation_info());
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/9aa1d619/src/v1/resources.cpp
----------------------------------------------------------------------
diff --git a/src/v1/resources.cpp b/src/v1/resources.cpp
index da4701c..0e3e73a 100644
--- a/src/v1/resources.cpp
+++ b/src/v1/resources.cpp
@@ -1056,6 +1056,24 @@ size_t Resources::count(const Resource& that) const
}
+void Resources::allocate(const string& role)
+{
+ foreach (Resource_& resource_, resources) {
+ resource_.resource.mutable_allocation_info()->set_role(role);
+ }
+}
+
+
+void Resources::unallocate()
+{
+ foreach (Resource_& resource_, resources) {
+ if (resource_.resource.has_allocation_info()) {
+ resource_.resource.clear_allocation_info();
+ }
+ }
+}
+
+
Resources Resources::filter(
const lambda::function<bool(const Resource&)>& predicate) const
{
[2/3] mesos git commit: Augmented printing of Resources to include
AllocationInfo.
Posted by bm...@apache.org.
Augmented printing of Resources to include AllocationInfo.
Ignoring whether there is a disk info, the format is as follows:
name(reservedrole[, principal[, labels]])(allocatedrole):value
Review: https://reviews.apache.org/r/55825
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/aa0ea8fc
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/aa0ea8fc
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/aa0ea8fc
Branch: refs/heads/master
Commit: aa0ea8fc8846f2914784d6ad229843adbbc8e400
Parents: 9aa1d61
Author: Benjamin Mahler <bm...@apache.org>
Authored: Fri Jan 20 18:31:45 2017 -0800
Committer: Benjamin Mahler <bm...@apache.org>
Committed: Sun Jan 29 17:12:30 2017 -0800
----------------------------------------------------------------------
src/common/resources.cpp | 4 ++++
src/tests/resources_tests.cpp | 7 +++++++
src/v1/resources.cpp | 4 ++++
3 files changed, 15 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/aa0ea8fc/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index 54486cf..b3eae51 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -1944,6 +1944,10 @@ ostream& operator<<(ostream& stream, const Resource& resource)
stream << ")";
+ if (resource.has_allocation_info()) {
+ stream << "(allocated: " << resource.allocation_info().role() << ")";
+ }
+
if (resource.has_disk()) {
stream << "[" << resource.disk() << "]";
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/aa0ea8fc/src/tests/resources_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp
index 0ed8f6b..e1ac164 100644
--- a/src/tests/resources_tests.cpp
+++ b/src/tests/resources_tests.cpp
@@ -814,6 +814,13 @@ TEST(ResourcesTest, PrintingExtendedAttributes)
stream << disk;
EXPECT_EQ("disk(alice):1", stream.str());
+ // Allocated resource.
+ stream.str("");
+ disk.mutable_allocation_info()->set_role("role");
+ stream << disk;
+ EXPECT_EQ("disk(alice)(allocated: role):1", stream.str());
+ disk.clear_allocation_info();
+
// Standard revocable resource.
stream.str("");
disk.mutable_revocable();
http://git-wip-us.apache.org/repos/asf/mesos/blob/aa0ea8fc/src/v1/resources.cpp
----------------------------------------------------------------------
diff --git a/src/v1/resources.cpp b/src/v1/resources.cpp
index 0e3e73a..5ba7b65 100644
--- a/src/v1/resources.cpp
+++ b/src/v1/resources.cpp
@@ -1933,6 +1933,10 @@ ostream& operator<<(ostream& stream, const Resource& resource)
stream << ")";
+ if (resource.has_allocation_info()) {
+ stream << "(allocated: " << resource.allocation_info().role() << ")";
+ }
+
if (resource.has_disk()) {
stream << "[" << resource.disk() << "]";
}
[3/3] mesos git commit: Updated resources quantity stripping to strip
AllocationInfo.
Posted by bm...@apache.org.
Updated resources quantity stripping to strip AllocationInfo.
Currently, `Resources::createStrippedScalarQuantity()` attempts to
create a notion of a "quantity" of resources. In order to do this,
all distinguishing metadata between resources of the same name are
stripped. This currently includes, disk metadata, reservation
metadata (only for dynamic reservations), and shared resource
metadata. To maintain the notion of a quantity, this patch also
strips the allocation metadata.
Review: https://reviews.apache.org/r/55829
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/9df201ff
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/9df201ff
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/9df201ff
Branch: refs/heads/master
Commit: 9df201ff2729ff9ebd290e0e8a8a67b4e1de3998
Parents: aa0ea8f
Author: Benjamin Mahler <bm...@apache.org>
Authored: Sun Jan 22 14:34:18 2017 -0800
Committer: Benjamin Mahler <bm...@apache.org>
Committed: Sun Jan 29 17:12:30 2017 -0800
----------------------------------------------------------------------
include/mesos/resources.hpp | 10 +++++-----
include/mesos/v1/resources.hpp | 10 +++++-----
src/common/resources.cpp | 1 +
src/tests/resources_tests.cpp | 15 +++++++++++++++
src/v1/resources.cpp | 1 +
5 files changed, 27 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/9df201ff/include/mesos/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp
index f8c3e8c..73e44be 100644
--- a/include/mesos/resources.hpp
+++ b/include/mesos/resources.hpp
@@ -420,11 +420,11 @@ public:
Resources flatten() const;
// Returns a Resources object that contains all the scalar resources
- // in this object, but with their ReservationInfo and DiskInfo
- // omitted. Note that the `role` and RevocableInfo, if any, are
- // preserved. Because we clear ReservationInfo but preserve `role`,
- // this means that stripping a dynamically reserved resource makes
- // it effectively statically reserved.
+ // in this object, but with their ReservationInfo, AllocationInfo,
+ // and DiskInfo omitted. Note that the `role` and RevocableInfo,
+ // if any, are preserved. Because we clear ReservationInfo but
+ // preserve `role`, this means that stripping a dynamically
+ // reserved resource makes it effectively statically reserved.
//
// This is intended for code that would like to aggregate together
// Resource values without regard for metadata like whether the
http://git-wip-us.apache.org/repos/asf/mesos/blob/9df201ff/include/mesos/v1/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/v1/resources.hpp b/include/mesos/v1/resources.hpp
index e8b586a..1913229 100644
--- a/include/mesos/v1/resources.hpp
+++ b/include/mesos/v1/resources.hpp
@@ -404,11 +404,11 @@ public:
Resources flatten() const;
// Returns a Resources object that contains all the scalar resources
- // in this object, but with their ReservationInfo and DiskInfo
- // omitted. Note that the `role` and RevocableInfo, if any, are
- // preserved. Because we clear ReservationInfo but preserve `role`,
- // this means that stripping a dynamically reserved resource makes
- // it effectively statically reserved.
+ // in this object, but with their ReservationInfo, AllocationInfo,
+ // and DiskInfo omitted. Note that the `role` and RevocableInfo,
+ // if any, are preserved. Because we clear ReservationInfo but
+ // preserve `role`, this means that stripping a dynamically
+ // reserved resource makes it effectively statically reserved.
//
// This is intended for code that would like to aggregate together
// Resource values without regard for metadata like whether the
http://git-wip-us.apache.org/repos/asf/mesos/blob/9df201ff/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index b3eae51..cdf6a84 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -1221,6 +1221,7 @@ Resources Resources::createStrippedScalarQuantity() const
foreach (const Resource& resource, resources) {
if (resource.type() == Value::SCALAR) {
Resource scalar = resource;
+ scalar.clear_allocation_info();
scalar.clear_reservation();
scalar.clear_disk();
scalar.clear_shared();
http://git-wip-us.apache.org/repos/asf/mesos/blob/9df201ff/src/tests/resources_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp
index e1ac164..33f0ee0 100644
--- a/src/tests/resources_tests.cpp
+++ b/src/tests/resources_tests.cpp
@@ -2284,6 +2284,21 @@ TEST(ResourcesOperationTest, StrippedResourcesVolume)
}
+TEST(ResourcesOperationTest, StrippedResourcesAllocated)
+{
+ Resources allocated = Resources::parse("cpus:1;mem:512").get();
+ allocated.allocate("role");
+
+ Resources stripped = allocated.createStrippedScalarQuantity();
+
+ // Allocation info should be stripped when
+ // converting to a quantity.
+ foreach (const Resource& resource, stripped) {
+ EXPECT_FALSE(resource.has_allocation_info());
+ }
+}
+
+
TEST(ResourcesOperationTest, StrippedResourcesReserved)
{
Resources unreserved = Resources::parse("cpus:1;mem:512").get();
http://git-wip-us.apache.org/repos/asf/mesos/blob/9df201ff/src/v1/resources.cpp
----------------------------------------------------------------------
diff --git a/src/v1/resources.cpp b/src/v1/resources.cpp
index 5ba7b65..6fd8687 100644
--- a/src/v1/resources.cpp
+++ b/src/v1/resources.cpp
@@ -1210,6 +1210,7 @@ Resources Resources::createStrippedScalarQuantity() const
foreach (const Resource& resource, resources) {
if (resource.type() == Value::SCALAR) {
Resource scalar = resource;
+ scalar.clear_allocation_info();
scalar.clear_reservation();
scalar.clear_disk();
scalar.clear_shared();