Implemented v1 operator update weights API.

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


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

Branch: refs/heads/master
Commit: 8570c1508574483c9e69474717c126beb36ff739
Parents: 965ac52
Author: zhou xing <xingz...@cn.ibm.com>
Authored: Tue Jun 28 13:20:59 2016 -0700
Committer: Vinod Kone <vinodk...@gmail.com>
Committed: Tue Jun 28 13:20:59 2016 -0700

----------------------------------------------------------------------
 include/mesos/master/master.proto    | 19 +++++-----
 include/mesos/v1/master/master.proto | 19 +++++-----
 src/master/http.cpp                  |  2 +-
 src/master/master.hpp                |  5 +++
 src/master/validation.cpp            |  3 ++
 src/master/weights_handler.cpp       | 12 +++++++
 src/tests/api_tests.cpp              | 58 +++++++++++++++++++++++++++++++
 7 files changed, 99 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/8570c150/include/mesos/master/master.proto
----------------------------------------------------------------------
diff --git a/include/mesos/master/master.proto 
b/include/mesos/master/master.proto
index ca373fa..798b536 100644
--- a/include/mesos/master/master.proto
+++ b/include/mesos/master/master.proto
@@ -194,15 +194,16 @@ message Call {
   optional ListFiles list_files = 4;
   optional ReadFile read_file = 5;
   optional GetTasks get_tasks = 6;
-  optional ReserveResources reserve_resources = 7;
-  optional UnreserveResources unreserve_resources = 8;
-  optional CreateVolumes create_volumes = 9;
-  optional DestroyVolumes destroy_volumes = 10;
-  optional UpdateMaintenanceSchedule update_maintenance_schedule = 11;
-  optional StartMaintenance start_maintenance = 12;
-  optional StopMaintenance stop_maintenance  = 13;
-  optional SetQuota set_quota = 14;
-  optional RemoveQuota remove_quota = 15;
+  optional UpdateWeights update_weights = 7;
+  optional ReserveResources reserve_resources = 8;
+  optional UnreserveResources unreserve_resources = 9;
+  optional CreateVolumes create_volumes = 10;
+  optional DestroyVolumes destroy_volumes = 11;
+  optional UpdateMaintenanceSchedule update_maintenance_schedule = 12;
+  optional StartMaintenance start_maintenance = 13;
+  optional StopMaintenance stop_maintenance  = 14;
+  optional SetQuota set_quota = 15;
+  optional RemoveQuota remove_quota = 16;
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/8570c150/include/mesos/v1/master/master.proto
----------------------------------------------------------------------
diff --git a/include/mesos/v1/master/master.proto 
b/include/mesos/v1/master/master.proto
index 31148b4..93157d5 100644
--- a/include/mesos/v1/master/master.proto
+++ b/include/mesos/v1/master/master.proto
@@ -195,15 +195,16 @@ message Call {
   optional ListFiles list_files = 4;
   optional ReadFile read_file = 5;
   optional GetTasks get_tasks = 6;
-  optional ReserveResources reserve_resources = 7;
-  optional UnreserveResources unreserve_resources = 8;
-  optional CreateVolumes create_volumes = 9;
-  optional DestroyVolumes destroy_volumes = 10;
-  optional UpdateMaintenanceSchedule update_maintenance_schedule = 11;
-  optional StartMaintenance start_maintenance = 12;
-  optional StopMaintenance stop_maintenance  = 13;
-  optional SetQuota set_quota = 14;
-  optional RemoveQuota remove_quota = 15;
+  optional UpdateWeights update_weights = 7;
+  optional ReserveResources reserve_resources = 8;
+  optional UnreserveResources unreserve_resources = 9;
+  optional CreateVolumes create_volumes = 10;
+  optional DestroyVolumes destroy_volumes = 11;
+  optional UpdateMaintenanceSchedule update_maintenance_schedule = 12;
+  optional StartMaintenance start_maintenance = 13;
+  optional StopMaintenance stop_maintenance  = 14;
+  optional SetQuota set_quota = 15;
+  optional RemoveQuota remove_quota = 16;
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/8570c150/src/master/http.cpp
----------------------------------------------------------------------
diff --git a/src/master/http.cpp b/src/master/http.cpp
index 0f9c2b9..a3b2110 100644
--- a/src/master/http.cpp
+++ b/src/master/http.cpp
@@ -556,7 +556,7 @@ Future<Response> Master::Http::api(
       return weightsHandler.get(call, principal, acceptType);
 
     case mesos::master::Call::UPDATE_WEIGHTS:
-      return NotImplemented();
+      return weightsHandler.update(call, principal, acceptType);
 
     case mesos::master::Call::GET_LEADING_MASTER:
       return getLeadingMaster(call, principal, acceptType);

http://git-wip-us.apache.org/repos/asf/mesos/blob/8570c150/src/master/master.hpp
----------------------------------------------------------------------
diff --git a/src/master/master.hpp b/src/master/master.hpp
index 8bcc16b..e2ab211 100644
--- a/src/master/master.hpp
+++ b/src/master/master.hpp
@@ -1120,6 +1120,11 @@ private:
         const process::http::Request& request,
         const Option<std::string>& principal) const;
 
+    process::Future<process::http::Response> update(
+        const mesos::master::Call& call,
+        const Option<std::string>& principal,
+        ContentType contentType) const;
+
   private:
     process::Future<bool> authorizeGetWeight(
         const Option<std::string>& principal,

http://git-wip-us.apache.org/repos/asf/mesos/blob/8570c150/src/master/validation.cpp
----------------------------------------------------------------------
diff --git a/src/master/validation.cpp b/src/master/validation.cpp
index 3009549..6939d0e 100644
--- a/src/master/validation.cpp
+++ b/src/master/validation.cpp
@@ -130,6 +130,9 @@ Option<Error> validate(
       return None();
 
     case mesos::master::Call::UPDATE_WEIGHTS:
+      if (!call.has_update_weights()) {
+        return Error("Expecting 'update_weights' to be present");
+      }
       return None();
 
     case mesos::master::Call::GET_LEADING_MASTER:

http://git-wip-us.apache.org/repos/asf/mesos/blob/8570c150/src/master/weights_handler.cpp
----------------------------------------------------------------------
diff --git a/src/master/weights_handler.cpp b/src/master/weights_handler.cpp
index 02a5a1c..5fc69c6 100644
--- a/src/master/weights_handler.cpp
+++ b/src/master/weights_handler.cpp
@@ -183,6 +183,18 @@ Future<http::Response> Master::WeightsHandler::update(
 }
 
 
+Future<http::Response> Master::WeightsHandler::update(
+    const mesos::master::Call& call,
+    const Option<std::string>& principal,
+    ContentType /*contentType*/) const
+{
+  CHECK_EQ(mesos::master::Call::UPDATE_WEIGHTS, call.type());
+  CHECK(call.has_update_weights());
+
+  return _updateWeights(principal, call.update_weights().weight_infos());
+}
+
+
 Future<http::Response> Master::WeightsHandler::_updateWeights(
     const Option<string>& principal,
     const RepeatedPtrField<WeightInfo>& weightInfos) const {

http://git-wip-us.apache.org/repos/asf/mesos/blob/8570c150/src/tests/api_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/api_tests.cpp b/src/tests/api_tests.cpp
index 136a9fb..b3127b7 100644
--- a/src/tests/api_tests.cpp
+++ b/src/tests/api_tests.cpp
@@ -1569,6 +1569,64 @@ TEST_P(MasterAPITest, GetWeights)
 }
 
 
+TEST_P(MasterAPITest, UpdateWeights)
+{
+  // Start a master with `--weights` flag.
+  master::Flags masterFlags = CreateMasterFlags();
+  masterFlags.weights = "role=2.0";
+  Try<Owned<cluster::Master>> master = StartMaster(masterFlags);
+  ASSERT_SOME(master);
+
+  v1::master::Call getCall, updateCall;
+  getCall.set_type(v1::master::Call::GET_WEIGHTS);
+  updateCall.set_type(v1::master::Call::UPDATE_WEIGHTS);
+
+  ContentType contentType = GetParam();
+
+  Future<v1::master::Response> getResponse =
+    post(master.get()->pid, getCall, contentType);
+
+  AWAIT_READY(getResponse);
+  ASSERT_TRUE(getResponse->IsInitialized());
+  ASSERT_EQ(v1::master::Response::GET_WEIGHTS, getResponse->type());
+  ASSERT_EQ(1, getResponse->get_weights().weight_infos_size());
+  ASSERT_EQ("role", getResponse->get_weights().weight_infos().Get(0).role());
+  ASSERT_EQ(2.0, getResponse->get_weights().weight_infos().Get(0).weight());
+
+  v1::WeightInfo* weightInfo =
+    updateCall.mutable_update_weights()->add_weight_infos();
+  weightInfo->set_role("role");
+  weightInfo->set_weight(4.0);
+
+  process::http::Headers headers = createBasicAuthHeaders(DEFAULT_CREDENTIAL);
+  headers["Accept"] = stringify(contentType);
+
+  Future<Nothing> updateResponse = process::http::post(
+      master.get()->pid,
+      "api/v1",
+      headers,
+      serialize(contentType, updateCall),
+      stringify(contentType))
+    .then([contentType](const Response& response) -> Future<Nothing> {
+      if (response.status != OK().status) {
+        return Failure("Unexpected response status " + response.status);
+      }
+      return Nothing();
+    });
+
+  AWAIT_READY(updateResponse);
+
+  getResponse = post(master.get()->pid, getCall, contentType);
+
+  AWAIT_READY(getResponse);
+  ASSERT_TRUE(getResponse->IsInitialized());
+  ASSERT_EQ(v1::master::Response::GET_WEIGHTS, getResponse->type());
+  ASSERT_EQ(1, getResponse->get_weights().weight_infos_size());
+  ASSERT_EQ("role", getResponse->get_weights().weight_infos().Get(0).role());
+  ASSERT_EQ(4.0, getResponse->get_weights().weight_infos().Get(0).weight());
+}
+
+
 class AgentAPITest
   : public MesosTest,
     public WithParamInterface<ContentType>

Reply via email to