Repository: mesos
Updated Branches:
  refs/heads/master 2d0b65ede -> 8ecf8b0a8


Added a new callback enabling custom attribute discovery logic.

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


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

Branch: refs/heads/master
Commit: 8ecf8b0a896733996fb922ac7f9a14b961c0c1c6
Parents: 2d0b65e
Author: Felix Abecassis <[email protected]>
Authored: Tue Oct 13 09:26:32 2015 -0700
Committer: Niklas Q. Nielsen <[email protected]>
Committed: Tue Oct 13 11:23:10 2015 -0700

----------------------------------------------------------------------
 include/mesos/hook.hpp            | 11 +++++++++++
 src/examples/test_hook_module.cpp | 14 ++++++++++++++
 src/hook/manager.cpp              | 27 +++++++++++++++++++++++++++
 src/hook/manager.hpp              |  3 +++
 src/slave/slave.cpp               |  7 +++++++
 src/tests/hook_tests.cpp          |  7 ++++++-
 6 files changed, 68 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/8ecf8b0a/include/mesos/hook.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/hook.hpp b/include/mesos/hook.hpp
index 0c1042a..6d7fee8 100644
--- a/include/mesos/hook.hpp
+++ b/include/mesos/hook.hpp
@@ -22,6 +22,7 @@
 #include <map>
 #include <string>
 
+#include <mesos/attributes.hpp>
 #include <mesos/mesos.hpp>
 #include <mesos/resources.hpp>
 
@@ -133,6 +134,16 @@ public:
   {
     return None();
   }
+
+  // This hook is called from within the slave when it initializes. A module
+  // implementing the hook creates and returns an Attributes object with the
+  // new list of attributes for the slave before they are advertised to the
+  // master. These new attributes overwrite the previous ones in SlaveInfo.
+  virtual Result<Attributes> slaveAttributesDecorator(
+      const SlaveInfo& slaveInfo)
+  {
+    return None();
+  }
 };
 
 } // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/8ecf8b0a/src/examples/test_hook_module.cpp
----------------------------------------------------------------------
diff --git a/src/examples/test_hook_module.cpp 
b/src/examples/test_hook_module.cpp
index cd7c184..43d6cb9 100644
--- a/src/examples/test_hook_module.cpp
+++ b/src/examples/test_hook_module.cpp
@@ -134,6 +134,7 @@ public:
     return Nothing();
   }
 
+
   // TODO(nnielsen): Split hook tests into multiple modules to avoid
   // interference.
   virtual Result<Labels> slaveRunTaskLabelDecorator(
@@ -161,6 +162,7 @@ public:
     return labels;
   }
 
+
   // In this hook, we create a new environment variable "FOO" and set
   // it's value to "bar".
   virtual Result<Environment> slaveExecutorEnvironmentDecorator(
@@ -287,6 +289,18 @@ public:
 
     return resources;
   }
+
+
+  virtual Result<Attributes> slaveAttributesDecorator(
+      const SlaveInfo& slaveInfo)
+  {
+    LOG(INFO) << "Executing 'slaveAttributesDecorator' hook";
+
+    Attributes attributes = slaveInfo.attributes();
+    attributes.add(Attributes::parse("rack", "rack1"));
+
+    return attributes;
+  }
 };
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/8ecf8b0a/src/hook/manager.cpp
----------------------------------------------------------------------
diff --git a/src/hook/manager.cpp b/src/hook/manager.cpp
index 108bd46..d9e660a 100644
--- a/src/hook/manager.cpp
+++ b/src/hook/manager.cpp
@@ -300,5 +300,32 @@ Resources HookManager::slaveResourcesDecorator(
   }
 }
 
+Attributes HookManager::slaveAttributesDecorator(
+    const SlaveInfo& slaveInfo)
+{
+  // We need a mutable copy of the Attributes object. Each hook will see the
+  // changes made by previous hooks, so the order of execution matters. The
+  // execution order is currently unspecified since availableHooks uses a
+  // hashmap.
+  SlaveInfo slaveInfo_ = slaveInfo;
+
+  synchronized (mutex) {
+    foreachpair (const string& name, Hook* hook, availableHooks) {
+      const Result<Attributes> result =
+        hook->slaveAttributesDecorator(slaveInfo_);
+
+      // NOTE: Attributes remain unchanged if the hook returns None().
+      if (result.isSome()) {
+        slaveInfo_.mutable_attributes()->CopyFrom(result.get());
+      } else if (result.isError()) {
+        LOG(WARNING) << "Slave Attributes decorator hook failed for "
+                     << "module '" << name << "': " << result.error();
+      }
+    }
+
+    return slaveInfo_.attributes();
+  }
+}
+
 } // namespace internal {
 } // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/8ecf8b0a/src/hook/manager.hpp
----------------------------------------------------------------------
diff --git a/src/hook/manager.hpp b/src/hook/manager.hpp
index 3af1ff8..3ae8ff2 100644
--- a/src/hook/manager.hpp
+++ b/src/hook/manager.hpp
@@ -77,6 +77,9 @@ public:
 
   static Resources slaveResourcesDecorator(
       const SlaveInfo& slaveInfo);
+
+  static Attributes slaveAttributesDecorator(
+      const SlaveInfo& slaveInfo);
 };
 
 } // namespace internal {

http://git-wip-us.apache.org/repos/asf/mesos/blob/8ecf8b0a/src/slave/slave.cpp
----------------------------------------------------------------------
diff --git a/src/slave/slave.cpp b/src/slave/slave.cpp
index 6b25b49..cb2d715 100644
--- a/src/slave/slave.cpp
+++ b/src/slave/slave.cpp
@@ -391,6 +391,13 @@ void Slave::initialize()
   LOG(INFO) << "Slave resources: " << info.resources();
 
   info.mutable_attributes()->CopyFrom(attributes);
+  if (HookManager::hooksAvailable()) {
+    info.mutable_attributes()->CopyFrom(
+        HookManager::slaveAttributesDecorator(info));
+  }
+
+  LOG(INFO) << "Slave attributes: " << info.attributes();
+
   // Checkpointing of slaves is always enabled.
   info.set_checkpoint(true);
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/8ecf8b0a/src/tests/hook_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/hook_tests.cpp b/src/tests/hook_tests.cpp
index b35ce72..5a5d019 100644
--- a/src/tests/hook_tests.cpp
+++ b/src/tests/hook_tests.cpp
@@ -709,7 +709,7 @@ TEST_F(HookTest, ROOT_DOCKER_VerifySlavePreLaunchDockerHook)
 
 // Test that the changes made by the resources decorator hook are correctly
 // propagated to the resource offer.
-TEST_F(HookTest, VerifySlaveResourcesDecorator)
+TEST_F(HookTest, VerifySlaveResourcesAndAttributesDecorator)
 {
   Try<PID<Master>> master = StartMaster(CreateMasterFlags());
   ASSERT_SOME(master);
@@ -751,6 +751,11 @@ TEST_F(HookTest, VerifySlaveResourcesDecorator)
   // present.
   EXPECT_SOME(resources.mem());
 
+  // The test hook adds an attribute named "rack" with value "rack1".
+  Attributes attributes = offers.get()[0].attributes();
+  ASSERT_EQ(attributes.get(0).name(), "rack");
+  ASSERT_EQ(attributes.get(0).text().value(), "rack1");
+
   driver.stop();
   driver.join();
 

Reply via email to