Class LoomConsistencySubsystem
The CP (Consistency and Partition tolerance) Subsystem provides strongly consistent, fault-tolerant distributed primitives backed by the Raft consensus protocol.
Supported Primitives
- Linearizable Lock:
getLock(String)— Mutual exclusion with fencing tokens - Semaphore: Use
LoomClientCP semaphore operations directly - Atomic Long:
getAtomicLong(String)— Atomic 64-bit integer with CAS operations - Atomic Reference:
getAtomicReference(String, Class)— Atomic object reference with CAS operations - CountDownLatch:
getCountDownLatch(String)— Linearizable countdown latch
Usage Example
LoomClient client = LoomClient.builder()
.addSeed("127.0.0.1:5701")
.build();
client.connect();
LoomConsistencySubsystem cp = client.consistencySubsystem();
// Linearizable lock with linearizable guarantees
LoomLinearizableLock lock = cp.getLock("resource-lock");
long fence = lock.lock();
try {
// Critical section
} finally {
lock.unlock();
}
// Atomic long for linearizable counters
LoomAtomicLong atomic = cp.getAtomicLong("metrics");
atomic.incrementAndGet();
// Atomic reference for linearizable compare-and-set state
LoomAtomicReference<String> ref = cp.getAtomicReference("leader-state", String.class);
ref.compareAndSet(null, "active");
// Countdown latch for cluster coordination
LoomLinearizableLatch latch = cp.getCountDownLatch("startup");
latch.trySetCount(3);
Thread Safety
This class is thread-safe. All returned proxies are thread-safe and can be shared across threads.- Since:
- 1.4
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiongetAtomicLong(String name) Get a distributed atomic long proxy.<T> LoomAtomicReference<T> getAtomicReference(String name, Class<T> type) Get a distributed atomic reference proxy.getCountDownLatch(String name) Get a distributed countdown latch proxy.Get a distributed linearizable lock proxy.
-
Method Details
-
getLock
Get a distributed linearizable lock proxy.The returned lock provides mutual exclusion across the cluster with support for fencing tokens to prevent split-brain anomalies. Lock ownership is tracked via requester IDs and fence tokens provide linearizable guarantees.
- Parameters:
name- the name of the lock (must not be null)- Returns:
- a linearizable lock proxy (never null)
- Throws:
NullPointerException- if name is nullLoomException- if the cluster is unavailable- See Also:
-
getAtomicLong
Get a distributed atomic long proxy.The returned atomic long provides linearizable 64-bit integer operations across the cluster. Useful for distributed counters with stronger guarantees than eventually-consistent counters. Multiple threads may safely perform atomic operations concurrently.
- Parameters:
name- the name of the atomic long (must not be null)- Returns:
- an atomic long proxy (never null)
- Throws:
NullPointerException- if name is nullLoomException- if the cluster is unavailable- See Also:
-
getAtomicReference
Get a distributed atomic reference proxy.The returned atomic reference provides linearizable get, set, and compare-and-set operations across the cluster. Values must be serializable by the configured wire serializer and are type-checked on reads by the supplied class token.
- Type Parameters:
T- the value type- Parameters:
name- the name of the atomic reference (must not be null)type- the expected value type (must not be null)- Returns:
- an atomic reference proxy (never null)
- Throws:
NullPointerException- if name or type is nullLoomException- if the cluster is unavailable- See Also:
-
getCountDownLatch
Get a distributed countdown latch proxy.The returned latch provides linearizable count updates and waits across the cluster. Use
LoomLinearizableLatch.trySetCount(long)to arm the latch before callingLoomLinearizableLatch.await(java.time.Duration)orLoomLinearizableLatch.countDown().- Parameters:
name- the name of the latch (must not be null)- Returns:
- a countdown latch proxy (never null)
- Throws:
NullPointerException- if name is nullLoomException- if the cluster is unavailable- See Also:
-