leaderelection

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2016 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package leaderelection implements leader election of a set of endpoints. It uses an annotation in the endpoints object to store the record of the election state.

This implementation does not guarantee that only one client is acting as a leader (a.k.a. fencing). A client observes timestamps captured locally to infer the state of the leader election. Thus the implementation is tolerant to arbitrary clock skew, but is not tolerant to arbitrary clock skew rate.

However the level of tolerance to skew rate can be configured by setting RenewDeadline and LeaseDuration appropriately. The tolerance expressed as a maximum tolerated ratio of time passed on the fastest node to time passed on the slowest node can be approximately achieved with a configuration that sets the same ratio of LeaseDuration to RenewDeadline. For example if a user wanted to tolerate some nodes progressing forward in time twice as fast as other nodes, the user could set LeaseDuration to 60 seconds and RenewDeadline to 30 seconds.

While not required, some method of clock synchronization between nodes in the cluster is highly recommended. It's important to keep in mind when configuring this client that the tolerance to skew rate varies inversely to master availability.

Larger clusters often have a more lenient SLA for API latency. This should be taken into account when configuring the client. The rate of leader transitions should be monitored and RetryPeriod and LeaseDuration should be increased until the rate is stable and acceptably low. It's important to keep in mind when configuring this client that the tolerance to API latency varies inversely to master availability.

DISCLAIMER: this is an alpha API. This library will likely change significantly or even be removed entirely in subsequent releases. Depend on this API at your own risk.

Index

Constants

View Source
const (
	JitterFactor = 1.2

	LeaderElectionRecordAnnotationKey = "control-plane.alpha.kubernetes.io/leader"

	DefaultLeaseDuration = 15 * time.Second
	DefaultRenewDeadline = 10 * time.Second
	DefaultRetryPeriod   = 2 * time.Second
)

Variables

This section is empty.

Functions

func BindFlags

BindFlags binds the common LeaderElectionCLIConfig flags to a flagset

func DefaultLeaderElectionConfiguration

func DefaultLeaderElectionConfiguration() componentconfig.LeaderElectionConfiguration

func RunOrDie

func RunOrDie(lec LeaderElectionConfig)

RunOrDie starts a client with the provided config or panics if the config fails to validate.

Types

type LeaderCallbacks

type LeaderCallbacks struct {
	// OnStartedLeading is called when a LeaderElector client starts leading
	OnStartedLeading func(stop <-chan struct{})
	// 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 {
	// EndpointsMeta should contain a Name and a Namespace of an
	// Endpoints object that the LeaderElector will attempt to lead.
	EndpointsMeta api.ObjectMeta
	// Identity is a unique identifier of the leader elector.
	Identity string

	Client        client.Interface
	EventRecorder record.EventRecorder

	// LeaseDuration is the duration that non-leader candidates will
	// wait to force acquire leadership. This is measured against time of
	// last observed ack.
	LeaseDuration time.Duration
	// RenewDeadline is the duration that the acting master will retry
	// refreshing leadership before giving up.
	RenewDeadline time.Duration
	// RetryPeriod is the duration the LeaderElector clients should wait
	// between tries of actions.
	RetryPeriod time.Duration

	// Callbacks are callbacks that are triggered during certain lifecycle
	// events of the LeaderElector
	Callbacks LeaderCallbacks
}

type LeaderElectionRecord

type LeaderElectionRecord struct {
	HolderIdentity       string           `json:"holderIdentity"`
	LeaseDurationSeconds int              `json:"leaseDurationSeconds"`
	AcquireTime          unversioned.Time `json:"acquireTime"`
	RenewTime            unversioned.Time `json:"renewTime"`
	LeaderTransitions    int              `json:"leaderTransitions"`
}

LeaderElectionRecord is the record that is stored in the leader election annotation. This information should be used for observational purposes only and could be replaced with a random string (e.g. UUID) with only slight modification of this code. TODO(mikedanese): this should potentially be versioned

type LeaderElector

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

LeaderElector is a leader election client.

possible future methods:

  • (le *LeaderElector) IsLeader()
  • (le *LeaderElector) GetLeader()

func NewLeaderElector

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

NewLeadereElector creates a LeaderElector from a LeaderElecitionConfig

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.

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

Run starts the leader election loop

Jump to

Keyboard shortcuts

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