Apache Mesos底層基礎(chǔ)庫
1. Protocol buffer
Protocal Buffer是google開源的用于數(shù)據(jù)交換的庫,常用于跨語言的數(shù)據(jù)訪問,擔任的角色一般為對象的序列化/反序列化。 另一個與之類似的開源軟件是facebook開源的thrift,它們兩個最大區(qū)別是thrift提供了自動生成RPC的功能而Protocal Buffer需要自己實現(xiàn),但Protocal Buffer的一個優(yōu)勢是其序列化/反序列化非常高效。
2. Libprocess
libprocess是采用C/C++編寫的高效消息傳遞編程模型(基于消息傳遞的網(wǎng)絡(luò)通信模型,而不是RPC),由伯克利開源。 其整個實現(xiàn)非常簡單,包括最基本的消息發(fā)送和接收等。
2.1 Libprocess模型
在mesos中,主要有四個角色,分別是:mesos-master,mesos-slave,framework(Hadoop/Spark /MPI等) scheduler,executor(在mesos-slave上執(zhí)行framework task的組件),每種角色均是一個Process,在實現(xiàn)時會繼承l(wèi)ibprocess中的ProtobufProcess類(它又繼承了 Process類),這樣,它們均會編成一個后臺運行且不斷監(jiān)聽protocal buffer消息的socket server,如下圖所示:
2.2 各種常用函數(shù)
Libprocess+protocol buffer組合是mesos最底層最重要的消息傳遞基礎(chǔ)庫(沒有采用RPC機制),由于該庫采用了基于Protocal Buffer消息傳遞的通信機制),因而非常高效。Mesos常用的兩個頭文件是libprocess\include\process下的 process.hpp和protobuf.hpp,這兩個提供了用于消息傳遞的API,其中process.hpp是最核心的文件,提供了原始的接口, 而protobuf.hpp是在process.hpp基礎(chǔ)上,加入了ProtocalBuffer對象參數(shù),使ProtocalBuffer使用起來更 加容易。
(1) install
void install(void (T::*method)(P1C),P1 (M::*param1)() const);
安裝一個處理ProtocalBuffer消息的handler,其中,消息類型是M,該消息對應的處理函數(shù)是method,函數(shù)參數(shù)為M::*param1。舉例:mesos中slave/slave.cpp:
- install(
- &Slave::newMasterDetected,
- &NewMasterDetectedMessage::pid);
安裝一個處理NewMasterDetectedMessage(ProtocalBuffer對象)的handler,mesos slave一旦接收到該消息,便會調(diào)用newMasterDetected函數(shù)處理, 且該函數(shù)的輸入?yún)?shù)是NewMasterDetectedMessage消息中的pid屬性。
- void install(const std::string& name,void (T::*method)(const UPID&, const std::string&))
安裝一個處理字符串的handler,也就是說,當收到字符串name后,調(diào)用函數(shù)method進行處理。這個API在mesos中的典型應用時維持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)
向某個UPID上發(fā)送消息,其中UPID代表一個socket,里面含有ip和port信息,而消息message是ProtocalBuffer定義的對象。
(3) dispatch
- void dispatch(const UPID& pid,
- const std::tr1::shared_ptr >& f)
執(zhí)行進程pid中的函數(shù)f,為了提高效率,該函數(shù)并不會等到函數(shù)f執(zhí)行完成,而是采用了異步的方法:將函數(shù)f放入一個函數(shù)隊列,由另外一個進程(或者多個)不斷從隊列中獲取函數(shù),依次執(zhí)行。
(4) delay
- Timer delay(double secs,const PID& pid,void (T::*method)())
延遲secs秒調(diào)度進程pid中的方法method,并返回一個計數(shù)器,通過這個計時器,可取消該調(diào)度。
在mesos中,巧妙地通過該函數(shù)構(gòu)造了一個無限循環(huán)以不斷檢測空閑資源,并將之分配給各個框架,代碼如下:
- 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
非常有名的開源C++基礎(chǔ)庫,里面的STL非常高效方便,已被很多著名軟件采用。
4. Zookeeper
是一個針對大型分布式系統(tǒng)的可靠協(xié)調(diào)系統(tǒng),提供的功能包括:配置維護、名字服務、分布式同步、組服務等。 Mesos采用zookeeper解決master單點故障問題,使用zookeeper搭建一個master集群,當master出現(xiàn)故障時,選擇一個 standby master 變?yōu)閙aster。
5. glog
Google開源的C++日志庫,主用于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
開源 C++ 單元測試框架
7. 參考資料
(1)Mesos主頁: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/