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

[08/25] mesos git commit: Shutdown tasks of completed frameworks on agent re-registration.

Shutdown tasks of completed frameworks on agent re-registration.

Previously, if a framework completed (e.g., due to a teardown operation
or framework shutdown), any framework tasks running on partitioned
agents would not be shutdown when the agent re-registered. For tasks
that are not partition-aware, the task would be shutdown on agent
re-registration anyway. But for partition-aware tasks, this could lead
to orphan tasks.

Fix this by changing the master to shutdown such tasks when the agent
reregisters.

Note that if the master fails over between the time the framework
completes and a partitioned agent re-registers, any framework tasks
running on the agent will NOT be shutdown. This is a known bug; fixing
it requires persisting the framework shutdown operation to the registry
(MESOS-1719).

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


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

Branch: refs/heads/master
Commit: 482796bb1fb3f65269b0fd40143eccb8ced2027a
Parents: 6fd373b
Author: Neil Conway <ne...@gmail.com>
Authored: Mon Jan 23 17:03:53 2017 -0800
Committer: Vinod Kone <vi...@gmail.com>
Committed: Mon Jan 23 17:06:33 2017 -0800

----------------------------------------------------------------------
 src/master/master.cpp         | 109 ++++++++++++++++-----
 src/tests/partition_tests.cpp | 196 ++++++++++++++++++++++++++-----------
 2 files changed, 225 insertions(+), 80 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/482796bb/src/master/master.cpp
----------------------------------------------------------------------
diff --git a/src/master/master.cpp b/src/master/master.cpp
index 062b210..c28ca25 100644
--- a/src/master/master.cpp
+++ b/src/master/master.cpp
@@ -5562,6 +5562,10 @@ void Master::_reregisterSlave(
   // such tasks "completed" when the agent was marked unreachable, so
   // no further cleanup for non-partition-aware tasks is required.
   //
+  // In addition, we also filter any tasks whose frameworks have
+  // completed. As in case (a), such frameworks will be shutdown and
+  // their tasks have already been marked "completed".
+  //
   // (b) If the master has failed over, all tasks are re-added to the
   // master. The master shouldn't have any record of the tasks running
   // on the agent, so no further cleanup is required.
@@ -5570,6 +5574,12 @@ void Master::_reregisterSlave(
     const FrameworkID& frameworkId = task.framework_id();
     Framework* framework = getFramework(frameworkId);
 
+    // Don't re-add tasks whose framework has been shutdown at the
+    // master. Such frameworks will be shutdown on the agent below.
+    if (isCompletedFramework(task.framework_id())) {
+      continue;
+    }
+
     // Always re-add partition-aware tasks.
     if (partitionAwareFrameworks.contains(frameworkId)) {
       tasks_.push_back(task);
@@ -5617,28 +5627,42 @@ void Master::_reregisterSlave(
   LOG(INFO) << "Re-registered agent " << *slave
             << " with " << slave->info.resources();
 
-  // Shutdown any frameworks running on the slave that don't have the
-  // PARTITION_AWARE capability, provided that this instance of the
-  // master previously added the slave to the `slaves.removed`
-  // collection. This matches the Mesos 1.0 "non-strict" semantics for
-  // frameworks that are not partition-aware: when a previously
-  // unreachable slave reregisters, its tasks are only shutdown if the
-  // master has not failed over.
-  if (slaveWasRemoved) {
-    foreach (const FrameworkInfo& framework, frameworks) {
-      if (!partitionAwareFrameworks.contains(framework.id())) {
-        LOG(INFO) << "Shutting down framework " << framework.id()
-                  << " at reregistered agent " << *slave
-                  << " because the framework is not partition-aware";
-
-        ShutdownFrameworkMessage message;
-        message.mutable_framework_id()->MergeFrom(framework.id());
-        send(slave->pid, message);
+  // Determine which frameworks on the slave to shutdown, if any. This
+  // happens in two cases:
+  //
+  // (1) If this master marked the slave unreachable (i.e., master has
+  // not failed over), we shutdown any non-partition-aware frameworks
+  // running on the slave. This matches the Mesos <= 1.0 "non-strict"
+  // registry semantics.
+  //
+  // (2) Any framework that is completed at the master but still
+  // running at the slave is shutdown. This can occur if the framework
+  // was removed when the slave was partitioned. NOTE: This is just a
+  // short-term hack because information about completed frameworks is
+  // lost when the master fails over. Also, we only store a limited
+  // number of completed frameworks. A proper fix likely involves
+  // storing framework information in the registry (MESOS-1719).
+  foreach (const FrameworkInfo& framework, frameworks) {
+    if (slaveWasRemoved && !partitionAwareFrameworks.contains(framework.id())) {
+      LOG(INFO) << "Shutting down framework " << framework.id()
+                << " at re-registered agent " << *slave
+                << " because the framework is not partition-aware";
 
-        // The framework's tasks should not be stored in the master's
-        // in-memory state, because they were not re-added above.
-        CHECK(!slave->tasks.contains(framework.id()));
-      }
+      ShutdownFrameworkMessage message;
+      message.mutable_framework_id()->MergeFrom(framework.id());
+      send(slave->pid, message);
+
+      // The framework's tasks should not be stored in the master's
+      // in-memory state, because they were not re-added filtered above.
+      CHECK(!slave->tasks.contains(framework.id()));
+    } else if (isCompletedFramework(framework.id())) {
+      LOG(INFO) << "Shutting down framework " << framework.id()
+                << " at re-registered agent " << *slave
+                << " because the framework has been shutdown at the master";
+
+      ShutdownFrameworkMessage message;
+      message.mutable_framework_id()->MergeFrom(framework.id());
+      send(slave->pid, message);
     }
   }
 
@@ -7479,7 +7503,8 @@ void Master::removeFramework(Framework* framework)
   // Remove the pending tasks from the framework.
   framework->pendingTasks.clear();
 
-  // Remove pointers to the framework's tasks in slaves.
+  // Remove pointers to the framework's tasks in slaves and mark those
+  // tasks as completed.
   foreachvalue (Task* task, utils::copy(framework->tasks)) {
     Slave* slave = slaves.registered.get(task->slave_id());
 
@@ -7497,7 +7522,15 @@ void Master::removeFramework(Framework* framework)
     // collect the possible finished status. We tolerate this,
     // because we expect that if the framework has been asked to shut
     // down, its user is not interested in results anymore.
+    //
     // TODO(alex): Consider a more descriptive state, e.g. TASK_ABANDONED.
+    //
+    // TODO(neilc): Marking the task KILLED before it has actually
+    // terminated is misleading. Instead, we should consider leaving
+    // the task in its current state at the master; if/when the agent
+    // shuts down the framework, we should arrange for a terminal
+    // status update to be delivered to the master and update the
+    // state of the task at that time (MESOS-6608).
     const StatusUpdate& update = protobuf::createStatusUpdate(
         task->framework_id(),
         task->slave_id(),
@@ -7515,6 +7548,38 @@ void Master::removeFramework(Framework* framework)
     removeTask(task);
   }
 
+  // Mark the framework's unreachable tasks as completed.
+  foreach (const TaskID& taskId, framework->unreachableTasks.keys()) {
+    const Owned<Task>& task = framework->unreachableTasks.at(taskId);
+
+    // TODO(neilc): Per comment above, using TASK_KILLED here is not
+    // ideal. It would be better to use TASK_UNREACHABLE here and only
+    // transition it to a terminal state when the agent re-registers
+    // and the task is shutdown (MESOS-6608).
+    const StatusUpdate& update = protobuf::createStatusUpdate(
+        task->framework_id(),
+        task->slave_id(),
+        task->task_id(),
+        TASK_KILLED,
+        TaskStatus::SOURCE_MASTER,
+        None(),
+        "Framework " + framework->id().value() + " removed",
+        TaskStatus::REASON_FRAMEWORK_REMOVED,
+        (task->has_executor_id()
+         ? Option<ExecutorID>(task->executor_id())
+         : None()));
+
+    updateTask(task.get(), update);
+
+    // We don't need to remove the task from the slave, because the
+    // task was removed when the agent was marked unreachable.
+    CHECK(!slaves.registered.contains(task->slave_id()));
+
+    // Move task from unreachable map to completed map.
+    framework->addCompletedTask(*task.get());
+    framework->unreachableTasks.erase(taskId);
+  }
+
   // Remove the framework's offers (if they weren't removed before).
   foreach (Offer* offer, utils::copy(framework->offers)) {
     allocator->recoverResources(

http://git-wip-us.apache.org/repos/asf/mesos/blob/482796bb/src/tests/partition_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/partition_tests.cpp b/src/tests/partition_tests.cpp
index a6919c5..47f6dc9 100644
--- a/src/tests/partition_tests.cpp
+++ b/src/tests/partition_tests.cpp
@@ -973,9 +973,8 @@ TEST_F_TEMP_DISABLED_ON_WINDOWS(
 
 // This test causes a slave to be partitioned while it is running a
 // task for a PARTITION_AWARE framework. The scheduler is shutdown
-// before the partition heals. Right now, the task is left running as
-// an orphan; when MESOS-6602 is fixed, the task will be shutdown when
-// the agent re-registers.
+// before the partition heals; the task should be shutdown after the
+// agent re-registers.
 TEST_F_TEMP_DISABLED_ON_WINDOWS(PartitionTest, PartitionedSlaveOrphanedTask)
 {
   Clock::pause();
@@ -1039,10 +1038,10 @@ TEST_F_TEMP_DISABLED_ON_WINDOWS(PartitionTest, PartitionedSlaveOrphanedTask)
   driver.launchTasks(offer.id(), {task});
 
   AWAIT_READY(runningStatus);
-  EXPECT_EQ(TASK_RUNNING, runningStatus.get().state());
-  EXPECT_EQ(task.task_id(), runningStatus.get().task_id());
+  EXPECT_EQ(TASK_RUNNING, runningStatus->state());
+  EXPECT_EQ(task.task_id(), runningStatus->task_id());
 
-  const SlaveID slaveId = runningStatus.get().slave_id();
+  const SlaveID slaveId = runningStatus->slave_id();
 
   AWAIT_READY(statusUpdateAck);
 
@@ -1070,20 +1069,12 @@ TEST_F_TEMP_DISABLED_ON_WINDOWS(PartitionTest, PartitionedSlaveOrphanedTask)
   Clock::advance(masterFlags.agent_ping_timeout);
   Clock::settle();
 
-  // Record the time at which we expect the master to have marked the
-  // agent as unhealthy. We then advance the clock -- this shouldn't
-  // do anything, but it ensures that the `unreachable_time` we check
-  // below is computed at the right time.
-  TimeInfo partitionTime = protobuf::getCurrentTime();
-
-  Clock::advance(Milliseconds(100));
-
   AWAIT_READY(unreachableStatus);
-  EXPECT_EQ(TASK_UNREACHABLE, unreachableStatus.get().state());
-  EXPECT_EQ(TaskStatus::REASON_SLAVE_REMOVED, unreachableStatus.get().reason());
-  EXPECT_EQ(task.task_id(), unreachableStatus.get().task_id());
-  EXPECT_EQ(slaveId, unreachableStatus.get().slave_id());
-  EXPECT_EQ(partitionTime, unreachableStatus.get().unreachable_time());
+  EXPECT_EQ(TASK_UNREACHABLE, unreachableStatus->state());
+  EXPECT_EQ(TaskStatus::REASON_SLAVE_REMOVED, unreachableStatus->reason());
+  EXPECT_EQ(task.task_id(), unreachableStatus->task_id());
+  EXPECT_EQ(slaveId, unreachableStatus->slave_id());
+  EXPECT_TRUE(unreachableStatus->has_unreachable_time());
 
   AWAIT_READY(slaveLost);
 
@@ -1092,69 +1083,158 @@ TEST_F_TEMP_DISABLED_ON_WINDOWS(PartitionTest, PartitionedSlaveOrphanedTask)
   driver.stop();
   driver.join();
 
-  // Cause the slave to re-register with the master.
+  // Before the agent re-registers, check how `task` is displayed by
+  // the master's "/state" endpoint.
+  {
+    Future<Response> response = process::http::get(
+        master.get()->pid,
+        "state",
+        None(),
+        createBasicAuthHeaders(DEFAULT_CREDENTIAL));
+
+    AWAIT_EXPECT_RESPONSE_STATUS_EQ(OK().status, response);
+    AWAIT_EXPECT_RESPONSE_HEADER_EQ(APPLICATION_JSON, "Content-Type", response);
+
+    Try<JSON::Object> parse = JSON::parse<JSON::Object>(response->body);
+    ASSERT_SOME(parse);
+
+    JSON::Array unregisteredFrameworks =
+      parse->values["unregistered_frameworks"].as<JSON::Array>();
+
+    EXPECT_TRUE(unregisteredFrameworks.values.empty());
+
+    EXPECT_TRUE(parse->values["frameworks"].as<JSON::Array>().values.empty());
+    EXPECT_TRUE(parse->values["orphan_tasks"].as<JSON::Array>().values.empty());
+
+    JSON::Array completedFrameworks =
+      parse->values["completed_frameworks"].as<JSON::Array>();
+
+    ASSERT_EQ(1u, completedFrameworks.values.size());
+
+    JSON::Object framework =
+      completedFrameworks.values.front().as<JSON::Object>();
+
+    EXPECT_EQ(
+        frameworkId.get(),
+        framework.values["id"].as<JSON::String>().value);
+
+    EXPECT_TRUE(framework.values["tasks"].as<JSON::Array>().values.empty());
+    EXPECT_TRUE(
+        framework.values["unreachable_tasks"].as<JSON::Array>().values.empty());
+
+    JSON::Array completedTasks =
+      framework.values["completed_tasks"].as<JSON::Array>();
+
+    ASSERT_EQ(1u, completedTasks.values.size());
+
+    JSON::Object completedTask =
+      completedTasks.values.front().as<JSON::Object>();
+
+    EXPECT_EQ(
+        task.task_id(),
+        completedTask.values["id"].as<JSON::String>().value);
+    EXPECT_EQ(
+        "TASK_KILLED",
+        completedTask.values["state"].as<JSON::String>().value);
+  }
+
+  // Cause the slave to re-register with the master; the master should
+  // send a `ShutdownFrameworkMessage` to the slave.
   Future<SlaveReregisteredMessage> slaveReregistered = FUTURE_PROTOBUF(
       SlaveReregisteredMessage(), master.get()->pid, slave.get()->pid);
 
+  Future<ShutdownFrameworkMessage> shutdownFramework = FUTURE_PROTOBUF(
+      ShutdownFrameworkMessage(), master.get()->pid, slave.get()->pid);
+
   detector.appoint(master.get()->pid);
 
   Clock::advance(agentFlags.registration_backoff_factor);
   AWAIT_READY(slaveReregistered);
+  AWAIT_READY(shutdownFramework);
 
   Clock::resume();
 
-  // Check if `task` is still running by querying master's state endpoint.
-  Future<Response> response = process::http::get(
-      master.get()->pid,
-      "state",
-      None(),
-      createBasicAuthHeaders(DEFAULT_CREDENTIAL));
+  // After the agent re-registers, check how `task` is displayed by
+  // the master's "/state" endpoint.
+  {
+    Future<Response> response = process::http::get(
+        master.get()->pid,
+        "state",
+        None(),
+        createBasicAuthHeaders(DEFAULT_CREDENTIAL));
 
-  AWAIT_EXPECT_RESPONSE_STATUS_EQ(OK().status, response);
-  AWAIT_EXPECT_RESPONSE_HEADER_EQ(APPLICATION_JSON, "Content-Type", response);
+    AWAIT_EXPECT_RESPONSE_STATUS_EQ(OK().status, response);
+    AWAIT_EXPECT_RESPONSE_HEADER_EQ(APPLICATION_JSON, "Content-Type", response);
 
-  Try<JSON::Object> parse = JSON::parse<JSON::Object>(response->body);
-  ASSERT_SOME(parse);
+    Try<JSON::Object> parse = JSON::parse<JSON::Object>(response->body);
+    ASSERT_SOME(parse);
 
-  JSON::Array unregisteredFrameworks =
-    parse->values["unregistered_frameworks"].as<JSON::Array>();
+    JSON::Array unregisteredFrameworks =
+      parse->values["unregistered_frameworks"].as<JSON::Array>();
 
-  // TODO(neilc): When MESOS-6602 is fixed, this should be empty.
-  EXPECT_EQ(1u, unregisteredFrameworks.values.size());
+    EXPECT_TRUE(unregisteredFrameworks.values.empty());
 
-  EXPECT_TRUE(parse->values["frameworks"].as<JSON::Array>().values.empty());
-  EXPECT_TRUE(parse->values["orphan_tasks"].as<JSON::Array>().values.empty());
+    EXPECT_TRUE(parse->values["frameworks"].as<JSON::Array>().values.empty());
+    EXPECT_TRUE(parse->values["orphan_tasks"].as<JSON::Array>().values.empty());
 
-  JSON::Array completedFrameworks =
-    parse->values["completed_frameworks"].as<JSON::Array>();
+    JSON::Array completedFrameworks =
+      parse->values["completed_frameworks"].as<JSON::Array>();
 
-  ASSERT_EQ(1u, completedFrameworks.values.size());
+    ASSERT_EQ(1u, completedFrameworks.values.size());
 
-  JSON::Object jsonFramework =
-    completedFrameworks.values.front().as<JSON::Object>();
+    JSON::Object framework =
+      completedFrameworks.values.front().as<JSON::Object>();
 
-  EXPECT_EQ(
-      frameworkId.get(),
-      jsonFramework.values["id"].as<JSON::String>().value);
+    EXPECT_EQ(
+        frameworkId.get(),
+        framework.values["id"].as<JSON::String>().value);
 
-  EXPECT_TRUE(jsonFramework.values["tasks"].as<JSON::Array>().values.empty());
-  EXPECT_TRUE(
-      jsonFramework.values["completed_tasks"].as<JSON::Array>().values.empty());
+    EXPECT_TRUE(framework.values["tasks"].as<JSON::Array>().values.empty());
+    EXPECT_TRUE(
+        framework.values["unreachable_tasks"].as<JSON::Array>().values.empty());
 
-  JSON::Array unreachableTasks =
-    jsonFramework.values["unreachable_tasks"].as<JSON::Array>();
+    JSON::Array completedTasks =
+      framework.values["completed_tasks"].as<JSON::Array>();
 
-  ASSERT_EQ(1u, unreachableTasks.values.size());
+    ASSERT_EQ(1u, completedTasks.values.size());
 
-  JSON::Object unreachableTask =
-    unreachableTasks.values.front().as<JSON::Object>();
+    JSON::Object completedTask =
+      completedTasks.values.front().as<JSON::Object>();
 
-  EXPECT_EQ(
-      task.task_id(),
-      unreachableTask.values["id"].as<JSON::String>().value);
-  EXPECT_EQ(
-      "TASK_UNREACHABLE",
-      unreachableTask.values["state"].as<JSON::String>().value);
+    EXPECT_EQ(
+        task.task_id(),
+        completedTask.values["id"].as<JSON::String>().value);
+    EXPECT_EQ(
+        "TASK_KILLED",
+        completedTask.values["state"].as<JSON::String>().value);
+  }
+
+  // Also check the master's "/tasks" endpoint.
+  {
+    Future<Response> response = process::http::get(
+        master.get()->pid,
+        "tasks",
+        None(),
+        createBasicAuthHeaders(DEFAULT_CREDENTIAL));
+
+    AWAIT_EXPECT_RESPONSE_STATUS_EQ(OK().status, response);
+    AWAIT_EXPECT_RESPONSE_HEADER_EQ(APPLICATION_JSON, "Content-Type", response);
+
+    Try<JSON::Object> parse = JSON::parse<JSON::Object>(response->body);
+    ASSERT_SOME(parse);
+
+    JSON::Array tasks = parse->values["tasks"].as<JSON::Array>();
+
+    ASSERT_EQ(1u, tasks.values.size());
+
+    JSON::Object jsonTask = tasks.values.front().as<JSON::Object>();
+
+    EXPECT_EQ(
+        task.task_id(), jsonTask.values["id"].as<JSON::String>().value);
+    EXPECT_EQ(
+        "TASK_KILLED",
+        jsonTask.values["state"].as<JSON::String>().value);
+  }
 }