You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2016/08/02 22:26:57 UTC

mesos git commit: Avoid unnecessary validation during resource subtraction.

Repository: mesos
Updated Branches:
  refs/heads/master 9c7789943 -> e1cb01a15


Avoid unnecessary validation during resource subtraction.

When subtract resources finished, we need to call `validate` to
check if the scalar resource is negative so as to remove this
resource if it is negative. This is a bit heavy as we only need
to check if a scalar has become negative.

This patch is clarifying the logic by checking if the resource
is a negative scalar directly.

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


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

Branch: refs/heads/master
Commit: e1cb01a15e447bdf59cc5ea46870734bebf77033
Parents: 9c77899
Author: Guangya Liu <gy...@gmail.com>
Authored: Tue Aug 2 15:25:22 2016 -0700
Committer: Benjamin Mahler <bm...@apache.org>
Committed: Tue Aug 2 15:26:37 2016 -0700

----------------------------------------------------------------------
 src/common/resources.cpp | 16 ++++++++++++----
 src/v1/resources.cpp     | 16 ++++++++++++----
 2 files changed, 24 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/e1cb01a1/src/common/resources.cpp
----------------------------------------------------------------------
diff --git a/src/common/resources.cpp b/src/common/resources.cpp
index 309b176..7b52c72 100644
--- a/src/common/resources.cpp
+++ b/src/common/resources.cpp
@@ -1619,10 +1619,18 @@ void Resources::subtract(const Resource_& that)
     if (internal::subtractable(resource_.resource, that)) {
       resource_ -= that;
 
-      // Remove the resource if it becomes invalid or zero. We need
-      // to do the validation because we want to strip negative
-      // scalar Resource object.
-      if (resource_.validate().isSome() || resource_.isEmpty()) {
+      // Remove the resource if it has become negative or empty.
+      // Note that a negative resource means the caller is
+      // subtracting more than they should!
+      //
+      // TODO(gyliu513): Provide a stronger interface to avoid
+      // silently allowing this to occur.
+
+      bool negative =
+        resource_.resource.type() == Value::SCALAR &&
+        resource_.resource.scalar().value() < 0;
+
+      if (negative || resource_.isEmpty()) {
         // As `resources` is not ordered, and erasing an element
         // from the middle is expensive, we swap with the last element
         // and then shrink the vector by one.

http://git-wip-us.apache.org/repos/asf/mesos/blob/e1cb01a1/src/v1/resources.cpp
----------------------------------------------------------------------
diff --git a/src/v1/resources.cpp b/src/v1/resources.cpp
index 3f67e32..d27c1a2 100644
--- a/src/v1/resources.cpp
+++ b/src/v1/resources.cpp
@@ -1622,10 +1622,18 @@ void Resources::subtract(const Resource_& that)
     if (internal::subtractable(resource_.resource, that)) {
       resource_ -= that;
 
-      // Remove the resource if it becomes invalid or zero. We need
-      // to do the validation because we want to strip negative
-      // scalar Resource object.
-      if (resource_.validate().isSome() || resource_.isEmpty()) {
+      // Remove the resource if it has become negative or empty.
+      // Note that a negative resource means the caller is
+      // subtracting more than they should!
+      //
+      // TODO(gyliu513): Provide a stronger interface to avoid
+      // silently allowing this to occur.
+
+      bool negative =
+        resource_.resource.type() == Value::SCALAR &&
+        resource_.resource.scalar().value() < 0;
+
+      if (negative || resource_.isEmpty()) {
         // As `resources` is not ordered, and erasing an element
         // from the middle is expensive, we swap with the last element
         // and then shrink the vector by one.