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 ...
随机推荐
- js-图片预加载
//图片预加载 //闭包模拟局部作用于 (function($){ function Preload(imgs,options){ this.imgs = (typeof imgs === 'st ...
- selenium对百度进行登录注销
#百度登录退出demo import time from selenium import webdriver from selenium.webdriver.common.action_chains ...
- MathType7.X链接:https://pan.baidu.com/s/1rQ5Cwk5_CC9UgvgaYPVCCg 提取码:6ojq 复制这段内容后打开百度网盘手机App,操作更方便哦完美解压,无限使用
最近在写论文的过程中使用到了MathType,但是由于MathType30天使用已经过期,有些特殊符号用不了,于是开始找各种破解版.好吧,花了整整两个小时才算搞定,真是一部血泪史,现在把安装破解教程贴 ...
- 微信H5支付开发(maven仓库版)
官方文档:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_1 开发之前确认开通了H5支付功能 一.安装微信sdk 二.创建config ...
- Jmeter组成结构及运行原理
Jmeter结构主要组成要素包括:测试计划,线程组,采样器以及监听器.对于各部件的作用域关系如下图: Jmeter是纯Java程序,使用JVM,运行采用多线程完成,往往单台负载机由于机器配置有限,支持 ...
- 2018-2019-2 网络对抗技术 20165206 Exp4 恶意代码分析
- 2018-2019-2 网络对抗技术 20165206 Exp4 恶意代码分析 - 实验任务 1系统运行监控(2分) (1)使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP ...
- Javal连接字符串为Json
public static String concatJson(String[] keys,String[] values,String[] alreadyJsonKeys){ if(keys==nu ...
- 语法之进化论之lambda表达式
namespace 匿名函数 { /// <summary> /// 语法之进化论 /// </summary> class Program { delegate bool M ...
- 为什么要使用getters和setters/访问器?
Why use getters and setters/accessors? 实际上会有很多人问这个问题....尤其是它成为Coding Style中一部分的时候. 文章出自LBushkin的回答 T ...
- SpringBoot的自动配置原理
一.入口 上篇注解@SpringBootApplication简单分析,说到了@SpringBootApplication注解的内部结构, 其中@EnableAutoConfiguration利用En ...