Class LeaderElector
Implements a thin wrapper over LeaderElector integrating optional metric counters for tracking leadership changes.
Namespace: Neon.Kube.K8s
Assembly: Neon.Kube.dll
Syntax
public sealed class LeaderElector
Remarks
This class implements leader election uses a V1Lease object to manage leadership election by trying to ensure that only one leader is active at any time.
note
Although this class tries fairly hard to ensure that only a single leader exists at any time, it is possible for two or more instances to believe they are the leader due to network partitions or latency issues.
A client only acts on timestamps captured locally to infer the state of the leader election. The client does not consider timestamps in the leader election record to be accurate because these timestamps may not have been produced by the local clock. The implemention does not depend on their accuracy and only uses their change to indicate that another client has renewed the leader lease. 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 control-plane node availability.
Larger clusters often need 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 control-plane availability.
This class is very easy to use:
- Use the LeaderElector constructor passing a IKubernetes client instance and your LeaderElectionConfig.
- Call RunAsync() to start the elector. This method will return when the elector is disposed.
Constructors
LeaderElector(IKubernetes, LeaderElectionConfig, Action, Action<string>, Action)
Constructor.
Declaration
public LeaderElector(IKubernetes k8s, LeaderElectionConfig config, Action onStartedLeading = null, Action<string> onNewLeader = null, Action onStoppedLeading = null)
Parameters
Type | Name | Description |
---|---|---|
IKubernetes | k8s | The IKubernetes client to be used to communicate with th\e cluster. |
LeaderElectionConfig | config | Specifies the elector configuration. |
Action | onStartedLeading | Optionally specifies the action to be called when the instance assumes leadership. |
Action<string> | onNewLeader | Optionally specifies the action to be called when leadership changes. |
Action | onStoppedLeading | Optionally specifies the action to be called when the instance is demoted. |
Properties
IsLeader
Returns true
if the current instance is currently the leader.
Declaration
public bool IsLeader { get; }
Property Value
Type | Description |
---|---|
bool |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | Thrown when the instance is disposed. |
Leader
Returns the identity of the current leader or null
when there's no leader.
Declaration
public string Leader { get; }
Property Value
Type | Description |
---|---|
string |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | Thrown when the instance is disposed. |
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Declaration
public void Dispose()
RunAsync()
Starts the elector. Note that this will return when the elector is disposed.
Declaration
public Task RunAsync()
Returns
Type | Description |
---|---|
Task | The tracking Task. |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | Thrown when the instance is disposed. |