Fabric網絡組織與主節點選舉

1、Fabric網絡組織

Fabric網絡組織按以下結構組成:Fabric網絡-->Channel通道-->組織(成員)-->節點。即整個網絡由數個通道組成,每一個通道都由多個組織構成,而每一個組織內部由數個節點組成(可能由功能或其餘劃分方式分爲多個節點)。以下圖所示: 算法

在這裏插入圖片描述

2、主節點(leader peer)選舉

一個組織(實際上是成員)在一個通道上能夠有多個Peer節點,這時候爲了提升通訊效率,須要選舉出來一個主節點(leader)做爲表明和排序服務節點通訊,負責從排序服務節點處獲取最新的區塊並在組織內部同步。有以下兩種方式:微信

1. 靜態指定

配置文件中配置網絡

# Gossip related configuration
 gossip:
        # Defines whenever peer will initialize dynamic algorithm for
        # "leader" selection, where leader is the peer to establish
        # connection with ordering service and use delivery protocol
        # to pull ledger blocks from ordering service
 useLeaderElection: false
        # Statically defines peer to be an organization "leader",
        # where this means that current peer will maintain connection
        # with ordering service and disseminate block across peers in
        # its own organization
 orgLeader: true
複製代碼

2. 動態選舉

相關配置:ide

# Gossip related configuration
 gossip:
        # Leader election service configuration
 election:
            # Longest time peer wait for stable membership during leader election startup (unit: second)
 startupGracePeriod: 15s
            # Interval gossip membership sampled to check its stability (unit: second)
 membershipSampleInterval: 1s
            # Time pass since last declaration message before peer decide to go to election (unit: second)
 leaderAliveThreshold: 10s
            # Time between peer sends propose message and declare itself as a leader (sends declaration message) (unit: second)
 leaderElectionDuration: 5s
複製代碼

3. leader節點選舉流程

選舉流程(簡要):區塊鏈

若是當前沒有leader,進入選舉算法
    若是當前是leader:廣播leadership declearation,若是收到比本身小的leadership declearation,本身變爲follower;
    若是當前是follower:指定時間內沒有收到leadership declearation,則認爲leader離線了,進入選舉流程
複製代碼

選舉算法(簡要):this

廣播提議本身爲leader消息
各個節點收集選舉消息
比對ID,若是本身ID最小,則本身爲leader
複製代碼

詳細過程以下圖所示: spa

在這裏插入圖片描述

僞代碼實現:code

// Gossip leader election module
// Algorithm properties:
// - Peers break symmetry by comparing IDs
// - Each peer is either a leader or a follower,
// and the aim is to have exactly 1 leader if the membership view
// is the same for all peers
// - If the network is partitioned into 2 or more sets, the number of leaders
// is the number of network partitions, but when the partition heals,
// only 1 leader should be left eventually
// - Peers communicate by gossiping leadership proposal or declaration messages

// The Algorithm, in pseudo code:
//
//
// variables:
// leaderKnown = false
//
// Invariant:
// Peer listens for messages from remote peers
// and whenever it receives a leadership declaration,
// leaderKnown is set to true
//
// Startup():
// wait for membership view to stabilize, or for a leadership declaration is received
// or the startup timeout expires.
// goto SteadyState()
//
// SteadyState():
// while true:
// If leaderKnown is false:
// LeaderElection()
// If you are the leader:
// Broadcast leadership declaration
// If a leadership declaration was received from
// a peer with a lower ID,
// become a follower
// Else, you're a follower:
// If haven't received a leadership declaration within
// a time threshold:
// set leaderKnown to false
//
// LeaderElection():
// Gossip leadership proposal message
// Collect messages from other peers sent within a time period
// If received a leadership declaration:
// return
// Iterate over all proposal messages collected.
// If a proposal message from a peer with an ID lower
// than yourself was received, return.
// Else, declare yourself a leader

複製代碼

4. 消息定義

// Leadership Message is sent during leader election to inform
// remote peers about intent of peer to proclaim itself as leader
message LeadershipMessage {
    bytes pki_id        = 1;
    PeerTime timestamp = 2;
    bool is_declaration = 3;
}
複製代碼

關注區塊鏈技術,可關注微信公衆號: orm

相關文章
相關標籤/搜索