You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by gr...@apache.org on 2018/06/28 16:27:08 UTC
[2/3] mesos git commit: Added a resource utility `isScalarQuantity`.
Added a resource utility `isScalarQuantity`.
`isScalarQuantity()` checks if a `Resources` object
is "pure" scalar quantity i.e. its `Resource`(s) only has
name, type (set to scalar) and scalar fields set.
Also added tests.
Review: https://reviews.apache.org/r/67516/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/ccb24bf3
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/ccb24bf3
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/ccb24bf3
Branch: refs/heads/1.6.x
Commit: ccb24bf3f7e098723179ee4b595aa99e3a0869e4
Parents: a615f36
Author: Meng Zhu <mz...@mesosphere.io>
Authored: Thu Jun 28 08:33:00 2018 -0700
Committer: Greg Mann <gr...@gmail.com>
Committed: Thu Jun 28 08:33:19 2018 -0700
----------------------------------------------------------------------
include/mesos/resources.hpp | 5 +++++
include/mesos/v1/resources.hpp | 5 +++++
src/common/resources.cpp | 11 +++++++++++
src/tests/resources_tests.cpp | 31 +++++++++++++++++++++++++++++++
src/v1/resources.cpp | 11 +++++++++++
5 files changed, 63 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/ccb24bf3/include/mesos/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp
index 7afe0d8..bd6d6d6 100644
--- a/include/mesos/resources.hpp
+++ b/include/mesos/resources.hpp
@@ -324,6 +324,11 @@ public:
// Tests if the given Resource object is shared.
static bool isShared(const Resource& resource);
+ // Tests if the given Resources object is a "pure" scalar quantity which
+ // consists of resource objects with ONLY name, type (set to "Scalar")
+ // and scalar fields set.
+ static bool isScalarQuantity(const Resources& resources);
+
// Tests if the given Resource object has refined reservations.
static bool hasRefinedReservations(const Resource& resource);
http://git-wip-us.apache.org/repos/asf/mesos/blob/ccb24bf3/include/mesos/v1/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/v1/resources.hpp b/include/mesos/v1/resources.hpp
index c712057..c065dd1 100644
--- a/include/mesos/v1/resources.hpp
+++ b/include/mesos/v1/resources.hpp
@@ -324,6 +324,11 @@ public:
// Tests if the given Resource object is shared.
static bool isShared(const Resource& resource);
+ // Tests if the given Resources object is a "pure" scalar quantity which
+ // only consists of resource object with ONLY name, type (set to "Scalar")
+ // and scalar fields set.
+ static bool isScalarQuantity(const Resources& resources);
+
// Tests if the given Resource object has refined reservations.
static bool hasRefinedReservations(const Resource& resource);
http://git-wip-us.apache.org/repos/asf/mesos/blob/ccb24bf3/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index 8518abf..b9f1c2d 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -1248,6 +1248,17 @@ bool Resources::isShared(const Resource& resource)
}
+bool Resources::isScalarQuantity(const Resources& resources)
+{
+ // Instead of checking the absence of non-scalar-quantity fields,
+ // we do an equality check between the original resources object and
+ // its stripped counterpart.
+ //
+ // We remove the static reservation metadata here via `toUnreserved()`.
+ return resources == resources.createStrippedScalarQuantity().toUnreserved();
+}
+
+
bool Resources::hasRefinedReservations(const Resource& resource)
{
CHECK(!resource.has_role()) << resource;
http://git-wip-us.apache.org/repos/asf/mesos/blob/ccb24bf3/src/tests/resources_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp
index 395c458..bccc403 100644
--- a/src/tests/resources_tests.cpp
+++ b/src/tests/resources_tests.cpp
@@ -1943,6 +1943,37 @@ TEST(ResourcesTest, AbsentResources)
}
+TEST(ResourcesTest, isScalarQuantity)
+{
+ Resources scalarQuantity1 = Resources::parse("cpus:1").get();
+ EXPECT_TRUE(Resources::isScalarQuantity(scalarQuantity1));
+
+ Resources scalarQuantity2 = Resources::parse("cpus:1;mem:1").get();
+ EXPECT_TRUE(Resources::isScalarQuantity(scalarQuantity2));
+
+ Resources range = Resources::parse("ports", "[1-16000]", "*").get();
+ EXPECT_FALSE(Resources::isScalarQuantity(range));
+
+ Resources set = Resources::parse("names:{foo,bar}").get();
+ EXPECT_FALSE(Resources::isScalarQuantity(set));
+
+ Resources reserved = createReservedResource(
+ "cpus", "1", createDynamicReservationInfo("role", "principal"));
+ EXPECT_FALSE(Resources::isScalarQuantity(reserved));
+
+ Resources disk = createDiskResource("10", "role1", "1", "path");
+ EXPECT_FALSE(Resources::isScalarQuantity(disk));
+
+ Resources allocated = Resources::parse("cpus:1;mem:512").get();
+ allocated.allocate("role");
+ EXPECT_FALSE(Resources::isScalarQuantity(allocated));
+
+ Resource revocable = Resources::parse("cpus", "1", "*").get();
+ revocable.mutable_revocable();
+ EXPECT_FALSE(Resources::isScalarQuantity(revocable));
+}
+
+
TEST(ReservedResourcesTest, Validation)
{
// Unreserved.
http://git-wip-us.apache.org/repos/asf/mesos/blob/ccb24bf3/src/v1/resources.cpp
----------------------------------------------------------------------
diff --git a/src/v1/resources.cpp b/src/v1/resources.cpp
index 04a45a5..3d06fc6 100644
--- a/src/v1/resources.cpp
+++ b/src/v1/resources.cpp
@@ -1268,6 +1268,17 @@ bool Resources::isShared(const Resource& resource)
}
+bool Resources::isScalarQuantity(const Resources& resources)
+{
+ // Instead of checking the absence of non-scalar-quantity fields,
+ // we do an equality check between the original `Resources` object and
+ // its stripped counterpart.
+ //
+ // We remove the static reservation metadata here via `toUnreserved()`.
+ return resources == resources.createStrippedScalarQuantity().toUnreserved();
+}
+
+
bool Resources::hasRefinedReservations(const Resource& resource)
{
CHECK(!resource.has_role()) << resource;