Version: v1.0.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2021 License: MIT Imports: 5 Imported by: 0




This section is empty.


This section is empty.


This section is empty.


type Elector

type Elector struct {
	// contains filtered or unexported fields

Elector Providing Object-Oriented API to manage the election process. Every node who wants to be the primary should initialize an Elector and participate in the election through it.

func NewElector

func NewElector(servers []string, timeout time.Duration, electionZnode string, electionPrefix string, electionAck string) (*Elector, error)

NewElector Create an Elector instance and ensures that both of {electionZnode} and {electionAck} has been created. Caller has no need to create them manually.

Elector helps the caller to engage the election by creating Ephemeral|Sequential Znode named as '{electionZnode}/{electionPrefix}', and when a primary won the election, it should write its identify info(e.g address) to Znode '{electionAck}'.


servers: a list of Zookeeper servers
timeout: Zookeeper session timeout. When a session timeouts, the node established it
is regarded as crashed, and lose it primary role(if it used to be) or its position
in primary waiting queue.
electionZnode, electionPrefix, electionAck: see Elector


error: not nil if failed to create those znodes.

func (*Elector) AckLeader

func (e *Elector) AckLeader(info string) error

AckLeader Write info into electionAck Znode if a node become the primary one successfully. And the others can understand that who is the primary through reading electionAck.

The new primary should only call this method when it has been prepared to serving requests, because outer world would be able to contact with it after AckLeader.

Secondary should never call this method.


conn: *zk.Conn created by Connect
electionAck: Znode path to store acknowledgement info
info: data to be written into electionAck


error: not nil if failed to write info.

func (*Elector) CreateProposal

func (e *Elector) CreateProposal() (string, error)

CreateProposal Create a Sequential|Ephemeral Znode named as '{electionZnode}/{electionPrefix}'. Because Sequential Flag is applied, the name of finally created Znode will be '{electionZnode}/{electionPrefix}-XXXXXXXXXX'. As mentioned in Zookeeper's programming guide, the suffix will be a sequential, 0-padded 10-digits decimal number. And the part of child's name in the new Znode, i.e. '{electionPrefix}-XXXXXXXXXX' is called "Proposal" of caller SheetFS node(MasterNode or DataNode). The proposal will be recorded by the Elector automatically, and is also returned for logging or other usages.


string: proposal of this node
error: not nil if fail to create proposal Znode.

func (*Elector) TryBeLeader

func (e *Elector) TryBeLeader() (bool, string, <-chan zk.Event, error)

TryBeLeader Check all children Znode of electionZnode(or all proposals) with smaller suffix than proposal of called itself. Due to sequential guarantee of Zookeeper, a node always gain a consistent view of its proceeding proposals. If the caller owns smallest proposal, than it is granted as primary, or it should turn into a secondary one, watching for its closest predecessor. When the latter crashed, its proposal will be deleted by Zookeeper according to Ephemeral flag, and this node is awaken. Caller should call this method again after that, to TryBeLeader.


bool: whether this node is primary or not
string: full path of znode to be watched, for logging or other purposes.
<-chan zk.Event: a channel for notification of crashed predecessor. If the predecessor
died, a Event can be received from this channel, and caller should call this method
error: not nil if failed to read all proposals or watch predecessor.

Source Files


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL