leaderelection

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2023 License: MIT Imports: 17 Imported by: 0

README

leaderelection

leader election package for golang project based on etcd v3

Documentation

Index

Constants

View Source
const (
	JitterFactor = 1.2
)

Variables

This section is empty.

Functions

func GetTlsConfig added in v1.0.3

func GetTlsConfig(filePath string) (config *tls.Config, err error)

func RunOrDie

func RunOrDie(ctx context.Context, lec LeaderElectionConfig)

RunOrDie starts a client with the provided config or panics if the config fails to validate. RunOrDie blocks until leader election loop is stopped by ctx or it has stopped holding the leader lease

func SetProvider

func SetProvider(metricsProvider MetricsProvider)

SetProvider sets the metrics provider for all subsequently created work queues. Only the first call has an effect.

Types

type HealthzAdaptor

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

HealthzAdaptor associates the /healthz endpoint with the LeaderElection object. It helps deal with the /healthz endpoint being set up prior to the LeaderElection. This contains the code needed to act as an adaptor between the leader election code the health check code. It allows us to provide health status about the leader election. Most specifically about if the leader has failed to renew without exiting the process. In that case we should report not healthy and rely on the kubelet to take down the process.

func NewLeaderHealthzAdaptor

func NewLeaderHealthzAdaptor(timeout time.Duration) *HealthzAdaptor

NewLeaderHealthzAdaptor creates a basic healthz adaptor to monitor a leader election. timeout determines the time beyond the lease expiry to be allowed for timeout. checks within the timeout period after the lease expires will still return healthy.

func (*HealthzAdaptor) Check

func (l *HealthzAdaptor) Check(req *http.Request) error

Check is called by the healthz endpoint handler. It fails (returns an error) if we own the lease but had not been able to renew it.

func (*HealthzAdaptor) Name

func (l *HealthzAdaptor) Name() string

Name returns the name of the health check we are implementing.

func (*HealthzAdaptor) SetLeaderElection

func (l *HealthzAdaptor) SetLeaderElection(le *LeaderElector)

SetLeaderElection ties a leader election object to a HealthzAdaptor

type LeaderCallbacks

type LeaderCallbacks struct {
	// OnStartedLeading is called when a LeaderElector client starts leading
	OnStartedLeading func(context.Context)
	// OnStoppedLeading is called when a LeaderElector client stops leading
	OnStoppedLeading func()
	// OnNewLeader is called when the client observes a leader that is
	// not the previously observed leader. This includes the first observed
	// leader when the client starts.
	OnNewLeader func(identity string)
}

LeaderCallbacks are callbacks that are triggered during certain lifecycle events of the LeaderElector. These are invoked asynchronously.

possible future callbacks:

  • OnChallenge()

type LeaderElectionConfig

type LeaderElectionConfig struct {
	// Lock is the resource that will be used for locking
	Lock rl.Interface

	// LeaseDuration is the duration that non-leader candidates will
	// wait to force acquire leadership. This is measured against time of
	// last observed ack.
	//
	// A client needs to wait a full LeaseDuration without observing a change to
	// the record before it can attempt to take over. When all clients are
	// shutdown and a new set of clients are started with different names against
	// the same leader record, they must wait the full LeaseDuration before
	// attempting to acquire the lease. Thus LeaseDuration should be as short as
	// possible (within your tolerance for clock skew rate) to avoid a possible
	// long waits in the scenario.
	//
	// Core clients default this value to 15 seconds.
	// 锁的时长,通过该值判断是否超时,超时会被认定为丢失锁
	LeaseDuration time.Duration
	// RenewDeadline is the duration that the acting master will retry
	// refreshing leadership before giving up.
	//
	// Core clients default this value to 10 seconds.
	// 续约时长,每次 lease ttl 的时长
	RenewDeadline time.Duration
	// RetryPeriod is the duration the LeaderElector clients should wait
	// between tries of actions.
	//
	// Core clients default this value to 2 seconds.
	// 周期性进行重试抢锁的时长
	RetryPeriod time.Duration

	// Callbacks are callbacks that are triggered during certain lifecycle
	// events of the LeaderElector
	// 注册的自定义方法
	Callbacks LeaderCallbacks

	// WatchDog is the associated health checker
	// WatchDog may be null if it's not needed/configured.
	WatchDog *HealthzAdaptor

	// ReleaseOnCancel should be set true if the lock should be released
	// when the run context is cancelled. If you set this to true, you must
	// ensure all code guarded by this lease has successfully completed
	// prior to cancelling the context, or you may have two processes
	// simultaneously acting on the critical path.
	ReleaseOnCancel bool

	// Name is the name of the resource lock for debugging
	Name string
}

type LeaderElector

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

LeaderElector is a leader election client. 这个是进行选举的结构体

func NewLeaderElector

func NewLeaderElector(lec LeaderElectionConfig) (*LeaderElector, error)

NewLeaderElector creates a LeaderElector from a LeaderElectionConfig 根据 leader election 配置 LeaderElectionConfig 实例话一个选举其 LeaderElector

func (*LeaderElector) Check

func (le *LeaderElector) Check(maxTolerableExpiredLease time.Duration) error

Check will determine if the current lease is expired by more than timeout.

func (*LeaderElector) GetLeader

func (le *LeaderElector) GetLeader() string

GetLeader returns the identity of the last observed leader or returns the empty string if no leader has yet been observed. This function is for informational purposes. (e.g. monitoring, logs, etc.)

func (*LeaderElector) IsLeader

func (le *LeaderElector) IsLeader() bool

IsLeader returns true if the last observed leader was this client else returns false.

func (*LeaderElector) Run

func (le *LeaderElector) Run(ctx context.Context)

Run starts the leader election loop. Run will not return before leader election loop is stopped by ctx or it has stopped holding the leader lease 启动选举调用函数Run(ctx context.Context)

type LeaderMetric

type LeaderMetric interface {
	On(name string)
	Off(name string)
	SlowpathExercised(name string)
}

LeaderMetric instruments metrics used in leader election.

type MetricsProvider

type MetricsProvider interface {
	NewLeaderMetric() LeaderMetric
}

MetricsProvider generates various metrics used by the leader election.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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