public static enum LeaderElection.State extends Enum<LeaderElection.State>
State Transition +----------------+ | +----------+ | | | LEADER | | | +----------+ | | A | | | | | V | +---------+ | +----------+ | | CREATED |----------->| ELECTING | | +---------+ | +----------+ | | | A | | | | | V | V | +---------+ | +----------+ | | DONE |<--------| | FOLLOWER | | +---------+ | +----------+ | +----------------+
The initial state of LeaderElection
is CREATED
.
LeaderElection.start()
method can be called only when the state
is CREATED
. When start()
method is called when the state
is not CREATED
, an IllegalStateException
is thrown.
In the implementation of start()
method, the state is changed
to ELECTING
before the method returns. This means that
Listener.onStateChanged()
is called before start()
method is
returned. In some unusual cases, however, the state is changed to
DONE
. This happens when the given ZooKeeper
instance
reports AUTH_FAILED
or CLOSED
, or when you have called LeaderElection.finish()
before
calling start()
method.
As a result of leader election, the state is changed to either LEADER
or FOLLOWER
. The state LEADER
means that
the LeaderElection
instance has won the leader election and
now is the leader. The state FOLLOWER
means that the LeaderElection
instance has lost the leader election and now is a
follower.
The state may be changed back to ELECTING
from LEADER
or FOLLOWER
. This happens when it is detected that the znode
for leader election has been deleted. In this case, another new
leader election will be executed without delay, and as a result, the
state will be changed to either LEADER
or FOLLOWER
again.
The implementation of LeaderElection
triggers a ZooKeeper
callback as necessary. At the timing, the state of the given
ZooKeeper
instance is checked by calling ZooKeeper.getState()
method. If the ZooKeeper's state is either
AUTH_FAILED
or CLOSED
, a ZooKeeper callback is not
triggered, and instead, the state of LeaderElection
is
changed to DONE
.
Enum Constant and Description |
---|
CREATED
The initial state of a
LeaderElection instance. |
DONE
The
LeaderElection instance has stopped working
and will not join leader election any further. |
ELECTING
Leader election is now being conducted.
|
FOLLOWER
The
LeaderElection instance is now a follower. |
LEADER
The
LeaderElection instance is now the leader. |
Modifier and Type | Method and Description |
---|---|
static LeaderElection.State |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static LeaderElection.State[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final LeaderElection.State CREATED
LeaderElection
instance.public static final LeaderElection.State LEADER
LeaderElection
instance is now the leader.public static final LeaderElection.State FOLLOWER
LeaderElection
instance is now a follower.public static final LeaderElection.State ELECTING
public static final LeaderElection.State DONE
LeaderElection
instance has stopped working
and will not join leader election any further.public static LeaderElection.State[] values()
for (LeaderElection.State c : LeaderElection.State.values()) System.out.println(c);
public static LeaderElection.State valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is nullCopyright © 2015. All rights reserved.