Mesos源码分析(4) Mesos Master的启动之三
3. ModuleManager::load(flags.modules.get())如果有参数--modules或者--modules_dir=dirpath,则会将路径中的so文件load进来
代码中加载模块的代码如下

对应的命令行参数如下:

都可以写什么Module呢?
首先是Allocator
默认是内置的Hierarchical Dominant Resource Fairness allocator
要写一个自己的Allocator:
- 通过--modules加载so
- 通过--allocator参数指定
当然很少有人会重写这个Allocator,因为这是Mesos的精髓所在,但是可以对默认的Alllocator进行加强的。
参考文档http://mesos.apache.org/documentation/latest/allocation-module/
Mesos Allocation Modules
The logic that the Mesos master uses to determine which frameworks to make resource offers to is encapsulated in the master's allocator module. The allocator is a pluggable component that organizations can use to implement their own sharing policy, e.g. fair-sharing, priority, etc., or tune the default hierarchical Dominant Resource Fairness algorithm (see the DRF paper).
To use a custom allocator in Mesos, one must:
- Implement the Allocator interface as defined in mesos/allocator/allocator.hpp,
- Wrap the allocator implementation in a module and load it in the Mesos master.
Writing a custom allocator
Allocator modules are implemented in C++, the same language in which Mesos is written. They must subclass the Allocator interface defined in mesos/allocator/allocator.hpp. However, your implementation can be a C++ proxy, which delegates calls to an actual allocator written in a language of your choice.
The default allocator is HierarchicalDRFAllocatorProcess, which lives in$MESOS_HOME/src/master/allocator/mesos/hierarchical.hpp. Like most Mesos components, it is actor-based, which means all interface methods are non-blocking and return immediately after putting the corresponding action into the actor's queue. If you would like to design your custom allocator in a similar manner, subclass MesosAllocatorProcess from$MESOS_HOME/src/master/allocator/mesos/allocator.hpp and wrap your actor-based allocator inMesosAllocator. This dispatches calls to the underlying actor and controls its lifetime. You can refer to HierarchicalDRFAllocatorProcess as a starting place if you choose to write your own actor-based allocation module.
Additionally, the built-in hierarchical allocator can be extended without the need to reimplement the entirety of the allocation logic. This is possible through the use of the Sorter abstraction. Sorters define the order in which hierarchy layers (e.g. roles or frameworks) should be offered resources by taking "client" objects and some information about those clients and returning an ordered list of clients.
Sorters are implemented in C++ and inherit the Sorter class defined in$MESOS_HOME/src/master/allocator/sorter/sorter.hpp. The default sorter is DRFSorter, which implements fair sharing and can be found in$MESOS_HOME/src/master/allocator/sorter/drf/sorter.hpp. This sorter is capable of expressing priorities by specifying weights in Sorter::add(). Each client's share is divided by its weight. For example, a role that has a weight of 2 will be offered twice as many resources as a role with weight 1.
Wiring up a custom allocator
Once a custom allocator has been written, the next step is to override the built-in implementation with your own. This process consists of several steps:
- Wrap your allocator in a Mesos allocator module,
- Load this module in Mesos master.
An allocator module is a factory function and a module description, as defined inmesos/module/allocator.hpp. Assuming the allocation logic is implemented by theExternalAllocator class declared in external_allocator.hpp, the following snippet describes the implementation of an allocator module named ExternalAllocatorModule:
#include <mesos/allocator/allocator.hpp>
#include <mesos/module/allocator.hpp>
#include <stout/try.hpp>
#include "external_allocator.hpp"
using namespace mesos;
using mesos::allocator::Allocator;
using mesos::internal::master::allocator::HierarchicalDRFAllocator;
static Allocator* createExternalAllocator(const Parameters& parameters)
{
Try<Allocator*> allocator = ExternalAllocator::create();
if (allocator.isError()) {
return nullptr;
}
return allocator.get();
}
// Declares an ExternalAllocator module named 'ExternalAllocatorModule'.
mesos::modules::Module<Allocator> ExternalAllocatorModule(
MESOS_MODULE_API_VERSION,
MESOS_VERSION,
"Mesos Contributor",
"engineer@example.com",
"External Allocator module.",
nullptr,
createExternalAllocator);
Refer to the Mesos Modules documentation for instructions on how to compile and load a module in Mesos master.
Hook
你可以写hook模块,讲代码插在很多关键的步骤,从而改写整个Executor或者Docker或者Task的启动的整个过程。
可以干预的hook的地方定义在mesos/hook.hpp中。
Class hook定义如下:

其中比较常用的是slavePrelaunchDockerHook,可以在Docker启动之前做一些事情,比如准备工作。
还有slaveRemoveExecutorHook,这个可以在executor结束的时候,做一些事情,比如清理工作。
要加载一个hook需要:
- 通过--modules加载so
- 通过--hooks加载hook
./bin/mesos-agent.sh --master=<IP>:<PORT> --modules="file://<path-to-modules-config>.json" --hooks=TestTaskHook
Isolator
可通过--isolation=VALUE指定,仅仅用于Mesos Containerizer。
在src/slave/containerizer/mesos/container.cpp里面

