election

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLeader

func GetLeader(c *clientv3.Client, leaderPath string) (*pdpb.Member, int64, error)

GetLeader gets the corresponding leader from etcd by given leaderPath (as the key).

Types

type Leadership

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

Leadership is used to manage the leadership campaigning.

func NewLeadership

func NewLeadership(client *clientv3.Client, leaderKey, purpose string) *Leadership

NewLeadership creates a new Leadership.

func (*Leadership) AddCampaignTimes

func (ls *Leadership) AddCampaignTimes()

AddCampaignTimes is used to add the campaign times of the leader.

func (*Leadership) Campaign

func (ls *Leadership) Campaign(leaseTimeout int64, leaderData string, cmps ...clientv3.Cmp) error

Campaign is used to campaign the leader with given lease and returns a leadership

func (*Leadership) Check

func (ls *Leadership) Check() bool

Check returns whether the leadership is still available.

func (*Leadership) DeleteLeaderKey

func (ls *Leadership) DeleteLeaderKey() error

DeleteLeaderKey deletes the corresponding leader from etcd by the leaderPath as the key.

func (*Leadership) GetCampaignTimesNum

func (ls *Leadership) GetCampaignTimesNum() int

GetCampaignTimesNum is used to get the campaign times of the leader within `campaignTimesRecordTimeout`. Need to make sure `AddCampaignTimes` is called before this function.

func (*Leadership) GetClient

func (ls *Leadership) GetClient() *clientv3.Client

GetClient is used to get the etcd client.

func (*Leadership) GetLeaderKey

func (ls *Leadership) GetLeaderKey() string

GetLeaderKey is used to get the leader key of etcd.

func (*Leadership) GetLease

func (ls *Leadership) GetLease() *Lease

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) IsPrimary

func (ls *Leadership) IsPrimary() bool

IsPrimary gets the primary watch flag.

func (*Leadership) Keep

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

Keep will keep the leadership available by update the lease's expired time continuously

func (*Leadership) LeaderTxn

func (ls *Leadership) LeaderTxn(cs ...clientv3.Cmp) clientv3.Txn

LeaderTxn returns txn() with a leader comparison to guarantee that the transaction can be executed only if the server is leader.

func (*Leadership) Reset

func (ls *Leadership) Reset()

Reset does some defer jobs such as closing lease, resetting lease etc.

func (*Leadership) ResetCampaignTimes

func (ls *Leadership) ResetCampaignTimes()

ResetCampaignTimes is used to reset the campaign times of the leader.

func (*Leadership) SetLease

func (ls *Leadership) SetLease(lease *Lease)

SetLease sets the lease of leadership.

func (*Leadership) SetPrimaryWatch

func (ls *Leadership) SetPrimaryWatch(val bool)

SetPrimaryWatch sets the primary watch flag.

func (*Leadership) Watch

func (ls *Leadership) Watch(serverCtx context.Context, revision int64)

Watch is used to watch the changes of the leadership, usually is used to detect the leadership stepping down and restart an election as soon as possible.

type Lease

type Lease struct {
	// purpose is used to show what this election for
	Purpose string

	ID atomic.Value // store as clientv3.LeaseID
	// contains filtered or unexported fields
}

Lease is used as the low-level mechanism for campaigning and renewing elected leadership. The way to gain and maintain leadership is to update and keep the lease alive continuously.

func NewLease

func NewLease(client *clientv3.Client, purpose string) *Lease

NewLease creates a new Lease instance.

func (*Lease) Close

func (l *Lease) Close() error

Close releases the lease.

func (*Lease) Grant

func (l *Lease) Grant(leaseTimeout int64) error

Grant uses `lease.Grant` to initialize the lease and expireTime.

func (*Lease) IsExpired

func (l *Lease) IsExpired() bool

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

func (*Lease) KeepAlive

func (l *Lease) KeepAlive(ctx context.Context)

KeepAlive auto renews the lease and update expireTime.

Jump to

Keyboard shortcuts

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