election

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Elector

type Elector interface {
	// CreateLeadship create a leadership
	CreateLeadship(purpose string, nodeName, nodeValue string, allowCampaign bool, becomeLeader, becomeFollower func(string) bool) *Leadership
	// Client etcd clientv3
	Client() *clientv3.Client
}

Elector a leader elector

func NewElector

func NewElector(client *clientv3.Client, options ...ElectorOption) (Elector, error)

NewElector create a elector

type ElectorOption

type ElectorOption func(*electorOptions)

ElectorOption elector option

func WithEmbedEtcd

func WithEmbedEtcd(etcd *embed.Etcd) ElectorOption

WithEmbedEtcd with embed etcd

func WithLeaderLeaseSeconds

func WithLeaderLeaseSeconds(value int64) ElectorOption

WithLeaderLeaseSeconds set leader lease in seconds

func WithLockIfBecomeLeader

func WithLockIfBecomeLeader(value bool) ElectorOption

WithLockIfBecomeLeader set lock enable flag if become leader, If true, will add a distributed lock, and will unlock on become follower, ensure that the other nodes can be changed to leaders after the previous leader has processed the role changes.

func WithPrefix

func WithPrefix(value string) ElectorOption

WithPrefix set data prefix in embed etcd server

type LeaderLease

type LeaderLease struct {
	// contains filtered or unexported fields
}

LeaderLease is used for renewing leadership.

func (*LeaderLease) Close

func (l *LeaderLease) Close(pctx context.Context) error

Close releases the lease.

func (*LeaderLease) IsExpired

func (l *LeaderLease) IsExpired() bool

IsExpired checks if the lease is expired. If it returns true, current node should step down and try to re-elect again.

type Leadership

type Leadership struct {
	// contains filtered or unexported fields
}

Leadership is used to manage the leadership campaigning.

func (*Leadership) ChangeLeaderTo

func (ls *Leadership) ChangeLeaderTo(newLeader string) error

ChangeLeaderTo change leader to new leader

func (*Leadership) Check

func (ls *Leadership) Check() bool

Check returns whether the leadership is still available

func (*Leadership) CurrentLeader

func (ls *Leadership) CurrentLeader() (string, int64, error)

CurrentLeader returns the current leader

func (*Leadership) DoIfLeader

func (ls *Leadership) DoIfLeader(conditions []clientv3.Cmp, ops ...clientv3.Op) (bool, error)

DoIfLeader do if i'm leader

func (*Leadership) ElectionLoop

func (ls *Leadership) ElectionLoop(ctx context.Context)

ElectionLoop start lead election

func (*Leadership) GetLease

func (ls *Leadership) GetLease() *LeaderLease

GetLease gets the lease of leadership, only if leadership is valid, i.e the owner is a true leader, the lease is not nil.

func (*Leadership) Stop

func (ls *Leadership) Stop() error

Stop stop the current leadship

Jump to

Keyboard shortcuts

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