Repository: mesos Updated Branches: refs/heads/master 474963306 -> 18344e03e
Added Resources support to return the port range for the given number of ports. Review: https://reviews.apache.org/r/24810 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/18344e03 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/18344e03 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/18344e03 Branch: refs/heads/master Commit: 18344e03e4bcfde6157997e28265084e2e10d971 Parents: 4749633 Author: Zuyu Zhang <[email protected]> Authored: Tue Sep 2 21:38:06 2014 -0700 Committer: Vinod Kone <[email protected]> Committed: Tue Sep 2 21:38:07 2014 -0700 ---------------------------------------------------------------------- include/mesos/resources.hpp | 4 ++++ src/Makefile.am | 3 ++- src/common/resources.cpp | 30 ++++++++++++++++++++++++++++++ src/tests/resources_tests.cpp | 28 ++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/18344e03/include/mesos/resources.hpp ---------------------------------------------------------------------- diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp index 12adedc..d7a9e8a 100644 --- a/include/mesos/resources.hpp +++ b/include/mesos/resources.hpp @@ -192,6 +192,10 @@ public: // TODO(vinod): Provide a Ranges abstraction. Option<Value::Ranges> ports() const; + // Helper function to extract the given number of ports + // from the "ports" resource. + Option<Value::Ranges> ports(size_t numPorts) const; + // TODO(jieyu): Consider returning an EphemeralPorts abstraction // which holds the ephemeral ports allocation logic. Option<Value::Ranges> ephemeral_ports() const; http://git-wip-us.apache.org/repos/asf/mesos/blob/18344e03/src/Makefile.am ---------------------------------------------------------------------- diff --git a/src/Makefile.am b/src/Makefile.am index 40b9f6b..5526189 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -562,6 +562,8 @@ libmesos_la_LIBTOOLFLAGS = --tag=CXX # Add the convenience library. libmesos_la_LIBADD = libmesos_no_3rdparty.la +libmesos_la_LIBADD += ../$(LIBPROCESS)/libprocess.la + # For non-convenience libraries we need to link them in to make the shared # library each time. (Currently, we don't support platforms where this is not # possible.) @@ -593,7 +595,6 @@ else libmesos_la_LIBADD += -lprotobuf endif -libmesos_la_LIBADD += ../$(LIBPROCESS)/libprocess.la # Binaries. sbin_PROGRAMS += mesos-master http://git-wip-us.apache.org/repos/asf/mesos/blob/18344e03/src/common/resources.cpp ---------------------------------------------------------------------- diff --git a/src/common/resources.cpp b/src/common/resources.cpp index edf36b1..29fc765 100644 --- a/src/common/resources.cpp +++ b/src/common/resources.cpp @@ -516,6 +516,36 @@ Option<Value::Ranges> Resources::ports() const return None(); } +Option<Value::Ranges> Resources::ports(size_t numPorts) const +{ + Value::Ranges total; + + foreach (const Resource& resource, resources) { + if (resource.name() == "ports" && + resource.type() == Value::RANGES && + isAllocatable(resource)) { + foreach (const Value::Range& range, resource.ranges().range()) { + size_t interval = range.end() - range.begin() + 1; + if (numPorts < interval) { + Value::Range* lastRange = total.add_range(); + lastRange->set_begin(range.begin()); + lastRange->set_end(range.begin() + numPorts - 1); + + return total; + } else { + total.add_range()->CopyFrom(range); + numPorts -= interval; + + if (numPorts == 0) { + return total; + } + } + } + } + } + + return None(); +} Option<Value::Ranges> Resources::ephemeral_ports() const { http://git-wip-us.apache.org/repos/asf/mesos/blob/18344e03/src/tests/resources_tests.cpp ---------------------------------------------------------------------- diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp index b5f2e8b..3e50889 100644 --- a/src/tests/resources_tests.cpp +++ b/src/tests/resources_tests.cpp @@ -159,6 +159,34 @@ TEST(ResourcesTest, Resources) } +TEST(ResourcesTest, Ports) +{ + // Extract one Value::Range. + Resources r = Resources::parse("ports:[10000-20000, 30000-50000]").get(); + Option<Value::Ranges> ports = r.ports(5); + EXPECT_SOME(ports); + EXPECT_EQ("[10000-10004]", stringify(ports.get())); + + // Extract two Value::Ranges. + r = Resources::parse("ports:[10000-10000, 20000-50000]").get(); + ports = r.ports(5); + EXPECT_SOME(ports); + EXPECT_EQ("[10000-10000, 20000-20003]", stringify(ports.get())); + + // Extract mutiple Value::Ranges. + r = Resources::parse("ports:[10000-10001, 10003-10004, 10007-10009," + "10020-20000]").get(); + ports = r.ports(10); + EXPECT_SOME(ports); + EXPECT_EQ("[10000-10001, 10003-10004, 10007-10009, 10020-10022]", + stringify(ports.get())); + + // Not enough ports. + r = Resources::parse("ports:[10000-10004]").get(); + EXPECT_TRUE(r.ports(10).isNone()); +} + + TEST(ResourcesTest, Printing) { Resources r = Resources::parse("cpus:45.55;"
