Class ClusterRegistry

java.lang.Object
org.openspaces.core.multicluster.internal.ClusterRegistry

public final class ClusterRegistry extends Object
The proxy's topology: which clusters are registered, which one is active, and in what order they take over.

Why copy-on-write

Every space operation reads this state, and it changes rarely - on registration and on failover. So the whole topology lives in one immutable ClusterRegistry.Snapshot behind an AtomicReference: readers take a snapshot with a single volatile read and never lock, and a reader can never observe a half-applied change (for example an active cluster that has just been unregistered).

Mutations are compare-and-set retry loops over a pure transform, so concurrent registration and failover cannot lose an update.

Identity, not equality

Reverse lookup (proxy -> name) uses an IdentityHashMap: GigaSpace implementations do not define equals, and two proxies to the same space are legitimately different targets. Registering the same instance twice under different names is therefore rejected - it would make nameOf(GigaSpace) ambiguous and silently skew fan-out counts.
  • Constructor Details

    • ClusterRegistry

      public ClusterRegistry()
  • Method Details

    • snapshot

      public ClusterRegistry.Snapshot snapshot()
      The current topology. One volatile read; safe to hold for the duration of an operation.
    • register

      Registers a cluster. The first cluster registered becomes active, so a single-cluster proxy needs no further wiring.
      Returns:
      the snapshot after registration
      Throws:
      IllegalArgumentException - if the name is taken, or this proxy instance is already registered
    • register

      public ClusterRegistry.Snapshot register(String name, GigaSpace proxy)
      Convenience for a proxy registered without topology metadata.
    • unregister

      public ClusterRegistry.Cluster unregister(String name)
      Removes a cluster by name. If it was active, the next cluster in ClusterRegistry.Snapshot.failoverOrder() is promoted; if it was the last one, the registry is left with no active cluster.
      Returns:
      the removed cluster
      Throws:
      IllegalArgumentException - if the name is not registered
    • unregister

      public ClusterRegistry.Cluster unregister(GigaSpace proxy)
      Removes a cluster by proxy instance.
    • setActive

      public ClusterRegistry.Cluster setActive(String name)
      Makes name the active cluster for both reads and writes, dropping any read pin.
      Returns:
      the previously active cluster, or null if there was none
      Throws:
      IllegalArgumentException - if the name is not registered
    • setWriteActive

      public ClusterRegistry.Cluster setWriteActive(String name)
      Makes name the active cluster for writes only. A pinned read cluster keeps serving READ_ACTIVE reads; without a pin, reads follow the write role as usual.
      Returns:
      the previously active cluster, or null if there was none
      Throws:
      IllegalArgumentException - if the name is not registered
    • setReadActive

      public ClusterRegistry.Cluster setReadActive(String name)
      Pins READ_ACTIVE reads to name, leaving the active cluster taking writes. Pinning the active cluster itself is allowed and keeps reads there even after the write role moves.
      Returns:
      the cluster that served READ_ACTIVE reads before, or null if there was none
      Throws:
      IllegalArgumentException - if the name is not registered
    • clearReadActive

      public ClusterRegistry.Cluster clearReadActive()
      Drops the read pin, so READ_ACTIVE reads follow the active cluster again.
      Returns:
      the cluster that served READ_ACTIVE reads before, or null if there was none
    • compareAndSetReadActive

      public boolean compareAndSetReadActive(String expectedPin, String newPin)
      Atomically moves the read pin from expectedPin to newPin (null drops it), failing if another thread moved it first. Single-flight for the read role, exactly as compareAndSetActive(String, String) is for the write role.
      Returns:
      true if this call moved the pin
    • setActive

      public ClusterRegistry.Cluster setActive(GigaSpace proxy)
      Makes the cluster behind this proxy instance active.
    • compareAndSetActive

      public boolean compareAndSetActive(String expectedActive, String newActive)
      Atomically promotes expectedActive to newActive, failing if another thread changed the active cluster first. This is how failover stays single-flight: concurrent failures produce one promotion, not one per failing operation.
      Returns:
      true if this call performed the promotion
    • setPriorityList

      public void setPriorityList(List<String> names)
      Sets the failover priority order. Names must be registered and unique; registered clusters left out of the list keep their registration order behind the listed ones.
    • addToReadRotation

      public boolean addToReadRotation(String name)
      Puts a cluster back into round-robin reads.

      Recovery notifies; it does not re-add. A cluster the application took out after a failure - or that WRITE_SYNC unregistered - comes back when the application says so, not when it starts answering pings again.

      Returns:
      true if this call changed the rotation
    • removeFromReadRotation

      public boolean removeFromReadRotation(String name)
      Takes a cluster out of round-robin reads, leaving it registered and usable as the active cluster.
    • addToPriorityList

      public boolean addToPriorityList(String name)
      Makes a cluster a promotion candidate again, appended to the configured priority order.
    • removeFromPriorityList

      public boolean removeFromPriorityList(String name)
      Removes a cluster from failover promotion, without unregistering it.
    • get

      public GigaSpace get(String name)
      Resolves a proxy by cluster name.
    • nameOf

      public String nameOf(GigaSpace proxy)
      Resolves the cluster name of a proxy instance.
    • toString

      public String toString()
      Overrides:
      toString in class Object