Documentation
¶
Index ¶
- func GetLeader(c *clientv3.Client, leaderPath string) (*pdpb.Member, int64, error)
- type Leadership
- func (ls *Leadership) AddCampaignTimes()
- func (ls *Leadership) Campaign(leaseTimeout int64, leaderData string, cmps ...clientv3.Cmp) error
- func (ls *Leadership) Check() bool
- func (ls *Leadership) DeleteLeaderKey() error
- func (ls *Leadership) GetCampaignTimesNum() int
- func (ls *Leadership) GetClient() *clientv3.Client
- func (ls *Leadership) GetLeaderKey() string
- func (ls *Leadership) GetLease() *Lease
- func (ls *Leadership) IsPrimary() bool
- func (ls *Leadership) Keep(ctx context.Context)
- func (ls *Leadership) LeaderTxn(cs ...clientv3.Cmp) clientv3.Txn
- func (ls *Leadership) Reset()
- func (ls *Leadership) ResetCampaignTimes()
- func (ls *Leadership) SetLease(lease *Lease)
- func (ls *Leadership) SetPrimaryWatch(val bool)
- func (ls *Leadership) Watch(serverCtx context.Context, revision int64)
- type Lease
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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.
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.