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 ...
随机推荐
- .Net ABP 框架 service 无法访问
最近在看ABP框架,按照文档写好service后,怎么也访问不到,后来发现,忘记把service类设置为public的了! 不写public ABP框架就不能将这个service映射为controll ...
- jmeter生成html格式接口自动化测试报告
jmeter生成html格式接口自动化测试报告 jmeter自带执行结果查看的插件,但是需要在jmeter工具中才能查看,如果要向领导提交测试结果,不够方便直观. 笔者刚做了这方面的尝试,总结出来分享 ...
- (转)前端开发-发布一个NPM包之最简单易懂流程
原文地址:https://www.cnblogs.com/sghy/p/6829747.html 1.npm官网创建npm账户 npm网站地址:https://www.npmjs.com/ npm网站 ...
- SQL语句:子查询
一,子查询定义: 子查询就是嵌套在主查询中的查询. 子查询可以嵌套在主查询中所有位置,包括SELECT.FROM.WHERE.GROUP BY.HAVING.ORDER BY. 但并不是每个位置嵌套子 ...
- C# Entity To Json
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...
- ASP.NET MVC 执行流程介绍
Routing 组件 Controller Controller中可用的ActionResult MVC-View(使用的抽象工厂模式的视图引擎) 视图模型
- 难受的ESlint语法检测
相信写过vue的各位小白都有过这样的体验,明明引入的文件语法是对的,明明自己写的代码是对的,但是总会报语法错误,没错,就是ESlint代码检测搞的鬼, 就算你在注释后面多打一个空格,它都会去搞事情,简 ...
- numpy库补充 mean函数应用
mean()函数功能:求取均值经常操作的参数为axis,以m * n矩阵举例: axis 不设置值,对 m*n 个数求均值,返回一个实数 axis = 0:压缩行,对各列求均值,返回 1* n 矩阵 ...
- HDU - 1827 Summer Holiday (强连通)
<题目链接> 题目大意: 听说lcy帮大家预定了新马泰7日游,Wiskey真是高兴的夜不能寐啊,他想着得快点把这消息告诉大家,虽然他手上有所有人的联系方式,但是一个一个联系过去实在太耗时间 ...
- ISP PIPLINE (十二) Sharpening
什么是sharpening? 不解释,从左到右为sharpen , 从右到左为blur. 简单理解为边缘增强,使得轮廓清晰.增强对比度. 如何进行sharpening? 下面是实际sharpen的过程 ...