mesos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject [1/2] mesos git commit: Added `Resources::createStrippedScalarQuantity()`.
Date Thu, 03 Mar 2016 01:18:05 GMT
Repository: mesos
Updated Branches:
  refs/heads/master effce8ca6 -> b4d746fc0


Added `Resources::createStrippedScalarQuantity()`.

This returns a new `Resources` object that omits dynamic reservation
and persistent volume information. This is intended primarily for
situations in which code wants to efficiently compute aggregate
statistics about many `Resource` values for which reservation and
persistent volume information is not relevant.

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


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

Branch: refs/heads/master
Commit: 204398e08be326d1192706ae4b263d2656edc453
Parents: effce8c
Author: Neil Conway <neil.conway@gmail.com>
Authored: Wed Mar 2 16:54:29 2016 -0800
Committer: Joris Van Remoortere <joris.van.remoortere@gmail.com>
Committed: Wed Mar 2 17:16:51 2016 -0800

----------------------------------------------------------------------
 include/mesos/resources.hpp    | 14 +++++++++
 include/mesos/v1/resources.hpp | 14 +++++++++
 src/common/resources.cpp       | 17 +++++++++++
 src/tests/resources_tests.cpp  | 57 +++++++++++++++++++++++++++++++++++++
 src/v1/resources.cpp           | 17 +++++++++++
 5 files changed, 119 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/204398e0/include/mesos/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp
index fe8a574..388c4f3 100644
--- a/include/mesos/resources.hpp
+++ b/include/mesos/resources.hpp
@@ -251,6 +251,20 @@ public:
       const std::string& role = "*",
       const Option<Resource::ReservationInfo>& reservation = None()) 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.
+  //
+  // This is intended for code that would like to aggregate together
+  // Resource values without regard for metadata like whether the
+  // resource is reserved or the particular volume ID in use. For
+  // example, when calculating the total resources in a cluster,
+  // preserving such information has a major performance cost.
+  Resources createStrippedScalarQuantity() const;
+
   // Finds a Resources object with the same amount of each resource
   // type as "targets" from these Resources. The roles specified in
   // "targets" set the preference order. For each resource type,

http://git-wip-us.apache.org/repos/asf/mesos/blob/204398e0/include/mesos/v1/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/v1/resources.hpp b/include/mesos/v1/resources.hpp
index c27927e..64ad8bf 100644
--- a/include/mesos/v1/resources.hpp
+++ b/include/mesos/v1/resources.hpp
@@ -251,6 +251,20 @@ public:
       const std::string& role = "*",
       const Option<Resource::ReservationInfo>& reservation = None()) 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.
+  //
+  // This is intended for code that would like to aggregate together
+  // Resource values without regard for metadata like whether the
+  // resource is reserved or the particular volume ID in use. For
+  // example, when calculating the total resources in a cluster,
+  // preserving such information has a major performance cost.
+  Resources createStrippedScalarQuantity() const;
+
   // Finds a Resources object with the same amount of each resource
   // type as "targets" from these Resources. The roles specified in
   // "targets" set the preference order. For each resource type,

http://git-wip-us.apache.org/repos/asf/mesos/blob/204398e0/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index 4fa1e78..cf07072 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -924,6 +924,23 @@ Resources Resources::flatten(
 }
 
 
+Resources Resources::createStrippedScalarQuantity() const
+{
+  Resources stripped;
+
+  foreach (const Resource& resource, resources) {
+    if (resource.type() == Value::SCALAR) {
+      Resource scalar = resource;
+      scalar.clear_reservation();
+      scalar.clear_disk();
+      stripped += scalar;
+    }
+  }
+
+  return stripped;
+}
+
+
 Option<Resources> Resources::find(const Resources& targets) const
 {
   Resources total;

http://git-wip-us.apache.org/repos/asf/mesos/blob/204398e0/src/tests/resources_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp
index e7525a0..5357275 100644
--- a/src/tests/resources_tests.cpp
+++ b/src/tests/resources_tests.cpp
@@ -2207,6 +2207,63 @@ TEST(ResourcesOperationTest, CreatePersistentVolume)
 }
 
 
+TEST(ResourcesOperationTest, StrippedResourcesVolume)
+{
+  Resources volume = createDiskResource("200", "role", "1", "path");
+  Resources stripped = volume.createStrippedScalarQuantity();
+
+  EXPECT_TRUE(stripped.persistentVolumes().empty());
+  EXPECT_EQ(Megabytes(200), stripped.disk().get());
+
+  // `createStrippedScalarQuantity` doesn't remove the `role` from a
+  // reserved resource.
+  EXPECT_FALSE(stripped.reserved("role").empty());
+
+  Resource strippedVolume = *(stripped.begin());
+
+  ASSERT_EQ(Value::SCALAR, strippedVolume.type());
+  EXPECT_FLOAT_EQ(200, strippedVolume.scalar().value());
+  EXPECT_EQ("role", strippedVolume.role());
+  EXPECT_EQ("disk", strippedVolume.name());
+  EXPECT_FALSE(strippedVolume.has_reservation());
+  EXPECT_FALSE(strippedVolume.has_disk());
+  EXPECT_FALSE(Resources::isPersistentVolume(strippedVolume));
+}
+
+
+TEST(ResourcesOperationTest, StrippedResourcesReserved)
+{
+  Resources unreserved = Resources::parse("cpus:1;mem:512").get();
+  Resources dynamicallyReserved = unreserved.flatten(
+      "role", createReservationInfo("principal"));
+
+  Resources stripped = dynamicallyReserved.createStrippedScalarQuantity();
+
+  // After being stripped, a dynamically reserved resource
+  // effectively becomes statically reserved.
+  EXPECT_FALSE(stripped.reserved("role").empty());
+
+  foreach (const Resource& resource, stripped) {
+    EXPECT_EQ("role", resource.role());
+    EXPECT_FALSE(resource.has_reservation());
+    EXPECT_FALSE(Resources::isDynamicallyReserved(resource));
+    EXPECT_FALSE(Resources::isUnreserved(resource));
+  }
+}
+
+
+TEST(ResourcesOperationTest, StrippedResourcesNonScalar)
+{
+  Resources ports = Resources::parse("ports:[10000-20000, 30000-50000]").get();
+
+  EXPECT_TRUE(ports.createStrippedScalarQuantity().empty());
+
+  Resources names = Resources::parse("names:{foo,bar}").get();
+
+  EXPECT_TRUE(names.createStrippedScalarQuantity().empty());
+}
+
+
 // Helper for creating a revocable resource.
 static Resource createRevocableResource(
     const string& name,

http://git-wip-us.apache.org/repos/asf/mesos/blob/204398e0/src/v1/resources.cpp
----------------------------------------------------------------------
diff --git a/src/v1/resources.cpp b/src/v1/resources.cpp
index bca5231..296c4c2 100644
--- a/src/v1/resources.cpp
+++ b/src/v1/resources.cpp
@@ -927,6 +927,23 @@ Resources Resources::flatten(
 }
 
 
+Resources Resources::createStrippedScalarQuantity() const
+{
+  Resources stripped;
+
+  foreach (const Resource& resource, resources) {
+    if (resource.type() == Value::SCALAR) {
+      Resource scalar = resource;
+      scalar.clear_reservation();
+      scalar.clear_disk();
+      stripped += scalar;
+    }
+  }
+
+  return stripped;
+}
+
+
 Option<Resources> Resources::find(const Resources& targets) const
 {
   Resources total;


Mime
View raw message