You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by me...@apache.org on 2015/11/09 19:25:00 UTC
[2/2] mesos git commit: Added JSON parsing for Resources.
Added JSON parsing for Resources.
This includes code changes necessary for JSON parsing of Resources.
Documentation changes will be posted soon in another review
(https://reviews.apache.org/r/39102/).
Review: https://reviews.apache.org/r/39018
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/62f17731
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/62f17731
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/62f17731
Branch: refs/heads/master
Commit: 62f177311ed47200998c67785d4a1a368dad259e
Parents: ba2bf9f
Author: Greg Mann <gr...@mesosphere.io>
Authored: Mon Nov 9 09:34:20 2015 -0800
Committer: Adam B <ad...@mesosphere.io>
Committed: Mon Nov 9 10:24:24 2015 -0800
----------------------------------------------------------------------
include/mesos/resources.hpp | 71 ++++-
include/mesos/v1/resources.hpp | 71 ++++-
src/common/resources.cpp | 283 +++++++++++++-----
src/tests/resources_tests.cpp | 571 ++++++++++++++++++++++++++++++++++++
src/v1/resources.cpp | 283 +++++++++++++-----
5 files changed, 1087 insertions(+), 192 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/62f17731/include/mesos/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/resources.hpp b/include/mesos/resources.hpp
index 6c3a065..ce12a26 100644
--- a/include/mesos/resources.hpp
+++ b/include/mesos/resources.hpp
@@ -62,32 +62,73 @@ namespace mesos {
class Resources
{
public:
- // Parses the text and returns a Resource object with the given name
- // and role. For example, "Resource r = parse("mem", "1024", "*");".
+ /**
+ * Returns a Resource with the given name, value, and role.
+ *
+ * Parses the text and returns a Resource object with the given name, value,
+ * and role. For example, "Resource r = parse("mem", "1024", "*");".
+ *
+ * @param name The name of the Resource.
+ * @param value The Resource's value.
+ * @param role The role associated with the Resource.
+ * @return A `Try` which contains the parsed Resource if parsing was
+ * successful, or an Error otherwise.
+ */
static Try<Resource> parse(
const std::string& name,
const std::string& value,
const std::string& role);
- // Parses Resources from text in the form "name:value(role);
- // name:value;...". Any name/value pair that doesn't specify a role
- // is assigned to defaultRole.
+ /**
+ * Parses Resources from an input string.
+ *
+ * Parses Resources from text in the form of a JSON array. If that fails,
+ * parses text in the form "name(role):value;name:value;...". Any resource
+ * that doesn't specify a role is assigned to the provided default role. See
+ * the `Resource` protobuf definition for precise JSON formatting.
+ *
+ * Example JSON: [{"name":cpus","type":"SCALAR","scalar":{"value":8}}]
+ *
+ * @param text The input string.
+ * @param defaultRole The default role.
+ * @return A `Try` which contains the parsed Resources if parsing was
+ * successful, or an Error otherwise.
+ */
static Try<Resources> parse(
const std::string& text,
const std::string& defaultRole = "*");
- // Validates the given Resource object. Returns Error if it is not
- // valid. A Resource object is valid if it has a name, a valid type,
- // i.e. scalar, range, or set, has the appropriate value set, and
- // a valid (role, reservation) pair for dynamic reservation.
+ /**
+ * Validates a Resource object.
+ *
+ * Validates the given Resource object. Returns Error if it is not valid. A
+ * Resource object is valid if it has a name, a valid type, i.e. scalar,
+ * range, or set, has the appropriate value set, and a valid (role,
+ * reservation) pair for dynamic reservation.
+ *
+ * @param resource The input resource to be validated.
+ * @return An `Option` which contains None() if the validation was successful,
+ * or an Error if not.
+ */
static Option<Error> validate(const Resource& resource);
- // Validates the given protobufs.
- // TODO(jieyu): Right now, it's the same as checking each individual
- // Resource object in the protobufs. In the future, we could add
- // more checks that are not possible if checking each Resource
- // object individually. For example, we could check multiple usage
- // of an item in a set or a ranges, etc.
+ /**
+ * Validates the given repeated Resource protobufs.
+ *
+ * Validates the given repeated Resource protobufs. Returns Error if an
+ * invalid Resource is found. A Resource object is valid if it has a name, a
+ * valid type, i.e. scalar, range, or set, has the appropriate value set, and
+ * a valid (role, reservation) pair for dynamic reservation.
+ *
+ * TODO(jieyu): Right now, it's the same as checking each individual Resource
+ * object in the protobufs. In the future, we could add more checks that are
+ * not possible if checking each Resource object individually. For example, we
+ * could check multiple usage of an item in a set or a range, etc.
+ *
+ * @param resources The repeated Resource objects to be validated.
+ * @return An `Option` which contains None() if the validation was successful,
+ * or an Error if not.
+ */
static Option<Error> validate(
const google::protobuf::RepeatedPtrField<Resource>& resources);
http://git-wip-us.apache.org/repos/asf/mesos/blob/62f17731/include/mesos/v1/resources.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/v1/resources.hpp b/include/mesos/v1/resources.hpp
index fe8925a..92db123 100644
--- a/include/mesos/v1/resources.hpp
+++ b/include/mesos/v1/resources.hpp
@@ -62,32 +62,73 @@ namespace v1 {
class Resources
{
public:
- // Parses the text and returns a Resource object with the given name
- // and role. For example, "Resource r = parse("mem", "1024", "*");".
+ /**
+ * Returns a Resource with the given name, value, and role.
+ *
+ * Parses the text and returns a Resource object with the given name, value,
+ * and role. For example, "Resource r = parse("mem", "1024", "*");".
+ *
+ * @param name The name of the Resource.
+ * @param value The Resource's value.
+ * @param role The role associated with the Resource.
+ * @return A `Try` which contains the parsed Resource if parsing was
+ * successful, or an Error otherwise.
+ */
static Try<Resource> parse(
const std::string& name,
const std::string& value,
const std::string& role);
- // Parses Resources from text in the form "name:value(role);
- // name:value;...". Any name/value pair that doesn't specify a role
- // is assigned to defaultRole.
+ /**
+ * Parses Resources from an input string.
+ *
+ * Parses Resources from text in the form of a JSON array. If that fails,
+ * parses text in the form "name(role):value;name:value;...". Any resource
+ * that doesn't specify a role is assigned to the provided default role. See
+ * the `Resource` protobuf definition for precise JSON formatting.
+ *
+ * Example JSON: [{"name":cpus","type":"SCALAR","scalar":{"value":8}}]
+ *
+ * @param text The input string.
+ * @param defaultRole The default role.
+ * @return A `Try` which contains the parsed Resources if parsing was
+ * successful, or an Error otherwise.
+ */
static Try<Resources> parse(
const std::string& text,
const std::string& defaultRole = "*");
- // Validates the given Resource object. Returns Error if it is not
- // valid. A Resource object is valid if it has a name, a valid type,
- // i.e. scalar, range, or set, has the appropriate value set, and
- // a valid (role, reservation) pair for dynamic reservation.
+ /**
+ * Validates a Resource object.
+ *
+ * Validates the given Resource object. Returns Error if it is not valid. A
+ * Resource object is valid if it has a name, a valid type, i.e. scalar,
+ * range, or set, has the appropriate value set, and a valid (role,
+ * reservation) pair for dynamic reservation.
+ *
+ * @param resource The input resource to be validated.
+ * @return An `Option` which contains None() if the validation was successful,
+ * or an Error if not.
+ */
static Option<Error> validate(const Resource& resource);
- // Validates the given protobufs.
- // TODO(jieyu): Right now, it's the same as checking each individual
- // Resource object in the protobufs. In the future, we could add
- // more checks that are not possible if checking each Resource
- // object individually. For example, we could check multiple usage
- // of an item in a set or a ranges, etc.
+ /**
+ * Validates the given repeated Resource protobufs.
+ *
+ * Validates the given repeated Resource protobufs. Returns Error if an
+ * invalid Resource is found. A Resource object is valid if it has a name, a
+ * valid type, i.e. scalar, range, or set, has the appropriate value set, and
+ * a valid (role, reservation) pair for dynamic reservation.
+ *
+ * TODO(jieyu): Right now, it's the same as checking each individual Resource
+ * object in the protobufs. In the future, we could add more checks that are
+ * not possible if checking each Resource object individually. For example, we
+ * could check multiple usage of an item in a set or a range, etc.
+ *
+ * @param resources The repeated Resource objects to be validated.
+ * @return An `Option` which contains None() if the validation was successful,
+ * or an Error if not.
+ */
static Option<Error> validate(
const google::protobuf::RepeatedPtrField<Resource>& resources);
http://git-wip-us.apache.org/repos/asf/mesos/blob/62f17731/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index 601388c..f1da323 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -30,7 +30,9 @@
#include <stout/foreach.hpp>
#include <stout/hashmap.hpp>
+#include <stout/json.hpp>
#include <stout/lambda.hpp>
+#include <stout/protobuf.hpp>
#include <stout/strings.hpp>
using std::map;
@@ -39,6 +41,8 @@ using std::set;
using std::string;
using std::vector;
+using google::protobuf::RepeatedPtrField;
+
namespace mesos {
@@ -254,6 +258,111 @@ static bool contains(const Resource& left, const Resource& right)
}
}
+
+/**
+ * Checks that a Resources object is valid for command line specification.
+ *
+ * Checks that the given Resources object is appropriate for specification at
+ * the command line. Resources are appropriate if they do not have two resources
+ * with the same name but different types, and do not attempt to specify
+ * persistent volumes, revocable resources, or dynamic reservations.
+ *
+ * @param resources The input Resources.
+ * @return An `Option` containing None() if validation was successful, or an
+ * Error otherwise.
+ */
+static Option<Error> validateCommandLineResources(const Resources& resources)
+{
+ hashmap<string, Value::Type> nameTypes;
+
+ foreach (const Resource& resource, resources) {
+ // These fields should only be provided programmatically,
+ // not at the command line.
+ if (Resources::isPersistentVolume(resource)) {
+ return Error(
+ "Persistent volumes cannot be specified at the command line");
+ } else if (Resources::isRevocable(resource)) {
+ return Error(
+ "Revocable resources cannot be specified at the command line; do "
+ "not include a 'revocable' key in the resources JSON");
+ } else if (Resources::isDynamicallyReserved(resource)) {
+ return Error(
+ "Dynamic reservations cannot be specified at the command line; "
+ "do not include a 'reservation' key in the resources JSON");
+ }
+
+ if (nameTypes.contains(resource.name()) &&
+ nameTypes[resource.name()] != resource.type()) {
+ return Error(
+ "Resources with the same name ('" + resource.name() + "') but "
+ "different types are not allowed");
+ } else if (!nameTypes.contains(resource.name())) {
+ nameTypes[resource.name()] = resource.type();
+ }
+ }
+
+ return None();
+}
+
+
+/**
+ * Converts a JSON Array to a Resources object.
+ *
+ * Converts a JSON Array to a Resources object. This uses JSON to protobuf
+ * conversion, so the Array should contain JSON Objects, each of which follows
+ * the format of the Resource protobuf message. If no role is specified, the
+ * provided default role will be assigned. `Resources::validate()` is used to
+ * validate the input objects, and empty Resource objects will return an error.
+ *
+ * Example: [{"name":cpus","type":"SCALAR","scalar":{"value":8}}]
+ *
+ * @param resourcesJSON The input JSON Array.
+ * @param defaultRole The default role.
+ * @return A `Try` containing a Resources object if conversion was successful,
+ * or an Error otherwise.
+ */
+static Try<Resources> convertJSON(
+ const JSON::Array& resourcesJSON,
+ const string& defaultRole)
+{
+ // Convert the JSON Array into a protobuf message and use
+ // that to construct a new Resources object.
+ Try<RepeatedPtrField<Resource>> resourcesProtobuf =
+ protobuf::parse<RepeatedPtrField<Resource>>(resourcesJSON);
+
+ if (resourcesProtobuf.isError()) {
+ return Error(
+ "Some JSON resources were not formatted properly: "
+ + resourcesProtobuf.error());
+ }
+
+ // TODO(greggomann): Refactor this `RepeatedPtrField<Resource>` to `Resources`
+ // conversion if/when there is a factory function for Resources. Use of the
+ // `Resources(RepeatedPtrField<Resource>)` constructor is avoided here because
+ // it doesn't allow us to catch errors that occur during construction.
+ // Note: the related JIRA ticket is MESOS-3852.
+ Resources result;
+
+ foreach (Resource& resource, resourcesProtobuf.get()) {
+ // Set the default role if none was specified.
+ if (!resource.has_role()) {
+ resource.set_role(defaultRole);
+ }
+
+ // Validate the Resource and make sure it isn't empty.
+ Option<Error> error = Resources::validate(resource);
+ if (error.isSome()) {
+ return error.get();
+ } else if (Resources::isEmpty(resource)) {
+ return Error("Some JSON resources were empty: " + stringify(resource));
+ }
+
+ result += resource;
+ }
+
+ return result;
+}
+
} // namespace internal {
@@ -305,7 +414,6 @@ Resource operator-(const Resource& left, const Resource& right)
// Public static functions.
/////////////////////////////////////////////////
-
Try<Resource> Resources::parse(
const string& name,
const string& value,
@@ -350,52 +458,64 @@ Try<Resources> Resources::parse(
const string& text,
const string& defaultRole)
{
- Resources resources;
- hashmap<string, Value_Type> nameTypes;
+ Resources result;
- foreach (const string& token, strings::tokenize(text, ";")) {
- vector<string> pair = strings::tokenize(token, ":");
- if (pair.size() != 2) {
- return Error("Bad value for resources, missing or extra ':' in " + token);
+ // Try to parse as a JSON Array.
+ Try<JSON::Array> resourcesJSON = JSON::parse<JSON::Array>(text);
+ if (resourcesJSON.isSome()) {
+ Try<Resources> resources =
+ internal::convertJSON(resourcesJSON.get(), defaultRole);
+ if (resources.isError()) {
+ return resources;
}
- string name;
- string role;
- size_t openParen = pair[0].find("(");
- if (openParen == string::npos) {
- name = strings::trim(pair[0]);
- role = defaultRole;
- } else {
- size_t closeParen = pair[0].find(")");
- if (closeParen == string::npos || closeParen < openParen) {
+ result = resources.get();
+ } else {
+ VLOG(1) << "Parsing resources as JSON failed: " << text << "\n"
+ << "Trying semicolon-delimited string format instead";
+
+ foreach (const string& token, strings::tokenize(text, ";")) {
+ vector<string> pair = strings::tokenize(token, ":");
+ if (pair.size() != 2) {
return Error(
- "Bad value for resources, mismatched parentheses in " + token);
+ "Bad value for resources, missing or extra ':' in " + token);
}
- name = strings::trim(pair[0].substr(0, openParen));
+ string name;
+ string role;
+ size_t openParen = pair[0].find("(");
+ if (openParen == string::npos) {
+ name = strings::trim(pair[0]);
+ role = defaultRole;
+ } else {
+ size_t closeParen = pair[0].find(")");
+ if (closeParen == string::npos || closeParen < openParen) {
+ return Error(
+ "Bad value for resources, mismatched parentheses in " + token);
+ }
- role = strings::trim(pair[0].substr(
- openParen + 1,
- closeParen - openParen - 1));
- }
+ name = strings::trim(pair[0].substr(0, openParen));
- Try<Resource> resource = Resources::parse(name, pair[1], role);
- if (resource.isError()) {
- return Error(resource.error());
- }
+ role = strings::trim(pair[0].substr(
+ openParen + 1,
+ closeParen - openParen - 1));
+ }
- if (nameTypes.contains(name) && nameTypes[name] != resource.get().type()) {
- return Error(
- "Resources with the same name ('" + name + "') but different types "
- "are not allowed");
- } else if (!nameTypes.contains(name)) {
- nameTypes[name] = resource.get().type();
+ Try<Resource> resource = Resources::parse(name, pair[1], role);
+ if (resource.isError()) {
+ return Error(resource.error());
+ }
+
+ result += resource.get();
}
+ }
- resources += resource.get();
+ Option<Error> error = internal::validateCommandLineResources(result);
+ if (error.isSome()) {
+ return error.get();
}
- return resources;
+ return result;
}
@@ -480,8 +600,7 @@ Option<Error> Resources::validate(const Resource& resource)
}
-Option<Error> Resources::validate(
- const google::protobuf::RepeatedPtrField<Resource>& resources)
+Option<Error> Resources::validate(const RepeatedPtrField<Resource>& resources)
{
foreach (const Resource& resource, resources) {
Option<Error> error = validate(resource);
@@ -545,11 +664,11 @@ bool Resources::isRevocable(const Resource& resource)
return resource.has_revocable();
}
+
/////////////////////////////////////////////////
// Public member functions.
/////////////////////////////////////////////////
-
Resources::Resources(const Resource& resource)
{
// NOTE: Invalid and zero Resource object will be ignored.
@@ -566,8 +685,7 @@ Resources::Resources(const vector<Resource>& _resources)
}
-Resources::Resources(
- const google::protobuf::RepeatedPtrField<Resource>& _resources)
+Resources::Resources(const RepeatedPtrField<Resource>& _resources)
{
foreach (const Resource& resource, _resources) {
// NOTE: Invalid and zero Resource objects will be ignored.
@@ -679,46 +797,6 @@ Resources Resources::flatten(
static bool any(const Resource&) { return true; }
-Option<Resources> Resources::find(const Resource& target) const
-{
- Resources found;
- Resources total = *this;
- Resources remaining = Resources(target).flatten();
-
- // First look in the target role, then unreserved, then any remaining role.
- // TODO(mpark): Use a lambda for 'any' instead once we get full C++11.
- vector<lambda::function<bool(const Resource&)>> predicates = {
- lambda::bind(isReserved, lambda::_1, target.role()),
- isUnreserved,
- any
- };
-
- foreach (const auto& predicate, predicates) {
- foreach (const Resource& resource, total.filter(predicate)) {
- // Need to flatten to ignore the roles in contains().
- Resources flattened = Resources(resource).flatten();
-
- if (flattened.contains(remaining)) {
- // Done!
- if (!resource.has_reservation()) {
- return found + remaining.flatten(resource.role());
- } else {
- return found +
- remaining.flatten(resource.role(), resource.reservation());
- }
- } else if (remaining.contains(flattened)) {
- found += resource;
- total -= resource;
- remaining -= flattened;
- break;
- }
- }
- }
-
- return None();
-}
-
-
Option<Resources> Resources::find(const Resources& targets) const
{
Resources total;
@@ -1034,6 +1112,10 @@ Option<Value::Ranges> Resources::ephemeral_ports() const
}
+/////////////////////////////////////////////////
+// Private member functions.
+/////////////////////////////////////////////////
+
bool Resources::_contains(const Resource& that) const
{
foreach (const Resource& resource, resources) {
@@ -1046,12 +1128,51 @@ bool Resources::_contains(const Resource& that) const
}
+Option<Resources> Resources::find(const Resource& target) const
+{
+ Resources found;
+ Resources total = *this;
+ Resources remaining = Resources(target).flatten();
+
+ // First look in the target role, then unreserved, then any remaining role.
+ // TODO(mpark): Use a lambda for 'any' instead once we get full C++11.
+ vector<lambda::function<bool(const Resource&)>> predicates = {
+ lambda::bind(isReserved, lambda::_1, target.role()),
+ isUnreserved,
+ any
+ };
+
+ foreach (const auto& predicate, predicates) {
+ foreach (const Resource& resource, total.filter(predicate)) {
+ // Need to flatten to ignore the roles in contains().
+ Resources flattened = Resources(resource).flatten();
+
+ if (flattened.contains(remaining)) {
+ // The target has been found, return the result.
+ if (!resource.has_reservation()) {
+ return found + remaining.flatten(resource.role());
+ } else {
+ return found +
+ remaining.flatten(resource.role(), resource.reservation());
+ }
+ } else if (remaining.contains(flattened)) {
+ found += resource;
+ total -= resource;
+ remaining -= flattened;
+ break;
+ }
+ }
+ }
+
+ return None();
+}
+
+
/////////////////////////////////////////////////
// Overloaded operators.
/////////////////////////////////////////////////
-
-Resources::operator const google::protobuf::RepeatedPtrField<Resource>&() const
+Resources::operator const RepeatedPtrField<Resource>&() const
{
return resources;
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/62f17731/src/tests/resources_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp
index 209d708..e663f5e 100644
--- a/src/tests/resources_tests.cpp
+++ b/src/tests/resources_tests.cpp
@@ -160,6 +160,577 @@ TEST(ResourcesTest, ParsingFromJSON)
ASSERT_SOME(parse);
EXPECT_EQ(resources, Resources(parse.get()));
+
+ // Use JSON strings both with and without newline characters.
+ string jsonString =
+ "[\n"
+ " {\n"
+ " \"name\": \"cpus\",\n"
+ " \"type\": \"SCALAR\",\n"
+ " \"scalar\": {\n"
+ " \"value\": 45.55\n"
+ " }\n"
+ " }\n"
+ "]";
+
+ Try<Resources> resourcesTry = Resources::parse(jsonString);
+ ASSERT_SOME(resourcesTry);
+
+ Resources cpuResources(resourcesTry.get());
+ auto cpus = cpuResources.begin();
+
+ EXPECT_EQ(Value::SCALAR, cpus->type());
+ EXPECT_EQ(45.55, cpus->scalar().value());
+
+ // Make sure there is only one Resource in cpuResources.
+ EXPECT_EQ(cpus + 1, cpuResources.end());
+
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"ports\","
+ " \"type\": \"RANGES\","
+ " \"ranges\": {"
+ " \"range\": ["
+ " {"
+ " \"begin\": 10000,"
+ " \"end\": 20000"
+ " },"
+ " {"
+ " \"begin\": 30000,"
+ " \"end\": 50000"
+ " }"
+ " ]"
+ " }"
+ " }"
+ "]";
+
+ resourcesTry = Resources::parse(jsonString);
+ ASSERT_SOME(resourcesTry);
+
+ Resources portResources(resourcesTry.get());
+ auto ports = portResources.begin();
+
+ EXPECT_EQ(Value::RANGES, ports->type());
+ EXPECT_EQ(2, ports->ranges().range_size());
+
+ // Do not specify the ordering of ranges, only check the values.
+ if (10000 != ports->ranges().range(0).begin()) {
+ EXPECT_EQ(30000, ports->ranges().range(0).begin());
+ EXPECT_EQ(10000, ports->ranges().range(1).begin());
+ } else {
+ EXPECT_EQ(30000, ports->ranges().range(1).begin());
+ }
+
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"lun_lun\","
+ " \"yang_yang\""
+ " ]"
+ " }"
+ " }"
+ "]";
+
+ resourcesTry = Resources::parse(jsonString);
+ ASSERT_SOME(resourcesTry);
+
+ Resources pandaResources(resourcesTry.get());
+ auto pandas = pandaResources.begin();
+
+ EXPECT_EQ(Value::SET, pandas->type());
+ EXPECT_EQ(2, pandas->set().item_size());
+ EXPECT_EQ("pandas", pandas->name());
+
+ // Do not specify the ordering of the set's items, only check the values.
+ if ("lun_lun" != pandas->set().item(0)) {
+ EXPECT_EQ("yang_yang", pandas->set().item(0));
+ EXPECT_EQ("lun_lun", pandas->set().item(1));
+ } else {
+ EXPECT_EQ("yang_yang", pandas->set().item(1));
+ }
+
+ jsonString =
+ "[\n"
+ " {\n"
+ " \"name\": \"cpus\",\n"
+ " \"type\": \"SCALAR\",\n"
+ " \"scalar\": {\n"
+ " \"value\": 45.55\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"ports\",\n"
+ " \"type\": \"RANGES\",\n"
+ " \"ranges\": {\n"
+ " \"range\": [\n"
+ " {\n"
+ " \"begin\": 10000,\n"
+ " \"end\": 20000\n"
+ " },\n"
+ " {\n"
+ " \"begin\": 30000,\n"
+ " \"end\": 50000\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"pandas\",\n"
+ " \"type\": \"SET\",\n"
+ " \"set\": {\n"
+ " \"item\": [\n"
+ " \"lun_lun\",\n"
+ " \"yang_yang\"\n"
+ " ]\n"
+ " }\n"
+ " }\n"
+ "]";
+
+ resourcesTry = Resources::parse(jsonString);
+ ASSERT_SOME(resourcesTry);
+
+ Resources r1(resourcesTry.get());
+
+ Resources r2;
+ r2 += *cpus;
+ r2 += *ports;
+ r2 += *pandas;
+
+ EXPECT_EQ(r1, r2);
+}
+
+
+TEST(ResourcesTest, ParsingFromJSONWithRoles)
+{
+ string jsonString =
+ "[\n"
+ " {\n"
+ " \"name\": \"cpus\",\n"
+ " \"type\": \"SCALAR\",\n"
+ " \"scalar\": {\n"
+ " \"value\": 45.55\n"
+ " },\n"
+ " \"role\": \"role1\"\n"
+ " }\n"
+ "]";
+
+ Try<Resources> resourcesTry = Resources::parse(jsonString);
+ ASSERT_SOME(resourcesTry);
+
+ Resources cpuResources(resourcesTry.get());
+ auto cpus = cpuResources.begin();
+
+ EXPECT_EQ("role1", cpus->role());
+
+ jsonString =
+ "[\n"
+ " {\n"
+ " \"name\": \"cpus\",\n"
+ " \"type\": \"SCALAR\",\n"
+ " \"scalar\": {\n"
+ " \"value\": 54.44\n"
+ " },\n"
+ " \"role\": \"role2\"\n"
+ " }\n"
+ "]";
+
+ resourcesTry = Resources::parse(jsonString);
+ ASSERT_SOME(resourcesTry);
+
+ Resources cpuResources2(resourcesTry.get());
+ auto cpus2 = cpuResources2.begin();
+
+ Resources resources;
+ resources += *cpus2;
+ resources += *cpus;
+ resources += *cpus;
+
+ EXPECT_TRUE(resources.contains(Resources(*cpus)));
+ EXPECT_EQ(145.54, resources.cpus().get());
+
+ foreach (Resource& resource, resources) {
+ if (resource.role() == "role1") {
+ EXPECT_EQ(91.1, resource.scalar().value());
+ } else {
+ EXPECT_EQ(54.44, resource.scalar().value());
+ }
+ }
+
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"ports\","
+ " \"type\": \"RANGES\","
+ " \"ranges\": {"
+ " \"range\": ["
+ " {"
+ " \"begin\": 10000,"
+ " \"end\": 20000"
+ " },"
+ " {"
+ " \"begin\": 30000,"
+ " \"end\": 50000"
+ " }"
+ " ]"
+ " },"
+ " \"role\": \"role1\""
+ " },"
+ " {"
+ " \"name\": \"pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"lun_lun\","
+ " \"yang_yang\""
+ " ]"
+ " },"
+ " \"role\": \"panda_liason\""
+ " }"
+ "]";
+
+ resourcesTry = Resources::parse(jsonString);
+ ASSERT_SOME(resourcesTry);
+
+ resources = resourcesTry.get();
+
+ foreach (Resource& resource, resources) {
+ if (resource.role() == "role1") {
+ EXPECT_EQ(Value::RANGES, resource.type());
+ EXPECT_EQ(2, resource.ranges().range_size());
+ } else {
+ EXPECT_EQ(Value::SET, resource.type());
+ EXPECT_EQ(2, resource.set().item_size());
+ }
+ }
+}
+
+
+TEST(ResourcesTest, ParsingFromJSONError)
+{
+ // A single JSON object, not an array.
+ string jsonString =
+ "{"
+ " \"name\": \"sad_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"bai_yun\","
+ " \"gao_gao\""
+ " ]"
+ " }"
+ "}";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Missing comma in Resource array.
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"sad_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"bai_yun\","
+ " \"gao_gao\""
+ " ]"
+ " }"
+ " }"
+ " {"
+ " \"name\": \"happy_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"yun_zi\","
+ " \"xiao_liwu\""
+ " ]"
+ " }"
+ " }"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Missing comma in Set list.
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"sad_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"bai_yun\","
+ " \"gao_gao\""
+ " ]"
+ " }"
+ " },"
+ " {"
+ " \"name\": \"happy_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"yun_zi\","
+ " \"xiao_liwu\""
+ " \"xi_lan\""
+ " ]"
+ " }"
+ " }"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // An array of arrays.
+ jsonString =
+ "["
+ " ["
+ " {"
+ " \"name\": \"sad_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"bai_yun\","
+ " \"gao_gao\""
+ " ]"
+ " }"
+ " },"
+ " {"
+ " \"name\": \"happy_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"yun_zi\","
+ " \"xiao_liwu\""
+ " ]"
+ " }"
+ " }"
+ " ]"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Missing array brackets.
+ jsonString =
+ "{"
+ " \"name\": \"sad_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"bai_yun\","
+ " \"gao_gao\""
+ " ]"
+ " }"
+ "},"
+ "{"
+ " \"name\": \"happy_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"yun_zi\","
+ " \"xiao_liwu\""
+ " ]"
+ " }"
+ "}";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Contains extraneous text.
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"sad_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"bai_yun\","
+ " \"gao_gao\""
+ " ]"
+ " }"
+ " }"
+ "]"
+ "once upon a time there was a sad panda...";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Wrong type of bracket.
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"lun_lun\","
+ " \"yang_yang\""
+ " }"
+ " }"
+ " }"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Missing 'type' field.
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"pandas\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"lun_lun\","
+ " \"yang_yang\""
+ " ]"
+ " }"
+ " }"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Empty name.
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"lun_lun\","
+ " \"yang_yang\""
+ " ]"
+ " }"
+ " }"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Unrecognized Resource 'type'.
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"pandas\","
+ " \"type\": \"FLOATINGPOINT\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"lun_lun\","
+ " \"yang_yang\""
+ " ]"
+ " }"
+ " }"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Empty Resources.
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"panda_power\","
+ " \"type\": \"SCALAR\","
+ " \"scalar\": {"
+ " \"value\": 0"
+ " }"
+ " },"
+ " {"
+ " \"name\": \"panda_window\","
+ " \"type\": \"RANGES\","
+ " \"ranges\": {"
+ " \"range\": []"
+ " },"
+ " \"role\": \"role1\""
+ " },"
+ " {"
+ " \"name\": \"actual_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": []"
+ " }"
+ " }"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Resources with the same name but different types.
+ jsonString =
+ "["
+ " {"
+ " \"name\": \"happy_pandas\","
+ " \"type\": \"SCALAR\","
+ " \"scalar\": {"
+ " \"value\": 14"
+ " }"
+ " },"
+ " {"
+ " \"name\": \"happy_pandas\","
+ " \"type\": \"SET\","
+ " \"set\": {"
+ " \"item\": ["
+ " \"yun_zi\","
+ " \"xiao_liwu\","
+ " \"xi_lan\""
+ " ]"
+ " }"
+ " }"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Attempts to specify a persistent volume.
+ jsonString =
+ "["
+ " {"
+ " \"name\" : \"disk\","
+ " \"type\" : \"SCALAR\","
+ " \"scalar\" : {"
+ " \"value\" : 2048"
+ " },"
+ " \"disk\": {"
+ " \"persistence\": {"
+ " \"id\" : \"persistent_volume_1\""
+ " },"
+ " \"volume\" : {"
+ " \"container_path\" : \"/var/lib/mesos/persist\","
+ " \"mode\" : \"RW\""
+ " }"
+ " }"
+ " }"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Attempts to specify a dynamic reservation.
+ jsonString =
+ "["
+ " {"
+ " \"name\" : \"disk\","
+ " \"type\" : \"SCALAR\","
+ " \"scalar\" : {"
+ " \"value\" : 2048"
+ " },"
+ " \"reservation\" : {"
+ " \"principal\" : \"default_principal\""
+ " },"
+ " \"role\": \"new_role\""
+ " }"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
+
+ // Attempts to specify revocable resources.
+ jsonString =
+ "[\n"
+ " {\n"
+ " \"name\": \"cpus\",\n"
+ " \"type\": \"SCALAR\",\n"
+ " \"scalar\": {\n"
+ " \"value\": 54.44\n"
+ " },\n"
+ " \"role\": \"role2\",\n"
+ " \"revocable\": {}\n"
+ " }\n"
+ "]";
+
+ EXPECT_ERROR(Resources::parse(jsonString));
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/62f17731/src/v1/resources.cpp
----------------------------------------------------------------------
diff --git a/src/v1/resources.cpp b/src/v1/resources.cpp
index dc86890..6f50101 100644
--- a/src/v1/resources.cpp
+++ b/src/v1/resources.cpp
@@ -30,7 +30,9 @@
#include <stout/foreach.hpp>
#include <stout/hashmap.hpp>
+#include <stout/json.hpp>
#include <stout/lambda.hpp>
+#include <stout/protobuf.hpp>
#include <stout/strings.hpp>
using std::map;
@@ -39,6 +41,8 @@ using std::set;
using std::string;
using std::vector;
+using google::protobuf::RepeatedPtrField;
+
namespace mesos {
namespace v1 {
@@ -255,6 +259,111 @@ static bool contains(const Resource& left, const Resource& right)
}
}
+
+/**
+ * Checks that a Resources object is valid for command line specification.
+ *
+ * Checks that the given Resources object is appropriate for specification at
+ * the command line. Resources are appropriate if they do not have two resources
+ * with the same name but different types, and do not attempt to specify
+ * persistent volumes, revocable resources, or dynamic reservations.
+ *
+ * @param resources The input Resources.
+ * @return An `Option` containing None() if validation was successful, or an
+ * Error otherwise.
+ */
+static Option<Error> validateCommandLineResources(const Resources& resources)
+{
+ hashmap<string, Value::Type> nameTypes;
+
+ foreach (const Resource& resource, resources) {
+ // These fields should only be provided programmatically,
+ // not at the command line.
+ if (Resources::isPersistentVolume(resource)) {
+ return Error(
+ "Persistent volumes cannot be specified at the command line");
+ } else if (Resources::isRevocable(resource)) {
+ return Error(
+ "Revocable resources cannot be specified at the command line; do "
+ "not include a 'revocable' key in the resources JSON");
+ } else if (Resources::isDynamicallyReserved(resource)) {
+ return Error(
+ "Dynamic reservations cannot be specified at the command line; "
+ "do not include a 'reservation' key in the resources JSON");
+ }
+
+ if (nameTypes.contains(resource.name()) &&
+ nameTypes[resource.name()] != resource.type()) {
+ return Error(
+ "Resources with the same name ('" + resource.name() + "') but "
+ "different types are not allowed");
+ } else if (!nameTypes.contains(resource.name())) {
+ nameTypes[resource.name()] = resource.type();
+ }
+ }
+
+ return None();
+}
+
+
+/**
+ * Converts a JSON Array to a Resources object.
+ *
+ * Converts a JSON Array to a Resources object. This uses JSON to protobuf
+ * conversion, so the Array should contain JSON Objects, each of which follows
+ * the format of the Resource protobuf message. If no role is specified, the
+ * provided default role will be assigned. `Resources::validate()` is used to
+ * validate the input objects, and empty Resource objects will return an error.
+ *
+ * Example: [{"name":cpus","type":"SCALAR","scalar":{"value":8}}]
+ *
+ * @param resourcesJSON The input JSON Array.
+ * @param defaultRole The default role.
+ * @return A `Try` containing a Resources object if conversion was successful,
+ * or an Error otherwise.
+ */
+static Try<Resources> convertJSON(
+ const JSON::Array& resourcesJSON,
+ const string& defaultRole)
+{
+ // Convert the JSON Array into a protobuf message and use
+ // that to construct a new Resources object.
+ Try<RepeatedPtrField<Resource>> resourcesProtobuf =
+ protobuf::parse<RepeatedPtrField<Resource>>(resourcesJSON);
+
+ if (resourcesProtobuf.isError()) {
+ return Error(
+ "Some JSON resources were not formatted properly: "
+ + resourcesProtobuf.error());
+ }
+
+ // TODO(greggomann): Refactor this `RepeatedPtrField<Resource>` to `Resources`
+ // conversion if/when there is a factory function for Resources. Use of the
+ // `Resources(RepeatedPtrField<Resource>)` constructor is avoided here because
+ // it doesn't allow us to catch errors that occur during construction.
+ // Note: the related JIRA ticket is MESOS-3852.
+ Resources result;
+
+ foreach (Resource& resource, resourcesProtobuf.get()) {
+ // Set the default role if none was specified.
+ if (!resource.has_role()) {
+ resource.set_role(defaultRole);
+ }
+
+ // Validate the Resource and make sure it isn't empty.
+ Option<Error> error = Resources::validate(resource);
+ if (error.isSome()) {
+ return error.get();
+ } else if (Resources::isEmpty(resource)) {
+ return Error("Some JSON resources were empty: " + stringify(resource));
+ }
+
+ result += resource;
+ }
+
+ return result;
+}
+
} // namespace internal {
@@ -306,7 +415,6 @@ Resource operator-(const Resource& left, const Resource& right)
// Public static functions.
/////////////////////////////////////////////////
-
Try<Resource> Resources::parse(
const string& name,
const string& value,
@@ -351,52 +459,64 @@ Try<Resources> Resources::parse(
const string& text,
const string& defaultRole)
{
- Resources resources;
- hashmap<string, Value_Type> nameTypes;
+ Resources result;
- foreach (const string& token, strings::tokenize(text, ";")) {
- vector<string> pair = strings::tokenize(token, ":");
- if (pair.size() != 2) {
- return Error("Bad value for resources, missing or extra ':' in " + token);
+ // Try to parse as a JSON Array.
+ Try<JSON::Array> resourcesJSON = JSON::parse<JSON::Array>(text);
+ if (resourcesJSON.isSome()) {
+ Try<Resources> resources =
+ internal::convertJSON(resourcesJSON.get(), defaultRole);
+ if (resources.isError()) {
+ return resources;
}
- string name;
- string role;
- size_t openParen = pair[0].find("(");
- if (openParen == string::npos) {
- name = strings::trim(pair[0]);
- role = defaultRole;
- } else {
- size_t closeParen = pair[0].find(")");
- if (closeParen == string::npos || closeParen < openParen) {
+ result = resources.get();
+ } else {
+ VLOG(1) << "Parsing resources as JSON failed: " << text << "\n"
+ << "Trying semicolon-delimited string format instead";
+
+ foreach (const string& token, strings::tokenize(text, ";")) {
+ vector<string> pair = strings::tokenize(token, ":");
+ if (pair.size() != 2) {
return Error(
- "Bad value for resources, mismatched parentheses in " + token);
+ "Bad value for resources, missing or extra ':' in " + token);
}
- name = strings::trim(pair[0].substr(0, openParen));
+ string name;
+ string role;
+ size_t openParen = pair[0].find("(");
+ if (openParen == string::npos) {
+ name = strings::trim(pair[0]);
+ role = defaultRole;
+ } else {
+ size_t closeParen = pair[0].find(")");
+ if (closeParen == string::npos || closeParen < openParen) {
+ return Error(
+ "Bad value for resources, mismatched parentheses in " + token);
+ }
- role = strings::trim(pair[0].substr(
- openParen + 1,
- closeParen - openParen - 1));
- }
+ name = strings::trim(pair[0].substr(0, openParen));
- Try<Resource> resource = Resources::parse(name, pair[1], role);
- if (resource.isError()) {
- return Error(resource.error());
- }
+ role = strings::trim(pair[0].substr(
+ openParen + 1,
+ closeParen - openParen - 1));
+ }
- if (nameTypes.contains(name) && nameTypes[name] != resource.get().type()) {
- return Error(
- "Resources with the same name ('" + name + "') but different types "
- "are not allowed");
- } else if (!nameTypes.contains(name)) {
- nameTypes[name] = resource.get().type();
+ Try<Resource> resource = Resources::parse(name, pair[1], role);
+ if (resource.isError()) {
+ return Error(resource.error());
+ }
+
+ result += resource.get();
}
+ }
- resources += resource.get();
+ Option<Error> error = internal::validateCommandLineResources(result);
+ if (error.isSome()) {
+ return error.get();
}
- return resources;
+ return result;
}
@@ -481,8 +601,7 @@ Option<Error> Resources::validate(const Resource& resource)
}
-Option<Error> Resources::validate(
- const google::protobuf::RepeatedPtrField<Resource>& resources)
+Option<Error> Resources::validate(const RepeatedPtrField<Resource>& resources)
{
foreach (const Resource& resource, resources) {
Option<Error> error = validate(resource);
@@ -546,11 +665,11 @@ bool Resources::isRevocable(const Resource& resource)
return resource.has_revocable();
}
+
/////////////////////////////////////////////////
// Public member functions.
/////////////////////////////////////////////////
-
Resources::Resources(const Resource& resource)
{
// NOTE: Invalid and zero Resource object will be ignored.
@@ -567,8 +686,7 @@ Resources::Resources(const vector<Resource>& _resources)
}
-Resources::Resources(
- const google::protobuf::RepeatedPtrField<Resource>& _resources)
+Resources::Resources(const RepeatedPtrField<Resource>& _resources)
{
foreach (const Resource& resource, _resources) {
// NOTE: Invalid and zero Resource objects will be ignored.
@@ -680,46 +798,6 @@ Resources Resources::flatten(
static bool any(const Resource&) { return true; }
-Option<Resources> Resources::find(const Resource& target) const
-{
- Resources found;
- Resources total = *this;
- Resources remaining = Resources(target).flatten();
-
- // First look in the target role, then unreserved, then any remaining role.
- // TODO(mpark): Use a lambda for 'any' instead once we get full C++11.
- vector<lambda::function<bool(const Resource&)>> predicates = {
- lambda::bind(isReserved, lambda::_1, target.role()),
- isUnreserved,
- any
- };
-
- foreach (const auto& predicate, predicates) {
- foreach (const Resource& resource, total.filter(predicate)) {
- // Need to flatten to ignore the roles in contains().
- Resources flattened = Resources(resource).flatten();
-
- if (flattened.contains(remaining)) {
- // Done!
- if (!resource.has_reservation()) {
- return found + remaining.flatten(resource.role());
- } else {
- return found +
- remaining.flatten(resource.role(), resource.reservation());
- }
- } else if (remaining.contains(flattened)) {
- found += resource;
- total -= resource;
- remaining -= flattened;
- break;
- }
- }
- }
-
- return None();
-}
-
-
Option<Resources> Resources::find(const Resources& targets) const
{
Resources total;
@@ -1035,6 +1113,10 @@ Option<Value::Ranges> Resources::ephemeral_ports() const
}
+/////////////////////////////////////////////////
+// Private member functions.
+/////////////////////////////////////////////////
+
bool Resources::_contains(const Resource& that) const
{
foreach (const Resource& resource, resources) {
@@ -1047,12 +1129,51 @@ bool Resources::_contains(const Resource& that) const
}
+Option<Resources> Resources::find(const Resource& target) const
+{
+ Resources found;
+ Resources total = *this;
+ Resources remaining = Resources(target).flatten();
+
+ // First look in the target role, then unreserved, then any remaining role.
+ // TODO(mpark): Use a lambda for 'any' instead once we get full C++11.
+ vector<lambda::function<bool(const Resource&)>> predicates = {
+ lambda::bind(isReserved, lambda::_1, target.role()),
+ isUnreserved,
+ any
+ };
+
+ foreach (const auto& predicate, predicates) {
+ foreach (const Resource& resource, total.filter(predicate)) {
+ // Need to flatten to ignore the roles in contains().
+ Resources flattened = Resources(resource).flatten();
+
+ if (flattened.contains(remaining)) {
+ // The target has been found, return the result.
+ if (!resource.has_reservation()) {
+ return found + remaining.flatten(resource.role());
+ } else {
+ return found +
+ remaining.flatten(resource.role(), resource.reservation());
+ }
+ } else if (remaining.contains(flattened)) {
+ found += resource;
+ total -= resource;
+ remaining -= flattened;
+ break;
+ }
+ }
+ }
+
+ return None();
+}
+
+
/////////////////////////////////////////////////
// Overloaded operators.
/////////////////////////////////////////////////
-
-Resources::operator const google::protobuf::RepeatedPtrField<Resource>&() const
+Resources::operator const RepeatedPtrField<Resource>&() const
{
return resources;
}
Re: [2/2] mesos git commit: Added JSON parsing for Resources.
Posted by Joris Van Remoortere <jo...@mesosphere.io>.
fixed
—
*Joris Van Remoortere*
Mesosphere
On Tue, Nov 10, 2015 at 9:53 PM, James Peach <jo...@gmail.com> wrote:
>
> > On Nov 9, 2015, at 10:25 AM, me@apache.org wrote:
> >
> > Added JSON parsing for Resources.
> >
> >
>
> [snip]
>
> >
> http://git-wip-us.apache.org/repos/asf/mesos/blob/62f17731/src/tests/resources_tests.cpp
> > ----------------------------------------------------------------------
> > diff --git a/src/tests/resources_tests.cpp
> b/src/tests/resources_tests.cpp
> > index 209d708..e663f5e 100644
> > --- a/src/tests/resources_tests.cpp
> > +++ b/src/tests/resources_tests.cpp
> > @@ -160,6 +160,577 @@ TEST(ResourcesTest, ParsingFromJSON)
> > ASSERT_SOME(parse);
> >
> >
>
> [snip]
>
> > + EXPECT_EQ(Value::RANGES, ports->type());
> > + EXPECT_EQ(2, ports->ranges().range_size());
> > +
> > + // Do not specify the ordering of ranges, only check the values.
> > + if (10000 != ports->ranges().range(0).begin()) {
> > + EXPECT_EQ(30000, ports->ranges().range(0).begin());
> > + EXPECT_EQ(10000, ports->ranges().range(1).begin());
> > + } else {
> > + EXPECT_EQ(30000, ports->ranges().range(1).begin());
> > + }
>
> GCC 4.9.2 really wants unsigned constants here ...
>
> CXX tests/mesos_tests-resources_tests.o
> In file included from
> /opt/home/src/mesos.git/src/tests/resources_tests.cpp:23:0:
> ../3rdparty/libprocess/3rdparty/gmock-1.7.0/gtest/include/gtest/gtest.h:
> In instantiation of ‘testing::AssertionResult
> testing::internal::CmpHelperEQ(const char*, const char*, const T1&, const
> T2&) [with T1 = int; T2 = long unsigned int]’:
> ../3rdparty/libprocess/3rdparty/gmock-1.7.0/gtest/include/gtest/gtest.h:1485:30:
> required from ‘static testing::AssertionResult
> testing::internal::EqHelper<lhs_is_null_literal>::Compare(const char*,
> const char*, const T1&, const T2&) [with T1 = int; T2 = long unsigned int;
> bool lhs_is_null_literal = false]’
> /opt/home/src/mesos.git/src/tests/resources_tests.cpp:219:5: required
> from here
> ../3rdparty/libprocess/3rdparty/gmock-1.7.0/gtest/include/gtest/gtest.h:1448:16:
> error: comparison between signed and unsigned integer expressions
> [-Werror=sign-compare]
> if (expected == actual) {
> ^
>
> J
>
>
Re: [2/2] mesos git commit: Added JSON parsing for Resources.
Posted by James Peach <jo...@gmail.com>.
> On Nov 9, 2015, at 10:25 AM, me@apache.org wrote:
>
> Added JSON parsing for Resources.
>
>
[snip]
> http://git-wip-us.apache.org/repos/asf/mesos/blob/62f17731/src/tests/resources_tests.cpp
> ----------------------------------------------------------------------
> diff --git a/src/tests/resources_tests.cpp b/src/tests/resources_tests.cpp
> index 209d708..e663f5e 100644
> --- a/src/tests/resources_tests.cpp
> +++ b/src/tests/resources_tests.cpp
> @@ -160,6 +160,577 @@ TEST(ResourcesTest, ParsingFromJSON)
> ASSERT_SOME(parse);
>
>
[snip]
> + EXPECT_EQ(Value::RANGES, ports->type());
> + EXPECT_EQ(2, ports->ranges().range_size());
> +
> + // Do not specify the ordering of ranges, only check the values.
> + if (10000 != ports->ranges().range(0).begin()) {
> + EXPECT_EQ(30000, ports->ranges().range(0).begin());
> + EXPECT_EQ(10000, ports->ranges().range(1).begin());
> + } else {
> + EXPECT_EQ(30000, ports->ranges().range(1).begin());
> + }
GCC 4.9.2 really wants unsigned constants here ...
CXX tests/mesos_tests-resources_tests.o
In file included from /opt/home/src/mesos.git/src/tests/resources_tests.cpp:23:0:
../3rdparty/libprocess/3rdparty/gmock-1.7.0/gtest/include/gtest/gtest.h: In instantiation of ‘testing::AssertionResult testing::internal::CmpHelperEQ(const char*, const char*, const T1&, const T2&) [with T1 = int; T2 = long unsigned int]’:
../3rdparty/libprocess/3rdparty/gmock-1.7.0/gtest/include/gtest/gtest.h:1485:30: required from ‘static testing::AssertionResult testing::internal::EqHelper<lhs_is_null_literal>::Compare(const char*, const char*, const T1&, const T2&) [with T1 = int; T2 = long unsigned int; bool lhs_is_null_literal = false]’
/opt/home/src/mesos.git/src/tests/resources_tests.cpp:219:5: required from here
../3rdparty/libprocess/3rdparty/gmock-1.7.0/gtest/include/gtest/gtest.h:1448:16: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
if (expected == actual) {
^
J