This is an automated email from the ASF dual-hosted git repository. mzhu pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mesos.git
commit da1f95714f29165ea603ad109efae4e822c7408a Author: Meng Zhu <[email protected]> AuthorDate: Tue Feb 26 15:06:07 2019 -0800 Added method `fromScalarResources` in `ResourceQuantities`. This method takes all the scalar type `Resource` entries in the `Resources` and combine them to a `ResourceQuantities`. Only the resource name and its scalar value are used and the rest of the meta-data is ignored. Non-scalar resource entries are silently dropped. Since internal resources are always validated to have positive scalar values, this conversion will always succeed. Also added a dedicated test. Review: https://reviews.apache.org/r/70061 --- src/common/resource_quantities.cpp | 15 +++++++++++++++ src/common/resource_quantities.hpp | 8 ++++++++ src/tests/resource_quantities_tests.cpp | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/common/resource_quantities.cpp b/src/common/resource_quantities.cpp index 1c8eec0..fbc19fb 100644 --- a/src/common/resource_quantities.cpp +++ b/src/common/resource_quantities.cpp @@ -71,6 +71,21 @@ Try<ResourceQuantities> ResourceQuantities::fromString(const string& text) } +ResourceQuantities ResourceQuantities::fromScalarResources( + const Resources& resources) +{ + ResourceQuantities result; + + foreach (const Resource& resource, resources) { + CHECK_EQ(Value::SCALAR, resource.type()) << " Resources: " << resources; + + result[resource.name()] += resource.scalar(); + } + + return result; +} + + ResourceQuantities::ResourceQuantities() { // Pre-reserve space for first-class resources. diff --git a/src/common/resource_quantities.hpp b/src/common/resource_quantities.hpp index 31ce7b9..d775bb7 100644 --- a/src/common/resource_quantities.hpp +++ b/src/common/resource_quantities.hpp @@ -22,6 +22,7 @@ #include <vector> #include <mesos/mesos.hpp> +#include <mesos/resources.hpp> #include <stout/try.hpp> @@ -82,6 +83,13 @@ public: // will be returned. static Try<ResourceQuantities> fromString(const std::string& text); + // Take scalar `Resources` and combine them into `ResourceQuantities`. + // Only the resource name and its scalar value are used and the rest of the + // meta-data is ignored. It is caller's responsibility to ensure all + // `Resource` entries are of scalar type. Otherwise a `CHECK` error will + // be triggered. + static ResourceQuantities fromScalarResources(const Resources& resources); + ResourceQuantities(); explicit ResourceQuantities( diff --git a/src/tests/resource_quantities_tests.cpp b/src/tests/resource_quantities_tests.cpp index 435a494..6a1aa39 100644 --- a/src/tests/resource_quantities_tests.cpp +++ b/src/tests/resource_quantities_tests.cpp @@ -144,6 +144,29 @@ TEST(QuantitiesTest, FromStringInvalid) } +TEST(QuantitiesTest, FromScalarResources) +{ + // Empty resources. + ResourceQuantities quantities = + ResourceQuantities::fromScalarResources(Resources()); + EXPECT_EQ(0u, quantities.size()); + + // Result entries are ordered alphabetically. + quantities = ResourceQuantities::fromScalarResources( + CHECK_NOTERROR(Resources::parse("cpus:1;mem:512;disk:800"))); + EXPECT_EQ(3u, quantities.size()); + auto it = quantities.begin(); + EXPECT_EQ("cpus", it->first); + EXPECT_DOUBLE_EQ(1, it->second.value()); + ++it; + EXPECT_EQ("disk", it->first); + EXPECT_DOUBLE_EQ(800, it->second.value()); + ++it; + EXPECT_EQ("mem", it->first); + EXPECT_DOUBLE_EQ(512, it->second.value()); +} + + TEST(QuantitiesTest, Insertion) { ResourceQuantities resourceQuantities =
