作者 | 白松
【注:本文為原創(chuàng),引用轉(zhuǎn)載需與博主聯(lián)系?!?/p>
Apache Giraph is an iterative graph processing system built for high scalability. For example, it is currently used at Facebook to analyze the social graph formed by users and their connections. Giraph originated as the open-source counterpart to Pregel, the graph processing architecture developed at Google and described in a 2010 paper. Both systems are inspired by the Bulk Synchronous Parallelmodel of distributed computation introduced by Leslie Valiant. Giraph adds several features beyond the basic Pregel model, including master computation, sharded aggregators, edge-oriented input, out-of-core computation, and more. With a steady development cycle and a growing community of users worldwide, Giraph is a natural choice for unleashing the potential of structured datasets at a massive scale.
Giraph基于Hadoop而建,將MapReduce中Mapper進(jìn)行封裝,未使用reducer。在Mapper中進(jìn)行多次迭代,每次迭代等價(jià)于BSP模型中的SuperStep。一個(gè)Hadoop Job等價(jià)于一次BSP作業(yè)?;A(chǔ)結(jié)構(gòu)如下圖所示。
每部分的功能如下:
1. ZooKeeper: responsible for computation state
–partition/worker mapping
–global state: #superstep
–checkpoint paths, aggregator values, statistics
2. Master: responsible for coordination
–assigns partitions to workers
–coordinates synchronization
–requests checkpoints
–aggregates aggregator values
–collects health statuses
3. Worker: responsible for vertices
–invokes active vertices compute() function
–sends, receives and assigns messages
–computes local aggregation values
(1)實(shí)驗(yàn)環(huán)境
三臺(tái)服務(wù)器:test165、test62、test63。test165同時(shí)是JobTracker和TaskTracker.
測試?yán)樱汗倬W(wǎng)自帶的SSSP程序,數(shù)據(jù)是自己模擬生成。
運(yùn)行命令:Hadoop jar giraph-examples-1.0.0-for-hadoop-0.20.203.0-jar-with-dependencies.jar org.apache.giraph.GiraphRunner org.apache.giraph.examples.SimpleShortestPathsVertex -vif org.apache.giraph.io.formats.JsonLongDoubleFloatDoubleVertexInputFormat -vip /user/giraph/SSSP -of org.apache.giraph.io.formats.IdWithValueTextOutputFormat -op /user/giraph/output-sssp-debug-7 -w 5
(2)為節(jié)約空間,下文中所有代碼均為核心代碼片段。
(3)core-site.xml中hadoop.tmp.dir的路徑設(shè)為:/home/hadoop/hadooptmp
(4)寫本文是多次調(diào)試完成的,故文中的JobID不一樣,讀者可理解為同一JobID.
(5)后續(xù)文章也遵循上述規(guī)則。
Giraph中自定義org.apache.giraph.graph.GraphMapper類來繼承Hadoop中的 org.apache.hadoop.mapreduce.Mapper<Object,Object,Object,Object>類,覆寫了setup()、map()、cleanup()和run()方法。GraphMapper類的說明如下:
“This mapper that will execute the BSP graph tasks alloted to this worker. All tasks will be performed by calling the GraphTaskManager object managed by this GraphMapper wrapper classs. Since this mapper will not be passing data by key-value pairs through the MR framework, the Mapper parameter types are irrelevant, and set to Object type.”
BSP的運(yùn)算邏輯被封裝在GraphMapper類中,其擁有一GraphTaskManager對(duì)象,用來管理Job的tasks。每個(gè)GraphMapper對(duì)象都相當(dāng)于BSP中的一個(gè)計(jì)算節(jié)點(diǎn)(compute node)。
在GraphMapper類中的setup()方法中,創(chuàng)建GraphTaskManager對(duì)象并調(diào)用其setup()方法進(jìn)行一些初始化工作。如下:
map()方法為空,因?yàn)樗胁僮鞫急环庋b在了GraphTaskManager類中。在run()方法中調(diào)用GraphTaskManager對(duì)象的execute()方法進(jìn)行BSP迭代計(jì)算。
功能:The Giraph-specific business logic for a single BSP compute node in whatever underlying type of cluster our Giraph job will run on. Owning object will provide the glue into the underlying cluster framework and will call this object to perform Giraph work.
下面講述setup()方法,代碼如下:
###?依次介紹每個(gè)方法的功能:
1、locateZookeeperClasspath(zkPathList)
找到ZK jar的本地副本,其路徑為:/home/hadoop/hadooptmp/mapred/local/taskTracker/root/jobcache/job_201403270456_0001/jars/job.jar ,用于啟動(dòng)ZooKeeper服務(wù)。
2、startZooKeeperManager(),初始化和配置ZooKeeperManager。
定義如下:
3、org.apache.giraph.zk.ZooKeeperManager 類
功能:Manages the election of ZooKeeper servers, starting/stopping the services, etc.
ZooKeeperManager類的setup()定義如下:
createCandidateStamp()方法在 HDFS上 的_bsp/_defaultZkManagerDir/job_201403301409_0006/_task 目錄下為每個(gè)task創(chuàng)建一個(gè)文件,文件內(nèi)容為空。文件名為本機(jī)的Hostname+taskPartition,如下截圖:
運(yùn)行時(shí)指定了5個(gè)workers(-w 5),再加上一個(gè)master,所有上面有6個(gè)task。
getZooKeeperServerList()方法中,taskPartition為0的task會(huì)調(diào)用createZooKeeperServerList()方法創(chuàng)建ZooKeeper server List,也是創(chuàng)建一個(gè)空文件,通過文件名來描述Zookeeper servers。
首先獲取taskDirectory(_bsp/_defaultZkManagerDir/job_201403301409_0006/_task)目錄下文件,如果當(dāng)前目錄下有文件,則把文件名(Hostname+taskPartition)中的Hostname和taskPartition存入到hostNameTaskMap中。掃描taskDirectory目錄后,若hostNameTaskMap的size大于serverCount(等于GiraphConstants.java中的ZOOKEEPER_SERVER_COUNT變量,定義為1),就停止外層的循環(huán)。外層循環(huán)的目的是:因?yàn)閠askDirectory下的文件每個(gè)task文件時(shí)多個(gè)task在分布式條件下創(chuàng)建的,有可能task 0在此創(chuàng)建server List時(shí),別的task還沒有生成后task文件。Giraph默認(rèn)為每個(gè)Job啟動(dòng)一個(gè)ZooKeeper服務(wù),也就是說只有一個(gè)task會(huì)啟動(dòng)ZooKeeper服務(wù)。
經(jīng)過多次測試,task 0總是被選為ZooKeeper Server ,因?yàn)樵谕贿M(jìn)程中,掃描taskDirectory時(shí),只有它對(duì)應(yīng)的task 文件(其他task的文件還沒有生成好),然后退出for循環(huán),發(fā)現(xiàn)hostNameTaskMap的size等于1,直接退出while循環(huán)。那么此處就選了test162 0。
最后,創(chuàng)建了文件:_bsp/_defaultZkManagerDir/job_201403301409_0006/zkServerList_test162 0
onlineZooKeeperServers(),根據(jù)zkServerList_test162 0文件,Task 0 先生成zoo.cfg配置文件,使用ProcessBuilder來創(chuàng)建ZooKeeper服務(wù)進(jìn)程,然后Task 0 再通過socket連接到ZooKeeper服務(wù)進(jìn)程上,最后創(chuàng)建文件 _bsp/_defaultZkManagerDir/job_201403301409_0006/_zkServer/test162 0 來標(biāo)記master任務(wù)已完成。worker一直在進(jìn)行循環(huán)檢測master是否生成好 _bsp/_defaultZkManagerDir/job_201403301409_0006/_zkServer/test162 0,即worker等待直到master上的ZooKeeper服務(wù)已經(jīng)啟動(dòng)完成。
啟動(dòng)ZooKeeper服務(wù)的命令如下:
4、determineGraphFunctions()。
GraphTaskManager類中有CentralizedServiceMaster對(duì)象和CentralizedServiceWorker 對(duì)象,分別對(duì)應(yīng)于master和worker。每個(gè)BSP compute node扮演的角色判定邏輯如下:
a) If not split master, everyone does the everything and/or running ZooKeeper.
b) If split master/worker, masters also run ZooKeeper
c) If split master/worker == true and giraph.zkList is set, the master will not instantiate a ZK instance, but will assume a quorum is already active on the cluster for Giraph to use.
該判定在GraphTaskManager 類中的靜態(tài)方法determineGraphFunctions()中定義,片段代碼如下:
默認(rèn)的,Giraph會(huì)區(qū)分master和worker。會(huì)在master上面啟動(dòng)zookeeper服務(wù),不會(huì)在worker上啟動(dòng)ZooKeeper服務(wù)。那么Task 0 就是master+ZooKeeper,其他Tasks就是workers
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文標(biāo)題:Giraph源碼分析(一)—啟動(dòng)ZooKeeper服務(wù)-創(chuàng)新互聯(lián)
文章地址:http://jinyejixie.com/article36/dijcsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、ChatGPT、網(wǎng)站改版、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容