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 2014/12/11 23:55:39 UTC

[02/11] mesos git commit: Moved ReservationAllocatorTest.ResourcesReturned to a unit test.

Moved ReservationAllocatorTest.ResourcesReturned to a unit test.

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


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

Branch: refs/heads/master
Commit: cc70b0eb2bff93fa03e163aaa2362bf276122eb5
Parents: a56c579
Author: Benjamin Mahler <be...@gmail.com>
Authored: Sun Dec 7 17:29:35 2014 -0800
Committer: Benjamin Mahler <be...@gmail.com>
Committed: Thu Dec 11 14:40:30 2014 -0800

----------------------------------------------------------------------
 src/tests/hierarchical_allocator_tests.cpp |  69 ++++++++++
 src/tests/master_allocator_tests.cpp       | 165 ------------------------
 2 files changed, 69 insertions(+), 165 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/cc70b0eb/src/tests/hierarchical_allocator_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/hierarchical_allocator_tests.cpp b/src/tests/hierarchical_allocator_tests.cpp
index f20be8c..4156769 100644
--- a/src/tests/hierarchical_allocator_tests.cpp
+++ b/src/tests/hierarchical_allocator_tests.cpp
@@ -155,6 +155,12 @@ Resources sum(const Iterable& iterable)
   return total;
 }
 
+// TODO(bmahler): These tests were transformed directly from
+// integration tests into unit tests. However, these tests
+// should be simplified even further to each test a single
+// expected behavor, at which point we can have more tests
+// that are each very small.
+
 
 // Checks that the DRF allocator implements the DRF algorithm
 // correctly. The test accomplishes this by adding frameworks and
@@ -451,3 +457,66 @@ TEST_F(HierarchicalAllocatorTest, Reservations)
   EXPECT_EQ(Resources(slave2.resources()).extract("role2"),
             sum(allocation.get().resources.values()));
 }
+
+
+// Checks that recovered resources are re-allocated correctly.
+TEST_F(HierarchicalAllocatorTest, RecoverResources)
+{
+  Clock::pause();
+
+  initialize({"role1"});
+
+  hashmap<FrameworkID, Resources> EMPTY;
+
+  SlaveInfo slave = createSlaveInfo(
+      "cpus(role1):1;mem(role1):200;"
+      "cpus:1;mem:200;disk:0");
+  allocator->addSlave(slave.id(), slave, slave.resources(), EMPTY);
+
+  // Initially, all the resources are allocated.
+  FrameworkInfo framework1 = createFrameworkInfo("role1");
+  allocator->addFramework(framework1.id(), framework1, Resources());
+
+  Future<Allocation> allocation = queue.get();
+  AWAIT_READY(allocation);
+  EXPECT_EQ(framework1.id(), allocation.get().frameworkId);
+  EXPECT_EQ(1u, allocation.get().resources.size());
+  EXPECT_TRUE(allocation.get().resources.contains(slave.id()));
+  EXPECT_EQ(slave.resources(), sum(allocation.get().resources.values()));
+
+  // Recover the reserved resources, expect them to be re-offered.
+  Resources reserved = Resources(slave.resources()).extract("role1");
+
+  allocator->recoverResources(
+      allocation.get().frameworkId,
+      slave.id(),
+      reserved,
+      None());
+
+  Clock::advance(flags.allocation_interval);
+
+  allocation = queue.get();
+  AWAIT_READY(allocation);
+  EXPECT_EQ(framework1.id(), allocation.get().frameworkId);
+  EXPECT_EQ(1u, allocation.get().resources.size());
+  EXPECT_TRUE(allocation.get().resources.contains(slave.id()));
+  EXPECT_EQ(reserved, sum(allocation.get().resources.values()));
+
+  // Recover the unreserved resources, expect them to be re-offered.
+  Resources unreserved = Resources(slave.resources()).extract("*");
+
+  allocator->recoverResources(
+      allocation.get().frameworkId,
+      slave.id(),
+      unreserved,
+      None());
+
+  Clock::advance(flags.allocation_interval);
+
+  allocation = queue.get();
+  AWAIT_READY(allocation);
+  EXPECT_EQ(framework1.id(), allocation.get().frameworkId);
+  EXPECT_EQ(1u, allocation.get().resources.size());
+  EXPECT_TRUE(allocation.get().resources.contains(slave.id()));
+  EXPECT_EQ(unreserved, sum(allocation.get().resources.values()));
+}

http://git-wip-us.apache.org/repos/asf/mesos/blob/cc70b0eb/src/tests/master_allocator_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/master_allocator_tests.cpp b/src/tests/master_allocator_tests.cpp
index 61a5f5c..ff99c72 100644
--- a/src/tests/master_allocator_tests.cpp
+++ b/src/tests/master_allocator_tests.cpp
@@ -68,171 +68,6 @@ using testing::Eq;
 using testing::SaveArg;
 
 
-class ReservationAllocatorTest : public MesosTest {};
-
-
-// Checks that statically allocated resources that are returned
-// either unused or after a task finishes are statically reallocated
-// appropriately.
-TEST_F(ReservationAllocatorTest, ResourcesReturned)
-{
-  MockAllocatorProcess<HierarchicalDRFAllocatorProcess> allocator;
-
-  EXPECT_CALL(allocator, initialize(_, _, _));
-
-  master::Flags masterFlags = CreateMasterFlags();
-  masterFlags.roles = Some("role1,role2");
-  masterFlags.allocation_interval = Milliseconds(50);
-  Try<PID<Master> > master = StartMaster(&allocator, masterFlags);
-
-  ASSERT_SOME(master);
-
-  MockExecutor exec(DEFAULT_EXECUTOR_ID);
-
-  EXPECT_CALL(allocator, addSlave(_, _, _, _))
-    .Times(2);
-
-  Future<Nothing> addSlave1 = FUTURE_DISPATCH(
-      allocator.real, &AllocatorProcess::addSlave);
-
-  slave::Flags flags1 = CreateSlaveFlags();
-  flags1.resources = Some("cpus(role1):1;mem(role1):200;cpus(role2):2;"
-                          "mem(role2):600;cpus:1;mem:200;disk:0");
-  Try<PID<Slave> > slave1 = StartSlave(&exec, flags1);
-  ASSERT_SOME(slave1);
-
-  // Wait until allocator has added slave1.
-  AWAIT_READY(addSlave1);
-
-  Future<Nothing> addSlave2 = FUTURE_DISPATCH(
-      allocator.real, &AllocatorProcess::addSlave);
-
-  // This slave's resources will never be offered to anyone,
-  // because there is no framework with role3.
-  slave::Flags flags2 = CreateSlaveFlags();
-  flags2.resources = Some("cpus(role3):4;mem(role3):1024;disk:0");
-  Try<PID<Slave> > slave2 = StartSlave(flags2);
-  ASSERT_SOME(slave2);
-
-  // Wait until allocator has added slave2.
-  AWAIT_READY(addSlave2);
-
-  FrameworkInfo frameworkInfo1; // Bug in gcc 4.1.*, must assign on next line.
-  frameworkInfo1 = DEFAULT_FRAMEWORK_INFO;
-  frameworkInfo1.set_user("user1");
-  frameworkInfo1.set_name("framework1");
-  frameworkInfo1.set_role("role1");
-  FrameworkID frameworkId1;
-
-  MockScheduler sched1;
-  MesosSchedulerDriver driver1(
-      &sched1, frameworkInfo1, master.get(), DEFAULT_CREDENTIAL);
-
-  EXPECT_CALL(allocator, addFramework(_, _, _));
-
-  EXPECT_CALL(sched1, registered(_, _, _));
-
-  // Initially, framework1 should be offered all of the resources on
-  // slave1 that aren't reserved to role2.
-  EXPECT_CALL(sched1, resourceOffers(_, OfferEq(2, 400)))
-    .WillOnce(LaunchTasks(DEFAULT_EXECUTOR_INFO, 1, 1, 100, "role1"));
-
-  EXPECT_CALL(allocator, recoverResources(_, _, _, _))
-    .WillOnce(InvokeResourcesRecoveredWithFilters(&allocator, 0));
-
-  EXPECT_CALL(exec, registered(_, _, _, _));
-
-  ExecutorDriver* execDriver;
-  TaskInfo taskInfo;
-  Future<Nothing> launchTask;
-  EXPECT_CALL(exec, launchTask(_, _))
-    .WillOnce(DoAll(SaveArg<0>(&execDriver),
-                    SaveArg<1>(&taskInfo),
-                    SendStatusUpdateFromTask(TASK_RUNNING),
-                    FutureSatisfy(&launchTask)));
-
-  EXPECT_CALL(sched1, statusUpdate(_, _))
-    .WillRepeatedly(DoDefault());
-
-  // After framework1's task launches, it should be offered all resources
-  // not dedicatd to role2 and not used by its task.
-  Future<Nothing> resourceOffers1;
-  EXPECT_CALL(sched1, resourceOffers(_, OfferEq(1, 300)))
-    .WillOnce(FutureSatisfy(&resourceOffers1));
-
-  driver1.start();
-
-  AWAIT_READY(launchTask);
-
-  AWAIT_READY(resourceOffers1);
-
-  FrameworkInfo frameworkInfo2; // Bug in gcc 4.1.*, must assign on next line.
-  frameworkInfo2 = DEFAULT_FRAMEWORK_INFO;
-  frameworkInfo2.set_user("user2");
-  frameworkInfo2.set_name("framework2");
-  frameworkInfo2.set_role("role2");
-  FrameworkID frameworkId2;
-
-  MockScheduler sched2;
-  MesosSchedulerDriver driver2(
-      &sched2, frameworkInfo2, master.get(), DEFAULT_CREDENTIAL);
-
-  EXPECT_CALL(allocator, addFramework(_, _, _));
-
-  EXPECT_CALL(sched2, registered(_, _, _));
-
-  // The first time framework2 is allocated to, it should be offered
-  // all of the resources on slave1 that are reserved to role2.
-  Future<Nothing> resourceOffers2;
-  EXPECT_CALL(sched2, resourceOffers(_, OfferEq(2, 600)))
-    .WillOnce(FutureSatisfy(&resourceOffers2));
-
-  driver2.start();
-
-  AWAIT_READY(resourceOffers2);
-
-  TaskStatus status;
-  status.mutable_task_id()->MergeFrom(taskInfo.task_id());
-  status.set_state(TASK_FINISHED);
-
-  EXPECT_CALL(allocator, recoverResources(_, _, _, _));
-
-  // After the task finishes, its resources should be reoffered to
-  // framework1.
-  Future<Nothing> resourceOffers3;
-  EXPECT_CALL(sched1, resourceOffers(_, OfferEq(1, 100)))
-    .WillOnce(FutureSatisfy(&resourceOffers3));
-
-  execDriver->sendStatusUpdate(status);
-
-  AWAIT_READY(resourceOffers3);
-
-  // Shut everything down.
-  EXPECT_CALL(allocator, recoverResources(_, _, _, _))
-    .WillRepeatedly(DoDefault());
-
-  EXPECT_CALL(allocator, deactivateFramework(_))
-    .Times(AtMost(2));
-
-  EXPECT_CALL(allocator, removeFramework(_))
-    .Times(AtMost(2));
-
-  Future<Nothing> shutdown;
-  EXPECT_CALL(exec, shutdown(_))
-    .WillOnce(FutureSatisfy(&shutdown));
-
-  driver2.stop();
-  driver1.stop();
-
-  AWAIT_READY(shutdown); // Ensures MockExecutor can be deallocated.
-
-  EXPECT_CALL(allocator, removeSlave(_))
-    .Times(AtMost(2));
-
-  this->Shutdown();
-}
-
-
 template <typename T>
 class MasterAllocatorTest : public MesosTest
 {