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/12/22 22:04:04 UTC
mesos git commit: Added a utility to shrink scalar resource while
keeping its meta-data.
Repository: mesos
Updated Branches:
refs/heads/master 7773ce827 -> c5654bc28
Added a utility to shrink scalar resource while keeping its meta-data.
This is particularly useful when performing fine-grained allocation
of resources by "chopping off" some of the resources when possible.
Note that some resources, e.g. MOUNT volumes, cannot be shrunk.
Review: https://reviews.apache.org/r/64798/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/c5654bc2
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/c5654bc2
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/c5654bc2
Branch: refs/heads/master
Commit: c5654bc2891a85450844b1944652b8858ed693c0
Parents: 7773ce8
Author: Meng Zhu <mz...@mesosphere.io>
Authored: Fri Dec 22 13:49:27 2017 -0800
Committer: Benjamin Mahler <bm...@apache.org>
Committed: Fri Dec 22 14:03:44 2017 -0800
----------------------------------------------------------------------
include/mesos/resources.hpp | 7 +++++++
include/mesos/v1/resources.hpp | 7 +++++++
src/common/resources.cpp | 23 +++++++++++++++++++++++
src/v1/resources.cpp | 23 +++++++++++++++++++++++
4 files changed, 60 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/c5654bc2/include/mesos/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp
index eefe9ea..69bf823 100644
--- a/include/mesos/resources.hpp
+++ b/include/mesos/resources.hpp
@@ -334,6 +334,13 @@ public:
// This must be called only when the resource is reserved!
static const std::string& reservationRole(const Resource& resource);
+ // Shrinks a scalar type `resource` to the target size.
+ // Returns true if the resource was shrunk to the target size,
+ // or the resource is already within the target size.
+ // Returns false otherwise (i.e. the resource is indivisible.
+ // E.g. MOUNT volume).
+ static bool shrink(Resource* resource, const Value::Scalar& target);
+
// Returns the summed up Resources given a hashmap<Key, Resources>.
//
// NOTE: While scalar resources such as "cpus" sum correctly,
http://git-wip-us.apache.org/repos/asf/mesos/blob/c5654bc2/include/mesos/v1/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/v1/resources.hpp b/include/mesos/v1/resources.hpp
index 7a5b0e3..c712057 100644
--- a/include/mesos/v1/resources.hpp
+++ b/include/mesos/v1/resources.hpp
@@ -334,6 +334,13 @@ public:
// This must be called only when the resource is reserved!
static const std::string& reservationRole(const Resource& resource);
+ // Shrinks a scalar type `resource` to the target size.
+ // Returns true if the resource was shrunk to the target size,
+ // or the resource is already within the target size.
+ // Returns false otherwise (i.e. the resource is indivisible.
+ // E.g. MOUNT volume).
+ static bool shrink(Resource* resource, const Value::Scalar& target);
+
// Returns the summed up Resources given a hashmap<Key, Resources>.
//
// NOTE: While scalar resources such as "cpus" sum correctly,
http://git-wip-us.apache.org/repos/asf/mesos/blob/c5654bc2/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index 919a03b..1291208 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -1247,6 +1247,29 @@ const string& Resources::reservationRole(const Resource& resource)
return resource.reservations().rbegin()->role();
}
+
+bool Resources::shrink(Resource* resource, const Value::Scalar& target)
+{
+ if (resource->scalar() <= target) {
+ return true; // Already within target.
+ }
+
+ Resource copy = *resource;
+ copy.mutable_scalar()->CopyFrom(target);
+
+ // Some resources (e.g. MOUNT disk) are indivisible. We use
+ // a containement check to verify this. Specifically, if a
+ // contains a smaller version of itself, then it can safely
+ // be chopped into a smaller amount.
+ if (Resources(*resource).contains(copy)) {
+ resource->CopyFrom(copy);
+ return true;
+ }
+
+ return false;
+}
+
+
/////////////////////////////////////////////////
// Public member functions.
/////////////////////////////////////////////////
http://git-wip-us.apache.org/repos/asf/mesos/blob/c5654bc2/src/v1/resources.cpp
----------------------------------------------------------------------
diff --git a/src/v1/resources.cpp b/src/v1/resources.cpp
index 365ffa7..06e310f 100644
--- a/src/v1/resources.cpp
+++ b/src/v1/resources.cpp
@@ -1280,6 +1280,29 @@ const string& Resources::reservationRole(const Resource& resource)
return resource.reservations().rbegin()->role();
}
+
+bool Resources::shrink(Resource* resource, const Value::Scalar& target)
+{
+ if (resource->scalar() <= target) {
+ return true; // Already within target.
+ }
+
+ Resource copy = *resource;
+ copy.mutable_scalar()->CopyFrom(target);
+
+ // Some resources (e.g. MOUNT disk) are indivisible. We use
+ // a containement check to verify this. Specifically, if a
+ // contains a smaller version of itself, then it can safely
+ // be chopped into a smaller amount.
+ if (Resources(*resource).contains(copy)) {
+ resource->CopyFrom(copy);
+ return true;
+ }
+
+ return false;
+}
+
+
/////////////////////////////////////////////////
// Public member functions.
/////////////////////////////////////////////////