• 沒有找到結果。

節點管理

第三章 系統設計與實作

3.6 實作細節

3.6.2 節點管理

26

3-10 使用者註冊 Message Flow

圖3-10 為 Client 進行註冊的簡易 Message Flow。一開始先向 Static Subscribe Server 要求 Public Key,取得後,將註冊資訊 RegMsg 用 Public Key 加密後傳送,

確保註冊資料的安全。註冊過程中的轉傳註冊資訊未必會發生,因為可能Client 恰好就是屬於此Static Subscribe Server 管理。

3.6.2 節點管理

節點管理主要是由Static Subscribe Server 中的 Node Table 負責。每個 Static Subscribe Server 中的 Node Table,儲存管理的所有節點資訊,新 Client 進行註冊 時,將註冊資訊加入Node Table 之中,由 Node Table 內部進行組織和分配的運 算。

27

3-11 Node Table 資料結構

圖3-11 為 Node Table 的資料結構,其中 D 代表 Dynamic Subscribe Server 節 點,C代表 Common Node 節點,虛線框內代表一個群組,由其中的 Dynamic Subscribe Server 管理。群組內的 Common Node 節點依照 IP Address 排序,而群 組之間也是依照IP Address 的順序排列。此外,為了考量節點數量增加時,搜尋 節點花費的時間成本,Node Table 內存有兩個 Search Tree 來幫助搜尋節點。

3-12 B-Tree 資料結構

28

B-Tree of order m, may be an empty tree, or it holds the properties:

1. Root has at least two child nodes.

2. Except root and failure node, all other nodes have at leastm/2child nodes.

3. Failure nodes are at the same level.

4. Search time complexity = height of search tree.

如圖3-12,我們採用B-Tree做為Search Tree,從網路上取得的B-Tree Source Code[18]加以修改,表 3-1 為B-Tree的相關特性。每個B-Tree Node以IP Address 為Key,另存放Dynamic Subscribe Server或Common Node在Node Table中節點的指 標,進行搜尋時,以IP Address搜尋到B-Tree中的Key值後,再從相對應的指標找 到Node Table中存放網路節點的位置。一般Search Tree找尋目標Key值,若搜尋到 則回報相關資訊,找不到則搜尋失敗。我們將此Search Tree新增功能,能夠找到 比Key值小且最接近的目標。在Node Table中包含以下操作:

Add:新增 Common Node 節點。

當Client 註冊節點時,Static Subscribe Server 將節點資訊新增到 Node Table 中。由Common Node Search Tree 搜尋到比新增節點 IP Address 小且最接近的節 點位置後插入,如圖3-13。

29

3-13 Node Table: Add

Remove:移除 Common Node 節點。

當Common Node 斷線或是停止運作時,由 Dynamic Subscribe Server 回報,

將此Common Node 的 IP Address 送入 Node Table 之中移除。同樣利用 Search Tree 搜尋到IP Address 小且最接近的節點後,將其下一節點移除,如圖 3-14。

3-14 Node Table: Remove

Add Group:新增群組。

當群組中的Slave Common Node 超過系統設置的 Max Group Number 之後,

從一群組的Slave Common Node 之中選取 Efficiency 較高者,使其成為新群組的 Dynamic Subscribe Server,將原群組中的 Slave Common Node 分成兩部份,比對 原群組的Dynamic Subscribe Server 與新 Dynamic Subscribe Server 的 IP Address 後,IP 位置較小者,管理前半部,較大者管理後半部。當群組的數目到達 Max Group Number 時,將 Max Group Number 增為兩倍,如圖 3-15。

30

3-15 Node Table: Add Group

Remove Group:移除群組。

當群組中的Slave Common Node 為 0 或是少於 1/4 Max Group Number,將此 群組中的Slave Common Node 分為前後兩半,分別加入此群組中的前後群組,而 Dynamic Subscribe Server 變更回 Common Node 的身份,由 Add 加入系統中。當 群組數目僅為Max Group Number 的 1/4 時,將 Max Group Number 減為一半,如 圖3-16。

3-16 Node Table: Remove Group

31

Balance: 平衡群組間的節點數目。

因為Dynamic Subscribe Server 是由一般使用者的 Common Node 之中選取,

為了避免少數使用者負擔較大,我們希望能維持群組之間節點數目的平衡,因 此,在前面提及的Add、Remove、Add Group、Remove Group 操作之後,會再 進行Balance。

首先計算 Static Subscribe Server 之中每個群組應該負責管理的 Common Node 數目平均值:

Avg. Common Node No. of Each Group = All Common Node No. / Group No.

