Apache Mesos底層基礎(chǔ)庫(kù)
1. Protocol buffer
Protocal Buffer是google開(kāi)源的用于數(shù)據(jù)交換的庫(kù),常用于跨語(yǔ)言的數(shù)據(jù)訪(fǎng)問(wèn),擔(dān)任的角色一般為對(duì)象的序列化/反序列化。 另一個(gè)與之類(lèi)似的開(kāi)源軟件是facebook開(kāi)源的thrift,它們兩個(gè)最大區(qū)別是thrift提供了自動(dòng)生成RPC的功能而Protocal Buffer需要自己實(shí)現(xiàn),但Protocal Buffer的一個(gè)優(yōu)勢(shì)是其序列化/反序列化非常高效。
2. Libprocess
libprocess是采用C/C++編寫(xiě)的高效消息傳遞編程模型(基于消息傳遞的網(wǎng)絡(luò)通信模型,而不是RPC),由伯克利開(kāi)源。 其整個(gè)實(shí)現(xiàn)非常簡(jiǎn)單,包括最基本的消息發(fā)送和接收等。
2.1 Libprocess模型
在mesos中,主要有四個(gè)角色,分別是:mesos-master,mesos-slave,framework(Hadoop/Spark /MPI等) scheduler,executor(在mesos-slave上執(zhí)行framework task的組件),每種角色均是一個(gè)Process,在實(shí)現(xiàn)時(shí)會(huì)繼承l(wèi)ibprocess中的ProtobufProcess類(lèi)(它又繼承了 Process類(lèi)),這樣,它們均會(huì)編成一個(gè)后臺(tái)運(yùn)行且不斷監(jiān)聽(tīng)protocal buffer消息的socket server,如下圖所示:
2.2 各種常用函數(shù)
Libprocess+protocol buffer組合是mesos最底層最重要的消息傳遞基礎(chǔ)庫(kù)(沒(méi)有采用RPC機(jī)制),由于該庫(kù)采用了基于Protocal Buffer消息傳遞的通信機(jī)制),因而非常高效。Mesos常用的兩個(gè)頭文件是libprocess\include\process下的 process.hpp和protobuf.hpp,這兩個(gè)提供了用于消息傳遞的API,其中process.hpp是最核心的文件,提供了原始的接口, 而protobuf.hpp是在process.hpp基礎(chǔ)上,加入了ProtocalBuffer對(duì)象參數(shù),使ProtocalBuffer使用起來(lái)更 加容易。
(1) install
void install(void (T::*method)(P1C),P1 (M::*param1)() const);
安裝一個(gè)處理ProtocalBuffer消息的handler,其中,消息類(lèi)型是M,該消息對(duì)應(yīng)的處理函數(shù)是method,函數(shù)參數(shù)為M::*param1。舉例:mesos中slave/slave.cpp:
- install(
- &Slave::newMasterDetected,
- &NewMasterDetectedMessage::pid);
安裝一個(gè)處理NewMasterDetectedMessage(ProtocalBuffer對(duì)象)的handler,mesos slave一旦接收到該消息,便會(huì)調(diào)用newMasterDetected函數(shù)處理, 且該函數(shù)的輸入?yún)?shù)是NewMasterDetectedMessage消息中的pid屬性。
- void install(const std::string& name,void (T::*method)(const UPID&, const std::string&))
安裝一個(gè)處理字符串的handler,也就是說(shuō),當(dāng)收到字符串name后,調(diào)用函數(shù)method進(jìn)行處理。這個(gè)API在mesos中的典型應(yīng)用時(shí)維持master與slave之間的心跳,以確定彼此活著:
在slave/slave.cpp中:
- install("PING", &Slave::ping);
- void Slave::ping(const UPID& from, const string& body)
- {
- send(from, "PONG");
- }
在master/master.cpp中:
- install("PONG", &SlaveObserver::pong);
- void pong(const UPID& from, const string& body)
- {
- timeouts = 0;
- pinged = false;
- }
- void timeout()
- {
- if (pinged) { // So we haven't got back a pong yet ...
- if (++timeouts >= MAX_SLAVE_TIMEOUTS) {
- deactivate();
- return;
- }
- }
- send(slave, "PING");
- pinged = true;
- delay(SLAVE_PONG_TIMEOUT, self(), &SlaveObserver::timeout);
- }
(2) send
- void send(const process::UPID& to, const google::protobuf::Message& message)
向某個(gè)UPID上發(fā)送消息,其中UPID代表一個(gè)socket,里面含有ip和port信息,而消息message是ProtocalBuffer定義的對(duì)象。
(3) dispatch
- void dispatch(const UPID& pid,
- const std::tr1::shared_ptr >& f)
執(zhí)行進(jìn)程pid中的函數(shù)f,為了提高效率,該函數(shù)并不會(huì)等到函數(shù)f執(zhí)行完成,而是采用了異步的方法:將函數(shù)f放入一個(gè)函數(shù)隊(duì)列,由另外一個(gè)進(jìn)程(或者多個(gè))不斷從隊(duì)列中獲取函數(shù),依次執(zhí)行。
(4) delay
- Timer delay(double secs,const PID& pid,void (T::*method)())
延遲secs秒調(diào)度進(jìn)程pid中的方法method,并返回一個(gè)計(jì)數(shù)器,通過(guò)這個(gè)計(jì)時(shí)器,可取消該調(diào)度。
在mesos中,巧妙地通過(guò)該函數(shù)構(gòu)造了一個(gè)無(wú)限循環(huán)以不斷檢測(cè)空閑資源,并將之分配給各個(gè)框架,代碼如下:
- void Master::initialize() {
- ……
- timerTickTimer = delay(1.0, self(), &Master::timerTick);
- }
- void Master::timerTick() {
- ……
- timerTickTimer = delay(1.0, self(), &Master::timerTick);
- }
上面函數(shù)代碼段可完成每1s調(diào)用一次timerTick函數(shù)的功能。
3. Boost
非常有名的開(kāi)源C++基礎(chǔ)庫(kù),里面的STL非常高效方便,已被很多著名軟件采用。
4. Zookeeper
是一個(gè)針對(duì)大型分布式系統(tǒng)的可靠協(xié)調(diào)系統(tǒng),提供的功能包括:配置維護(hù)、名字服務(wù)、分布式同步、組服務(wù)等。 Mesos采用zookeeper解決master單點(diǎn)故障問(wèn)題,使用zookeeper搭建一個(gè)master集群,當(dāng)master出現(xiàn)故障時(shí),選擇一個(gè) standby master 變?yōu)閙aster。
5. glog
Google開(kāi)源的C++日志庫(kù),主用于C++程序中打印日志,打印格式如下:
I0411 17:26:54.150193 20653 main.cpp:111] Creating “process” isolation module
I0411 17:26:54.150400 20653 main.cpp:119] Build: 2012-04-11 16:50:21 by root
I0411 17:26:54.150658 20653 main.cpp:120] Starting Mesos slave
I0411 17:26:54.152981 20669 slave.cpp:191] Slave started on 123.145.2.2:34694
I0411 17:26:54.153024 20669 slave.cpp:192] Slave resources: cpus=2; mem=490
6. gmock
開(kāi)源 C++ 單元測(cè)試框架
7. 參考資料
(1)Mesos主頁(yè):http://www.mesosproject.org/index.html
(2)Mesos代碼:https://svn.apache.org/repos/asf/incubator/mesos/trunk/
原文鏈接:http://dongxicheng.org/apache-mesos/mesos-base-libarary/