Mesos Modules

Mesos module 在 Mesos 0.21.0版本中被引用。

什么是 Mesos Modules

Mesos module 提供了一种通过创建和按需使用共享库来轻松扩展 Mesos 内部工作。 Module 可以定制 Mesos 而无需通过重新编译/链接每个特定的实例。 Module 可以把外部依赖放到单独的库中,从而精简 Mesos 核心。 Module 还可以非常容易的尝试新的功能。例如,假设加载配置包含一个虚拟机( Lua, Python, … ),可以尝试编写新的配置脚本,而不强迫这些以来关系进入到项目中。最后,Module 提供一种简单的方法为第三方轻松扩展 Mesos ,而不必知道具体的内部细节。

调用 Mesos Modules

命令行使用 —modules 可用于 Mesos master, slave 以及 tests 以指定模块列表被加载并提供给内部子系统。

使用 —modules=filepath 通过包含指定模块列表的JSON格式字符串文件。filepath 格式可以是‘ file:///path/to/file ’ 或者 ‘ /path/to/file ’。

使用 —modules=”{…}” 指定的模块内部名单。

JSON strings 例子:

  1. 加载一个包含 org_apache_mesos_barorg_apache_mesos_baz 两个模块的 libfoo.so 库文件

    1. {
    2. "libraries": [
    3. {
    4. "file": "/path/to/libfoo.so",
    5. "modules": [
    6. {
    7. "name": "org_apache_mesos_bar",
    8. },
    9. {
    10. "name": "org_apache_mesos_baz"
    11. }
    12. ]
    13. }
    14. ]
    15. }
  2. 通过 foo 库文件指定模块 org_apache_mesos_bar 的 key/valuse 参数为 X/Y ( org_apache_mesos_baz 模块不包含任何参数加载):

    1. {
    2. "libraries": [
    3. {
    4. "name": "foo",
    5. "modules": [
    6. {
    7. "name": "org_apache_mesos_bar"
    8. "parameters": [
    9. {
    10. "key": "X",
    11. "value": "Y",
    12. }
    13. ]
    14. },
    15. {
    16. "name": "org_apache_mesos_baz"
    17. }
    18. ]
    19. }
    20. ]
    21. }

3.在命令行中指定:

  1. --modules='{"libraries":[{"file":"/path/to/libfoo.so", "modules":[{"name":"org_apache_mesos_bar"}]}]}'

库名称

每个库,至少有一个” file “或者” path “在参数中被指定。” file “参数可以是文件名称(如” libfoo.so “),相对路径(如” myLibs/libfoo.so “),或者绝对路径(如” /home/mesos/lib/libfoo.so “)。 参数” name “指的是一个库名称(如” foo “)。如果指定了” name “,它会自动将匹配当前平台的扩展名(如,” foo “在 Linux 上扩展为 libfoo.so ,在 OS X 则为” libfoo.dylib “)。

如果库没有指定” file “参数,该库会搜索标准库的路径或目录指向的环境变量 LD_LIBRARY_PATH (OS X 则为 DYLD_LIBRARY_PATH)。

如果” file “和” name “同时指定了,则” name “会被忽略。

支持哪些类型的模块?

下面是目前可用的各种模块的种类。

分配器( Allocator )

Mesos master 的分配器会定期将资源分配给某个framework。 要加载一个自定义的分配器到 Mesos master 中,需要如下:

  • 采用它的 Mesos master 清单中的 —modules 配置。

  • 通过 —allocator 标志将其声明成分配器

例如,下面的命令将通过 ExternalAllocatorModule 运行 Mesos master:

  1. ./bin/mesos-master.sh --work_dir=m/work --modules="file://<modules-including-allocator>.json" --allocator=ExternalAllocatorModule

Anonymous

Anonymous 模块不接收任何回调,只是与它们的父进程共存。 Anonymous 模块不需要任何特定的选择器( flags ),他们会立即实例化通过 —modules 标志在 Mesos master 或者 slave。

Hook

待翻译

隔离(Isolator)

Isolator 模块可以与专门的隔离与监控功能交互。

编写 Mesos modules

A HelloWorld module

下面代码片段描述了” TestModule “中的” org_apache_mesos_bar “模块的实现:

  1. #include <iostream>
  2. #include "test_module.hpp"
  3. class TestModuleImpl : public TestModule
  4. {
  5. public:
  6. TestModuleImpl()
  7. {
  8. std::cout << "HelloWorld!" << std::endl;
  9. }
  10. virtual int foo(char a, long b)
  11. {
  12. return a + b;
  13. }
  14. virtual int bar(float a, double b)
  15. {
  16. return a * b;
  17. }
  18. };
  19. static TestModule* create()
  20. {
  21. return new TestModule();
  22. }
  23. static bool compatible()
  24. {
  25. return true;
  26. }
  27. // Declares a module named 'org_apache_mesos_TestModule' of
  28. // 'TestModule' kind.
  29. // Mesos core binds the module instance pointer as needed.
  30. // The compatible() hook is provided by the module for compatibility checks.
  31. // The create() hook returns an object of type 'TestModule'.
  32. mesos::modules::Module<TestModule> org_apache_mesos_TestModule(
  33. MESOS_MODULE_API_VERSION,
  34. MESOS_VERSION,
  35. "Apache Mesos",
  36. "modules@mesos.apache.org",
  37. "This is a test module.",
  38. compatible,
  39. create);

构建模块

下面假设 Mesos 安装在标准位置,即 Mesos 动态库和头文件是可用的。

  1. G ++ -lmesos -fpic -o test_module.o test_module.cpp
  2. $ GCC -shared -o libtest_module.so test_module.o

测试模块

下面的命令将测试 Mesos 与 org_apache_mesos_TestCpuIsolator 隔离模块:

  1. ./bin/mesos-tests.sh --modules="/home/kapil/mesos/isolator-module/modules.json" --isolation="org_apache_mesos_TestCpuIsolator"

模块的命名规则

每个模块的名称都应该是唯一的。在 Json String中有相同模块名字会引起进程的异常终止。

附录:

JSON格式:

  1. {
  2. "type":"object",
  3. "required":false,
  4. "properties":{
  5. "libraries":{
  6. "type":"array",
  7. "required":false,
  8. "items":{
  9. "type":"object",
  10. "required":false,
  11. "properties":{
  12. "file":{
  13. "type":"string",
  14. "required":false
  15. },
  16. "name":{
  17. "type":"string",
  18. "required":false
  19. },
  20. "modules":{
  21. "type":"array",
  22. "required":false,
  23. "items":{
  24. "type":"object",
  25. "required":false,
  26. "properties":{
  27. "name":{
  28. "type":"string",
  29. "required":true
  30. },
  31. "parameters":{
  32. "type":"array",
  33. "required":false,
  34. "items":{
  35. "type":"object",
  36. "required":false,
  37. "properties":{
  38. "key":{
  39. "type":"string",
  40. "required":true
  41. },
  42. "value":{
  43. "type":"string",
  44. "required":true
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }