Mesos源码分析(2): Mesos Master的启动之一
Mesos Master的启动参数如下:
/usr/sbin/mesos-master --zk=zk://127.0.0.1:2181/mesos --port=5050 --log_dir=/var/log/mesos --hostname=192.168.56.101 --hostname_lookup=false --ip=192.168.56.101 --quorum=1 --registry=replicated_log --work_dir=/var/lib/mesos/master
Mesos Master的启动可以包含很多的参数,参考文档http://mesos.apache.org/documentation/latest/configuration/
Mesos Master的参数可以通过下面两种方式指定:
- 通过命令行参数:--option_name=value
- 通过环境变量:MESOS_OPTION_NAME
Mesos Master的启动从代码src/master/main.cpp开始的。
1. master::Flags flags 解析命令行参数和环境变量
|
上面便是解析命令行参数的代码。
接下来,需要解析环境变量的参数了。
|
那Flags是个什么东西呢,能做这件事情,参考文档https://mesosphere.com/blog/2015/05/14/using-stout-to-parse-command-line-options
Almost every program needs to parse some form of command-line argument. Often, this is a pretty large set of possible options, and the management of the various options is usually pretty tedious while adding virtually no value to the program's functionality.
Google's gflags are thus a very welcome contribution in that they remove the tediousness. They let program developers give their users a fairly complex set of options to choose from, without wasting time re-inventing the wheel.
However, the use of macros and certain other quirks within gflags led the team developing Apache Mesos to create a more object-oriented approach to the same problem. This new approach yields a more familiar pattern to the programmer (and those familiar with Python's 'argparse' library will see several similarities there too).
Stout is a header-only library that can be used independently from Mesos. However, the most up-to-date and recent version should be extracted from the Apache Mesos 3rdparty folder.
Beyond Flags, Stout offers a wealth of modern abstractions that make coding in C++ a more pleasant experience, especially for folks used to the facilities offered "natively" by other languages such as Scala or Python: 'Try'/'Option' classes (see also below); facilities to deal with Hashmaps; IP/MAC addresses manipulation; the 'Duration' class for time units (which will look very familiar to users of Joda Time) and the nifty 'Stopwatch' utility.
In the following, we show how to use Stout to simplify management of command-line argument flags, but we invite you to explore the library and find out how it can make your coding life easier.
Use
To use Stout_ CLI arguments ("flags") all we have to do is include the header file and derive our custom flags' class from 'FlagsBase.' In 'FlagsXxxx, add the (appropriately typed) fields that will be, at runtime, populated with the correct values from the command line (or the given default values, if any):
#include <stout/flags/flags.hpp>
using std::string;
// Program flags, allows user to run the tests (--test) or the Scheduler
// against a Mesos Master at --master IP:PORT; or the Executor, which will
// invoke Mongo using the --config FILE configuration file.
//
// All the flags are optional, but at least ONE (and at most one) MUST be
// present.
class
MongoFlags: public flags::FlagsBase
{
public:
MongoFlags();
Option<string> master;
Option<string> config;
string role;
bool test;
};
In the class's constructor, the actual value of the flag (the '–flag') is defined along with a simple help message and, where necessary, a default value:
MongoFlags::MongoFlags()
{
add(&MongoFlags::master, "master", "The host address of the Mesos Master.");
add(&MongoFlags::config, "config", "The location of the configuration file,"
" on the Worker node (this file MUST exist).");
add(&MongoFlags::role, "role", "The role for the executor", "*");
add(&MongoFlags::test, "test", "Will only run unit tests and exit.", false);
}
One convenient feature is that flags gives you a 'usage()' method that generates a nicely-formatted string that is suitable to be emitted to the user (either upon request, or if something goes wrong):
void printUsage(const
string& prog, const
MongoFlags& flags)
{
cout << "Usage: " << os::basename(prog).get() << " [options]\n\n"
"One (and only one) of the following options MUST be present.\n\n"
"Options:\n" << flags.usage() << endl;
}
Finally, in your 'main()' you simply call the FlagsBase::load()' method to initialize the class's members, which can then be used as you would normally:
int main(int argc, char** argv)
{
MongoFlags flags;
bool help;
// flags can be also added outside the Flags class:
flags.add(&help, "help", "Prints this help message", false);
Try<Nothing> load = flags.load(None(), argc, argv);
if (load.isError()) {
std::cerr << "Failed to load flags: " << load.error() << std::endl;
return -1;
}
if (!help) {
if (flags.test) {
cout << "Running unit tests for Playground App\n";
return test(argc, argv);
}
if (flags.config.isSome()) {
return run_executor(flags.config.get());
}
if (flags.master.isSome()) {
string uri = os::realpath(argv[0]).get();
auto masterIp = flags.master.get();
cout << "MongoExecutor starting - launching Scheduler rev. "
<< MongoScheduler::REV << " starting Executor at: " << uri << '\n';
return run_scheduler(uri, masterIp);
}
}
printUsage(argv[0], flags);
}
For a full-fledged (and extensive) use of 'stout/flags,' see the 'master.cpp and associated header file in the 'src/master' folder of the Apache Mesos repo.
Optional Values
Optional arguments can be wrapped in Stout's 'Option type, which is an extremely convenient abstraction of objects that may optionally be unassigned. That means circumventing all the awkwardness of using 'NULL' — which, in fact, you should avoid at all costs in your code.
The customary pattern of usage for an 'Option' object is exemplified in the snippet:
void doSomething(const std::string& widget) {
// as far as this method is concerned, strings are all there is
// ...
}
// in another part of your program
Option<std::string> foo;
// other code that may (or may not) set foo to some value
if (foo.isSome()) {
doSomething(foo.get());
}
Again, more examples can be found in several places in the source code of Mesos (see, for example, 'main.cpp' in the same source folder as above).
Mesos就是封装了Google的gflags来解析命令行参数和环境变量
在src/master/flags.cpp里面下面的代码:
|
里面的参数和http://mesos.apache.org/documentation/latest/configuration/中的参数列表一模一样。
Mesos源码分析(2): Mesos Master的启动之一的更多相关文章
- Mesos源码分析(5): Mesos Master的启动之四
5. Create an instance of allocator. 代码如下 Mesos源码中默认的Allocator,即HierarchicalDRFAllocator的位置在$ME ...
- Mesos源码分析(4) Mesos Master的启动之三
3. ModuleManager::load(flags.modules.get())如果有参数--modules或者--modules_dir=dirpath,则会将路径中的so文件load进来 ...
- 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 ...
随机推荐
- SpringJUnit4ClassRunner (单元测试)
1.在Maven的pom.xml中加入 <dependency> <groupId>junit</groupId> <artifactId>junit& ...
- 程序员不能忍996了!全民 fuck ,GitHub来说话
前两天有个Github超级火的一个项目,在一小时之内星标上千. https://github.com/997icu/996.ICU 截至目前 这个项目start数量超过63K.Issues5000 ...
- MQTT报文格式
MQTT报文结构 控制报文由三部分组成: 1.Fixed header 固定报头,所有报文都包含 2.Variable header 可变报头,部分报文包含 3.Body 有效载荷,部分报文包含 固定 ...
- bat实现固定时间循环抓取设备log
背景:测试时需要实时抓取android设备log,但是一份log抓取过来非常庞大(有时超过500M+,编辑器都打不开,还得找工具进行分割,甚是蛋疼),查看也非常不方便. 解决:基于上述情况,与其之后进 ...
- DDT驱动selenium自动化测试
建两个.py文件分别是是读取xlsx文件内容,一个是测试用例使用ddt驱动 获取xlsx文件内容 import xlrd class ParseExcel(object): def __init__( ...
- python-装饰器&生成器&迭代器&推导式
一:普通装饰器 概念:在不改变原函数内部代码的基础上,在函数执行之前和之后自动执行某个功能,为已存在的对象添加某个功能 普通装饰器编写的格式 def 外层函数(参数) def 内层函数(*args,* ...
- So, How About UMD模块-Universal Module Definition
在ES6模块解决方案出现之前,工具库或包常用三种解决方案提供对外使用的接口,最早是直接暴露给全局变量,比如我们熟知的Jquery暴露的全局变量是$,Lodash对外暴露的全局变量是_,后来出现了AMD ...
- C# 通过 Quartz .NET 实现 schedule job 的处理
在实际项目的开发过程中,会有这样的功能需求:要求创建一些Job定时触发运行,比如进行一些数据的同步. 那么在 .Net Framework 中如何实现这个Timer Job的功能呢? 这里所讲的是借助 ...
- Kubernetes - 腾讯蓝鲸配置平台(CMDB)开源版部署
蓝鲸CMDB 蓝鲸配置平台(蓝鲸CMDB)是一个基于运维场景设计的企业配置管理服务.主要功能: 1. 拓扑化的主机管理:主机基础属性.主机快照数据.主机归属关系管理 2. 组织架构管理:可扩展的基于业 ...
- 前端如何做好seo
一:什么是SEO? 搜索引擎优化(Search Engine Optimization),简称SEO.是按照搜索引擎给出的优化建议,以增强网站核心价值为目标,从网站结构.内容建设方案.用户互动传播等角 ...