Repository: mesos
Updated Branches:
  refs/heads/master 5f77cbcb2 -> ebb8b590b


Added and implemented 'update()' API call to Sorter.


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

Branch: refs/heads/master
Commit: 5e20c583f57d26b8f2bdb9a78dd1a2f5b44ad84f
Parents: 5f77cbc
Author: Vinod Kone <[email protected]>
Authored: Fri May 22 10:45:46 2015 -0700
Committer: Vinod Kone <[email protected]>
Committed: Fri May 22 17:01:54 2015 -0700

----------------------------------------------------------------------
 src/master/allocator/sorter/drf/sorter.cpp | 16 +++++
 src/master/allocator/sorter/drf/sorter.hpp | 29 +++++----
 src/master/allocator/sorter/sorter.hpp     | 26 +++++---
 src/tests/sorter_tests.cpp                 | 83 ++++++++++++++++++++++++-
 4 files changed, 131 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/5e20c583/src/master/allocator/sorter/drf/sorter.cpp
----------------------------------------------------------------------
diff --git a/src/master/allocator/sorter/drf/sorter.cpp 
b/src/master/allocator/sorter/drf/sorter.cpp
index f53a934..ac05afd 100644
--- a/src/master/allocator/sorter/drf/sorter.cpp
+++ b/src/master/allocator/sorter/drf/sorter.cpp
@@ -186,6 +186,8 @@ void DRFSorter::add(const SlaveID& slaveId, const 
Resources& _resources)
 
 void DRFSorter::remove(const SlaveID& slaveId, const Resources& _resources)
 {
+  CHECK(resources.contains(slaveId));
+
   resources[slaveId] -= _resources;
   if (resources[slaveId].empty()) {
     resources.erase(slaveId);
@@ -195,6 +197,20 @@ void DRFSorter::remove(const SlaveID& slaveId, const 
Resources& _resources)
 }
 
 
+void DRFSorter::update(const SlaveID& slaveId, const Resources& _resources)
+{
+  CHECK(resources.contains(slaveId));
+
+  resources[slaveId] = _resources;
+
+  if (resources[slaveId].empty()) {
+    resources.erase(slaveId);
+  }
+
+  dirty = true;
+}
+
+
 list<string> DRFSorter::sort()
 {
   if (dirty) {

http://git-wip-us.apache.org/repos/asf/mesos/blob/5e20c583/src/master/allocator/sorter/drf/sorter.hpp
----------------------------------------------------------------------
diff --git a/src/master/allocator/sorter/drf/sorter.hpp 
b/src/master/allocator/sorter/drf/sorter.hpp
index fd00c8c..35dc1a4 100644
--- a/src/master/allocator/sorter/drf/sorter.hpp
+++ b/src/master/allocator/sorter/drf/sorter.hpp
@@ -73,18 +73,21 @@ public:
 
   virtual void deactivate(const std::string& name);
 
-  virtual void allocated(const std::string& name,
-                         const SlaveID& slaveId,
-                         const Resources& resources);
-
-  virtual void update(const std::string& name,
-                      const SlaveID& slaveId,
-                      const Resources& oldAllocation,
-                      const Resources& newAllocation);
-
-  virtual void unallocated(const std::string& name,
-                           const SlaveID& slaveId,
-                           const Resources& resources);
+  virtual void allocated(
+      const std::string& name,
+      const SlaveID& slaveId,
+      const Resources& resources);
+
+  virtual void update(
+      const std::string& name,
+      const SlaveID& slaveId,
+      const Resources& oldAllocation,
+      const Resources& newAllocation);
+
+  virtual void unallocated(
+      const std::string& name,
+      const SlaveID& slaveId,
+      const Resources& resources);
 
   virtual hashmap<SlaveID, Resources> allocation(const std::string& name);
 
@@ -92,6 +95,8 @@ public:
 
   virtual void remove(const SlaveID& slaveId, const Resources& resources);
 
+  virtual void update(const SlaveID& slaveId, const Resources& resources);
+
   virtual std::list<std::string> sort();
 
   virtual bool contains(const std::string& name);

http://git-wip-us.apache.org/repos/asf/mesos/blob/5e20c583/src/master/allocator/sorter/sorter.hpp
----------------------------------------------------------------------
diff --git a/src/master/allocator/sorter/sorter.hpp 
b/src/master/allocator/sorter/sorter.hpp
index f06b946..9f7d3cc 100644
--- a/src/master/allocator/sorter/sorter.hpp
+++ b/src/master/allocator/sorter/sorter.hpp
@@ -56,23 +56,26 @@ public:
   virtual void deactivate(const std::string& client) = 0;
 
   // Specify that resources have been allocated to the given client.
-  virtual void allocated(const std::string& client,
-                         const SlaveID& slaveId,
-                         const Resources& resources) = 0;
+  virtual void allocated(
+      const std::string& client,
+      const SlaveID& slaveId,
+      const Resources& resources) = 0;
 
   // Updates a portion of the allocation for the client, in order to
   // augment the resources with additional metadata (e.g., volumes)
   // This means that the new allocation must not affect the static
   // roles, or the overall quantities of resources!
-  virtual void update(const std::string& client,
-                      const SlaveID& slaveId,
-                      const Resources& oldAllocation,
-                      const Resources& newAllocation) = 0;
+  virtual void update(
+      const std::string& client,
+      const SlaveID& slaveId,
+      const Resources& oldAllocation,
+      const Resources& newAllocation) = 0;
 
   // Specify that resources have been unallocated from the given client.
-  virtual void unallocated(const std::string& client,
-                           const SlaveID& slaveId,
-                           const Resources& resources) = 0;
+  virtual void unallocated(
+      const std::string& client,
+      const SlaveID& slaveId,
+      const Resources& resources) = 0;
 
   // Returns the resources that have been allocated to this client.
   virtual hashmap<SlaveID, Resources> allocation(const std::string& client) = 
0;
@@ -84,6 +87,9 @@ public:
   // Remove resources from the total pool.
   virtual void remove(const SlaveID& slaveId, const Resources& resources) = 0;
 
+  // Updates the total pool of resources.
+  virtual void update(const SlaveID& slaveId, const Resources& resources) = 0;
+
   // Returns a list of all clients, in the order that they
   // should be allocated to, according to this Sorter's policy.
   virtual std::list<std::string> sort() = 0;

http://git-wip-us.apache.org/repos/asf/mesos/blob/5e20c583/src/tests/sorter_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/sorter_tests.cpp b/src/tests/sorter_tests.cpp
index 6b0389b..e1a7e6e 100644
--- a/src/tests/sorter_tests.cpp
+++ b/src/tests/sorter_tests.cpp
@@ -214,7 +214,7 @@ TEST(SorterTest, SplitResourceShares)
 }
 
 
-TEST(SorterTest, Update)
+TEST(SorterTest, UpdateAllocation)
 {
   DRFSorter sorter;
 
@@ -326,6 +326,87 @@ TEST(SorterTest, MultipleSlaveUpdates)
   EXPECT_EQ(newAllocation.get(), allocation[slaveB]);
 }
 
+
+// This test verifies that when the total pool of resources is updated
+// the sorting order of clients reflects the new total.
+TEST(SorterTest, UpdateTotal)
+{
+  DRFSorter sorter;
+
+  SlaveID slaveId;
+  slaveId.set_value("slaveId");
+
+  sorter.add("a");
+  sorter.add("b");
+
+  sorter.add(slaveId, Resources::parse("cpus:10;mem:100").get());
+
+  // Dominant share of "a" is 0.2 (cpus).
+  sorter.allocated(
+      "a", slaveId, Resources::parse("cpus:2;mem:1").get());
+
+  // Dominant share of "b" is 0.1 (cpus).
+  sorter.allocated(
+      "b", slaveId, Resources::parse("cpus:1;mem:2").get());
+
+  list<string> sorted = sorter.sort();
+  ASSERT_EQ(2, sorted.size());
+  EXPECT_EQ("b", sorted.front());
+  EXPECT_EQ("a", sorted.back());
+
+  // Update the total resources.
+  sorter.update(slaveId, Resources::parse("cpus:100;mem:10").get());
+
+  // Now the dominant share of "a" is 0.1 (mem) and "b" is 0.2 (mem),
+  // which should change the sort order.
+  sorted = sorter.sort();
+  ASSERT_EQ(2, sorted.size());
+  EXPECT_EQ("a", sorted.front());
+  EXPECT_EQ("b", sorted.back());
+}
+
+
+// This test verifies that revocable resources are properly accounted
+// for in the DRF sorter.
+TEST(SorterTest, RevocableResources)
+{
+  DRFSorter sorter;
+
+  SlaveID slaveId;
+  slaveId.set_value("slaveId");
+
+  sorter.add("a");
+  sorter.add("b");
+
+  // Create a total resource pool of 10 revocable cpus and 10 cpus and
+  // 10 MB mem.
+  Resource revocable = Resources::parse("cpus", "10", "*").get();
+  revocable.mutable_revocable();
+  Resources total = Resources::parse("cpus:10;mem:100").get() + revocable;
+
+  sorter.add(slaveId, revocable);
+
+  // Dominant share of "a" is 0.1 (cpus).
+  Resources a = Resources::parse("cpus:2;mem:1").get();
+  sorter.allocated("a", slaveId, a);
+
+  // Dominant share of "b" is 0.5 (cpus).
+  revocable = Resources::parse("cpus", "9", "*").get();
+  revocable.mutable_revocable();
+  Resources b = Resources::parse("cpus:1;mem:1").get() + revocable;
+  sorter.allocated("b", slaveId, b);
+
+  // Check that the allocations are correct.
+  ASSERT_EQ(a, sorter.allocation("a")[slaveId]);
+  ASSERT_EQ(b, sorter.allocation("b")[slaveId]);
+
+  // Check that the sort is correct.
+  list<string> sorted = sorter.sort();
+  ASSERT_EQ(2, sorted.size());
+  EXPECT_EQ("a", sorted.front());
+  EXPECT_EQ("b", sorted.back());
+}
+
 } // namespace tests {
 } // namespace internal {
 } // namespace mesos {

Reply via email to