You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by mp...@apache.org on 2017/07/05 10:54:10 UTC

[2/4] mesos git commit: Updated `validateAndNormalizeResources` to operate on `Operation`s.

Updated `validateAndNormalizeResources` to operate on `Operation`s.

Review: https://reviews.apache.org/r/60563


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/63c8b1d6
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/63c8b1d6
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/63c8b1d6

Branch: refs/heads/master
Commit: 63c8b1d63be363e96d7c2672d9d65b9d67ca48ed
Parents: 710b721
Author: Michael Park <mp...@apache.org>
Authored: Thu Jun 29 23:30:16 2017 -0700
Committer: Michael Park <mp...@apache.org>
Committed: Wed Jul 5 03:41:37 2017 -0700

----------------------------------------------------------------------
 src/common/resources_utils.cpp | 213 ++++++++++++++++++++++++++++++++++--
 src/common/resources_utils.hpp |  15 ++-
 2 files changed, 215 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/63c8b1d6/src/common/resources_utils.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources_utils.cpp b/src/common/resources_utils.cpp
index 3a6a578..821bd09 100644
--- a/src/common/resources_utils.cpp
+++ b/src/common/resources_utils.cpp
@@ -196,16 +196,215 @@ void convertResourceFormat(
 }
 
 
-Option<Error> validateAndUpgradeResources(RepeatedPtrField<Resource>* resources)
+Option<Error> validateAndNormalizeResources(Offer::Operation* operation)
 {
-  Option<Error> error = Resources::validate(*resources);
-  if (error.isSome()) {
-    return Error("Invalid resources upgrade: " + error->message);
-  }
+  CHECK_NOTNULL(operation);
+
+  switch (operation->type()) {
+    case Offer::Operation::RESERVE: {
+      // TODO(mpark): Once we perform a sanity check validation for
+      // offer operations as specified in MESOS-7760, this should no
+      // longer have to be handled in this function.
+      if (!operation->has_reserve()) {
+        return Error(
+            "A RESERVE offer operation must have"
+            " the Offer.Operation.reserve field set.");
+      }
+
+      Option<Error> error =
+        Resources::validate(operation->reserve().resources());
+
+      if (error.isSome()) {
+        return error;
+      }
+
+      convertResourceFormat(
+          operation->mutable_reserve()->mutable_resources(),
+          POST_RESERVATION_REFINEMENT);
+
+      return None();
+    }
+    case Offer::Operation::UNRESERVE: {
+      // TODO(mpark): Once we perform a sanity check validation for
+      // offer operations as specified in MESOS-7760, this should no
+      // longer have to be handled in this function.
+      if (!operation->has_unreserve()) {
+        return Error(
+            "An UNRESERVE offer operation must have"
+            " the Offer.Operation.unreserve field set.");
+      }
+
+      Option<Error> error =
+        Resources::validate(operation->unreserve().resources());
+
+      if (error.isSome()) {
+        return error;
+      }
+
+      convertResourceFormat(
+          operation->mutable_unreserve()->mutable_resources(),
+          POST_RESERVATION_REFINEMENT);
+
+      return None();
+    }
+    case Offer::Operation::CREATE: {
+      // TODO(mpark): Once we perform a sanity check validation for
+      // offer operations as specified in MESOS-7760, this should no
+      // longer have to be handled in this function.
+      if (!operation->has_create()) {
+        return Error(
+            "A CREATE offer operation must have"
+            " the Offer.Operation.create field set.");
+      }
+
+      Option<Error> error =
+        Resources::validate(operation->create().volumes());
+
+      if (error.isSome()) {
+        return error;
+      }
+
+      convertResourceFormat(
+          operation->mutable_create()->mutable_volumes(),
+          POST_RESERVATION_REFINEMENT);
+
+      return None();
+    }
+    case Offer::Operation::DESTROY: {
+      // TODO(mpark): Once we perform a sanity check validation for
+      // offer operations as specified in MESOS-7760, this should no
+      // longer have to be handled in this function.
+      if (!operation->has_destroy()) {
+        return Error(
+            "A DESTROY offer operation must have"
+            " the Offer.Operation.destroy field set.");
+      }
+
+      Option<Error> error =
+        Resources::validate(operation->destroy().volumes());
+
+      if (error.isSome()) {
+        return error;
+      }
+
+      convertResourceFormat(
+          operation->mutable_destroy()->mutable_volumes(),
+          POST_RESERVATION_REFINEMENT);
+
+      return None();
+    }
+    case Offer::Operation::LAUNCH: {
+      // TODO(mpark): Once we perform a sanity check validation for
+      // offer operations as specified in MESOS-7760, this should no
+      // longer have to be handled in this function.
+      if (!operation->has_launch()) {
+        return Error(
+            "A LAUNCH offer operation must have"
+            " the Offer.Operation.launch field set.");
+      }
+
+      // Validate resources in LAUNCH.
+      foreach (const TaskInfo& task, operation->launch().task_infos()) {
+        Option<Error> error = Resources::validate(task.resources());
+        if (error.isSome()) {
+          return error;
+        }
 
-  convertResourceFormat(resources, POST_RESERVATION_REFINEMENT);
+        if (task.has_executor()) {
+          Option<Error> error =
+            Resources::validate(task.executor().resources());
 
-  return None();
+          if (error.isSome()) {
+            return error;
+          }
+        }
+      }
+
+      // Normalize resources in LAUNCH.
+      foreach (
+          TaskInfo& task, *operation->mutable_launch()->mutable_task_infos()) {
+        convertResourceFormat(
+            task.mutable_resources(),
+            POST_RESERVATION_REFINEMENT);
+
+        if (task.has_executor()) {
+          convertResourceFormat(
+              task.mutable_executor()->mutable_resources(),
+              POST_RESERVATION_REFINEMENT);
+        }
+      }
+
+      return None();
+    }
+    case Offer::Operation::LAUNCH_GROUP: {
+      // TODO(mpark): Once we perform a sanity check validation for
+      // offer operations as specified in MESOS-7760, this should no
+      // longer have to be handled in this function.
+      if (!operation->has_launch_group()) {
+        return Error(
+            "A LAUNCH_GROUP offer operation must have"
+            " the Offer.Operation.launch_group field set.");
+      }
+
+      Offer::Operation::LaunchGroup* launchGroup =
+        operation->mutable_launch_group();
+
+      // Validate resources in LAUNCH_GROUP.
+      if (launchGroup->has_executor()) {
+        Option<Error> error =
+          Resources::validate(launchGroup->executor().resources());
+
+        if (error.isSome()) {
+          return error;
+        }
+      }
+
+      foreach (const TaskInfo& task, launchGroup->task_group().tasks()) {
+        Option<Error> error = Resources::validate(task.resources());
+        if (error.isSome()) {
+          return error;
+        }
+
+        if (task.has_executor()) {
+          Option<Error> error =
+            Resources::validate(task.executor().resources());
+
+          if (error.isSome()) {
+            return error;
+          }
+        }
+      }
+
+      // Normalize resources in LAUNCH_GROUP.
+      if (launchGroup->has_executor()) {
+        convertResourceFormat(
+            launchGroup->mutable_executor()->mutable_resources(),
+            POST_RESERVATION_REFINEMENT);
+      }
+
+      foreach (
+          TaskInfo& task, *launchGroup->mutable_task_group()->mutable_tasks()) {
+        convertResourceFormat(
+            task.mutable_resources(),
+            POST_RESERVATION_REFINEMENT);
+
+        if (task.has_executor()) {
+          convertResourceFormat(
+              task.mutable_executor()->mutable_resources(),
+              POST_RESERVATION_REFINEMENT);
+        }
+      }
+
+      return None();
+    }
+    case Offer::Operation::UNKNOWN: {
+      // TODO(mpark): Once we perform a sanity check validation for
+      // offer operations as specified in MESOS-7760, this should no
+      // longer have to be handled in this function.
+      return Error("Unknown offer operation");
+    }
+  }
+  UNREACHABLE();
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/63c8b1d6/src/common/resources_utils.hpp
----------------------------------------------------------------------
diff --git a/src/common/resources_utils.hpp b/src/common/resources_utils.hpp
index 7128297..18e3d9d 100644
--- a/src/common/resources_utils.hpp
+++ b/src/common/resources_utils.hpp
@@ -132,12 +132,15 @@ void convertResourceFormat(
     ResourceFormat format);
 
 
-// Convert the given resources to the "post-reservation-refinement" format
-// from any format ("pre-", "post-" or "endpoint") if all of the resources
-// are valid. Returns an `Error` if there are any invalid resources present;
-// in this case, the resources are left unchanged.
-Option<Error> validateAndUpgradeResources(
-    google::protobuf::RepeatedPtrField<Resource>* resources);
+// Convert the resources in the given `Operation` to the
+// "post-reservation-refinement" format from any format
+// ("pre-", "post-" or "endpoint") if all of the resources are valid.
+// Returns an `Error` if there are any invalid resources present;
+// in this case, all resources are left unchanged.
+// NOTE: The validate and upgrade steps are bundled because currently
+// it would be an error to validate but not upgrade or to upgrade
+// without validating.
+Option<Error> validateAndNormalizeResources(Offer::Operation* operation);
 
 
 // Convert the given resources to the "pre-reservation-refinement" format