leadelection

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("item not found")

ErrNotFound means item not found in nacos

Functions

func RunOrDie

func RunOrDie(ctx context.Context, lec LeaderElectionConfig, logger logging.ILogger)

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

Types

type ILock

type ILock interface {
	// Get returns the LeaderElectionRecord
	Get(ctx context.Context) (*LeaderElectionRecord, []byte, error)

	// Create attempts to create a LeaderElectionRecord
	Create(ctx context.Context, ler LeaderElectionRecord) error

	// Update will update and existing LeaderElectionRecord
	Update(ctx context.Context, ler LeaderElectionRecord) error

	// RecordEvent is used to record events
	RecordEvent(string)

	// Identity will return the locks Identity
	Identity() string

	// Describe is used to convert details on current resource lock
	// into a string
	Describe() string
}

ILock is a holder for LeaderElectionRecord

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)
	OnStopRunning func()
}

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 ILock

	// 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.
	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

	// 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
}

LeaderElectionConfig is config for LeaderElector

type LeaderElectionRecord

type LeaderElectionRecord struct {
	// HolderIdentity is the ID that owns the lease. If empty, no one owns this lease and
	// all callers may acquire. Versions of this library prior to Kubernetes 1.14 will not
	// attempt to acquire leases with empty identities and will wait for the full lease
	// interval to expire before attempting to reacquire. This value is set to empty when
	// a client voluntarily steps down.
	HolderIdentity       string `json:"holderIdentity"`
	LeaseDurationSeconds int    `json:"leaseDurationSeconds"`
	AcquireTime          int64  `json:"acquireTime"`
	RenewTime            int64  `json:"renewTime"`
	LeaderTransitions    int    `json:"leaderTransitions"`
}

LeaderElectionRecord is a record for leader election operation

type LeaderElector

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

LeaderElector is a leader election client.

func NewLeaderElector

func NewLeaderElector(lec LeaderElectionConfig, logger logging.ILogger) (*LeaderElector, error)

NewLeaderElector creates a LeaderElector from a LeaderElectionConfig

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

type NacosLock

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

NacosLock is a lock using nacos as backend

func NewNacosLock

func NewNacosLock(group, dataID, identity string, client *configuration.NacosClient) *NacosLock

NewNacosLock return a new NacosLock

func (NacosLock) Create

func (lock NacosLock) Create(ctx context.Context, ler LeaderElectionRecord) error

Create attempts to create a LeaderElectionRecord

func (NacosLock) Describe

func (lock NacosLock) Describe() string

Describe is used to convert details on current resource lock into a string

func (NacosLock) Get

func (lock NacosLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error)

Get returns the LeaderElectionRecord

func (NacosLock) Identity

func (lock NacosLock) Identity() string

Identity will return the locks Identity

func (NacosLock) RecordEvent

func (lock NacosLock) RecordEvent(string)

RecordEvent is used to record events

func (NacosLock) Update

func (lock NacosLock) Update(ctx context.Context, ler LeaderElectionRecord) error

Update will update and existing LeaderElectionRecord

Jump to

Keyboard shortcuts

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