Add a member variable for the agent's total resources.

`totalResources` is updated by CheckpointResourcesMessages and doesn't
have to be re-calculated in Slave::usage(). This change allows us to
expose `totalResources` for other purposes.

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


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

Branch: refs/heads/master
Commit: 1a0a6052765db6846c7f8c293a1be1081a57065a
Parents: b24d99b
Author: Jiang Yan Xu <xuj...@apple.com>
Authored: Fri Sep 9 01:31:49 2016 -0700
Committer: Jiang Yan Xu <xuj...@apple.com>
Committed: Mon Sep 19 14:59:11 2016 -0700

----------------------------------------------------------------------
 src/slave/slave.cpp | 25 ++++++++++---------------
 src/slave/slave.hpp |  4 ++++
 2 files changed, 14 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/1a0a6052/src/slave/slave.cpp
----------------------------------------------------------------------
diff --git a/src/slave/slave.cpp b/src/slave/slave.cpp
index 4ced3e7..5ef5594 100644
--- a/src/slave/slave.cpp
+++ b/src/slave/slave.cpp
@@ -2740,15 +2740,17 @@ void Slave::checkpointResources(const vector<Resource>& 
_checkpointedResources)
   // resources are compatible with the agent resources specified
   // through the '--resources' command line flag. The resources
   // should be guaranteed compatible by the master.
-  Try<Resources> totalResources = applyCheckpointedResources(
+  Try<Resources> _totalResources = applyCheckpointedResources(
       info.resources(),
       newCheckpointedResources);
 
-  CHECK_SOME(totalResources)
+  CHECK_SOME(_totalResources)
     << "Failed to apply checkpointed resources "
     << newCheckpointedResources << " to agent's resources "
     << info.resources();
 
+  totalResources = _totalResources.get();
+
   // Store the target checkpoint resources. We commit the checkpoint
   // only after all operations are successful. If any of the operations
   // fail, the agent exits and the update to checkpointed resources
@@ -5069,17 +5071,19 @@ Future<Nothing> Slave::recover(const 
Result<state::State>& state)
     // verified by the old agent but the flag may have changed during
     // agent restart in an incompatible way and the operator may need
     // to either fix the flag or the checkpointed resources.
-    Try<Resources> totalResources = applyCheckpointedResources(
+    Try<Resources> _totalResources = applyCheckpointedResources(
         info.resources(), checkpointedResources);
 
-    if (totalResources.isError()) {
+    if (_totalResources.isError()) {
       return Failure(
           "Checkpointed resources " +
           stringify(checkpointedResources) +
           " are incompatible with agent resources " +
           stringify(info.resources()) + ": " +
-          totalResources.error());
+          _totalResources.error());
     }
+
+    totalResources = _totalResources.get();
   }
 
   if (slaveState.isSome() && slaveState.get().info.isSome()) {
@@ -5629,16 +5633,7 @@ Future<ResourceUsage> Slave::usage()
     }
   }
 
-  Try<Resources> totalResources = applyCheckpointedResources(
-      info.resources(),
-      checkpointedResources);
-
-  CHECK_SOME(totalResources)
-    << "Failed to apply checkpointed resources "
-    << checkpointedResources << " to agent's resources "
-    << info.resources();
-
-  usage->mutable_total()->CopyFrom(totalResources.get());
+  usage->mutable_total()->CopyFrom(totalResources);
 
   return await(futures).then(
       [usage](const list<Future<ResourceStatistics>>& futures) {

http://git-wip-us.apache.org/repos/asf/mesos/blob/1a0a6052/src/slave/slave.hpp
----------------------------------------------------------------------
diff --git a/src/slave/slave.hpp b/src/slave/slave.hpp
index 13c76d1..02d2eca 100644
--- a/src/slave/slave.hpp
+++ b/src/slave/slave.hpp
@@ -668,6 +668,10 @@ private:
   // Resources that are checkpointed by the slave.
   Resources checkpointedResources;
 
+  // The current total resources of the agent, i.e.,
+  // `info.resources()` with checkpointed resources applied.
+  Resources totalResources;
+
   Option<process::UPID> master;
 
   hashmap<FrameworkID, Framework*> frameworks;

Reply via email to