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 2015/01/15 00:41:15 UTC
[1/4] mesos git commit: Added example hook module.
Repository: mesos
Updated Branches:
refs/heads/master 60007b9e7 -> 4e110d5d0
Added example hook module.
This module provides hooks for master label decorator, slave executor
environment decorator and slave remove executor. A couple of test
cases are also provided to verify the hooks from this module.
Review: https://reviews.apache.org/r/29496
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/4e110d5d
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/4e110d5d
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/4e110d5d
Branch: refs/heads/master
Commit: 4e110d5d0f0c6cd65b8b6e5edff30bb4c589a278
Parents: 0773763
Author: Kapil Arya <ka...@mesosphere.io>
Authored: Wed Jan 14 13:49:06 2015 -0800
Committer: Niklas Q. Nielsen <ni...@mesosphere.io>
Committed: Wed Jan 14 15:11:37 2015 -0800
----------------------------------------------------------------------
src/Makefile.am | 7 +
src/examples/test_hook_module.cpp | 138 +++++++++++++++++
src/tests/hook_tests.cpp | 272 +++++++++++++++++++++++++++++++++
src/tests/module.cpp | 21 +++
src/tests/module.hpp | 3 +-
5 files changed, 440 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index 100db39..256384e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1237,6 +1237,12 @@ libtestauthentication_la_SOURCES = examples/test_authentication_modules.cpp
libtestauthentication_la_CPPFLAGS = $(MESOS_CPPFLAGS)
libtestauthentication_la_LDFLAGS = -release $(PACKAGE_VERSION) -shared
+# Library containing test Hook module.
+lib_LTLIBRARIES += libtesthook.la
+libtesthook_la_SOURCES = examples/test_hook_module.cpp
+libtesthook_la_CPPFLAGS = $(MESOS_CPPFLAGS)
+libtesthook_la_LDFLAGS = -release $(PACKAGE_VERSION) -shared
+
mesos_tests_SOURCES = \
tests/attributes_tests.cpp \
tests/authentication_tests.cpp \
@@ -1260,6 +1266,7 @@ mesos_tests_SOURCES = \
tests/flags.cpp \
tests/gc_tests.cpp \
tests/hierarchical_allocator_tests.cpp \
+ tests/hook_tests.cpp \
tests/isolator_tests.cpp \
tests/log_tests.cpp \
tests/logging_tests.cpp \
http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/examples/test_hook_module.cpp
----------------------------------------------------------------------
diff --git a/src/examples/test_hook_module.cpp b/src/examples/test_hook_module.cpp
new file mode 100644
index 0000000..04fd43e
--- /dev/null
+++ b/src/examples/test_hook_module.cpp
@@ -0,0 +1,138 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+
+#include <mesos/mesos.hpp>
+#include <mesos/module.hpp>
+
+#include <stout/foreach.hpp>
+#include <stout/os.hpp>
+#include <stout/try.hpp>
+
+#include "hook/hook.hpp"
+#include "master/master.hpp"
+#include "module/hook.hpp"
+#include "slave/slave.hpp"
+
+using std::string;
+
+using namespace mesos;
+using namespace mesos::internal;
+
+// Must be kept in sync with variables of the same name in
+// tests/hook_tests.cpp.
+const char* testLabelKey = "MESOS_Test_Label";
+const char* testLabelValue = "ApacheMesos";
+const char* testEnvironmentVariableName = "MESOS_TEST_ENVIRONMENT_VARIABLE";
+
+class TestHook : public Hook
+{
+public:
+ virtual Result<Labels> masterLaunchTaskLabelDecorator(
+ const TaskInfo& taskInfo,
+ const FrameworkInfo& frameworkInfo,
+ const SlaveInfo& slaveInfo)
+ {
+ LOG(INFO) << "Executing 'masterLaunchTaskLabelDecorator' hook";
+
+ Labels labels;
+ Label *label = labels.add_labels();
+ label->set_key(testLabelKey);
+ label->set_value(testLabelValue);
+
+ return labels;
+ }
+
+
+ // In this hook, we create a temporary file and add its path to an
+ // environment variable. Later on, this environment variable is
+ // looked up by the removeExecutorHook to locate and delete this
+ // file.
+ virtual Result<Environment> slaveLaunchExecutorEnvironmentDecorator(
+ const ExecutorInfo& executorInfo,
+ const TaskInfo& taskInfo)
+ {
+ LOG(INFO) << "Executing 'slaveLaunchExecutorEnvironmentDecorator' hook";
+
+ // Find the label value for the label that was created in the
+ // label decorator hook above.
+ Option<string> labelValue;
+ foreach (const Label& label, taskInfo.labels().labels()) {
+ if (label.key() == testLabelKey) {
+ labelValue = label.value();
+ CHECK_EQ(labelValue.get(), testLabelValue);
+ }
+ }
+ CHECK_SOME(labelValue);
+
+ // Create a temporary file.
+ Try<string> file = os::mktemp();
+ CHECK_SOME(file);
+ CHECK_SOME(os::write(file.get(), labelValue.get()));
+
+ // Inject file path into command environment.
+ Environment environment;
+ Environment::Variable* variable = environment.add_variables();
+ variable->set_name(testEnvironmentVariableName);
+ variable->set_value(file.get());
+
+ return environment;
+ }
+
+
+ // This hook locates the file created by environment decorator hook
+ // and deletes it.
+ virtual Try<Nothing> slaveRemoveExecutorHook(
+ const FrameworkInfo& frameworkInfo,
+ const ExecutorInfo& executorInfo)
+ {
+ LOG(INFO) << "Executing 'slaveRemoveExecutorHook'";
+
+ foreach (const Environment::Variable& variable,
+ executorInfo.command().environment().variables()) {
+ if (variable.name() == testEnvironmentVariableName) {
+ string path = variable.value();
+ // The removeExecutor hook may be called multiple times; thus
+ // we ignore the subsequent calls.
+ if (os::isfile(path)) {
+ CHECK_SOME(os::rm(path));
+ }
+ break;
+ }
+ }
+ return Nothing();
+ }
+};
+
+
+static Hook* createHook(const Parameters& parameters)
+{
+ return new TestHook();
+}
+
+
+// Declares a Hook module named 'TestHook'.
+mesos::modules::Module<Hook> org_apache_mesos_TestHook(
+ MESOS_MODULE_API_VERSION,
+ MESOS_VERSION,
+ "Apache Mesos",
+ "modules@mesos.apache.org",
+ "Test Hook module.",
+ NULL,
+ createHook);
http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/hook_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/hook_tests.cpp b/src/tests/hook_tests.cpp
new file mode 100644
index 0000000..4e9a3a7
--- /dev/null
+++ b/src/tests/hook_tests.cpp
@@ -0,0 +1,272 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <mesos/module.hpp>
+
+#include <process/future.hpp>
+#include <process/gmock.hpp>
+#include <process/pid.hpp>
+
+#include <stout/option.hpp>
+#include <stout/os.hpp>
+#include <stout/try.hpp>
+
+#include "hook/manager.hpp"
+#include "module/manager.hpp"
+#include "master/flags.hpp"
+#include "master/master.hpp"
+#include "slave/flags.hpp"
+#include "slave/slave.hpp"
+
+#include "tests/containerizer.hpp"
+#include "tests/flags.hpp"
+#include "tests/mesos.hpp"
+
+using std::string;
+
+using namespace mesos;
+using namespace mesos::internal;
+using namespace mesos::internal::tests;
+using namespace mesos::modules;
+
+using mesos::internal::master::Master;
+using mesos::internal::slave::Slave;
+
+using process::Future;
+using process::PID;
+
+using std::string;
+using std::vector;
+
+using testing::_;
+using testing::Return;
+
+const char* HOOK_MODULE_LIBRARY_NAME = "testhook";
+const char* HOOK_MODULE_NAME = "org_apache_mesos_TestHook";
+
+// Must be kept in sync with variables of the same name in
+// examples/test_hook_module.cpp.
+const char* testLabelKey = "MESOS_Test_Label";
+const char* testLabelValue = "ApacheMesos";
+const char* testEnvironmentVariableName = "MESOS_TEST_ENVIRONMENT_VARIABLE";
+
+class HookTest : public MesosTest
+{
+protected:
+ // TODO(karya): Replace constructor/destructor with SetUp/TearDown.
+ // Currently, using SetUp/TearDown causes VerifySlave* test to
+ // fail with a duplicate slave id message. However, everything
+ // seems normal when using this construction/destructor combo.
+ HookTest()
+ {
+ // Install hooks.
+ EXPECT_SOME(HookManager::initialize(HOOK_MODULE_NAME));
+ }
+
+ ~HookTest()
+ {
+ // Unload the hooks so a subsequent install may succeed.
+ EXPECT_SOME(HookManager::unload(HOOK_MODULE_NAME));
+ }
+};
+
+
+// Test varioud hook install/uninstall mechanisms.
+TEST_F(HookTest, HookLoading)
+{
+ // Installing unknown hooks should fail.
+ EXPECT_ERROR(HookManager::initialize("Unknown Hook"));
+
+ // Uninstalling an unknown hook should fail.
+ EXPECT_ERROR(HookManager::unload("Unknown Hook"));
+
+ // Installing an already installed hook should fail.
+ EXPECT_ERROR(HookManager::initialize(HOOK_MODULE_NAME));
+
+ // Uninstalling a hook should succeed.
+ EXPECT_SOME(HookManager::unload(HOOK_MODULE_NAME));
+
+ // Uninstalling an already uninstalled hook should fail.
+ EXPECT_ERROR(HookManager::unload(HOOK_MODULE_NAME));
+ // This is needed to allow the tear-down to succeed.
+ EXPECT_SOME(HookManager::initialize(HOOK_MODULE_NAME));
+}
+
+
+// Test that the label decorator hook hangs a new label off the
+// taskinfo message during master launch task.
+TEST_F(HookTest, VerifyMasterLaunchTaskHook)
+{
+ Try<PID<Master> > master = StartMaster(CreateMasterFlags());
+ ASSERT_SOME(master);
+
+ TestContainerizer containerizer;
+
+ StandaloneMasterDetector detector(master.get());
+
+ // Start a mock slave since we aren't testing the slave hooks yet.
+ MockSlave slave(CreateSlaveFlags(), &detector, &containerizer);
+ process::spawn(slave);
+
+ MockScheduler sched;
+ MesosSchedulerDriver driver(
+ &sched, DEFAULT_FRAMEWORK_INFO, master.get(), DEFAULT_CREDENTIAL);
+
+ EXPECT_CALL(sched, registered(&driver, _, _))
+ .Times(1);
+
+ Future<vector<Offer> > offers;
+ EXPECT_CALL(sched, resourceOffers(&driver, _))
+ .WillOnce(FutureArg<1>(&offers))
+ .WillRepeatedly(Return()); // Ignore subsequent offers.
+
+ driver.start();
+
+ AWAIT_READY(offers);
+ EXPECT_NE(0u, offers.get().size());
+
+ CommandInfo command;
+ command.set_value("sleep 10");
+
+ // Launch a task with the command executor.
+ TaskInfo task;
+ task.set_name("");
+ task.mutable_task_id()->set_value("1");
+ task.mutable_slave_id()->MergeFrom(offers.get()[0].slave_id());
+ task.mutable_resources()->MergeFrom(offers.get()[0].resources());
+ task.mutable_command()->MergeFrom(command);
+
+ vector<TaskInfo> tasks;
+ tasks.push_back(task);
+
+ Future<TaskInfo> taskInfo;
+ EXPECT_CALL(slave, runTask(_, _, _, _, _))
+ .Times(1)
+ .WillOnce(FutureArg<4>(&taskInfo));
+
+ driver.launchTasks(offers.get()[0].id(), tasks);
+
+ AWAIT_READY(taskInfo);
+
+ // At launchTasks, the label decorator hook inside should have been
+ // executed and we should see the labels now.
+ Option<string> labelValue;
+ foreach (const Label& label, taskInfo.get().labels().labels()) {
+ if (label.key() == testLabelKey) {
+ labelValue = label.value();
+ }
+ }
+ EXPECT_SOME_EQ(testLabelValue, labelValue);
+
+ driver.stop();
+ driver.join();
+
+ process::terminate(slave);
+ process::wait(slave);
+
+ Shutdown(); // Must shutdown before 'containerizer' gets deallocated.
+}
+
+
+// Test executor environment decorator hook and remove executor hook
+// for slave. We expect the environment-decorator hook to create a
+// temporary file and the remove-executor hook to delete that file.
+TEST_F(HookTest, VerifySlaveLaunchExecutorHook)
+{
+ master::Flags masterFlags = CreateMasterFlags();
+
+ Try<PID<Master> > master = StartMaster(masterFlags);
+ ASSERT_SOME(master);
+
+ slave::Flags slaveFlags = CreateSlaveFlags();
+
+ MockExecutor exec(DEFAULT_EXECUTOR_ID);
+
+ TestContainerizer containerizer(&exec);
+
+ Try<PID<Slave> > slave = StartSlave(&containerizer);
+ ASSERT_SOME(slave);
+
+ MockScheduler sched;
+ MesosSchedulerDriver driver(
+ &sched, DEFAULT_FRAMEWORK_INFO, master.get(), DEFAULT_CREDENTIAL);
+
+ EXPECT_CALL(sched, registered(&driver, _, _))
+ .Times(1);
+
+ Future<vector<Offer> > offers;
+ EXPECT_CALL(sched, resourceOffers(&driver, _))
+ .WillOnce(FutureArg<1>(&offers))
+ .WillRepeatedly(Return()); // Ignore subsequent offers.
+
+ driver.start();
+
+ AWAIT_READY(offers);
+ EXPECT_NE(0u, offers.get().size());
+
+ // Launch a task with the command executor.
+ TaskInfo task;
+ task.set_name("");
+ task.mutable_task_id()->set_value("1");
+ task.mutable_slave_id()->MergeFrom(offers.get()[0].slave_id());
+ task.mutable_resources()->MergeFrom(offers.get()[0].resources());
+ task.mutable_executor()->MergeFrom(DEFAULT_EXECUTOR_INFO);
+
+ vector<TaskInfo> tasks;
+ tasks.push_back(task);
+
+ EXPECT_CALL(exec, launchTask(_, _));
+
+ Future<ExecutorInfo> executorInfo;
+ EXPECT_CALL(exec, registered(_, _, _, _))
+ .Times(1)
+ .WillOnce(FutureArg<1>(&executorInfo));
+
+ driver.launchTasks(offers.get()[0].id(), tasks);
+
+ AWAIT_READY(executorInfo);
+
+ // At launchTasks, the label decorator hook inside should have been
+ // executed and we should see the labels now.
+ // Further, the environment decorator hook should also have been
+ // executed. In that hook, we create a temp file and set the path
+ // as the value of the environment variable.
+ // Here we verify that the environment variable is present and the
+ // file is present on the disk.
+ Option<string> path;
+ foreach (const Environment::Variable& variable,
+ executorInfo.get().command().environment().variables()) {
+ if (variable.name() == testEnvironmentVariableName) {
+ path = variable.value();
+ break;
+ }
+ }
+
+ EXPECT_SOME(path);
+ // The file must have been create by the environment decorator hook.
+ EXPECT_TRUE(os::isfile(path.get()));
+
+ driver.stop();
+ driver.join();
+
+ Shutdown(); // Must shutdown before 'containerizer' gets deallocated.
+
+ // The removeExecutor hook in the test module deletes the temp file.
+ // Verify that the file is not present.
+ EXPECT_FALSE(os::isfile(path.get()));
+}
http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/module.cpp
----------------------------------------------------------------------
diff --git a/src/tests/module.cpp b/src/tests/module.cpp
index 6cec1cb..e6dbf94 100644
--- a/src/tests/module.cpp
+++ b/src/tests/module.cpp
@@ -93,6 +93,24 @@ static void addAuthenticationModules(Modules& modules)
}
+static void addHookModules(Modules& modules)
+{
+ const string libraryPath = path::join(
+ tests::flags.build_dir,
+ "src",
+ ".libs",
+ os::libraries::expandName("testhook"));
+
+ // Now add our test hook module.
+ Modules::Library* library = modules.add_libraries();
+ library->set_file(libraryPath);
+
+ // To add a new module from this library, create a new ModuleID enum
+ // and tie it with a module name.
+ addModule(library, TestHook, "org_apache_mesos_TestHook");
+}
+
+
Try<Nothing> tests::initModules(const Option<Modules>& modules)
{
// First get the user provided modules.
@@ -107,6 +125,9 @@ Try<Nothing> tests::initModules(const Option<Modules>& modules)
// Add authentication modules from testauthentication library.
addAuthenticationModules(mergedModules);
+ // Add hook modules from testhook library.
+ addHookModules(mergedModules);
+
return ModuleManager::load(mergedModules);
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/module.hpp
----------------------------------------------------------------------
diff --git a/src/tests/module.hpp b/src/tests/module.hpp
index bc1a37d..21e8fc4 100644
--- a/src/tests/module.hpp
+++ b/src/tests/module.hpp
@@ -42,7 +42,8 @@ enum ModuleID
TestMemIsolator,
TestCpuIsolator,
TestCRAMMD5Authenticatee,
- TestCRAMMD5Authenticator
+ TestCRAMMD5Authenticator,
+ TestHook
};
[2/4] mesos git commit: Added environment decorator for slave.
Posted by nn...@apache.org.
Added environment decorator for slave.
Review: https://reviews.apache.org/r/28656
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/f6bcb085
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/f6bcb085
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/f6bcb085
Branch: refs/heads/master
Commit: f6bcb085b41e374f01b9453b6fe0816f3cd1320a
Parents: 1fee1ec
Author: Kapil Arya <ka...@mesosphere.io>
Authored: Wed Jan 14 13:46:58 2015 -0800
Committer: Niklas Q. Nielsen <ni...@mesosphere.io>
Committed: Wed Jan 14 15:11:37 2015 -0800
----------------------------------------------------------------------
src/hook/hook.hpp | 14 ++++++++++++++
src/hook/manager.cpp | 22 ++++++++++++++++++++++
src/hook/manager.hpp | 4 ++++
src/slave/flags.hpp | 6 ++++++
src/slave/main.cpp | 12 +++++++++++-
src/slave/slave.cpp | 24 +++++++++++++++++++++++-
6 files changed, 80 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/f6bcb085/src/hook/hook.hpp
----------------------------------------------------------------------
diff --git a/src/hook/hook.hpp b/src/hook/hook.hpp
index ca7f867..21f319b 100644
--- a/src/hook/hook.hpp
+++ b/src/hook/hook.hpp
@@ -21,9 +21,11 @@
#include <mesos/mesos.hpp>
+#include <stout/none.hpp>
#include <stout/result.hpp>
#include "master/master.hpp"
+#include "slave/slave.hpp"
namespace mesos {
namespace internal {
@@ -44,6 +46,18 @@ public:
{
return None();
}
+
+ // This environment decorator hook is called from within slave
+ // when launching a new executor. A module implementing the hook
+ // creates and returns a set of environment variables. These
+ // environment variables are then merged into the executorInfo
+ // and become part of the executor's environment.
+ virtual Result<Environment> slaveLaunchExecutorEnvironmentDecorator(
+ const ExecutorInfo& executorInfo,
+ const TaskInfo& taskInfo)
+ {
+ return None();
+ }
};
} // namespace internal {
http://git-wip-us.apache.org/repos/asf/mesos/blob/f6bcb085/src/hook/manager.cpp
----------------------------------------------------------------------
diff --git a/src/hook/manager.cpp b/src/hook/manager.cpp
index c1069b4..cadbf87 100644
--- a/src/hook/manager.cpp
+++ b/src/hook/manager.cpp
@@ -110,5 +110,27 @@ Labels HookManager::masterLaunchTaskLabelDecorator(
return labels;
}
+
+Environment HookManager::slaveLaunchExecutorEnvironmentDecorator(
+ const ExecutorInfo& executorInfo,
+ const TaskInfo& taskInfo)
+{
+ Lock lock(&mutex);
+ Environment environment;
+
+ foreachpair (const string& name, Hook* hook, availableHooks) {
+ const Result<Environment>& result =
+ hook->slaveLaunchExecutorEnvironmentDecorator(executorInfo, taskInfo);
+ if (result.isSome()) {
+ environment.MergeFrom(result.get());
+ } else if (result.isError()) {
+ LOG(WARNING) << "Slave environment decorator hook failed for module '"
+ << name << "': " << result.error();
+ }
+ }
+
+ return environment;
+}
+
} // namespace internal {
} // namespace mesos {
http://git-wip-us.apache.org/repos/asf/mesos/blob/f6bcb085/src/hook/manager.hpp
----------------------------------------------------------------------
diff --git a/src/hook/manager.hpp b/src/hook/manager.hpp
index c115911..007beb5 100644
--- a/src/hook/manager.hpp
+++ b/src/hook/manager.hpp
@@ -44,6 +44,10 @@ public:
const TaskInfo& taskInfo,
const FrameworkInfo& frameworkInfo,
const SlaveInfo& slaveInfo);
+
+ static Environment slaveLaunchExecutorEnvironmentDecorator(
+ const ExecutorInfo& executorInfo,
+ const TaskInfo& taskInfo);
};
} // namespace internal {
http://git-wip-us.apache.org/repos/asf/mesos/blob/f6bcb085/src/slave/flags.hpp
----------------------------------------------------------------------
diff --git a/src/slave/flags.hpp b/src/slave/flags.hpp
index f1b8dfb..a09ac73 100644
--- a/src/slave/flags.hpp
+++ b/src/slave/flags.hpp
@@ -434,6 +434,11 @@ public:
"master. Use the default '" + DEFAULT_AUTHENTICATEE + "', or\n"
"load an alternate authenticatee module using --modules.",
DEFAULT_AUTHENTICATEE);
+
+ add(&Flags::hooks,
+ "hooks",
+ "A comma separated list of hook modules to be\n"
+ "installed inside the slave.");
}
bool version;
@@ -488,6 +493,7 @@ public:
#endif
Option<Modules> modules;
std::string authenticatee;
+ Option<std::string> hooks;
};
} // namespace slave {
http://git-wip-us.apache.org/repos/asf/mesos/blob/f6bcb085/src/slave/main.cpp
----------------------------------------------------------------------
diff --git a/src/slave/main.cpp b/src/slave/main.cpp
index 2ff2b0d..42e46c5 100644
--- a/src/slave/main.cpp
+++ b/src/slave/main.cpp
@@ -29,10 +29,12 @@
#include "common/build.hpp"
-#include "master/detector.hpp"
+#include "hook/manager.hpp"
#include "logging/logging.hpp"
+#include "master/detector.hpp"
+
#include "messages/messages.hpp"
#include "module/manager.hpp"
@@ -128,6 +130,14 @@ int main(int argc, char** argv)
}
}
+ // Initialize hooks.
+ if (flags.hooks.isSome()) {
+ Try<Nothing> result = HookManager::initialize(flags.hooks.get());
+ if (result.isError()) {
+ EXIT(1) << "Error installing hooks: " << result.error();
+ }
+ }
+
// Initialize libprocess.
if (ip.isSome()) {
os::setenv("LIBPROCESS_IP", ip.get());
http://git-wip-us.apache.org/repos/asf/mesos/blob/f6bcb085/src/slave/slave.cpp
----------------------------------------------------------------------
diff --git a/src/slave/slave.cpp b/src/slave/slave.cpp
index 910b335..e20bb78 100644
--- a/src/slave/slave.cpp
+++ b/src/slave/slave.cpp
@@ -69,6 +69,8 @@
#include "credentials/credentials.hpp"
+#include "hook/manager.hpp"
+
#include "logging/logging.hpp"
#include "module/authenticatee.hpp"
@@ -3862,11 +3864,31 @@ Framework::~Framework()
}
+// Environment decorator for executor.
+static ExecutorInfo decorateExecutorEnvironment(
+ ExecutorInfo executorInfo,
+ const TaskInfo& taskInfo)
+{
+ // Merge environment variables retrieved from label-decorator hooks.
+ executorInfo.mutable_command()->mutable_environment()->MergeFrom(
+ HookManager::slaveLaunchExecutorEnvironmentDecorator(
+ executorInfo,
+ taskInfo));
+
+ return executorInfo;
+}
+
+
// Create and launch an executor.
Executor* Framework::launchExecutor(
- const ExecutorInfo& executorInfo,
+ const ExecutorInfo& executorInfo__,
const TaskInfo& taskInfo)
{
+ // Merge environment variables retrieved from environment-decorator
+ // hooks.
+ const ExecutorInfo& executorInfo =
+ decorateExecutorEnvironment(executorInfo__, taskInfo);
+
// Generate an ID for the executor's container.
// TODO(idownes) This should be done by the containerizer but we need the
// ContainerID to create the executor's directory and to set up monitoring.
[4/4] mesos git commit: Introduced Mesos Hooks abstraction.
Posted by nn...@apache.org.
Introduced Mesos Hooks abstraction.
Hooks are implemented as Mesos modules. The first hook is a task
label-decorator that is called during the task launch sequence in
Mesos master.
This hook allows hook modules to add additional labels to the incoming
TaskInfo object. The labels are then read on the slave/executor side
which may then act upon them.
Review: https://reviews.apache.org/r/28655
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/1fee1ec0
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/1fee1ec0
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/1fee1ec0
Branch: refs/heads/master
Commit: 1fee1ec04d1741d96deff12e820fb81608c9e693
Parents: 60007b9
Author: Kapil Arya <ka...@mesosphere.io>
Authored: Wed Jan 14 13:46:33 2015 -0800
Committer: Niklas Q. Nielsen <ni...@mesosphere.io>
Committed: Wed Jan 14 15:11:37 2015 -0800
----------------------------------------------------------------------
src/Makefile.am | 4 ++
src/hook/hook.hpp | 52 ++++++++++++++++++++
src/hook/manager.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++
src/hook/manager.hpp | 52 ++++++++++++++++++++
src/master/flags.hpp | 6 +++
src/master/main.cpp | 10 ++++
src/master/master.cpp | 9 ++++
src/module/hook.hpp | 65 +++++++++++++++++++++++++
src/module/manager.cpp | 1 +
9 files changed, 313 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/1fee1ec0/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index fc0c322..100db39 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -281,6 +281,7 @@ libmesos_no_3rdparty_la_SOURCES = \
docker/docker.cpp \
exec/exec.cpp \
files/files.cpp \
+ hook/manager.cpp \
local/local.cpp \
logging/logging.cpp \
master/contender.cpp \
@@ -439,6 +440,8 @@ libmesos_no_3rdparty_la_SOURCES += \
examples/test_module.hpp \
files/files.hpp \
hdfs/hdfs.hpp \
+ hook/hook.hpp \
+ hook/manager.hpp \
linux/cgroups.hpp \
linux/fs.hpp \
linux/ns.hpp \
@@ -462,6 +465,7 @@ libmesos_no_3rdparty_la_SOURCES += \
messages/messages.hpp \
module/authenticatee.hpp \
module/authenticator.hpp \
+ module/hook.hpp \
module/isolator.hpp \
module/manager.hpp \
sched/constants.hpp \
http://git-wip-us.apache.org/repos/asf/mesos/blob/1fee1ec0/src/hook/hook.hpp
----------------------------------------------------------------------
diff --git a/src/hook/hook.hpp b/src/hook/hook.hpp
new file mode 100644
index 0000000..ca7f867
--- /dev/null
+++ b/src/hook/hook.hpp
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __HOOK_HOOK_HPP__
+#define __HOOK_HOOK_HPP__
+
+#include <mesos/mesos.hpp>
+
+#include <stout/result.hpp>
+
+#include "master/master.hpp"
+
+namespace mesos {
+namespace internal {
+
+class Hook
+{
+public:
+ virtual ~Hook() {};
+
+ // This label decorator hook is called from within master during
+ // the launchTask routine. A module implementing the hook creates
+ // and returns a set of labels. These labels are then merged with
+ // the task labels and passed on to the slave/executor.
+ virtual Result<Labels> masterLaunchTaskLabelDecorator(
+ const TaskInfo& taskInfo,
+ const FrameworkInfo& frameworkInfo,
+ const SlaveInfo& slaveInfo)
+ {
+ return None();
+ }
+};
+
+} // namespace internal {
+} // namespace mesos {
+
+#endif // __HOOK_HOOK_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/1fee1ec0/src/hook/manager.cpp
----------------------------------------------------------------------
diff --git a/src/hook/manager.cpp b/src/hook/manager.cpp
new file mode 100644
index 0000000..c1069b4
--- /dev/null
+++ b/src/hook/manager.cpp
@@ -0,0 +1,114 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <pthread.h>
+
+#include <string>
+#include <vector>
+
+#include <stout/check.hpp>
+#include <stout/foreach.hpp>
+#include <stout/nothing.hpp>
+#include <stout/strings.hpp>
+#include <stout/try.hpp>
+
+#include "common/lock.hpp"
+#include "hook/hook.hpp"
+#include "hook/manager.hpp"
+#include "module/hook.hpp"
+#include "module/manager.hpp"
+
+using std::string;
+using std::vector;
+
+using mesos::modules::ModuleManager;
+
+namespace mesos {
+namespace internal {
+
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+static hashmap<string, Hook*> availableHooks;
+
+
+Try<Nothing> HookManager::initialize(const string& hookList)
+{
+ Lock lock(&mutex);
+
+ const vector<string> hooks = strings::split(hookList, ",");
+ foreach (const string& hook, hooks) {
+ if (availableHooks.contains(hook)) {
+ return Error("Hook module '" + hook + "' already loaded");
+ }
+
+ if (!ModuleManager::contains<Hook>(hook)) {
+ return Error("No hook module named '" + hook + "' available");
+ }
+
+ // Let's create an instance of the hook module.
+ Try<Hook*> module = ModuleManager::create<Hook>(hook);
+ if (module.isError()) {
+ return Error(
+ "Failed to instantiate hook module '" + hook + "': " +
+ module.error());
+ }
+
+ // Add the hook module to the list of available hooks.
+ availableHooks[hook] = module.get();
+ }
+ return Nothing();
+}
+
+
+Try<Nothing> HookManager::unload(const std::string& hookName)
+{
+ Lock lock(&mutex);
+ if (!availableHooks.contains(hookName)) {
+ return Error(
+ "Error unloading hook module '" + hookName + "': module not loaded");
+ }
+
+ // Now remove the hook from the list of available hooks.
+ availableHooks.erase(hookName);
+ return Nothing();
+}
+
+
+Labels HookManager::masterLaunchTaskLabelDecorator(
+ const TaskInfo& taskInfo,
+ const FrameworkInfo& frameworkInfo,
+ const SlaveInfo& slaveInfo)
+{
+ Lock lock(&mutex);
+ Labels labels;
+
+ foreachpair (const string& name, Hook* hook, availableHooks) {
+ const Result<Labels>& result =
+ hook->masterLaunchTaskLabelDecorator(taskInfo, frameworkInfo, slaveInfo);
+ if (result.isSome()) {
+ labels.MergeFrom(result.get());
+ } else if (result.isError()) {
+ LOG(WARNING) << "Master label decorator hook failed for module '"
+ << name << "': " << result.error();
+ }
+ }
+
+ return labels;
+}
+
+} // namespace internal {
+} // namespace mesos {
http://git-wip-us.apache.org/repos/asf/mesos/blob/1fee1ec0/src/hook/manager.hpp
----------------------------------------------------------------------
diff --git a/src/hook/manager.hpp b/src/hook/manager.hpp
new file mode 100644
index 0000000..c115911
--- /dev/null
+++ b/src/hook/manager.hpp
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __HOOK_MANAGER_HPP__
+#define __HOOK_MANAGER_HPP__
+
+#include <string>
+
+#include <mesos/mesos.hpp>
+
+#include "hook/hook.hpp"
+#include "master/master.hpp"
+#include "messages/messages.hpp"
+
+namespace mesos {
+namespace internal {
+
+class HookManager
+{
+public:
+ static Try<Nothing> initialize(const std::string& hookList);
+
+ // Exposed just for testing so that we can unload a given
+ // hook and remove it from the list of available hooks.
+ static Try<Nothing> unload(const std::string& hookName);
+
+
+ static Labels masterLaunchTaskLabelDecorator(
+ const TaskInfo& taskInfo,
+ const FrameworkInfo& frameworkInfo,
+ const SlaveInfo& slaveInfo);
+};
+
+} // namespace internal {
+} // namespace mesos {
+
+#endif // __HOOK_MANAGER_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/1fee1ec0/src/master/flags.hpp
----------------------------------------------------------------------
diff --git a/src/master/flags.hpp b/src/master/flags.hpp
index f5c8d2a..6c18a1a 100644
--- a/src/master/flags.hpp
+++ b/src/master/flags.hpp
@@ -357,6 +357,11 @@ public:
"and/or slaves. Use the default '" + DEFAULT_AUTHENTICATOR + "', or\n"
"load an alternate authenticator module using --modules.",
DEFAULT_AUTHENTICATOR);
+
+ add(&Flags::hooks,
+ "hooks",
+ "A comma separated list of hook modules to be\n"
+ "installed inside master.");
}
bool version;
@@ -388,6 +393,7 @@ public:
Option<Duration> offer_timeout;
Option<Modules> modules;
std::string authenticators;
+ Option<std::string> hooks;
#ifdef WITH_NETWORK_ISOLATOR
Option<size_t> max_executors_per_slave;
http://git-wip-us.apache.org/repos/asf/mesos/blob/1fee1ec0/src/master/main.cpp
----------------------------------------------------------------------
diff --git a/src/master/main.cpp b/src/master/main.cpp
index 193d53f..e5e76ce 100644
--- a/src/master/main.cpp
+++ b/src/master/main.cpp
@@ -41,6 +41,8 @@
#include "common/build.hpp"
#include "common/protobuf_utils.hpp"
+#include "hook/manager.hpp"
+
#include "logging/flags.hpp"
#include "logging/logging.hpp"
@@ -153,6 +155,14 @@ int main(int argc, char** argv)
}
}
+ // Initialize hooks.
+ if (flags.hooks.isSome()) {
+ Try<Nothing> result = HookManager::initialize(flags.hooks.get());
+ if (result.isError()) {
+ EXIT(1) << "Error installing hooks: " << result.error();
+ }
+ }
+
// Initialize libprocess.
if (ip.isSome()) {
os::setenv("LIBPROCESS_IP", ip.get());
http://git-wip-us.apache.org/repos/asf/mesos/blob/1fee1ec0/src/master/master.cpp
----------------------------------------------------------------------
diff --git a/src/master/master.cpp b/src/master/master.cpp
index 78d5f47..e9dcca3 100644
--- a/src/master/master.cpp
+++ b/src/master/master.cpp
@@ -65,6 +65,8 @@
#include "credentials/credentials.hpp"
+#include "hook/manager.hpp"
+
#include "logging/flags.hpp"
#include "logging/logging.hpp"
@@ -2903,6 +2905,13 @@ void Master::_launchTasks(
message.set_pid(framework->pid);
message.mutable_task()->MergeFrom(task);
+ // Merge labels retrieved from label-decorator hooks.
+ message.mutable_task()->mutable_labels()->MergeFrom(
+ HookManager::masterLaunchTaskLabelDecorator(
+ task,
+ framework->info,
+ slave->info));
+
send(slave->pid, message);
}
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/1fee1ec0/src/module/hook.hpp
----------------------------------------------------------------------
diff --git a/src/module/hook.hpp b/src/module/hook.hpp
new file mode 100644
index 0000000..a7c51f2
--- /dev/null
+++ b/src/module/hook.hpp
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MODULE_HOOK_HPP__
+#define __MODULE_HOOK_HPP__
+
+#include <mesos/mesos.hpp>
+#include <mesos/module.hpp>
+
+#include "hook/hook.hpp"
+
+namespace mesos {
+namespace modules {
+
+template<>
+inline const char* kind<mesos::internal::Hook>()
+{
+ return "Hook";
+}
+
+
+template <>
+struct Module<mesos::internal::Hook> : ModuleBase
+{
+ Module(
+ const char* _moduleApiVersion,
+ const char* _mesosVersion,
+ const char* _authorName,
+ const char* _authorEmail,
+ const char* _description,
+ bool (*_compatible)(),
+ mesos::internal::Hook* (*_create)(const Parameters& parameters))
+ : ModuleBase(
+ _moduleApiVersion,
+ _mesosVersion,
+ mesos::modules::kind<mesos::internal::Hook>(),
+ _authorName,
+ _authorEmail,
+ _description,
+ _compatible),
+ create(_create)
+ { }
+
+ mesos::internal::Hook* (*create)(const Parameters& parameters);
+};
+
+} // namespace modules {
+} // namespace mesos {
+
+#endif // __MODULE_HOOK_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/1fee1ec0/src/module/manager.cpp
----------------------------------------------------------------------
diff --git a/src/module/manager.cpp b/src/module/manager.cpp
index 5e779e4..5abea26 100644
--- a/src/module/manager.cpp
+++ b/src/module/manager.cpp
@@ -59,6 +59,7 @@ void ModuleManager::initialize()
kindToVersion["Authenticatee"] = MESOS_VERSION;
kindToVersion["Authenticator"] = MESOS_VERSION;
+ kindToVersion["Hook"] = MESOS_VERSION;
kindToVersion["Isolator"] = MESOS_VERSION;
kindToVersion["TestModule"] = MESOS_VERSION;
[3/4] mesos git commit: Added remove-executor hook for slave.
Posted by nn...@apache.org.
Added remove-executor hook for slave.
Review: https://reviews.apache.org/r/28875
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/0773763f
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/0773763f
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/0773763f
Branch: refs/heads/master
Commit: 0773763f74054b56c42942d66f57da60c4c3dd63
Parents: f6bcb08
Author: Kapil Arya <ka...@mesosphere.io>
Authored: Wed Jan 14 13:47:13 2015 -0800
Committer: Niklas Q. Nielsen <ni...@mesosphere.io>
Committed: Wed Jan 14 15:11:37 2015 -0800
----------------------------------------------------------------------
src/hook/hook.hpp | 10 ++++++++++
src/hook/manager.cpp | 15 +++++++++++++++
src/hook/manager.hpp | 4 ++++
src/slave/slave.cpp | 2 ++
4 files changed, 31 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/0773763f/src/hook/hook.hpp
----------------------------------------------------------------------
diff --git a/src/hook/hook.hpp b/src/hook/hook.hpp
index 21f319b..9401e03 100644
--- a/src/hook/hook.hpp
+++ b/src/hook/hook.hpp
@@ -58,6 +58,16 @@ public:
{
return None();
}
+
+ // This hook is called from within slave when an executor is being
+ // removed. A typical module implementing the hook will perform
+ // some cleanup as required.
+ virtual Try<Nothing> slaveRemoveExecutorHook(
+ const FrameworkInfo& frameworkInfo,
+ const ExecutorInfo& executorInfo)
+ {
+ return Nothing();
+ }
};
} // namespace internal {
http://git-wip-us.apache.org/repos/asf/mesos/blob/0773763f/src/hook/manager.cpp
----------------------------------------------------------------------
diff --git a/src/hook/manager.cpp b/src/hook/manager.cpp
index cadbf87..6bf1ef7 100644
--- a/src/hook/manager.cpp
+++ b/src/hook/manager.cpp
@@ -132,5 +132,20 @@ Environment HookManager::slaveLaunchExecutorEnvironmentDecorator(
return environment;
}
+
+void HookManager::slaveRemoveExecutorHook(
+ const FrameworkInfo& frameworkInfo,
+ const ExecutorInfo& executorInfo)
+{
+ foreachpair (const string& name, Hook* hook, availableHooks) {
+ const Try<Nothing>& result =
+ hook->slaveRemoveExecutorHook(frameworkInfo, executorInfo);
+ if (result.isError()) {
+ LOG(WARNING) << "Slave remove executor hook failed for module '"
+ << name << "': " << result.error();
+ }
+ }
+}
+
} // namespace internal {
} // namespace mesos {
http://git-wip-us.apache.org/repos/asf/mesos/blob/0773763f/src/hook/manager.hpp
----------------------------------------------------------------------
diff --git a/src/hook/manager.hpp b/src/hook/manager.hpp
index 007beb5..aadc17a 100644
--- a/src/hook/manager.hpp
+++ b/src/hook/manager.hpp
@@ -48,6 +48,10 @@ public:
static Environment slaveLaunchExecutorEnvironmentDecorator(
const ExecutorInfo& executorInfo,
const TaskInfo& taskInfo);
+
+ static void slaveRemoveExecutorHook(
+ const FrameworkInfo& frameworkInfo,
+ const ExecutorInfo& executorInfo);
};
} // namespace internal {
http://git-wip-us.apache.org/repos/asf/mesos/blob/0773763f/src/slave/slave.cpp
----------------------------------------------------------------------
diff --git a/src/slave/slave.cpp b/src/slave/slave.cpp
index e20bb78..fca83b3 100644
--- a/src/slave/slave.cpp
+++ b/src/slave/slave.cpp
@@ -3123,6 +3123,8 @@ void Slave::removeExecutor(Framework* framework, Executor* executor)
}
}
+ HookManager::slaveRemoveExecutorHook(framework->info, executor->info);
+
framework->destroyExecutor(executor->id);
}
Re: [1/4] mesos git commit: Added example hook module.
Posted by Michael Park <mc...@gmail.com>.
Hey Vinod,
I've closed https://issues.apache.org/jira/browse/MESOS-2227 as a duplicate
of https://issues.apache.org/jira/browse/MESOS-2049 and the review request
is available at: https://reviews.apache.org/r/29948.
Thanks,
MPark
On 15 January 2015 at 15:51, Niklas Nielsen <ni...@mesosphere.io> wrote:
> Sure thing - apologize the inconvenience.
>
> Niklas
>
> On 15 January 2015 at 12:46, Vinod Kone <vi...@apache.org> wrote:
>
> > Hey Niklas. One of the hook tests is continously failing on our internal
> > CI. I've filed https://issues.apache.org/jira/browse/MESOS-2226. Can you
> > take a look?
> >
> > Also, https://issues.apache.org/jira/browse/MESOS-2227
> >
> > On Wed, Jan 14, 2015 at 3:41 PM, <nn...@apache.org> wrote:
> >
> > > Repository: mesos
> > > Updated Branches:
> > > refs/heads/master 60007b9e7 -> 4e110d5d0
> > >
> > >
> > > Added example hook module.
> > >
> > > This module provides hooks for master label decorator, slave executor
> > > environment decorator and slave remove executor. A couple of test
> > > cases are also provided to verify the hooks from this module.
> > >
> > > Review: https://reviews.apache.org/r/29496
> > >
> > >
> > > Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
> > > Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/4e110d5d
> > > Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/4e110d5d
> > > Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/4e110d5d
> > >
> > > Branch: refs/heads/master
> > > Commit: 4e110d5d0f0c6cd65b8b6e5edff30bb4c589a278
> > > Parents: 0773763
> > > Author: Kapil Arya <ka...@mesosphere.io>
> > > Authored: Wed Jan 14 13:49:06 2015 -0800
> > > Committer: Niklas Q. Nielsen <ni...@mesosphere.io>
> > > Committed: Wed Jan 14 15:11:37 2015 -0800
> > >
> > > ----------------------------------------------------------------------
> > > src/Makefile.am | 7 +
> > > src/examples/test_hook_module.cpp | 138 +++++++++++++++++
> > > src/tests/hook_tests.cpp | 272
> > +++++++++++++++++++++++++++++++++
> > > src/tests/module.cpp | 21 +++
> > > src/tests/module.hpp | 3 +-
> > > 5 files changed, 440 insertions(+), 1 deletion(-)
> > > ----------------------------------------------------------------------
> > >
> > >
> > >
> >
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/Makefile.am
> > > ----------------------------------------------------------------------
> > > diff --git a/src/Makefile.am b/src/Makefile.am
> > > index 100db39..256384e 100644
> > > --- a/src/Makefile.am
> > > +++ b/src/Makefile.am
> > > @@ -1237,6 +1237,12 @@ libtestauthentication_la_SOURCES =
> > > examples/test_authentication_modules.cpp
> > > libtestauthentication_la_CPPFLAGS = $(MESOS_CPPFLAGS)
> > > libtestauthentication_la_LDFLAGS = -release $(PACKAGE_VERSION) -shared
> > >
> > > +# Library containing test Hook module.
> > > +lib_LTLIBRARIES += libtesthook.la
> > > +libtesthook_la_SOURCES = examples/test_hook_module.cpp
> > > +libtesthook_la_CPPFLAGS = $(MESOS_CPPFLAGS)
> > > +libtesthook_la_LDFLAGS = -release $(PACKAGE_VERSION) -shared
> > > +
> > > mesos_tests_SOURCES = \
> > > tests/attributes_tests.cpp \
> > > tests/authentication_tests.cpp \
> > > @@ -1260,6 +1266,7 @@ mesos_tests_SOURCES =
> \
> > > tests/flags.cpp \
> > > tests/gc_tests.cpp \
> > > tests/hierarchical_allocator_tests.cpp \
> > > + tests/hook_tests.cpp \
> > > tests/isolator_tests.cpp \
> > > tests/log_tests.cpp \
> > > tests/logging_tests.cpp \
> > >
> > >
> > >
> >
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/examples/test_hook_module.cpp
> > > ----------------------------------------------------------------------
> > > diff --git a/src/examples/test_hook_module.cpp
> > > b/src/examples/test_hook_module.cpp
> > > new file mode 100644
> > > index 0000000..04fd43e
> > > --- /dev/null
> > > +++ b/src/examples/test_hook_module.cpp
> > > @@ -0,0 +1,138 @@
> > > +/**
> > > + * Licensed to the Apache Software Foundation (ASF) under one
> > > + * or more contributor license agreements. See the NOTICE file
> > > + * distributed with this work for additional information
> > > + * regarding copyright ownership. The ASF licenses this file
> > > + * to you under the Apache License, Version 2.0 (the
> > > + * "License"); you may not use this file except in compliance
> > > + * with the License. You may obtain a copy of the License at
> > > + *
> > > + * http://www.apache.org/licenses/LICENSE-2.0
> > > + *
> > > + * Unless required by applicable law or agreed to in writing, software
> > > + * distributed under the License is distributed on an "AS IS" BASIS,
> > > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> > > implied.
> > > + * See the License for the specific language governing permissions and
> > > + * limitations under the License.
> > > + */
> > > +
> > > +#include <string>
> > > +
> > > +#include <mesos/mesos.hpp>
> > > +#include <mesos/module.hpp>
> > > +
> > > +#include <stout/foreach.hpp>
> > > +#include <stout/os.hpp>
> > > +#include <stout/try.hpp>
> > > +
> > > +#include "hook/hook.hpp"
> > > +#include "master/master.hpp"
> > > +#include "module/hook.hpp"
> > > +#include "slave/slave.hpp"
> > > +
> > > +using std::string;
> > > +
> > > +using namespace mesos;
> > > +using namespace mesos::internal;
> > > +
> > > +// Must be kept in sync with variables of the same name in
> > > +// tests/hook_tests.cpp.
> > > +const char* testLabelKey = "MESOS_Test_Label";
> > > +const char* testLabelValue = "ApacheMesos";
> > > +const char* testEnvironmentVariableName =
> > > "MESOS_TEST_ENVIRONMENT_VARIABLE";
> > > +
> > > +class TestHook : public Hook
> > > +{
> > > +public:
> > > + virtual Result<Labels> masterLaunchTaskLabelDecorator(
> > > + const TaskInfo& taskInfo,
> > > + const FrameworkInfo& frameworkInfo,
> > > + const SlaveInfo& slaveInfo)
> > > + {
> > > + LOG(INFO) << "Executing 'masterLaunchTaskLabelDecorator' hook";
> > > +
> > > + Labels labels;
> > > + Label *label = labels.add_labels();
> > > + label->set_key(testLabelKey);
> > > + label->set_value(testLabelValue);
> > > +
> > > + return labels;
> > > + }
> > > +
> > > +
> > > + // In this hook, we create a temporary file and add its path to an
> > > + // environment variable. Later on, this environment variable is
> > > + // looked up by the removeExecutorHook to locate and delete this
> > > + // file.
> > > + virtual Result<Environment> slaveLaunchExecutorEnvironmentDecorator(
> > > + const ExecutorInfo& executorInfo,
> > > + const TaskInfo& taskInfo)
> > > + {
> > > + LOG(INFO) << "Executing 'slaveLaunchExecutorEnvironmentDecorator'
> > > hook";
> > > +
> > > + // Find the label value for the label that was created in the
> > > + // label decorator hook above.
> > > + Option<string> labelValue;
> > > + foreach (const Label& label, taskInfo.labels().labels()) {
> > > + if (label.key() == testLabelKey) {
> > > + labelValue = label.value();
> > > + CHECK_EQ(labelValue.get(), testLabelValue);
> > > + }
> > > + }
> > > + CHECK_SOME(labelValue);
> > > +
> > > + // Create a temporary file.
> > > + Try<string> file = os::mktemp();
> > > + CHECK_SOME(file);
> > > + CHECK_SOME(os::write(file.get(), labelValue.get()));
> > > +
> > > + // Inject file path into command environment.
> > > + Environment environment;
> > > + Environment::Variable* variable = environment.add_variables();
> > > + variable->set_name(testEnvironmentVariableName);
> > > + variable->set_value(file.get());
> > > +
> > > + return environment;
> > > + }
> > > +
> > > +
> > > + // This hook locates the file created by environment decorator hook
> > > + // and deletes it.
> > > + virtual Try<Nothing> slaveRemoveExecutorHook(
> > > + const FrameworkInfo& frameworkInfo,
> > > + const ExecutorInfo& executorInfo)
> > > + {
> > > + LOG(INFO) << "Executing 'slaveRemoveExecutorHook'";
> > > +
> > > + foreach (const Environment::Variable& variable,
> > > + executorInfo.command().environment().variables()) {
> > > + if (variable.name() == testEnvironmentVariableName) {
> > > + string path = variable.value();
> > > + // The removeExecutor hook may be called multiple times; thus
> > > + // we ignore the subsequent calls.
> > > + if (os::isfile(path)) {
> > > + CHECK_SOME(os::rm(path));
> > > + }
> > > + break;
> > > + }
> > > + }
> > > + return Nothing();
> > > + }
> > > +};
> > > +
> > > +
> > > +static Hook* createHook(const Parameters& parameters)
> > > +{
> > > + return new TestHook();
> > > +}
> > > +
> > > +
> > > +// Declares a Hook module named 'TestHook'.
> > > +mesos::modules::Module<Hook> org_apache_mesos_TestHook(
> > > + MESOS_MODULE_API_VERSION,
> > > + MESOS_VERSION,
> > > + "Apache Mesos",
> > > + "modules@mesos.apache.org",
> > > + "Test Hook module.",
> > > + NULL,
> > > + createHook);
> > >
> > >
> > >
> >
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/hook_tests.cpp
> > > ----------------------------------------------------------------------
> > > diff --git a/src/tests/hook_tests.cpp b/src/tests/hook_tests.cpp
> > > new file mode 100644
> > > index 0000000..4e9a3a7
> > > --- /dev/null
> > > +++ b/src/tests/hook_tests.cpp
> > > @@ -0,0 +1,272 @@
> > > +/**
> > > + * Licensed to the Apache Software Foundation (ASF) under one
> > > + * or more contributor license agreements. See the NOTICE file
> > > + * distributed with this work for additional information
> > > + * regarding copyright ownership. The ASF licenses this file
> > > + * to you under the Apache License, Version 2.0 (the
> > > + * "License"); you may not use this file except in compliance
> > > + * with the License. You may obtain a copy of the License at
> > > + *
> > > + * http://www.apache.org/licenses/LICENSE-2.0
> > > + *
> > > + * Unless required by applicable law or agreed to in writing, software
> > > + * distributed under the License is distributed on an "AS IS" BASIS,
> > > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> > > implied.
> > > + * See the License for the specific language governing permissions and
> > > + * limitations under the License.
> > > + */
> > > +
> > > +#include <mesos/module.hpp>
> > > +
> > > +#include <process/future.hpp>
> > > +#include <process/gmock.hpp>
> > > +#include <process/pid.hpp>
> > > +
> > > +#include <stout/option.hpp>
> > > +#include <stout/os.hpp>
> > > +#include <stout/try.hpp>
> > > +
> > > +#include "hook/manager.hpp"
> > > +#include "module/manager.hpp"
> > > +#include "master/flags.hpp"
> > > +#include "master/master.hpp"
> > > +#include "slave/flags.hpp"
> > > +#include "slave/slave.hpp"
> > > +
> > > +#include "tests/containerizer.hpp"
> > > +#include "tests/flags.hpp"
> > > +#include "tests/mesos.hpp"
> > > +
> > > +using std::string;
> > > +
> > > +using namespace mesos;
> > > +using namespace mesos::internal;
> > > +using namespace mesos::internal::tests;
> > > +using namespace mesos::modules;
> > > +
> > > +using mesos::internal::master::Master;
> > > +using mesos::internal::slave::Slave;
> > > +
> > > +using process::Future;
> > > +using process::PID;
> > > +
> > > +using std::string;
> > > +using std::vector;
> > > +
> > > +using testing::_;
> > > +using testing::Return;
> > > +
> > > +const char* HOOK_MODULE_LIBRARY_NAME = "testhook";
> > > +const char* HOOK_MODULE_NAME = "org_apache_mesos_TestHook";
> > > +
> > > +// Must be kept in sync with variables of the same name in
> > > +// examples/test_hook_module.cpp.
> > > +const char* testLabelKey = "MESOS_Test_Label";
> > > +const char* testLabelValue = "ApacheMesos";
> > > +const char* testEnvironmentVariableName =
> > > "MESOS_TEST_ENVIRONMENT_VARIABLE";
> > > +
> > > +class HookTest : public MesosTest
> > > +{
> > > +protected:
> > > + // TODO(karya): Replace constructor/destructor with SetUp/TearDown.
> > > + // Currently, using SetUp/TearDown causes VerifySlave* test to
> > > + // fail with a duplicate slave id message. However, everything
> > > + // seems normal when using this construction/destructor combo.
> > > + HookTest()
> > > + {
> > > + // Install hooks.
> > > + EXPECT_SOME(HookManager::initialize(HOOK_MODULE_NAME));
> > > + }
> > > +
> > > + ~HookTest()
> > > + {
> > > + // Unload the hooks so a subsequent install may succeed.
> > > + EXPECT_SOME(HookManager::unload(HOOK_MODULE_NAME));
> > > + }
> > > +};
> > > +
> > > +
> > > +// Test varioud hook install/uninstall mechanisms.
> > > +TEST_F(HookTest, HookLoading)
> > > +{
> > > + // Installing unknown hooks should fail.
> > > + EXPECT_ERROR(HookManager::initialize("Unknown Hook"));
> > > +
> > > + // Uninstalling an unknown hook should fail.
> > > + EXPECT_ERROR(HookManager::unload("Unknown Hook"));
> > > +
> > > + // Installing an already installed hook should fail.
> > > + EXPECT_ERROR(HookManager::initialize(HOOK_MODULE_NAME));
> > > +
> > > + // Uninstalling a hook should succeed.
> > > + EXPECT_SOME(HookManager::unload(HOOK_MODULE_NAME));
> > > +
> > > + // Uninstalling an already uninstalled hook should fail.
> > > + EXPECT_ERROR(HookManager::unload(HOOK_MODULE_NAME));
> > > + // This is needed to allow the tear-down to succeed.
> > > + EXPECT_SOME(HookManager::initialize(HOOK_MODULE_NAME));
> > > +}
> > > +
> > > +
> > > +// Test that the label decorator hook hangs a new label off the
> > > +// taskinfo message during master launch task.
> > > +TEST_F(HookTest, VerifyMasterLaunchTaskHook)
> > > +{
> > > + Try<PID<Master> > master = StartMaster(CreateMasterFlags());
> > > + ASSERT_SOME(master);
> > > +
> > > + TestContainerizer containerizer;
> > > +
> > > + StandaloneMasterDetector detector(master.get());
> > > +
> > > + // Start a mock slave since we aren't testing the slave hooks yet.
> > > + MockSlave slave(CreateSlaveFlags(), &detector, &containerizer);
> > > + process::spawn(slave);
> > > +
> > > + MockScheduler sched;
> > > + MesosSchedulerDriver driver(
> > > + &sched, DEFAULT_FRAMEWORK_INFO, master.get(),
> DEFAULT_CREDENTIAL);
> > > +
> > > + EXPECT_CALL(sched, registered(&driver, _, _))
> > > + .Times(1);
> > > +
> > > + Future<vector<Offer> > offers;
> > > + EXPECT_CALL(sched, resourceOffers(&driver, _))
> > > + .WillOnce(FutureArg<1>(&offers))
> > > + .WillRepeatedly(Return()); // Ignore subsequent offers.
> > > +
> > > + driver.start();
> > > +
> > > + AWAIT_READY(offers);
> > > + EXPECT_NE(0u, offers.get().size());
> > > +
> > > + CommandInfo command;
> > > + command.set_value("sleep 10");
> > > +
> > > + // Launch a task with the command executor.
> > > + TaskInfo task;
> > > + task.set_name("");
> > > + task.mutable_task_id()->set_value("1");
> > > + task.mutable_slave_id()->MergeFrom(offers.get()[0].slave_id());
> > > + task.mutable_resources()->MergeFrom(offers.get()[0].resources());
> > > + task.mutable_command()->MergeFrom(command);
> > > +
> > > + vector<TaskInfo> tasks;
> > > + tasks.push_back(task);
> > > +
> > > + Future<TaskInfo> taskInfo;
> > > + EXPECT_CALL(slave, runTask(_, _, _, _, _))
> > > + .Times(1)
> > > + .WillOnce(FutureArg<4>(&taskInfo));
> > > +
> > > + driver.launchTasks(offers.get()[0].id(), tasks);
> > > +
> > > + AWAIT_READY(taskInfo);
> > > +
> > > + // At launchTasks, the label decorator hook inside should have been
> > > + // executed and we should see the labels now.
> > > + Option<string> labelValue;
> > > + foreach (const Label& label, taskInfo.get().labels().labels()) {
> > > + if (label.key() == testLabelKey) {
> > > + labelValue = label.value();
> > > + }
> > > + }
> > > + EXPECT_SOME_EQ(testLabelValue, labelValue);
> > > +
> > > + driver.stop();
> > > + driver.join();
> > > +
> > > + process::terminate(slave);
> > > + process::wait(slave);
> > > +
> > > + Shutdown(); // Must shutdown before 'containerizer' gets
> deallocated.
> > > +}
> > > +
> > > +
> > > +// Test executor environment decorator hook and remove executor hook
> > > +// for slave. We expect the environment-decorator hook to create a
> > > +// temporary file and the remove-executor hook to delete that file.
> > > +TEST_F(HookTest, VerifySlaveLaunchExecutorHook)
> > > +{
> > > + master::Flags masterFlags = CreateMasterFlags();
> > > +
> > > + Try<PID<Master> > master = StartMaster(masterFlags);
> > > + ASSERT_SOME(master);
> > > +
> > > + slave::Flags slaveFlags = CreateSlaveFlags();
> > > +
> > > + MockExecutor exec(DEFAULT_EXECUTOR_ID);
> > > +
> > > + TestContainerizer containerizer(&exec);
> > > +
> > > + Try<PID<Slave> > slave = StartSlave(&containerizer);
> > > + ASSERT_SOME(slave);
> > > +
> > > + MockScheduler sched;
> > > + MesosSchedulerDriver driver(
> > > + &sched, DEFAULT_FRAMEWORK_INFO, master.get(),
> DEFAULT_CREDENTIAL);
> > > +
> > > + EXPECT_CALL(sched, registered(&driver, _, _))
> > > + .Times(1);
> > > +
> > > + Future<vector<Offer> > offers;
> > > + EXPECT_CALL(sched, resourceOffers(&driver, _))
> > > + .WillOnce(FutureArg<1>(&offers))
> > > + .WillRepeatedly(Return()); // Ignore subsequent offers.
> > > +
> > > + driver.start();
> > > +
> > > + AWAIT_READY(offers);
> > > + EXPECT_NE(0u, offers.get().size());
> > > +
> > > + // Launch a task with the command executor.
> > > + TaskInfo task;
> > > + task.set_name("");
> > > + task.mutable_task_id()->set_value("1");
> > > + task.mutable_slave_id()->MergeFrom(offers.get()[0].slave_id());
> > > + task.mutable_resources()->MergeFrom(offers.get()[0].resources());
> > > + task.mutable_executor()->MergeFrom(DEFAULT_EXECUTOR_INFO);
> > > +
> > > + vector<TaskInfo> tasks;
> > > + tasks.push_back(task);
> > > +
> > > + EXPECT_CALL(exec, launchTask(_, _));
> > > +
> > > + Future<ExecutorInfo> executorInfo;
> > > + EXPECT_CALL(exec, registered(_, _, _, _))
> > > + .Times(1)
> > > + .WillOnce(FutureArg<1>(&executorInfo));
> > > +
> > > + driver.launchTasks(offers.get()[0].id(), tasks);
> > > +
> > > + AWAIT_READY(executorInfo);
> > > +
> > > + // At launchTasks, the label decorator hook inside should have been
> > > + // executed and we should see the labels now.
> > > + // Further, the environment decorator hook should also have been
> > > + // executed. In that hook, we create a temp file and set the path
> > > + // as the value of the environment variable.
> > > + // Here we verify that the environment variable is present and the
> > > + // file is present on the disk.
> > > + Option<string> path;
> > > + foreach (const Environment::Variable& variable,
> > > + executorInfo.get().command().environment().variables()) {
> > > + if (variable.name() == testEnvironmentVariableName) {
> > > + path = variable.value();
> > > + break;
> > > + }
> > > + }
> > > +
> > > + EXPECT_SOME(path);
> > > + // The file must have been create by the environment decorator hook.
> > > + EXPECT_TRUE(os::isfile(path.get()));
> > > +
> > > + driver.stop();
> > > + driver.join();
> > > +
> > > + Shutdown(); // Must shutdown before 'containerizer' gets
> deallocated.
> > > +
> > > + // The removeExecutor hook in the test module deletes the temp file.
> > > + // Verify that the file is not present.
> > > + EXPECT_FALSE(os::isfile(path.get()));
> > > +}
> > >
> > >
> > >
> >
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/module.cpp
> > > ----------------------------------------------------------------------
> > > diff --git a/src/tests/module.cpp b/src/tests/module.cpp
> > > index 6cec1cb..e6dbf94 100644
> > > --- a/src/tests/module.cpp
> > > +++ b/src/tests/module.cpp
> > > @@ -93,6 +93,24 @@ static void addAuthenticationModules(Modules&
> modules)
> > > }
> > >
> > >
> > > +static void addHookModules(Modules& modules)
> > > +{
> > > + const string libraryPath = path::join(
> > > + tests::flags.build_dir,
> > > + "src",
> > > + ".libs",
> > > + os::libraries::expandName("testhook"));
> > > +
> > > + // Now add our test hook module.
> > > + Modules::Library* library = modules.add_libraries();
> > > + library->set_file(libraryPath);
> > > +
> > > + // To add a new module from this library, create a new ModuleID enum
> > > + // and tie it with a module name.
> > > + addModule(library, TestHook, "org_apache_mesos_TestHook");
> > > +}
> > > +
> > > +
> > > Try<Nothing> tests::initModules(const Option<Modules>& modules)
> > > {
> > > // First get the user provided modules.
> > > @@ -107,6 +125,9 @@ Try<Nothing> tests::initModules(const
> > Option<Modules>&
> > > modules)
> > > // Add authentication modules from testauthentication library.
> > > addAuthenticationModules(mergedModules);
> > >
> > > + // Add hook modules from testhook library.
> > > + addHookModules(mergedModules);
> > > +
> > > return ModuleManager::load(mergedModules);
> > > }
> > >
> > >
> > >
> > >
> >
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/module.hpp
> > > ----------------------------------------------------------------------
> > > diff --git a/src/tests/module.hpp b/src/tests/module.hpp
> > > index bc1a37d..21e8fc4 100644
> > > --- a/src/tests/module.hpp
> > > +++ b/src/tests/module.hpp
> > > @@ -42,7 +42,8 @@ enum ModuleID
> > > TestMemIsolator,
> > > TestCpuIsolator,
> > > TestCRAMMD5Authenticatee,
> > > - TestCRAMMD5Authenticator
> > > + TestCRAMMD5Authenticator,
> > > + TestHook
> > > };
> > >
> > >
> > >
> > >
> >
>
Re: [1/4] mesos git commit: Added example hook module.
Posted by Niklas Nielsen <ni...@mesosphere.io>.
Sure thing - apologize the inconvenience.
Niklas
On 15 January 2015 at 12:46, Vinod Kone <vi...@apache.org> wrote:
> Hey Niklas. One of the hook tests is continously failing on our internal
> CI. I've filed https://issues.apache.org/jira/browse/MESOS-2226. Can you
> take a look?
>
> Also, https://issues.apache.org/jira/browse/MESOS-2227
>
> On Wed, Jan 14, 2015 at 3:41 PM, <nn...@apache.org> wrote:
>
> > Repository: mesos
> > Updated Branches:
> > refs/heads/master 60007b9e7 -> 4e110d5d0
> >
> >
> > Added example hook module.
> >
> > This module provides hooks for master label decorator, slave executor
> > environment decorator and slave remove executor. A couple of test
> > cases are also provided to verify the hooks from this module.
> >
> > Review: https://reviews.apache.org/r/29496
> >
> >
> > Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
> > Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/4e110d5d
> > Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/4e110d5d
> > Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/4e110d5d
> >
> > Branch: refs/heads/master
> > Commit: 4e110d5d0f0c6cd65b8b6e5edff30bb4c589a278
> > Parents: 0773763
> > Author: Kapil Arya <ka...@mesosphere.io>
> > Authored: Wed Jan 14 13:49:06 2015 -0800
> > Committer: Niklas Q. Nielsen <ni...@mesosphere.io>
> > Committed: Wed Jan 14 15:11:37 2015 -0800
> >
> > ----------------------------------------------------------------------
> > src/Makefile.am | 7 +
> > src/examples/test_hook_module.cpp | 138 +++++++++++++++++
> > src/tests/hook_tests.cpp | 272
> +++++++++++++++++++++++++++++++++
> > src/tests/module.cpp | 21 +++
> > src/tests/module.hpp | 3 +-
> > 5 files changed, 440 insertions(+), 1 deletion(-)
> > ----------------------------------------------------------------------
> >
> >
> >
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/Makefile.am
> > ----------------------------------------------------------------------
> > diff --git a/src/Makefile.am b/src/Makefile.am
> > index 100db39..256384e 100644
> > --- a/src/Makefile.am
> > +++ b/src/Makefile.am
> > @@ -1237,6 +1237,12 @@ libtestauthentication_la_SOURCES =
> > examples/test_authentication_modules.cpp
> > libtestauthentication_la_CPPFLAGS = $(MESOS_CPPFLAGS)
> > libtestauthentication_la_LDFLAGS = -release $(PACKAGE_VERSION) -shared
> >
> > +# Library containing test Hook module.
> > +lib_LTLIBRARIES += libtesthook.la
> > +libtesthook_la_SOURCES = examples/test_hook_module.cpp
> > +libtesthook_la_CPPFLAGS = $(MESOS_CPPFLAGS)
> > +libtesthook_la_LDFLAGS = -release $(PACKAGE_VERSION) -shared
> > +
> > mesos_tests_SOURCES = \
> > tests/attributes_tests.cpp \
> > tests/authentication_tests.cpp \
> > @@ -1260,6 +1266,7 @@ mesos_tests_SOURCES = \
> > tests/flags.cpp \
> > tests/gc_tests.cpp \
> > tests/hierarchical_allocator_tests.cpp \
> > + tests/hook_tests.cpp \
> > tests/isolator_tests.cpp \
> > tests/log_tests.cpp \
> > tests/logging_tests.cpp \
> >
> >
> >
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/examples/test_hook_module.cpp
> > ----------------------------------------------------------------------
> > diff --git a/src/examples/test_hook_module.cpp
> > b/src/examples/test_hook_module.cpp
> > new file mode 100644
> > index 0000000..04fd43e
> > --- /dev/null
> > +++ b/src/examples/test_hook_module.cpp
> > @@ -0,0 +1,138 @@
> > +/**
> > + * Licensed to the Apache Software Foundation (ASF) under one
> > + * or more contributor license agreements. See the NOTICE file
> > + * distributed with this work for additional information
> > + * regarding copyright ownership. The ASF licenses this file
> > + * to you under the Apache License, Version 2.0 (the
> > + * "License"); you may not use this file except in compliance
> > + * with the License. You may obtain a copy of the License at
> > + *
> > + * http://www.apache.org/licenses/LICENSE-2.0
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> > implied.
> > + * See the License for the specific language governing permissions and
> > + * limitations under the License.
> > + */
> > +
> > +#include <string>
> > +
> > +#include <mesos/mesos.hpp>
> > +#include <mesos/module.hpp>
> > +
> > +#include <stout/foreach.hpp>
> > +#include <stout/os.hpp>
> > +#include <stout/try.hpp>
> > +
> > +#include "hook/hook.hpp"
> > +#include "master/master.hpp"
> > +#include "module/hook.hpp"
> > +#include "slave/slave.hpp"
> > +
> > +using std::string;
> > +
> > +using namespace mesos;
> > +using namespace mesos::internal;
> > +
> > +// Must be kept in sync with variables of the same name in
> > +// tests/hook_tests.cpp.
> > +const char* testLabelKey = "MESOS_Test_Label";
> > +const char* testLabelValue = "ApacheMesos";
> > +const char* testEnvironmentVariableName =
> > "MESOS_TEST_ENVIRONMENT_VARIABLE";
> > +
> > +class TestHook : public Hook
> > +{
> > +public:
> > + virtual Result<Labels> masterLaunchTaskLabelDecorator(
> > + const TaskInfo& taskInfo,
> > + const FrameworkInfo& frameworkInfo,
> > + const SlaveInfo& slaveInfo)
> > + {
> > + LOG(INFO) << "Executing 'masterLaunchTaskLabelDecorator' hook";
> > +
> > + Labels labels;
> > + Label *label = labels.add_labels();
> > + label->set_key(testLabelKey);
> > + label->set_value(testLabelValue);
> > +
> > + return labels;
> > + }
> > +
> > +
> > + // In this hook, we create a temporary file and add its path to an
> > + // environment variable. Later on, this environment variable is
> > + // looked up by the removeExecutorHook to locate and delete this
> > + // file.
> > + virtual Result<Environment> slaveLaunchExecutorEnvironmentDecorator(
> > + const ExecutorInfo& executorInfo,
> > + const TaskInfo& taskInfo)
> > + {
> > + LOG(INFO) << "Executing 'slaveLaunchExecutorEnvironmentDecorator'
> > hook";
> > +
> > + // Find the label value for the label that was created in the
> > + // label decorator hook above.
> > + Option<string> labelValue;
> > + foreach (const Label& label, taskInfo.labels().labels()) {
> > + if (label.key() == testLabelKey) {
> > + labelValue = label.value();
> > + CHECK_EQ(labelValue.get(), testLabelValue);
> > + }
> > + }
> > + CHECK_SOME(labelValue);
> > +
> > + // Create a temporary file.
> > + Try<string> file = os::mktemp();
> > + CHECK_SOME(file);
> > + CHECK_SOME(os::write(file.get(), labelValue.get()));
> > +
> > + // Inject file path into command environment.
> > + Environment environment;
> > + Environment::Variable* variable = environment.add_variables();
> > + variable->set_name(testEnvironmentVariableName);
> > + variable->set_value(file.get());
> > +
> > + return environment;
> > + }
> > +
> > +
> > + // This hook locates the file created by environment decorator hook
> > + // and deletes it.
> > + virtual Try<Nothing> slaveRemoveExecutorHook(
> > + const FrameworkInfo& frameworkInfo,
> > + const ExecutorInfo& executorInfo)
> > + {
> > + LOG(INFO) << "Executing 'slaveRemoveExecutorHook'";
> > +
> > + foreach (const Environment::Variable& variable,
> > + executorInfo.command().environment().variables()) {
> > + if (variable.name() == testEnvironmentVariableName) {
> > + string path = variable.value();
> > + // The removeExecutor hook may be called multiple times; thus
> > + // we ignore the subsequent calls.
> > + if (os::isfile(path)) {
> > + CHECK_SOME(os::rm(path));
> > + }
> > + break;
> > + }
> > + }
> > + return Nothing();
> > + }
> > +};
> > +
> > +
> > +static Hook* createHook(const Parameters& parameters)
> > +{
> > + return new TestHook();
> > +}
> > +
> > +
> > +// Declares a Hook module named 'TestHook'.
> > +mesos::modules::Module<Hook> org_apache_mesos_TestHook(
> > + MESOS_MODULE_API_VERSION,
> > + MESOS_VERSION,
> > + "Apache Mesos",
> > + "modules@mesos.apache.org",
> > + "Test Hook module.",
> > + NULL,
> > + createHook);
> >
> >
> >
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/hook_tests.cpp
> > ----------------------------------------------------------------------
> > diff --git a/src/tests/hook_tests.cpp b/src/tests/hook_tests.cpp
> > new file mode 100644
> > index 0000000..4e9a3a7
> > --- /dev/null
> > +++ b/src/tests/hook_tests.cpp
> > @@ -0,0 +1,272 @@
> > +/**
> > + * Licensed to the Apache Software Foundation (ASF) under one
> > + * or more contributor license agreements. See the NOTICE file
> > + * distributed with this work for additional information
> > + * regarding copyright ownership. The ASF licenses this file
> > + * to you under the Apache License, Version 2.0 (the
> > + * "License"); you may not use this file except in compliance
> > + * with the License. You may obtain a copy of the License at
> > + *
> > + * http://www.apache.org/licenses/LICENSE-2.0
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> > implied.
> > + * See the License for the specific language governing permissions and
> > + * limitations under the License.
> > + */
> > +
> > +#include <mesos/module.hpp>
> > +
> > +#include <process/future.hpp>
> > +#include <process/gmock.hpp>
> > +#include <process/pid.hpp>
> > +
> > +#include <stout/option.hpp>
> > +#include <stout/os.hpp>
> > +#include <stout/try.hpp>
> > +
> > +#include "hook/manager.hpp"
> > +#include "module/manager.hpp"
> > +#include "master/flags.hpp"
> > +#include "master/master.hpp"
> > +#include "slave/flags.hpp"
> > +#include "slave/slave.hpp"
> > +
> > +#include "tests/containerizer.hpp"
> > +#include "tests/flags.hpp"
> > +#include "tests/mesos.hpp"
> > +
> > +using std::string;
> > +
> > +using namespace mesos;
> > +using namespace mesos::internal;
> > +using namespace mesos::internal::tests;
> > +using namespace mesos::modules;
> > +
> > +using mesos::internal::master::Master;
> > +using mesos::internal::slave::Slave;
> > +
> > +using process::Future;
> > +using process::PID;
> > +
> > +using std::string;
> > +using std::vector;
> > +
> > +using testing::_;
> > +using testing::Return;
> > +
> > +const char* HOOK_MODULE_LIBRARY_NAME = "testhook";
> > +const char* HOOK_MODULE_NAME = "org_apache_mesos_TestHook";
> > +
> > +// Must be kept in sync with variables of the same name in
> > +// examples/test_hook_module.cpp.
> > +const char* testLabelKey = "MESOS_Test_Label";
> > +const char* testLabelValue = "ApacheMesos";
> > +const char* testEnvironmentVariableName =
> > "MESOS_TEST_ENVIRONMENT_VARIABLE";
> > +
> > +class HookTest : public MesosTest
> > +{
> > +protected:
> > + // TODO(karya): Replace constructor/destructor with SetUp/TearDown.
> > + // Currently, using SetUp/TearDown causes VerifySlave* test to
> > + // fail with a duplicate slave id message. However, everything
> > + // seems normal when using this construction/destructor combo.
> > + HookTest()
> > + {
> > + // Install hooks.
> > + EXPECT_SOME(HookManager::initialize(HOOK_MODULE_NAME));
> > + }
> > +
> > + ~HookTest()
> > + {
> > + // Unload the hooks so a subsequent install may succeed.
> > + EXPECT_SOME(HookManager::unload(HOOK_MODULE_NAME));
> > + }
> > +};
> > +
> > +
> > +// Test varioud hook install/uninstall mechanisms.
> > +TEST_F(HookTest, HookLoading)
> > +{
> > + // Installing unknown hooks should fail.
> > + EXPECT_ERROR(HookManager::initialize("Unknown Hook"));
> > +
> > + // Uninstalling an unknown hook should fail.
> > + EXPECT_ERROR(HookManager::unload("Unknown Hook"));
> > +
> > + // Installing an already installed hook should fail.
> > + EXPECT_ERROR(HookManager::initialize(HOOK_MODULE_NAME));
> > +
> > + // Uninstalling a hook should succeed.
> > + EXPECT_SOME(HookManager::unload(HOOK_MODULE_NAME));
> > +
> > + // Uninstalling an already uninstalled hook should fail.
> > + EXPECT_ERROR(HookManager::unload(HOOK_MODULE_NAME));
> > + // This is needed to allow the tear-down to succeed.
> > + EXPECT_SOME(HookManager::initialize(HOOK_MODULE_NAME));
> > +}
> > +
> > +
> > +// Test that the label decorator hook hangs a new label off the
> > +// taskinfo message during master launch task.
> > +TEST_F(HookTest, VerifyMasterLaunchTaskHook)
> > +{
> > + Try<PID<Master> > master = StartMaster(CreateMasterFlags());
> > + ASSERT_SOME(master);
> > +
> > + TestContainerizer containerizer;
> > +
> > + StandaloneMasterDetector detector(master.get());
> > +
> > + // Start a mock slave since we aren't testing the slave hooks yet.
> > + MockSlave slave(CreateSlaveFlags(), &detector, &containerizer);
> > + process::spawn(slave);
> > +
> > + MockScheduler sched;
> > + MesosSchedulerDriver driver(
> > + &sched, DEFAULT_FRAMEWORK_INFO, master.get(), DEFAULT_CREDENTIAL);
> > +
> > + EXPECT_CALL(sched, registered(&driver, _, _))
> > + .Times(1);
> > +
> > + Future<vector<Offer> > offers;
> > + EXPECT_CALL(sched, resourceOffers(&driver, _))
> > + .WillOnce(FutureArg<1>(&offers))
> > + .WillRepeatedly(Return()); // Ignore subsequent offers.
> > +
> > + driver.start();
> > +
> > + AWAIT_READY(offers);
> > + EXPECT_NE(0u, offers.get().size());
> > +
> > + CommandInfo command;
> > + command.set_value("sleep 10");
> > +
> > + // Launch a task with the command executor.
> > + TaskInfo task;
> > + task.set_name("");
> > + task.mutable_task_id()->set_value("1");
> > + task.mutable_slave_id()->MergeFrom(offers.get()[0].slave_id());
> > + task.mutable_resources()->MergeFrom(offers.get()[0].resources());
> > + task.mutable_command()->MergeFrom(command);
> > +
> > + vector<TaskInfo> tasks;
> > + tasks.push_back(task);
> > +
> > + Future<TaskInfo> taskInfo;
> > + EXPECT_CALL(slave, runTask(_, _, _, _, _))
> > + .Times(1)
> > + .WillOnce(FutureArg<4>(&taskInfo));
> > +
> > + driver.launchTasks(offers.get()[0].id(), tasks);
> > +
> > + AWAIT_READY(taskInfo);
> > +
> > + // At launchTasks, the label decorator hook inside should have been
> > + // executed and we should see the labels now.
> > + Option<string> labelValue;
> > + foreach (const Label& label, taskInfo.get().labels().labels()) {
> > + if (label.key() == testLabelKey) {
> > + labelValue = label.value();
> > + }
> > + }
> > + EXPECT_SOME_EQ(testLabelValue, labelValue);
> > +
> > + driver.stop();
> > + driver.join();
> > +
> > + process::terminate(slave);
> > + process::wait(slave);
> > +
> > + Shutdown(); // Must shutdown before 'containerizer' gets deallocated.
> > +}
> > +
> > +
> > +// Test executor environment decorator hook and remove executor hook
> > +// for slave. We expect the environment-decorator hook to create a
> > +// temporary file and the remove-executor hook to delete that file.
> > +TEST_F(HookTest, VerifySlaveLaunchExecutorHook)
> > +{
> > + master::Flags masterFlags = CreateMasterFlags();
> > +
> > + Try<PID<Master> > master = StartMaster(masterFlags);
> > + ASSERT_SOME(master);
> > +
> > + slave::Flags slaveFlags = CreateSlaveFlags();
> > +
> > + MockExecutor exec(DEFAULT_EXECUTOR_ID);
> > +
> > + TestContainerizer containerizer(&exec);
> > +
> > + Try<PID<Slave> > slave = StartSlave(&containerizer);
> > + ASSERT_SOME(slave);
> > +
> > + MockScheduler sched;
> > + MesosSchedulerDriver driver(
> > + &sched, DEFAULT_FRAMEWORK_INFO, master.get(), DEFAULT_CREDENTIAL);
> > +
> > + EXPECT_CALL(sched, registered(&driver, _, _))
> > + .Times(1);
> > +
> > + Future<vector<Offer> > offers;
> > + EXPECT_CALL(sched, resourceOffers(&driver, _))
> > + .WillOnce(FutureArg<1>(&offers))
> > + .WillRepeatedly(Return()); // Ignore subsequent offers.
> > +
> > + driver.start();
> > +
> > + AWAIT_READY(offers);
> > + EXPECT_NE(0u, offers.get().size());
> > +
> > + // Launch a task with the command executor.
> > + TaskInfo task;
> > + task.set_name("");
> > + task.mutable_task_id()->set_value("1");
> > + task.mutable_slave_id()->MergeFrom(offers.get()[0].slave_id());
> > + task.mutable_resources()->MergeFrom(offers.get()[0].resources());
> > + task.mutable_executor()->MergeFrom(DEFAULT_EXECUTOR_INFO);
> > +
> > + vector<TaskInfo> tasks;
> > + tasks.push_back(task);
> > +
> > + EXPECT_CALL(exec, launchTask(_, _));
> > +
> > + Future<ExecutorInfo> executorInfo;
> > + EXPECT_CALL(exec, registered(_, _, _, _))
> > + .Times(1)
> > + .WillOnce(FutureArg<1>(&executorInfo));
> > +
> > + driver.launchTasks(offers.get()[0].id(), tasks);
> > +
> > + AWAIT_READY(executorInfo);
> > +
> > + // At launchTasks, the label decorator hook inside should have been
> > + // executed and we should see the labels now.
> > + // Further, the environment decorator hook should also have been
> > + // executed. In that hook, we create a temp file and set the path
> > + // as the value of the environment variable.
> > + // Here we verify that the environment variable is present and the
> > + // file is present on the disk.
> > + Option<string> path;
> > + foreach (const Environment::Variable& variable,
> > + executorInfo.get().command().environment().variables()) {
> > + if (variable.name() == testEnvironmentVariableName) {
> > + path = variable.value();
> > + break;
> > + }
> > + }
> > +
> > + EXPECT_SOME(path);
> > + // The file must have been create by the environment decorator hook.
> > + EXPECT_TRUE(os::isfile(path.get()));
> > +
> > + driver.stop();
> > + driver.join();
> > +
> > + Shutdown(); // Must shutdown before 'containerizer' gets deallocated.
> > +
> > + // The removeExecutor hook in the test module deletes the temp file.
> > + // Verify that the file is not present.
> > + EXPECT_FALSE(os::isfile(path.get()));
> > +}
> >
> >
> >
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/module.cpp
> > ----------------------------------------------------------------------
> > diff --git a/src/tests/module.cpp b/src/tests/module.cpp
> > index 6cec1cb..e6dbf94 100644
> > --- a/src/tests/module.cpp
> > +++ b/src/tests/module.cpp
> > @@ -93,6 +93,24 @@ static void addAuthenticationModules(Modules& modules)
> > }
> >
> >
> > +static void addHookModules(Modules& modules)
> > +{
> > + const string libraryPath = path::join(
> > + tests::flags.build_dir,
> > + "src",
> > + ".libs",
> > + os::libraries::expandName("testhook"));
> > +
> > + // Now add our test hook module.
> > + Modules::Library* library = modules.add_libraries();
> > + library->set_file(libraryPath);
> > +
> > + // To add a new module from this library, create a new ModuleID enum
> > + // and tie it with a module name.
> > + addModule(library, TestHook, "org_apache_mesos_TestHook");
> > +}
> > +
> > +
> > Try<Nothing> tests::initModules(const Option<Modules>& modules)
> > {
> > // First get the user provided modules.
> > @@ -107,6 +125,9 @@ Try<Nothing> tests::initModules(const
> Option<Modules>&
> > modules)
> > // Add authentication modules from testauthentication library.
> > addAuthenticationModules(mergedModules);
> >
> > + // Add hook modules from testhook library.
> > + addHookModules(mergedModules);
> > +
> > return ModuleManager::load(mergedModules);
> > }
> >
> >
> >
> >
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/module.hpp
> > ----------------------------------------------------------------------
> > diff --git a/src/tests/module.hpp b/src/tests/module.hpp
> > index bc1a37d..21e8fc4 100644
> > --- a/src/tests/module.hpp
> > +++ b/src/tests/module.hpp
> > @@ -42,7 +42,8 @@ enum ModuleID
> > TestMemIsolator,
> > TestCpuIsolator,
> > TestCRAMMD5Authenticatee,
> > - TestCRAMMD5Authenticator
> > + TestCRAMMD5Authenticator,
> > + TestHook
> > };
> >
> >
> >
> >
>
Re: [1/4] mesos git commit: Added example hook module.
Posted by Vinod Kone <vi...@apache.org>.
Hey Niklas. One of the hook tests is continously failing on our internal
CI. I've filed https://issues.apache.org/jira/browse/MESOS-2226. Can you
take a look?
Also, https://issues.apache.org/jira/browse/MESOS-2227
On Wed, Jan 14, 2015 at 3:41 PM, <nn...@apache.org> wrote:
> Repository: mesos
> Updated Branches:
> refs/heads/master 60007b9e7 -> 4e110d5d0
>
>
> Added example hook module.
>
> This module provides hooks for master label decorator, slave executor
> environment decorator and slave remove executor. A couple of test
> cases are also provided to verify the hooks from this module.
>
> Review: https://reviews.apache.org/r/29496
>
>
> Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
> Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/4e110d5d
> Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/4e110d5d
> Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/4e110d5d
>
> Branch: refs/heads/master
> Commit: 4e110d5d0f0c6cd65b8b6e5edff30bb4c589a278
> Parents: 0773763
> Author: Kapil Arya <ka...@mesosphere.io>
> Authored: Wed Jan 14 13:49:06 2015 -0800
> Committer: Niklas Q. Nielsen <ni...@mesosphere.io>
> Committed: Wed Jan 14 15:11:37 2015 -0800
>
> ----------------------------------------------------------------------
> src/Makefile.am | 7 +
> src/examples/test_hook_module.cpp | 138 +++++++++++++++++
> src/tests/hook_tests.cpp | 272 +++++++++++++++++++++++++++++++++
> src/tests/module.cpp | 21 +++
> src/tests/module.hpp | 3 +-
> 5 files changed, 440 insertions(+), 1 deletion(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/Makefile.am
> ----------------------------------------------------------------------
> diff --git a/src/Makefile.am b/src/Makefile.am
> index 100db39..256384e 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -1237,6 +1237,12 @@ libtestauthentication_la_SOURCES =
> examples/test_authentication_modules.cpp
> libtestauthentication_la_CPPFLAGS = $(MESOS_CPPFLAGS)
> libtestauthentication_la_LDFLAGS = -release $(PACKAGE_VERSION) -shared
>
> +# Library containing test Hook module.
> +lib_LTLIBRARIES += libtesthook.la
> +libtesthook_la_SOURCES = examples/test_hook_module.cpp
> +libtesthook_la_CPPFLAGS = $(MESOS_CPPFLAGS)
> +libtesthook_la_LDFLAGS = -release $(PACKAGE_VERSION) -shared
> +
> mesos_tests_SOURCES = \
> tests/attributes_tests.cpp \
> tests/authentication_tests.cpp \
> @@ -1260,6 +1266,7 @@ mesos_tests_SOURCES = \
> tests/flags.cpp \
> tests/gc_tests.cpp \
> tests/hierarchical_allocator_tests.cpp \
> + tests/hook_tests.cpp \
> tests/isolator_tests.cpp \
> tests/log_tests.cpp \
> tests/logging_tests.cpp \
>
>
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/examples/test_hook_module.cpp
> ----------------------------------------------------------------------
> diff --git a/src/examples/test_hook_module.cpp
> b/src/examples/test_hook_module.cpp
> new file mode 100644
> index 0000000..04fd43e
> --- /dev/null
> +++ b/src/examples/test_hook_module.cpp
> @@ -0,0 +1,138 @@
> +/**
> + * Licensed to the Apache Software Foundation (ASF) under one
> + * or more contributor license agreements. See the NOTICE file
> + * distributed with this work for additional information
> + * regarding copyright ownership. The ASF licenses this file
> + * to you under the Apache License, Version 2.0 (the
> + * "License"); you may not use this file except in compliance
> + * with the License. You may obtain a copy of the License at
> + *
> + * http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +
> +#include <string>
> +
> +#include <mesos/mesos.hpp>
> +#include <mesos/module.hpp>
> +
> +#include <stout/foreach.hpp>
> +#include <stout/os.hpp>
> +#include <stout/try.hpp>
> +
> +#include "hook/hook.hpp"
> +#include "master/master.hpp"
> +#include "module/hook.hpp"
> +#include "slave/slave.hpp"
> +
> +using std::string;
> +
> +using namespace mesos;
> +using namespace mesos::internal;
> +
> +// Must be kept in sync with variables of the same name in
> +// tests/hook_tests.cpp.
> +const char* testLabelKey = "MESOS_Test_Label";
> +const char* testLabelValue = "ApacheMesos";
> +const char* testEnvironmentVariableName =
> "MESOS_TEST_ENVIRONMENT_VARIABLE";
> +
> +class TestHook : public Hook
> +{
> +public:
> + virtual Result<Labels> masterLaunchTaskLabelDecorator(
> + const TaskInfo& taskInfo,
> + const FrameworkInfo& frameworkInfo,
> + const SlaveInfo& slaveInfo)
> + {
> + LOG(INFO) << "Executing 'masterLaunchTaskLabelDecorator' hook";
> +
> + Labels labels;
> + Label *label = labels.add_labels();
> + label->set_key(testLabelKey);
> + label->set_value(testLabelValue);
> +
> + return labels;
> + }
> +
> +
> + // In this hook, we create a temporary file and add its path to an
> + // environment variable. Later on, this environment variable is
> + // looked up by the removeExecutorHook to locate and delete this
> + // file.
> + virtual Result<Environment> slaveLaunchExecutorEnvironmentDecorator(
> + const ExecutorInfo& executorInfo,
> + const TaskInfo& taskInfo)
> + {
> + LOG(INFO) << "Executing 'slaveLaunchExecutorEnvironmentDecorator'
> hook";
> +
> + // Find the label value for the label that was created in the
> + // label decorator hook above.
> + Option<string> labelValue;
> + foreach (const Label& label, taskInfo.labels().labels()) {
> + if (label.key() == testLabelKey) {
> + labelValue = label.value();
> + CHECK_EQ(labelValue.get(), testLabelValue);
> + }
> + }
> + CHECK_SOME(labelValue);
> +
> + // Create a temporary file.
> + Try<string> file = os::mktemp();
> + CHECK_SOME(file);
> + CHECK_SOME(os::write(file.get(), labelValue.get()));
> +
> + // Inject file path into command environment.
> + Environment environment;
> + Environment::Variable* variable = environment.add_variables();
> + variable->set_name(testEnvironmentVariableName);
> + variable->set_value(file.get());
> +
> + return environment;
> + }
> +
> +
> + // This hook locates the file created by environment decorator hook
> + // and deletes it.
> + virtual Try<Nothing> slaveRemoveExecutorHook(
> + const FrameworkInfo& frameworkInfo,
> + const ExecutorInfo& executorInfo)
> + {
> + LOG(INFO) << "Executing 'slaveRemoveExecutorHook'";
> +
> + foreach (const Environment::Variable& variable,
> + executorInfo.command().environment().variables()) {
> + if (variable.name() == testEnvironmentVariableName) {
> + string path = variable.value();
> + // The removeExecutor hook may be called multiple times; thus
> + // we ignore the subsequent calls.
> + if (os::isfile(path)) {
> + CHECK_SOME(os::rm(path));
> + }
> + break;
> + }
> + }
> + return Nothing();
> + }
> +};
> +
> +
> +static Hook* createHook(const Parameters& parameters)
> +{
> + return new TestHook();
> +}
> +
> +
> +// Declares a Hook module named 'TestHook'.
> +mesos::modules::Module<Hook> org_apache_mesos_TestHook(
> + MESOS_MODULE_API_VERSION,
> + MESOS_VERSION,
> + "Apache Mesos",
> + "modules@mesos.apache.org",
> + "Test Hook module.",
> + NULL,
> + createHook);
>
>
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/hook_tests.cpp
> ----------------------------------------------------------------------
> diff --git a/src/tests/hook_tests.cpp b/src/tests/hook_tests.cpp
> new file mode 100644
> index 0000000..4e9a3a7
> --- /dev/null
> +++ b/src/tests/hook_tests.cpp
> @@ -0,0 +1,272 @@
> +/**
> + * Licensed to the Apache Software Foundation (ASF) under one
> + * or more contributor license agreements. See the NOTICE file
> + * distributed with this work for additional information
> + * regarding copyright ownership. The ASF licenses this file
> + * to you under the Apache License, Version 2.0 (the
> + * "License"); you may not use this file except in compliance
> + * with the License. You may obtain a copy of the License at
> + *
> + * http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +
> +#include <mesos/module.hpp>
> +
> +#include <process/future.hpp>
> +#include <process/gmock.hpp>
> +#include <process/pid.hpp>
> +
> +#include <stout/option.hpp>
> +#include <stout/os.hpp>
> +#include <stout/try.hpp>
> +
> +#include "hook/manager.hpp"
> +#include "module/manager.hpp"
> +#include "master/flags.hpp"
> +#include "master/master.hpp"
> +#include "slave/flags.hpp"
> +#include "slave/slave.hpp"
> +
> +#include "tests/containerizer.hpp"
> +#include "tests/flags.hpp"
> +#include "tests/mesos.hpp"
> +
> +using std::string;
> +
> +using namespace mesos;
> +using namespace mesos::internal;
> +using namespace mesos::internal::tests;
> +using namespace mesos::modules;
> +
> +using mesos::internal::master::Master;
> +using mesos::internal::slave::Slave;
> +
> +using process::Future;
> +using process::PID;
> +
> +using std::string;
> +using std::vector;
> +
> +using testing::_;
> +using testing::Return;
> +
> +const char* HOOK_MODULE_LIBRARY_NAME = "testhook";
> +const char* HOOK_MODULE_NAME = "org_apache_mesos_TestHook";
> +
> +// Must be kept in sync with variables of the same name in
> +// examples/test_hook_module.cpp.
> +const char* testLabelKey = "MESOS_Test_Label";
> +const char* testLabelValue = "ApacheMesos";
> +const char* testEnvironmentVariableName =
> "MESOS_TEST_ENVIRONMENT_VARIABLE";
> +
> +class HookTest : public MesosTest
> +{
> +protected:
> + // TODO(karya): Replace constructor/destructor with SetUp/TearDown.
> + // Currently, using SetUp/TearDown causes VerifySlave* test to
> + // fail with a duplicate slave id message. However, everything
> + // seems normal when using this construction/destructor combo.
> + HookTest()
> + {
> + // Install hooks.
> + EXPECT_SOME(HookManager::initialize(HOOK_MODULE_NAME));
> + }
> +
> + ~HookTest()
> + {
> + // Unload the hooks so a subsequent install may succeed.
> + EXPECT_SOME(HookManager::unload(HOOK_MODULE_NAME));
> + }
> +};
> +
> +
> +// Test varioud hook install/uninstall mechanisms.
> +TEST_F(HookTest, HookLoading)
> +{
> + // Installing unknown hooks should fail.
> + EXPECT_ERROR(HookManager::initialize("Unknown Hook"));
> +
> + // Uninstalling an unknown hook should fail.
> + EXPECT_ERROR(HookManager::unload("Unknown Hook"));
> +
> + // Installing an already installed hook should fail.
> + EXPECT_ERROR(HookManager::initialize(HOOK_MODULE_NAME));
> +
> + // Uninstalling a hook should succeed.
> + EXPECT_SOME(HookManager::unload(HOOK_MODULE_NAME));
> +
> + // Uninstalling an already uninstalled hook should fail.
> + EXPECT_ERROR(HookManager::unload(HOOK_MODULE_NAME));
> + // This is needed to allow the tear-down to succeed.
> + EXPECT_SOME(HookManager::initialize(HOOK_MODULE_NAME));
> +}
> +
> +
> +// Test that the label decorator hook hangs a new label off the
> +// taskinfo message during master launch task.
> +TEST_F(HookTest, VerifyMasterLaunchTaskHook)
> +{
> + Try<PID<Master> > master = StartMaster(CreateMasterFlags());
> + ASSERT_SOME(master);
> +
> + TestContainerizer containerizer;
> +
> + StandaloneMasterDetector detector(master.get());
> +
> + // Start a mock slave since we aren't testing the slave hooks yet.
> + MockSlave slave(CreateSlaveFlags(), &detector, &containerizer);
> + process::spawn(slave);
> +
> + MockScheduler sched;
> + MesosSchedulerDriver driver(
> + &sched, DEFAULT_FRAMEWORK_INFO, master.get(), DEFAULT_CREDENTIAL);
> +
> + EXPECT_CALL(sched, registered(&driver, _, _))
> + .Times(1);
> +
> + Future<vector<Offer> > offers;
> + EXPECT_CALL(sched, resourceOffers(&driver, _))
> + .WillOnce(FutureArg<1>(&offers))
> + .WillRepeatedly(Return()); // Ignore subsequent offers.
> +
> + driver.start();
> +
> + AWAIT_READY(offers);
> + EXPECT_NE(0u, offers.get().size());
> +
> + CommandInfo command;
> + command.set_value("sleep 10");
> +
> + // Launch a task with the command executor.
> + TaskInfo task;
> + task.set_name("");
> + task.mutable_task_id()->set_value("1");
> + task.mutable_slave_id()->MergeFrom(offers.get()[0].slave_id());
> + task.mutable_resources()->MergeFrom(offers.get()[0].resources());
> + task.mutable_command()->MergeFrom(command);
> +
> + vector<TaskInfo> tasks;
> + tasks.push_back(task);
> +
> + Future<TaskInfo> taskInfo;
> + EXPECT_CALL(slave, runTask(_, _, _, _, _))
> + .Times(1)
> + .WillOnce(FutureArg<4>(&taskInfo));
> +
> + driver.launchTasks(offers.get()[0].id(), tasks);
> +
> + AWAIT_READY(taskInfo);
> +
> + // At launchTasks, the label decorator hook inside should have been
> + // executed and we should see the labels now.
> + Option<string> labelValue;
> + foreach (const Label& label, taskInfo.get().labels().labels()) {
> + if (label.key() == testLabelKey) {
> + labelValue = label.value();
> + }
> + }
> + EXPECT_SOME_EQ(testLabelValue, labelValue);
> +
> + driver.stop();
> + driver.join();
> +
> + process::terminate(slave);
> + process::wait(slave);
> +
> + Shutdown(); // Must shutdown before 'containerizer' gets deallocated.
> +}
> +
> +
> +// Test executor environment decorator hook and remove executor hook
> +// for slave. We expect the environment-decorator hook to create a
> +// temporary file and the remove-executor hook to delete that file.
> +TEST_F(HookTest, VerifySlaveLaunchExecutorHook)
> +{
> + master::Flags masterFlags = CreateMasterFlags();
> +
> + Try<PID<Master> > master = StartMaster(masterFlags);
> + ASSERT_SOME(master);
> +
> + slave::Flags slaveFlags = CreateSlaveFlags();
> +
> + MockExecutor exec(DEFAULT_EXECUTOR_ID);
> +
> + TestContainerizer containerizer(&exec);
> +
> + Try<PID<Slave> > slave = StartSlave(&containerizer);
> + ASSERT_SOME(slave);
> +
> + MockScheduler sched;
> + MesosSchedulerDriver driver(
> + &sched, DEFAULT_FRAMEWORK_INFO, master.get(), DEFAULT_CREDENTIAL);
> +
> + EXPECT_CALL(sched, registered(&driver, _, _))
> + .Times(1);
> +
> + Future<vector<Offer> > offers;
> + EXPECT_CALL(sched, resourceOffers(&driver, _))
> + .WillOnce(FutureArg<1>(&offers))
> + .WillRepeatedly(Return()); // Ignore subsequent offers.
> +
> + driver.start();
> +
> + AWAIT_READY(offers);
> + EXPECT_NE(0u, offers.get().size());
> +
> + // Launch a task with the command executor.
> + TaskInfo task;
> + task.set_name("");
> + task.mutable_task_id()->set_value("1");
> + task.mutable_slave_id()->MergeFrom(offers.get()[0].slave_id());
> + task.mutable_resources()->MergeFrom(offers.get()[0].resources());
> + task.mutable_executor()->MergeFrom(DEFAULT_EXECUTOR_INFO);
> +
> + vector<TaskInfo> tasks;
> + tasks.push_back(task);
> +
> + EXPECT_CALL(exec, launchTask(_, _));
> +
> + Future<ExecutorInfo> executorInfo;
> + EXPECT_CALL(exec, registered(_, _, _, _))
> + .Times(1)
> + .WillOnce(FutureArg<1>(&executorInfo));
> +
> + driver.launchTasks(offers.get()[0].id(), tasks);
> +
> + AWAIT_READY(executorInfo);
> +
> + // At launchTasks, the label decorator hook inside should have been
> + // executed and we should see the labels now.
> + // Further, the environment decorator hook should also have been
> + // executed. In that hook, we create a temp file and set the path
> + // as the value of the environment variable.
> + // Here we verify that the environment variable is present and the
> + // file is present on the disk.
> + Option<string> path;
> + foreach (const Environment::Variable& variable,
> + executorInfo.get().command().environment().variables()) {
> + if (variable.name() == testEnvironmentVariableName) {
> + path = variable.value();
> + break;
> + }
> + }
> +
> + EXPECT_SOME(path);
> + // The file must have been create by the environment decorator hook.
> + EXPECT_TRUE(os::isfile(path.get()));
> +
> + driver.stop();
> + driver.join();
> +
> + Shutdown(); // Must shutdown before 'containerizer' gets deallocated.
> +
> + // The removeExecutor hook in the test module deletes the temp file.
> + // Verify that the file is not present.
> + EXPECT_FALSE(os::isfile(path.get()));
> +}
>
>
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/module.cpp
> ----------------------------------------------------------------------
> diff --git a/src/tests/module.cpp b/src/tests/module.cpp
> index 6cec1cb..e6dbf94 100644
> --- a/src/tests/module.cpp
> +++ b/src/tests/module.cpp
> @@ -93,6 +93,24 @@ static void addAuthenticationModules(Modules& modules)
> }
>
>
> +static void addHookModules(Modules& modules)
> +{
> + const string libraryPath = path::join(
> + tests::flags.build_dir,
> + "src",
> + ".libs",
> + os::libraries::expandName("testhook"));
> +
> + // Now add our test hook module.
> + Modules::Library* library = modules.add_libraries();
> + library->set_file(libraryPath);
> +
> + // To add a new module from this library, create a new ModuleID enum
> + // and tie it with a module name.
> + addModule(library, TestHook, "org_apache_mesos_TestHook");
> +}
> +
> +
> Try<Nothing> tests::initModules(const Option<Modules>& modules)
> {
> // First get the user provided modules.
> @@ -107,6 +125,9 @@ Try<Nothing> tests::initModules(const Option<Modules>&
> modules)
> // Add authentication modules from testauthentication library.
> addAuthenticationModules(mergedModules);
>
> + // Add hook modules from testhook library.
> + addHookModules(mergedModules);
> +
> return ModuleManager::load(mergedModules);
> }
>
>
>
> http://git-wip-us.apache.org/repos/asf/mesos/blob/4e110d5d/src/tests/module.hpp
> ----------------------------------------------------------------------
> diff --git a/src/tests/module.hpp b/src/tests/module.hpp
> index bc1a37d..21e8fc4 100644
> --- a/src/tests/module.hpp
> +++ b/src/tests/module.hpp
> @@ -42,7 +42,8 @@ enum ModuleID
> TestMemIsolator,
> TestCpuIsolator,
> TestCRAMMD5Authenticatee,
> - TestCRAMMD5Authenticator
> + TestCRAMMD5Authenticator,
> + TestHook
> };
>
>
>
>