取此平均值的Ceiling 和 Floor,定為每個群組管理 Common Node 數目的上下界:

Upper Bound = Ceiling (Avg. Common Node No. of Each Group) Lower Bound = Floor (Avg. Common Node No. of Each Group)

Balance 分為 Add Balance 和 Remove Balance 兩種模式,另分為 Previous Balance 和 Next Balance 兩種方向。進行 Add 或 Remove Group 之後,會進行 Add Balance,而 Remove 或 Add Group 之後,會進行 Remove Balance。Add Balance 是指進行Balance 操作的群組管理的 Common Node 數目超過 Upper Bound,將部 份的節點移往鄰近的群組管理;而Remove Balance 則是指進行 Balance 操作的群 組管理的Common Node 數目少於 Lower Bound,由鄰近群組調動過來。Direction 的選擇一般是依據進行Balance 的群組和兩邊群組的節點數目比較的結果。進行 Add Balance 時,若是 Previous Group 的節點數目較少,就進行 Previous Balance,

反之進行Next Balance;進行 Remove Balance 時,若是 Previous Group 的節點數 目較多,進行Previous Balance,反之進行 Next Balance。

32

此外,為了避免連續新增的節點落在Node Table 中兩極處,造成群組數目的 偏斜或是Balance 的運作需要花費較多的時間,如圖 3-17,所以我們將群組之間 的關係造成環狀架構,如圖3-18。

3-17 線性群組架構的 Balance

3-18 環狀群組架構的 Balance

因為環狀架構的緣故,IP Address 較大的節點和 IP Address 較小的節點可能 會落在同一群組中,因此在Balance 選擇 Direction 時,當鄰近兩側的群組擁有相 同節點數目時,會傾向於讓IP Address 分佈正常的方式,如圖 3-19。

33

3-19 Balance 示例

在 Add Balance 模式中,群組的 Common Node 移動數目取決於群組超過 Upper Bound 的數目和移動目標群組少於 Lower Bound 的數目,若是兩數相較前 者較大或相等,則移動數目設定為前者;反之移動數目設定為群組超過 Lower Bound 的數目。Remove 模式中,群組的 Common Node 移動數目取決於群組少 於Lower Bound 的數目和調動目標群組多於 Upper Bound 的數目,若是兩數相較 前者較大或相等,則移動數目設定為前者;反之移動數目設定為群組少於Upper Bound 的數目。演算法如下:

Add Balance Algorithm:

if(no. of group > upper bound ){

if(no. of group – upper bound >= lower bound – no. of target group){

move no. = no. of group – upper bound;

} else{

move no. = no. of group – lower bound;

} }

34

else if(no. of group == upper bound && upper bound > lower bound &&

no. of target group < lower bound){

move no. = 1;

}

Remove Balance Algorithm:

if(no. of group < lower bound){

if( lower bound – no. of group >= no. oftargetgroup – upper bound){

move no. = lower bound – no. of group;

else if(no. of group ==lower bound && upper bound > lower bound

&& no. oftarget group > upperbound){

move no. = 1;

}

其中no. of X 代表 Common Node Number of X,move no.代表所需移動的 Common Node 數目,move no.預設為 0,若經過以上演算法運算後,move no.依 然為0,則判定不需要進行 Balance,程序結束,否則依照 move no.數目將 Common Node 移往目標群組或是從目標群組調動過來,繼續往目標群組進行 Balance。

以上演算法的基本精神是將進行Balance 的群組維持 Upper Bound 或 Lower Bound 的 Common Node 數量,且以移動數目較少為原則,並且繼續往目標群組 進行Balance,直到不需進行 Balance 的群組為止。經過 Balance 運作後,群組的 Common Node 數目分佈能大致成為一個均衡的狀態。

35

Static Subscribe Server 管理的節點組織依照 Node Table 運算的結果分配,傳 送Command Message 給各群組的 Dynamic Subscribe Server,使其依照分配結果 和Common Node 聯繫,交換訊息。

圖 3-20 中,一開始僅有兩位使用者加入系統,接著第三位使用者加入,系 統預設的初始 Max Group Number 為 2,所以 3 個使用者超過,從其中選取 Efficiency 值較高者成為新群組的 Dynamic Subscribe Server。接著第四位使用者 加入,再度超過Max Group Number,選取 Dynamic Subscribe Server 成立新的群 組。此時群組數目和Max Group Number 相等,Max Group Number 成長為兩倍,

Max Group Number = 2 x Max Group Number,變成 4。

3-20 節點管理範例 1

相關文件