Class ClusterHealth

java.lang.Object
org.openspaces.core.multicluster.internal.health.ClusterHealth

public final class ClusterHealth extends Object
One cluster's health, as a small state machine over a sliding window of failures.
  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 becomes ClusterHealth.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.

  • Constructor Details

    • ClusterHealth

      public ClusterHealth(String clusterName, int failureThreshold, Duration failureWindow)
  • Method Details

    • clusterName

      public String clusterName()
    • state

      public ClusterHealth.State 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

      public Throwable lastFailure()
      The failure that most recently counted against this cluster, or null.
    • apply

      Applies a state change and reports both ends of it, atomically.

      The lock is reentrant, so action calls the ordinary recordXxx methods 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

      public ClusterHealth.State recordSuccess()
      Records a successful operation or ping. It clears a suspicion, but never restores an ClusterHealth.State.UNHEALTHY cluster.
      Returns:
      the state after recording, so a caller can react to a transition
    • recordFailure

      public ClusterHealth.State recordFailure(Throwable cause)
      Records a failure that says something about the cluster - the caller has already decided that, via FailureClassifier. Application errors must never be passed here.
      Returns:
      the state after recording
    • markUnreachable

      public ClusterHealth.State 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.

      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

      public String toString()
      Overrides:
      toString in class Object