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 ...
随机推荐
- tensorflow--交叉熵
学而不思则罔,思而不学则怠. 最近在看<TensorFlow 实战Google深度学习框架第二版>这本书.从头开始学习深度学习,对于细节方面进行探究.相当于重新拾起来这门”手艺“. 这篇随 ...
- 初识C语言(六)
数组 程序中需要容器,该容器有点特殊,它在程序中是一块连续的,大小固定并且里面的数据类型一致的内存空间,它的名字叫数组. 声明一个数组: 数据类型 数组名称[长度]; C语言中的数组初始化是有三种形式 ...
- scikit-learn中机器学习模型比较(逻辑回归与KNN)
本文源自于Kevin Markham 的模型评估:https://github.com/justmarkham/scikit-learn-videos/blob/master/05_model_eva ...
- 学习笔记_J2EE_Mybatis_01_mybatis入门
mybatis入门 1.概述 因为觉得自己写的概念信息未必比别人好,而且这些理论知识了解就好,内核信息还是要看源码.所以从相对权威的百度百科转载了基本信息,也因此它的真实性是经过检验的. 1.1 什么 ...
- Security配置文件的基本配置及参数名详解
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- 中国居民18位身份证号验证方法,Java算法实现
public static boolean validate18Idcard(String idcard){ if(idcard == null ) { return false; } if(idca ...
- MyCat分片规则--笔记(二)
概述 myCat实现分库分表的策略,对数据量的处理带来很大的便利,这里主要整理下MyCat的使用以及常用路由算法,针对MyCat里面的事务.集群后续再做整理:另外内容整理,不免会参考技术大牛的博客,内 ...
- dbus-launch
NAME dbus-launch - Utility to start a message bus from a shell script dbus-launch - 从shell脚本启动一个消息总线 ...
- linux学习之命令的排列、替换和别名--2019-04-23
1.命令的排列 1)使用“;” 使用“;”命令时,不管命令1是否出错,接下来都执行命令2. 2)使用“&&” 使用“&&”命令时,只有命令1正确运行,接下来才会执行命令 ...
- 练习html,css,js仿制百度首页
1.练习目的 练习使用html,scc,js 完成界面样式,用ul标签实现文本框下拉,通过js完成添加列表内容等功能 2.效果 3.程序代码 <!DOCTYPE html> <htm ...