Implemented 'GetState' call in v1 agent API. Review: https://reviews.apache.org/r/49760/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/a2981795 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/a2981795 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/a2981795 Branch: refs/heads/master Commit: a298179585c727255c6637d77229d251fb8dd1af Parents: d8e4b05 Author: haosdent huang <haosd...@gmail.com> Authored: Wed Jul 13 12:10:22 2016 -0700 Committer: Vinod Kone <vinodk...@gmail.com> Committed: Wed Jul 13 12:10:22 2016 -0700 ---------------------------------------------------------------------- include/mesos/agent/agent.proto | 6 ++- include/mesos/v1/agent/agent.proto | 6 ++- src/slave/http.cpp | 78 ++++++++++++++++++++++++++++++++- src/slave/slave.hpp | 10 +++++ 4 files changed, 97 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/a2981795/include/mesos/agent/agent.proto ---------------------------------------------------------------------- diff --git a/include/mesos/agent/agent.proto b/include/mesos/agent/agent.proto index 528b8b3..5b91677 100644 --- a/include/mesos/agent/agent.proto +++ b/include/mesos/agent/agent.proto @@ -167,8 +167,12 @@ message Response { required bytes data = 2; } + // Contains full state of the agent i.e. information about the tasks, + // frameworks and executors running in the cluster. message GetState { - // TODO(vinod): Fill in the fields. + optional GetTasks get_tasks = 1; + optional GetExecutors get_executors = 2; + optional GetFrameworks get_frameworks = 3; } // Information about containers running on this agent. It contains http://git-wip-us.apache.org/repos/asf/mesos/blob/a2981795/include/mesos/v1/agent/agent.proto ---------------------------------------------------------------------- diff --git a/include/mesos/v1/agent/agent.proto b/include/mesos/v1/agent/agent.proto index 699a17b..8145669 100644 --- a/include/mesos/v1/agent/agent.proto +++ b/include/mesos/v1/agent/agent.proto @@ -167,8 +167,12 @@ message Response { required bytes data = 2; } + // Contains full state of the agent i.e. information about the tasks, + // frameworks and executors running in the cluster. message GetState { - // TODO(vinod): Fill in the fields. + optional GetTasks get_tasks = 1; + optional GetExecutors get_executors = 2; + optional GetFrameworks get_frameworks = 3; } // Information about containers running on this agent. It contains http://git-wip-us.apache.org/repos/asf/mesos/blob/a2981795/src/slave/http.cpp ---------------------------------------------------------------------- diff --git a/src/slave/http.cpp b/src/slave/http.cpp index 63968bf..2cbf3c5 100644 --- a/src/slave/http.cpp +++ b/src/slave/http.cpp @@ -385,7 +385,7 @@ Future<Response> Slave::Http::api( return readFile(call, principal, acceptType); case agent::Call::GET_STATE: - return NotImplemented(); + return getState(call, principal, acceptType); case agent::Call::GET_CONTAINERS: return getContainers(call, principal, acceptType); @@ -1411,6 +1411,82 @@ agent::Response::GetTasks Slave::Http::_getTasks( } +Future<Response> Slave::Http::getState( + const agent::Call& call, + const Option<string>& principal, + ContentType contentType) const +{ + CHECK_EQ(agent::Call::GET_STATE, call.type()); + + // Retrieve Approvers for authorizing frameworks and tasks. + Future<Owned<ObjectApprover>> frameworksApprover; + Future<Owned<ObjectApprover>> tasksApprover; + Future<Owned<ObjectApprover>> executorsApprover; + if (slave->authorizer.isSome()) { + authorization::Subject subject; + if (principal.isSome()) { + subject.set_value(principal.get()); + } + + frameworksApprover = slave->authorizer.get()->getObjectApprover( + subject, authorization::VIEW_FRAMEWORK); + + tasksApprover = slave->authorizer.get()->getObjectApprover( + subject, authorization::VIEW_TASK); + + executorsApprover = slave->authorizer.get()->getObjectApprover( + subject, authorization::VIEW_EXECUTOR); + } else { + frameworksApprover = Owned<ObjectApprover>(new AcceptingObjectApprover()); + tasksApprover = Owned<ObjectApprover>(new AcceptingObjectApprover()); + executorsApprover = Owned<ObjectApprover>(new AcceptingObjectApprover()); + } + + return collect(frameworksApprover, tasksApprover, executorsApprover) + .then(defer(slave->self(), + [=](const tuple<Owned<ObjectApprover>, + Owned<ObjectApprover>, + Owned<ObjectApprover>>& approvers) + -> Future<Response> { + // Get approver from tuple. + Owned<ObjectApprover> frameworksApprover; + Owned<ObjectApprover> tasksApprover; + Owned<ObjectApprover> executorsApprover; + tie(frameworksApprover, tasksApprover, executorsApprover) = approvers; + + agent::Response response; + response.set_type(agent::Response::GET_STATE); + response.mutable_get_state()->CopyFrom( + _getState(frameworksApprover, + tasksApprover, + executorsApprover)); + + return OK(serialize(contentType, evolve(response)), + stringify(contentType)); + })); +} + + +agent::Response::GetState Slave::Http::_getState( + const Owned<ObjectApprover>& frameworksApprover, + const Owned<ObjectApprover>& tasksApprover, + const Owned<ObjectApprover>& executorsApprover) const +{ + agent::Response::GetState getState; + + getState.mutable_get_tasks()->CopyFrom( + _getTasks(frameworksApprover, tasksApprover, executorsApprover)); + + getState.mutable_get_executors()->CopyFrom( + _getExecutors(frameworksApprover, executorsApprover)); + + getState.mutable_get_frameworks()->CopyFrom( + _getFrameworks(frameworksApprover)); + + return getState; +} + + string Slave::Http::STATISTICS_HELP() { return HELP( http://git-wip-us.apache.org/repos/asf/mesos/blob/a2981795/src/slave/slave.hpp ---------------------------------------------------------------------- diff --git a/src/slave/slave.hpp b/src/slave/slave.hpp index 4995c84..9864cf4 100644 --- a/src/slave/slave.hpp +++ b/src/slave/slave.hpp @@ -576,6 +576,16 @@ private: const process::Owned<ObjectApprover>& tasksApprover, const process::Owned<ObjectApprover>& executorsApprover) const; + process::Future<process::http::Response> getState( + const mesos::agent::Call& call, + const Option<std::string>& principal, + ContentType contentType) const; + + mesos::agent::Response::GetState _getState( + const process::Owned<ObjectApprover>& frameworksApprover, + const process::Owned<ObjectApprover>& taskApprover, + const process::Owned<ObjectApprover>& executorsApprover) const; + Slave* slave; // Used to rate limit the statistics endpoint.