Repository: mesos
Updated Branches:
  refs/heads/master 6b675dd1b -> d3847398d


Added Python binding for the acceptOffers API.

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


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

Branch: refs/heads/master
Commit: d3847398d346b64112ae06a3fc60e53c2cec5212
Parents: 6a6cb4b
Author: Jie Yu <[email protected]>
Authored: Tue Mar 10 11:08:24 2015 -0700
Committer: Jie Yu <[email protected]>
Committed: Wed Mar 11 18:21:53 2015 -0700

----------------------------------------------------------------------
 src/examples/python/test_framework.py           |  6 +-
 .../interface/src/mesos/interface/__init__.py   | 11 +++
 .../native/mesos_scheduler_driver_impl.cpp      | 84 ++++++++++++++++++++
 .../native/mesos_scheduler_driver_impl.hpp      |  4 +
 4 files changed, 104 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/d3847398/src/examples/python/test_framework.py
----------------------------------------------------------------------
diff --git a/src/examples/python/test_framework.py 
b/src/examples/python/test_framework.py
index 2710614..a179df5 100755
--- a/src/examples/python/test_framework.py
+++ b/src/examples/python/test_framework.py
@@ -91,7 +91,11 @@ class TestScheduler(mesos.interface.Scheduler):
                 remainingCpus -= TASK_CPUS
                 remainingMem -= TASK_MEM
 
-            driver.launchTasks(offer.id, tasks)
+            operation = mesos_pb2.Offer.Operation()
+            operation.type = mesos_pb2.Offer.Operation.LAUNCH
+            operation.launch.task_infos.extend(tasks)
+
+            driver.acceptOffers([offer.id], [operation])
 
     def statusUpdate(self, driver, update):
         print "Task %s is in state %s" % \

http://git-wip-us.apache.org/repos/asf/mesos/blob/d3847398/src/python/interface/src/mesos/interface/__init__.py
----------------------------------------------------------------------
diff --git a/src/python/interface/src/mesos/interface/__init__.py 
b/src/python/interface/src/mesos/interface/__init__.py
index f3d96a4..15af555 100644
--- a/src/python/interface/src/mesos/interface/__init__.py
+++ b/src/python/interface/src/mesos/interface/__init__.py
@@ -201,6 +201,17 @@ class SchedulerDriver(object):
       dropped (these semantics may be changed in the future).
     """
 
+  def acceptOffers(self, offerIds, operations, filters=None):
+    """
+      Accepts the given offers and performs a sequence of operations
+      on those accepted offers. See Offer.Operation in mesos.proto for
+      the set of available operations. Available resources are
+      aggregated when multiple offers are provided. Note that all
+      offers must belong to the same slave. Any unused resources will
+      be considered declined. The specified filters are applied on all
+      unused resources (see mesos.proto for a description of Filters).
+    """
+
   def declineOffer(self, offerId, filters=None):
     """
       Declines an offer in its entirety and applies the specified

http://git-wip-us.apache.org/repos/asf/mesos/blob/d3847398/src/python/native/src/mesos/native/mesos_scheduler_driver_impl.cpp
----------------------------------------------------------------------
diff --git a/src/python/native/src/mesos/native/mesos_scheduler_driver_impl.cpp 
b/src/python/native/src/mesos/native/mesos_scheduler_driver_impl.cpp
index bb18845..b0a5ef4 100644
--- a/src/python/native/src/mesos/native/mesos_scheduler_driver_impl.cpp
+++ b/src/python/native/src/mesos/native/mesos_scheduler_driver_impl.cpp
@@ -128,6 +128,11 @@ PyMethodDef MesosSchedulerDriverImpl_methods[] = {
     METH_VARARGS,
     "Kill the task with the given ID"
   },
