You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by nn...@apache.org on 2013/12/04 23:54:55 UTC

git commit: Added master/tasks.json endpoint.

Updated Branches:
  refs/heads/master cf7dcd3fb -> e55bde93c


Added master/tasks.json endpoint.

This patch lists all running and recent completed tasks, sorted by date
and starting at offset and limited by limit parameter.

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


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

Branch: refs/heads/master
Commit: e55bde93c22c54a43a984dfdf3ee88f1584bded1
Parents: cf7dcd3
Author: Niklas Q. Nielsen <ni...@mesosphere.io>
Authored: Wed Dec 4 22:46:32 2013 +0000
Committer: Niklas Q. Nielsen <ni...@mesosphere.io>
Committed: Wed Dec 4 22:46:32 2013 +0000

----------------------------------------------------------------------
 src/master/constants.cpp |   1 +
 src/master/constants.hpp |   3 ++
 src/master/http.cpp      | 100 ++++++++++++++++++++++++++++++++++++++++++
 src/master/master.cpp    |   3 ++
 src/master/master.hpp    |   5 +++
 5 files changed, 112 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/e55bde93/src/master/constants.cpp
----------------------------------------------------------------------
diff --git a/src/master/constants.cpp b/src/master/constants.cpp
index 4475a0d..0b7c9f7 100644
--- a/src/master/constants.cpp
+++ b/src/master/constants.cpp
@@ -32,6 +32,7 @@ const uint32_t MAX_SLAVE_PING_TIMEOUTS = 5;
 const uint32_t MAX_COMPLETED_FRAMEWORKS = 50;
 const uint32_t MAX_COMPLETED_TASKS_PER_FRAMEWORK = 1000;
 const Duration WHITELIST_WATCH_INTERVAL = Seconds(5);
+const uint32_t TASK_LIMIT = 100;
 
 } // namespace mesos {
 } // namespace internal {

http://git-wip-us.apache.org/repos/asf/mesos/blob/e55bde93/src/master/constants.hpp
----------------------------------------------------------------------
diff --git a/src/master/constants.hpp b/src/master/constants.hpp
index 634067a..8498c9b 100644
--- a/src/master/constants.hpp
+++ b/src/master/constants.hpp
@@ -62,6 +62,9 @@ extern const uint32_t MAX_COMPLETED_TASKS_PER_FRAMEWORK;
 // Time interval to check for updated watchers list.
 extern const Duration WHITELIST_WATCH_INTERVAL;
 
+// Default number of tasks (limit) for /master/tasks.json endpoint
+extern const uint32_t TASK_LIMIT;
+
 } // namespace mesos {
 } // namespace internal {
 } // namespace master {

http://git-wip-us.apache.org/repos/asf/mesos/blob/e55bde93/src/master/http.cpp
----------------------------------------------------------------------
diff --git a/src/master/http.cpp b/src/master/http.cpp
index c8ae437..3463365 100644
--- a/src/master/http.cpp
+++ b/src/master/http.cpp
@@ -465,6 +465,106 @@ Future<Response> Master::Http::roles(const Request& request)
   return OK(object, request.query.get("jsonp"));
 }
 
