Repository: mesos Updated Branches: refs/heads/master d2985542a -> c67d1dd3a
Changed Resourcs JSON model() to combine nonrevocable resources and ignore revocable resources. Review: https://reviews.apache.org/r/35367 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/c67d1dd3 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/c67d1dd3 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/c67d1dd3 Branch: refs/heads/master Commit: c67d1dd3a1e81e4d80e8d7bdbf0b85bfcd991e33 Parents: d298554 Author: Jiang Yan Xu <y...@jxu.me> Authored: Thu Jun 11 12:52:43 2015 -0700 Committer: Jiang Yan Xu <y...@jxu.me> Committed: Thu Jun 11 17:43:29 2015 -0700 ---------------------------------------------------------------------- include/mesos/resources.hpp | 6 ++++++ src/common/http.cpp | 28 +++++++++++++++++++++------- src/common/resources.cpp | 12 ++++++++++++ src/tests/common/http_tests.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/tests/resources_tests.cpp | 14 ++++++++++++++ 5 files changed, 89 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/c67d1dd3/include/mesos/resources.hpp ---------------------------------------------------------------------- diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp index 7da6e6e..0ccc337 100644 --- a/include/mesos/resources.hpp +++ b/include/mesos/resources.hpp @@ -19,6 +19,7 @@ #ifndef __RESOURCES_HPP__ #define __RESOURCES_HPP__ +#include <map> #include <iostream> #include <set> #include <string> @@ -250,6 +251,11 @@ public: // Get the set of unique resource names. std::set<std::string> names() const; + // Get the types of resources associated with each resource name. + // NOTE: Resources of the same name must have the same type, as + // enforced by Resources::parse(). + std::map<std::string, Value_Type> types() const; + // Helpers to get known resource types. // TODO(vinod): Fix this when we make these types as first class // protobufs. http://git-wip-us.apache.org/repos/asf/mesos/blob/c67d1dd3/src/common/http.cpp ---------------------------------------------------------------------- diff --git a/src/common/http.cpp b/src/common/http.cpp index 2ac7fba..c9a441f 100644 --- a/src/common/http.cpp +++ b/src/common/http.cpp @@ -16,6 +16,9 @@ * limitations under the License. */ +#include <map> +#include <set> +#include <string> #include <utility> #include <vector> @@ -30,6 +33,9 @@ #include "messages/messages.hpp" +using std::map; +using std::set; +using std::string; using std::vector; namespace mesos { @@ -45,21 +51,29 @@ JSON::Object model(const Resources& resources) object.values["mem"] = 0; object.values["disk"] = 0; - foreach (const Resource& resource, resources) - { - switch(resource.type()) + // To maintain backwards compatibility we exclude revocable + // resources in the reporting. + Resources nonRevocable = resources - resources.revocable(); + + map<string, Value_Type> types = nonRevocable.types(); + + foreachpair (const string& name, const Value_Type& type, types) { + switch(type) { case Value::SCALAR: - object.values[resource.name()] = resource.scalar().value(); + object.values[name] = + nonRevocable.get<Value::Scalar>(name).get().value(); break; case Value::RANGES: - object.values[resource.name()] = stringify(resource.ranges()); + object.values[name] = + stringify(nonRevocable.get<Value::Ranges>(name).get()); break; case Value::SET: - object.values[resource.name()] = stringify(resource.set()); + object.values[name] = + stringify(nonRevocable.get<Value::Set>(name).get()); break; default: - LOG(FATAL) << "Unexpected Value type: " << resource.type(); + LOG(FATAL) << "Unexpected Value type: " << type; } } http://git-wip-us.apache.org/repos/asf/mesos/blob/c67d1dd3/src/common/resources.cpp ---------------------------------------------------------------------- diff --git a/src/common/resources.cpp b/src/common/resources.cpp index 0596919..3b79243 100644 --- a/src/common/resources.cpp +++ b/src/common/resources.cpp @@ -33,6 +33,7 @@ #include <stout/lambda.hpp> #include <stout/strings.hpp> +using std::map; using std::ostream; using std::set; using std::string; @@ -959,6 +960,17 @@ set<string> Resources::names() const } +map<string, Value_Type> Resources::types() const +{ + map<string, Value_Type> result; + foreach(const Resource& resource, resources) { + result[resource.name()] = resource.type(); + } + + return result; +} + + Option<double> Resources::cpus() const { Option<Value::Scalar> value = get<Value::Scalar>("cpus"); http://git-wip-us.apache.org/repos/asf/mesos/blob/c67d1dd3/src/tests/common/http_tests.cpp ---------------------------------------------------------------------- diff --git a/src/tests/common/http_tests.cpp b/src/tests/common/http_tests.cpp index f087b23..97a0eba 100644 --- a/src/tests/common/http_tests.cpp +++ b/src/tests/common/http_tests.cpp @@ -21,6 +21,7 @@ #include <vector> #include <mesos/mesos.hpp> +#include <mesos/resources.hpp> #include <stout/gtest.hpp> #include <stout/json.hpp> @@ -112,3 +113,38 @@ TEST(HTTP, ModelTask) // Ensure both are modeled the same. EXPECT_EQ(object, object_); } + + +// This test verifies that Resources model combines all resources of different +// roles and filters out revocable resources. +TEST(HTTP, ModelResources) +{ + // Resources of mixed types, roles, duplicate names; standard ( + // e.g., 'cpus') and custom (i.e., 'bar'). + Resources nonRevocable = Resources::parse( + "cpus:1;cpus(foo):1;mem:512;disk:1024;ports(foo):[1-10];bar:1").get(); + + Resource revocableCpus = Resources::parse("cpus", "1.1", "*").get(); + revocableCpus.mutable_revocable(); + Resource revocableMem = Resources::parse("mem", "513", "*").get(); + revocableMem.mutable_revocable(); + Resource revocableDisk = Resources::parse("disk", "1025", "*").get(); + revocableDisk.mutable_revocable(); + + Resources total = + nonRevocable + revocableCpus + revocableMem + revocableDisk; + + JSON::Value object = model(total); + + Try<JSON::Value> expected = JSON::parse( + "{" + " \"bar\":1," + " \"cpus\":2," + " \"disk\":1024," + " \"mem\":512," + " \"ports\":\"[1-10]\"" + "}"); + + ASSERT_SOME(expected); + EXPECT_EQ(expected.get(), object); +} http://git-wip-us.apache.org/repos/asf/mesos/blob/c67d1dd3/src/tests/resources_tests.cpp ---------------------------------------------------------------------- diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp index ad12648..9f96b14 100644 --- a/src/tests/resources_tests.cpp +++ b/src/tests/resources_tests.cpp @@ -31,6 +31,7 @@ using namespace mesos::internal::master; +using std::map; using std::ostringstream; using std::pair; using std::set; @@ -919,6 +920,19 @@ TEST(ResourcesTest, Names) } +TEST(ResourcesTest, Types) +{ + Resources resources = + Resources::parse("cpus(role1):2;cpus:4;ports:[1-10];ports:[11-20]").get(); + + map<string, Value_Type> types{ + {"cpus", Value::SCALAR}, + {"ports", Value::RANGES} + }; + ASSERT_EQ(types, resources.types()); +} + + // Helper for creating a reserved resource. static Resource createReservedResource( const string& name,