+  { "acceptOffers",
+    (PyCFunction) MesosSchedulerDriverImpl_acceptOffers,
+    METH_VARARGS,
+    "Reply to a Mesos offer with a list of offer operations"
+  },
   { "declineOffer",
     (PyCFunction) MesosSchedulerDriverImpl_declineOffer,
     METH_VARARGS,
@@ -523,6 +528,85 @@ PyObject* 
MesosSchedulerDriverImpl_killTask(MesosSchedulerDriverImpl* self,
 }
 
 
+PyObject* MesosSchedulerDriverImpl_acceptOffers(MesosSchedulerDriverImpl* self,
+                                                PyObject* args)
+{
+  if (self->driver == NULL) {
+    PyErr_Format(PyExc_Exception, "MesosSchedulerDriverImpl.driver is NULL");
+    return NULL;
+  }
+
+  PyObject* offerIdsObj = NULL;
+  PyObject* operationsObj = NULL;
+  PyObject* filtersObj = NULL;
+  Py_ssize_t len = 0;
+  vector<OfferID> offerIds;
+  vector<Offer::Operation> operations;
+  Filters filters;
+
+  if (!PyArg_ParseTuple(args,
+                        "OO|O",
+                        &offerIdsObj,
+                        &operationsObj,
+                        &filtersObj)) {
+    return NULL;
+  }
+
+  if (!PyList_Check(offerIdsObj)) {
+    PyErr_Format(PyExc_Exception, "Parameter 1 to acceptOffers is not a list");
+    return NULL;
+  }
+
+  len = PyList_Size(offerIdsObj);
+  for (int i = 0; i < len; i++) {
+    PyObject* offerObj = PyList_GetItem(offerIdsObj, i);
+    if (offerObj == NULL) {
+      return NULL;
+    }
+
+    OfferID offerId;
+    if (!readPythonProtobuf(offerObj, &offerId)) {
+      PyErr_Format(PyExc_Exception,
+                   "Could not deserialize Python OfferID");
+      return NULL;
+    }
+    offerIds.push_back(offerId);
+  }
+
+  if (!PyList_Check(operationsObj)) {
+    PyErr_Format(PyExc_Exception, "Parameter 2 to acceptOffers is not a list");
+    return NULL;
+  }
+
+  len = PyList_Size(operationsObj);
+  for (int i = 0; i < len; i++) {
+    PyObject* operationObj = PyList_GetItem(operationsObj, i);
+    if (operationObj == NULL) {
+      return NULL; // Exception will have been set by PyList_GetItem.
+    }
+
+    Offer::Operation operation;
+    if (!readPythonProtobuf(operationObj, &operation)) {
+      PyErr_Format(PyExc_Exception,
+                   "Could not deserialize Python Offer.Operation");
+      return NULL;
+    }
+    operations.push_back(operation);
+  }
+
+  if (filtersObj != NULL) {
+    if (!readPythonProtobuf(filtersObj, &filters)) {
+      PyErr_Format(PyExc_Exception,
+                   "Could not deserialize Python Filters");
+      return NULL;
+    }
+  }
+
+  Status status = self->driver->acceptOffers(offerIds, operations, filters);
+  return PyInt_FromLong(status); // Sets exception if creating long fails.
+}
+
+
 PyObject* MesosSchedulerDriverImpl_declineOffer(MesosSchedulerDriverImpl* self,
                                                 PyObject* args)
 {

http://git-wip-us.apache.org/repos/asf/mesos/blob/d3847398/src/python/native/src/mesos/native/mesos_scheduler_driver_impl.hpp
----------------------------------------------------------------------
diff --git a/src/python/native/src/mesos/native/mesos_scheduler_driver_impl.hpp 
b/src/python/native/src/mesos/native/mesos_scheduler_driver_impl.hpp
index a698000..b1ad8e5 100644
--- a/src/python/native/src/mesos/native/mesos_scheduler_driver_impl.hpp
+++ b/src/python/native/src/mesos/native/mesos_scheduler_driver_impl.hpp
@@ -105,6 +105,10 @@ PyObject* MesosSchedulerDriverImpl_killTask(
     MesosSchedulerDriverImpl* self,
     PyObject* args);
 
+PyObject* MesosSchedulerDriverImpl_acceptOffers(
+    MesosSchedulerDriverImpl* self,
+    PyObject* args);
+
 PyObject* MesosSchedulerDriverImpl_declineOffer(
     MesosSchedulerDriverImpl* self,
     PyObject* args);

Reply via email to