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 ...
随机推荐
- ASP.NET Core + Vue.js 开发
1.新建 项目文件夹 pro,在 VS CODE 打开终端,输入dotnet new mvc 命令,新建asp.net core项目. 2.在Startup.cs添加webpack的引用与配置 usi ...
- 【JS】Javascript数组操作
1.提取数组中每个对象的某个属性组成新的数组 如数组为: let arry = [ { name: 'zhao', 'age': 18 }, { name: 'qian', 'age': 19 }, ...
- .Net Core 技巧
避免同步调用 正确的使用异步编程 async/await 使用异步编程尽量避免 Task.Await() 或者 Task.Result(因为会阻塞线程,直到任务完成,有违异步的设计思想) 尽量始终执行 ...
- 自定义Section
转载 :http://www.cnblogs.com/gaobing/p/6047746.html <configSections> 元素必须是 configuration 元素的第一个子 ...
- webpack分片chunk加载原理
首先,使用create-react-app快速创建一个demo npx create-react-app react-demo # npx命令需要npm5.2+ cd react-demo npm s ...
- YOLOv3:训练自己的数据(附优化与问题总结)
环境说明 系统:ubuntu16.04 显卡:Tesla k80 12G显存 python环境: 2.7 && 3.6 前提条件:cuda9.0 cudnn7.0 opencv3.4. ...
- Cocos坐标之convertToNodeSpace、convertToWorldSpace、convertToNodeSpaceAR、convertToWorldSpaceAR区别和用法
convertToNodeSpace.convertToWorldSpace.convertToNodeSpaceAR.convertToWorldSpaceAR,在他们的下一层看到下面的注释: /* ...
- Linux版 php5.4 升级php7
开篇 本操作是在VirtualBox里面进行的,所以开篇先说下,本地如何操作VB里面的Linux 1.secureCRT登陆虚拟机ubuntu 直接连接虚拟机的ip (ifconfig)会提示拒绝访问 ...
- 16 道嵌入式C语言面试题
1. 用预处理指令#define 声明一个常数,用以表明 1 年中有多少秒(忽略闰年问题) #define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL 我在这想看到 ...
- c/c++再学习:排序算法了解
1.冒泡排序 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成. ...