默认只会有cpu和memory
|
然后是创建Mesos Containerizer的Launcher
|
然后就是创建Isolator
|
所有的isolator会放在一个vector里面
|
最终创建MesosContainerizer
|
那isolator是如何起作用的呢?
当一个MesosContainerizer启动一个进程的时候,会调用如下的函数。
在src/slave/containerizer/mesos/containerizer.cpp中有

里面最重要的一步是fork一个进程
|
最后还会调用isolate不断的检验这个进程是否使用适当的资源。
|
Isolator定义了以下函数


中会调用

Master Contender and Detector
Detector是检测哪个是Mesos-Master的Leader,Contender是自己竞争成为Mesos-Master的leader。默认使用zookeeper。
4. Mesos-Master启动的第四步是加载Hook, HookManager::initialize(flags.hooks.get())

Mesos源码分析(4) Mesos Master的启动之三的更多相关文章
- Mesos源码分析(5): Mesos Master的启动之四
5. Create an instance of allocator. 代码如下 Mesos源码中默认的Allocator,即HierarchicalDRFAllocator的位置在$ME ...
- Mesos源码分析(2): Mesos Master的启动之一
Mesos Master的启动参数如下: /usr/sbin/mesos-master --zk=zk://127.0.0.1:2181/mesos --port=5050 --log_dir=/va ...
- Mesos源码分析(6): Mesos Master的初始化
Mesos Master的初始化在src/master/master.cpp中 在Mesos Master的log中,是能看到这一行的. 1.初始化role,并设置weight权重 ...
- Mesos源码分析(3): Mesos Master的启动之二
2. process::firewall::install(move(rules));如果有参数--firewall_rules则会添加规则 对应的代码如下: // Initialize fire ...
- Mesos源码分析(1): Mesos的启动过程总论
- Mesos源码分析(9): Test Framework的启动
我们以Test Framework为例子解释Framework的启动方式. Test Framework的代码在src/examples/test_framework.cpp中的main函数 首先要指 ...
- Mesos源码分析
Mesos源码分析(1): Mesos的启动过程总论 Mesos源码分析(2): Mesos Master的启动之一 Mesos源码分析(3): Mesos Master的启动之二 Mesos源码分析 ...
- Mesos源码分析(11): Mesos-Master接收到launchTasks消息
根据Mesos源码分析(6): Mesos Master的初始化中的代码分析,当Mesos-Master接收到launchTask消息的时候,会调用Master::launchTasks函数. v ...
- Mesos源码分析(10): MesosSchedulerDriver的启动及运行一个Task
MesosSchedulerDriver的代码在src/sched/sched.cpp里面实现. Driver->run()调用start() 首先检测Mesos-Maste ...
随机推荐
- 搭建jenkins实现自动化部署
搭建jenkins实现自动化部署 一.安装jenkins 1.添加yum repos,然后安装 sudo wget -O /etc/yum.repos.d/jenkins.repo https://p ...
- SP283 NAPTIME - Naptime
SP283 NAPTIME - Naptime 题意: 在某个星球上,一天由N小时构成.我们称0-1点为第一个小时,1-2点为第二个小时,以此类推.在第i个小时睡觉能恢复Ui点体力.在这座星球上住着一 ...
- maven与eclipse连接的配置
1.修改本地仓库位置 maven从中心仓库下载的文件一般默认放在本地用户文件加下的.m2/repository文件夹中,修改则需要找到所下载的maven文件夹下的conf文件夹下的setting.xm ...
- Python3学习笔记(urllib模块的使用)
转载地址:https://www.cnblogs.com/Lands-ljk/p/5447127.html 1.基本方法 urllib.request.urlopen(url, data=None, ...
- ELK搭建<一>:搭建ES集群
1.首先进入官网下载ES,如果下载最新之前的版本 点击past releases就行了. 2.解压后进入config修改配置文件elasticsearch.yml #集群名称 cluster.name ...
- maven 导包报错
作为初学者本应当是持之以恒的但是很长时间没有冒泡了这次冒个泡写maven项目的时候遇到了很多的bug,今天给大家分享一下解决的办法(常见的错误就是导不进来自己想要的包)要么就是导包报错以下是解决方法 ...
- 第三次java作业
编写“学生”类及其测试类. 5.1 “学生”类: ² 类名:Student ² 属性:姓名.性别.年龄.学号.5门课程的成绩 ² 方法1:在控制台输出各个属性的值. ² 方法2:计算平均成绩 ² 方法 ...
- Core 2.0使用Nlog记录日志+Mysql
一.先创建一个Core2.0的项目,并在NuGet中引入3个类库文件 MySql.Data.dll NLog.dll NLog.Web.AspNetCore.dll 二.创建一个nlog.config ...
- MVC中ztree异步加载
var setting = { async: { enable: true, url: "*****/LoadChild", autoParam: ["id"] ...
- 开发自己的react-native组件并发布到npm[转]
原文链接:https://www.jianshu.com/p/091a68ea1ca7 写在前面 在做react-native开发的时候,我们经常会找到一些第三方组件,并且通过npm install的 ...