This is an automated email from the ASF dual-hosted git repository.

alexr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 63e9096b0cd883d9edc8907a577bcba0b150b541
Author: Benno Evers <[email protected]>
AuthorDate: Tue Aug 28 21:26:03 2018 +0200

    Added '/tasks' to the set of batched master endpoints.
    
    Review: https://reviews.apache.org/r/68440/
---
 src/master/http.cpp   | 212 ++++++++++++++++++++++++++------------------------
 src/master/master.hpp |   5 ++
 2 files changed, 115 insertions(+), 102 deletions(-)

diff --git a/src/master/http.cpp b/src/master/http.cpp
index 5cd177e..897c80d 100644
--- a/src/master/http.cpp
+++ b/src/master/http.cpp
@@ -4041,25 +4041,10 @@ string Master::Http::TASKS_HELP()
         "See the authorization documentation for details."));
 }
 
-
-Future<Response> Master::Http::tasks(
-    const Request& request,
-    const Option<Principal>& principal) const
+process::http::Response Master::ReadOnlyHandler::tasks(
+  const process::http::Request& request,
+  const process::Owned<ObjectApprovers>& approvers) const
 {
-  // TODO(greggomann): Remove this check once the `Principal` type is used in
-  // `ReservationInfo`, `DiskInfo`, and within the master's `principals` map.
-  // See MESOS-7202.
-  if (principal.isSome() && principal->value.isNone()) {
-    return Forbidden(
-        "The request's authenticated principal contains claims, but no value "
-        "string. The master currently requires that principals have a value");
-  }
-
-  // When current master is not the leader, redirect to the leading master.
-  if (!master->elected()) {
-    return redirect(request);
-  }
-
   // Get list options (limit and offset).
   Result<int> result = numify<int>(request.url.query.get("limit"));
   size_t limit = result.isSome() ? result.get() : TASK_LIMIT;
@@ -4073,103 +4058,126 @@ Future<Response> Master::Http::tasks(
   Option<string> frameworkId = request.url.query.get("framework_id");
   Option<string> taskId = request.url.query.get("task_id");
 
-  return ObjectApprovers::create(
-      master->authorizer,
-      principal,
-      {VIEW_FRAMEWORK, VIEW_TASK})
-    .then(defer(
-        master->self(),
-        [=](const Owned<ObjectApprovers>& approvers) -> Response {
-          IDAcceptor<FrameworkID> selectFrameworkId(frameworkId);
-          IDAcceptor<TaskID> selectTaskId(taskId);
-
-          // Construct framework list with both active and completed 
frameworks.
-          vector<const Framework*> frameworks;
-          foreachvalue (Framework* framework, master->frameworks.registered) {
-            // Skip unauthorized frameworks or frameworks without matching
-            // framework ID.
-            if (!selectFrameworkId.accept(framework->id()) ||
-                !approvers->approved<VIEW_FRAMEWORK>(framework->info)) {
-              continue;
-            }
+  IDAcceptor<FrameworkID> selectFrameworkId(frameworkId);
+  IDAcceptor<TaskID> selectTaskId(taskId);
 
-            frameworks.push_back(framework);
-          }
+  // Construct framework list with both active and completed frameworks.
+  vector<const Framework*> frameworks;
+  foreachvalue (Framework* framework, master->frameworks.registered) {
+    // Skip unauthorized frameworks or frameworks without matching
+    // framework ID.
+    if (!selectFrameworkId.accept(framework->id()) ||
+        !approvers->approved<VIEW_FRAMEWORK>(framework->info)) {
+      continue;
+    }
 
-          foreachvalue (const Owned<Framework>& framework,
-                        master->frameworks.completed) {
-            // Skip unauthorized frameworks or frameworks without matching
-            // framework ID.
-            if (!selectFrameworkId.accept(framework->id()) ||
-                !approvers->approved<VIEW_FRAMEWORK>(framework->info)) {
-             continue;
-            }
+    frameworks.push_back(framework);
+  }
 
-            frameworks.push_back(framework.get());
-          }
+  foreachvalue (const Owned<Framework>& framework,
+                master->frameworks.completed) {
+    // Skip unauthorized frameworks or frameworks without matching
+    // framework ID.
+    if (!selectFrameworkId.accept(framework->id()) ||
+        !approvers->approved<VIEW_FRAMEWORK>(framework->info)) {
+     continue;
+    }
 
-          // Construct task list with both running,
-          // completed and unreachable tasks.
-          vector<const Task*> tasks;
-          foreach (const Framework* framework, frameworks) {
-            foreachvalue (Task* task, framework->tasks) {
-              CHECK_NOTNULL(task);
-              // Skip unauthorized tasks or tasks without matching task ID.
-              if (!selectTaskId.accept(task->task_id()) ||
-                  !approvers->approved<VIEW_TASK>(*task, framework->info)) {
-                continue;
-              }
+    frameworks.push_back(framework.get());
+  }
 
-              tasks.push_back(task);
-            }
+  // Construct task list with both running,
+  // completed and unreachable tasks.
+  vector<const Task*> tasks;
+  foreach (const Framework* framework, frameworks) {
+    foreachvalue (Task* task, framework->tasks) {
+      CHECK_NOTNULL(task);
+      // Skip unauthorized tasks or tasks without matching task ID.
+      if (!selectTaskId.accept(task->task_id()) ||
+          !approvers->approved<VIEW_TASK>(*task, framework->info)) {
+        continue;
+      }
 
-            foreachvalue (
-                const Owned<Task>& task,
-                framework->unreachableTasks) {
-              // Skip unauthorized tasks or tasks without matching task ID.
-              if (!selectTaskId.accept(task->task_id()) ||
-                  !approvers->approved<VIEW_TASK>(*task, framework->info)) {
-                continue;
-              }
+      tasks.push_back(task);
+    }
 
-              tasks.push_back(task.get());
-            }
+    foreachvalue (
+        const Owned<Task>& task,
+        framework->unreachableTasks) {
+      // Skip unauthorized tasks or tasks without matching task ID.
+      if (!selectTaskId.accept(task->task_id()) ||
+          !approvers->approved<VIEW_TASK>(*task, framework->info)) {
+        continue;
+      }
 
-            foreach (const Owned<Task>& task, framework->completedTasks) {
-              // Skip unauthorized tasks or tasks without matching task ID.
-              if (!selectTaskId.accept(task->task_id()) ||
-                  !approvers->approved<VIEW_TASK>(*task, framework->info)) {
-                continue;
-              }
+      tasks.push_back(task.get());
+    }
 
-              tasks.push_back(task.get());
+    foreach (const Owned<Task>& task, framework->completedTasks) {
+      // Skip unauthorized tasks or tasks without matching task ID.
+      if (!selectTaskId.accept(task->task_id()) ||
+          !approvers->approved<VIEW_TASK>(*task, framework->info)) {
+        continue;
+      }
+
+      tasks.push_back(task.get());
+    }
+  }
+
+  // Sort tasks by task status timestamp. Default order is descending.
+  // The earliest timestamp is chosen for comparison when
+  // multiple are present.
+  if (_order == "asc") {
+    sort(tasks.begin(), tasks.end(), TaskComparator::ascending);
+  } else {
+    sort(tasks.begin(), tasks.end(), TaskComparator::descending);
+  }
+
+  auto tasksWriter =
+    [&tasks, limit, offset](JSON::ObjectWriter* writer) {
+      writer->field(
+          "tasks",
+          [&tasks, limit, offset](JSON::ArrayWriter* writer) {
+            // Collect 'limit' number of tasks starting from 'offset'.
+            size_t end = std::min(offset + limit, tasks.size());
+            for (size_t i = offset; i < end; i++) {
+              writer->element(*tasks[i]);
             }
-          }
+          });
+  };
 
-          // Sort tasks by task status timestamp. Default order is descending.
-          // The earliest timestamp is chosen for comparison when
-          // multiple are present.
-          if (_order == "asc") {
-            sort(tasks.begin(), tasks.end(), TaskComparator::ascending);
-          } else {
-            sort(tasks.begin(), tasks.end(), TaskComparator::descending);
-          }
+  return OK(jsonify(tasksWriter), request.url.query.get("jsonp"));
+}
 
-          auto tasksWriter =
-            [&tasks, limit, offset](JSON::ObjectWriter* writer) {
-              writer->field(
-                  "tasks",
-                  [&tasks, limit, offset](JSON::ArrayWriter* writer) {
-                    // Collect 'limit' number of tasks starting from 'offset'.
-                    size_t end = std::min(offset + limit, tasks.size());
-                    for (size_t i = offset; i < end; i++) {
-                      writer->element(*tasks[i]);
-                    }
-                  });
-          };
 
-          return OK(jsonify(tasksWriter), request.url.query.get("jsonp"));
-  }));
+Future<Response> Master::Http::tasks(
+    const Request& request,
+    const Option<Principal>& principal) const
+{
+  // TODO(greggomann): Remove this check once the `Principal` type is used in
+  // `ReservationInfo`, `DiskInfo`, and within the master's `principals` map.
+  // See MESOS-7202.
+  if (principal.isSome() && principal->value.isNone()) {
+    return Forbidden(
+        "The request's authenticated principal contains claims, but no value "
+        "string. The master currently requires that principals have a value");
+  }
+
+  // When current master is not the leader, redirect to the leading master.
+  if (!master->elected()) {
+    return redirect(request);
+  }
+
+  return ObjectApprovers::create(
+      master->authorizer,
+      principal,
+      {VIEW_FRAMEWORK, VIEW_TASK})
+    .then(defer(
+        master->self(),
+        [this, request](const Owned<ObjectApprovers>& approvers) {
+          return deferBatchedRequest(
+              &Master::ReadOnlyHandler::tasks, request, approvers);
+        }));
 }
 
 
diff --git a/src/master/master.hpp b/src/master/master.hpp
index 9db5aae..381db90 100644
--- a/src/master/master.hpp
+++ b/src/master/master.hpp
@@ -1406,6 +1406,11 @@ private:
         const process::http::Request& request,
         const process::Owned<ObjectApprovers>& approvers) const;
 
+    // /tasks
+    process::http::Response tasks(
+        const process::http::Request& request,
+        const process::Owned<ObjectApprovers>& approvers) const;
+
   private:
     const Master* master;
   };

Reply via email to