ps lite原始碼學習筆記

2021-07-03 12:04:34 字數 4005 閱讀 7762

ps-lite**解析

首先檢視ps-lit的原始碼結構:

base

filter kv

parameter

proto ps

system

ps.h

ps_main.cc

其中,ps_main.cc是其程式入口,ps.h是其介面,其他資料夾則包含各個功能模組。下面詳細闡釋各部分。

ps_main.cc**很短,如下所示:

#include

"ps.h"

namespaceps}

}  // namespace ps

intmain(int argc, char *argv)

首先,只看main函式,main函式的流程很明晰,可以展示為下圖: 

那麼,main函式的功能不難理解,它就是用來建立workernode和severnode的。

ps.h:接下來,我們展開介面函式ps.h,理解了它,結合main函式中的流程,我們就不難理解整個ps-lite的架構,首先觀察其原始碼,筆者將整個介面分為4部分,分別是:

worker node apis

server node apis

scheduler node apis(目前為空)

more advanced apis

由於其中scheduler node apis 為空,所以我們分三部分來研究。

第一部分——worker node apis :

//syncopts是push和pull操作的選項集

struct syncopts;

//kvworker是用來收發引數伺服器key-value對的快取器。

template

class kvworker

//id是用來在引數伺服器中尋找kvstore的唯一標識,負數id的kvworker為系統專用。//注:宣告為explicit表示其不可被隱式轉換。

~kvworker()

// basic push and pull

intpush(const std::vector& keys,

const std::vector& vals,

const syncopts& opts = syncopts())

//push操作將一串key-value對傳到引數伺服器。

//它是乙個非阻塞呼叫,當要傳送的資訊進入系統的傳送佇列立刻返回。真正的push操作只在wait返回時或者提供的callback被呼叫時才結束。

intpull(const std::vector& keys,

std::vector* vals,

const syncopts& opts = syncopts())

//pull操作根據key將value從引數伺服器取出。

//它是乙個非阻塞呼叫,當要傳送的資訊進入系統的傳送佇列立刻返回。真正的pull操作只在wait返回時或者提供的callback被呼叫時才結束。

void

wait(int timestamp)

//wait操作用來阻塞下乙個請求,直到當前請求完成。

//  zero-copy push and pull

//預設情況下,push和pull操作都會先拷貝資料,然後使用者程式可以重寫或者刪除資料。然而,有些情況下memcpy十分耗費時間,所以我們用zpush和zpull以減少時間,但需要注意的是,使用者需要自己保持key和value的值不變直到請求完成。

intzpush(const std::shared_ptr>& keys,

const std::shared_ptr>& vals,

const syncopts& opts = syncopts())

intzpull(const std::shared_ptr>& keys,

std::vector* vals,

const syncopts& opts = syncopts()) …

private:

task gettask(const syncopts& opts);

kvcache* cache_;

};int workernodemain(int argc,char *argv);

//workernodemain是乙個workernode的main函式。

第二部分——server node apis :

//ionlinehandle是乙個用於處理請求的控制代碼例子。

template

class

ionlinehandle

virtual

~ionlinehandle()

inline

void

start(bool push, int timestamp, int cmd, void* msg)

//開始處理乙個worker的請求。

inline

void

finish()

//請求已被處理。

inline

void

init(key key, val& val)

//handle初始化。

inline

void

push(key recv_key, blob recv_val, val& my_val)

//處理來自worker節點的push請求

inline

void

pull(key recv_key, const val& my_val, blob& send_val)

//處理來自worker節點的pull請求

inline

void

setcaller(void *obj)

//接收caller

};//sever節點用onlineserver儲存key-value對,key-value對從worker發過來,被handle依次接收,然後儲存進kvstore中。

template typename

handle = ionlinehandle>

class

onlineserver;

~onlineserver()

kvstore* server()

private:

kvstore* server_ = null;

};//sever節點的main函式。

intcreateservernode(int argc, char *argv);

第三部分——more advanced apis :

//返回當前節點的全域性id

inline std::string mynodeid()

//返回我的所有節點的id

inline

intisworkernode() 

//判斷當前節點是否為worker節點

inline

intisservernode() 

//判斷當前節點是否為sever節點

inline

intisschedulernode() 

//判斷當前節點是否為scheduler節點

inline std::string schedulerid()

//返回schedulerid

inline

intmyrank()

//返回節點所在組的rankid,

inlineint

ranksize

() //返回當前組的節點數。

inline

intnumworkers()

inline

intnumservers()

inline

void

startsystem(int* argc, char ***argv)

inline

void

stopsystem()

inline

intrunsystem(int* argc, char ***argv)

HashMap原始碼學習筆記

hashmap的底層主要是基於陣列和鍊錶來實現的,它之所以有相當快的查詢速度主要是因為它是通過計算雜湊碼來決定儲存的位置。hashmap中主要是通過key的hashcode來計算hash值的,只要hashcode相同,計算出來的hash值就一樣。如果儲存的物件對多了,就有可能不同的物件所算出來的ha...

Vue原始碼學習筆記

最近偷懶好久沒有寫部落格了,一直想繼續vue學習系列,想深入vue原始碼來寫。結果發現自己層次不夠,對js的理解差好多。所以一直想寫一直擱置著。最近重新振作決心看完vue原始碼,並且以我們這類前端小白的角度來一步步弄懂vue原始碼。vue.js 本質上就是乙個包含各種邏輯的乙個function。而我...

jQuery原始碼學習筆記

整個jquery是乙個自呼叫的匿名函式 1 function global,factory 9return factory w 10 11 else 14 typeof window undefined window this,function window,noglobal 自呼叫函式大家都不陌生...