Class ClusterHealth
HEALTHY --failure--> SUSPECT --enough failures in the window--> UNHEALTHY
^ |
+------success-------+
Why three states and not two
A single failure is not evidence that a site is down - a timeout, a GC pause, one dropped connection. Excluding a cluster on the first error would make the proxy flap between clusters under normal jitter, and every flap moves traffic across a WAN. So a failing cluster becomesClusterHealth.State.SUSPECT and
keeps taking traffic; only failureThreshold consecutive failures inside
failureWindow take it out of rotation.
Why there is no way back
ClusterHealth.State.UNHEALTHY is final for this record. Nothing probes a cluster in the background, and an
unhealthy cluster receives no traffic, so no success could ever be observed to restore it; a success
that does arrive - an operation already in flight - is not evidence that the cluster is ready to serve.
Coming back is the application's decision: unregistering the cluster discards this record, and
registering it again starts a fresh one.
The clock is injected so the window is testable without sleeping. All methods are synchronised: the state changes rarely, and correctness of the window matters more than uncontended reads.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic final recordA state change, captured atomically. -
Constructor Summary
ConstructorsConstructorDescriptionClusterHealth(String clusterName, int failureThreshold, Duration failureWindow) -
Method Summary
Modifier and TypeMethodDescriptionapply(Function<ClusterHealth, ClusterHealth.State> action) Applies a state change and reports both ends of it, atomically.intFailures still inside the rolling window.booleanWhether this cluster may receive traffic.booleanWhether anything has been observed yet, so callers know whether the state is informed or assumed.The failure that most recently counted against this cluster, ornull.markUnreachable(Throwable cause) Records a failure that is proof the cluster cannot be reached rather than evidence towards it, and takes the cluster out in one step.recordFailure(Throwable cause) Records a failure that says something about the cluster - the caller has already decided that, viaFailureClassifier.Records a successful operation or ping.voidreset()Forces the cluster back to healthy - used when an operator intervenes.state()toString()
-
Constructor Details
-
ClusterHealth
-
-
Method Details
-
clusterName
-
state
-
isAvailable
public boolean isAvailable()Whether this cluster may receive traffic. A suspect cluster still can - it is a warning, not a ban. -
isObserved
public boolean isObserved()Whether anything has been observed yet, so callers know whether the state is informed or assumed. -
lastFailure
The failure that most recently counted against this cluster, ornull. -
apply
Applies a state change and reports both ends of it, atomically.The lock is reentrant, so
actioncalls the ordinaryrecordXxxmethods on this same instance without deadlocking - the point is only that nothing else can interleave between reading the previous state and applying the new one. -
failuresInWindow
public int failuresInWindow()Failures still inside the rolling window. -
recordSuccess
Records a successful operation or ping. It clears a suspicion, but never restores anClusterHealth.State.UNHEALTHYcluster.- Returns:
- the state after recording, so a caller can react to a transition
-
recordFailure
Records a failure that says something about the cluster - the caller has already decided that, viaFailureClassifier. Application errors must never be passed here.- Returns:
- the state after recording
-
markUnreachable
Records a failure that is proof the cluster cannot be reached rather than evidence towards it, and takes the cluster out in one step.The sliding window exists because one failed read says little: a timeout, a GC pause, a dropped connection. A failed mutation is a different thing. The cluster has missed a write the others took, so it is already behind, and leaving it in traffic means every following write diverges the clusters a little further while balanced reads serve the stale copy. Taking it out at once and saying so - PIC-6085's rule for mutations - costs a rejoin when the failure was transient, which is the cheaper mistake.
- Returns:
- the state after recording, which is always
ClusterHealth.State.UNHEALTHY
-
reset
public void reset()Forces the cluster back to healthy - used when an operator intervenes. -
toString
-