+
+const string Master::Http::TASKS_HELP = HELP(
+    TLDR(
+      "Lists tasks from all active frameworks."),
+    USAGE(
+      "/master/tasks.json"),
+    DESCRIPTION(
+      "Lists known tasks.",
+      "",
+      "Query parameters:",
+      "",
+      ">        limit=VALUE          Maximum number of tasks returned "
+      "(default is " + stringify(TASK_LIMIT) + ").",
+      ">        offset=VALUE         Starts task list at offset.",
+      ">        order=(asc|desc)     Ascending or descending sort order "
+      "(default is descending)."
+      ""));
+
+
+struct TaskComparator
+{
+  static bool ascending(const Task* lhs, const Task* rhs)
+  {
+    if (lhs->statuses().size() == 0) {
+      return true;
+    }
+
+    if (rhs->statuses().size() == 0) {
+      return false;
+    }
+
+    return (lhs->statuses(0).timestamp() < rhs->statuses(0).timestamp());
+  }
+
+  static bool descending(const Task* lhs, const Task* rhs)
+  {
+    return !ascending(lhs, rhs);
+  }
+};
+
+
+Future<Response> Master::Http::tasks(const Request& request)
+{
+  LOG(INFO) << "HTTP request for '" << request.path << "'";
+
+  // Get list options (limit and offset).
+  Result<int> result = numify<int>(request.query.get("limit"));
+  size_t limit = result.isSome() ? result.get() : TASK_LIMIT;
+
+  result = numify<int>(request.query.get("offset"));
+  size_t offset = result.isSome() ? result.get() : 0;
+
+  // TODO(nnielsen): Currently, formatting errors in offset and/or limit
+  // will silently be ignored. This could be reported to the user instead.
+
+  // Construct framework list with both active and completed framwworks.
+  vector<const Framework*> frameworks;
+  foreachvalue (Framework* framework, master.frameworks) {
+    frameworks.push_back(framework);
+  }
+  foreach (const std::tr1::shared_ptr<Framework>& framework,
+           master.completedFrameworks) {
+    frameworks.push_back(framework.get());
+  }
+
+  // Construct task list with both running and finished tasks.
+  vector<const Task*> tasks;
+  foreach (const Framework* framework, frameworks) {
+    foreachvalue (Task* task, framework->tasks) {
+      CHECK_NOTNULL(task);
+      tasks.push_back(task);
+    }
+    foreach (const Task& task, framework->completedTasks) {
+      tasks.push_back(&task);
+    }
+  }
+
+  // Sort tasks by task status timestamp. Default order is descending.
+  // The earlist timestamp is chosen for comparison when multiple are present.
+  Option<string> order = request.query.get("order");
+  if (order.isSome() && (order.get() == "asc")) {
+    sort(tasks.begin(), tasks.end(), TaskComparator::ascending);
+  } else {
+    sort(tasks.begin(), tasks.end(), TaskComparator::descending);
+  }
+
+  JSON::Array array;
+  size_t end = std::min(offset + limit, tasks.size());
+  for (size_t i = offset; i < end; i++) {
+    const Task* task = tasks[i];
+    array.values.push_back(model(*task));
+  }
+
+  JSON::Object object;
+  object.values["tasks"] = array;
+
+  return OK(object, request.query.get("jsonp"));
+}
+
+
 } // namespace master {
 } // namespace internal {
 } // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/e55bde93/src/master/master.cpp
----------------------------------------------------------------------
diff --git a/src/master/master.cpp b/src/master/master.cpp
index 4f4db93..b2b8db3 100644
--- a/src/master/master.cpp
+++ b/src/master/master.cpp
@@ -521,6 +521,9 @@ void Master::initialize()
   route("/roles.json",
         None(),
         lambda::bind(&Http::roles, http, lambda::_1));
+  route("/tasks.json",
+        Http::TASKS_HELP,
+        lambda::bind(&Http::tasks, http, lambda::_1));
 
   // Provide HTTP assets from a "webui" directory. This is either
   // specified via flags (which is necessary for running out of the

http://git-wip-us.apache.org/repos/asf/mesos/blob/e55bde93/src/master/master.hpp
----------------------------------------------------------------------
diff --git a/src/master/master.hpp b/src/master/master.hpp
index a7bf963..6c168a2 100644
--- a/src/master/master.hpp
+++ b/src/master/master.hpp
@@ -295,8 +295,13 @@ private:
     process::Future<process::http::Response> roles(
         const process::http::Request& request);
 
+    // /master/tasks.json
+    process::Future<process::http::Response> tasks(
+        const process::http::Request& request);
+
     const static std::string HEALTH_HELP;
     const static std::string REDIRECT_HELP;
+    const static std::string TASKS_HELP;
 
   private:
     const Master& master;