Class IdempotencyManager
java.lang.Object
com.loomcache.server.handler.IdempotencyManager
- All Implemented Interfaces:
AutoCloseable
Manages server-side request deduplication for idempotent operations.
Tracks completed requests by client ID + request ID and returns cached responses for retried requests. Uses LRU eviction when cache is full and a background cleanup thread for expired entries.
Thread-safe: uses ReentrantLock for coordinated operations and ConcurrentHashMap for the underlying cache.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRecord representing a cached response with metadata.static final recordRecord representing an idempotency key (client ID + request ID).static final recordStatistics for idempotency tracking. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new IdempotencyManager with default TTL (60s) and cache size (100,000).IdempotencyManager(long ttlMillis, int maxCacheSize) Creates a new IdempotencyManager with custom TTL and max cache size.IdempotencyManager(long ttlMillis, int maxCacheSize, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry) -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all cached entries.voidClears expired entries from the cache.voidclose()deduplicate(IdempotencyManager.IdempotencyKey key, Supplier<Message> supplier) Deduplicates a request.deduplicate(IdempotencyManager.IdempotencyKey key, Supplier<Message> supplier, Predicate<Exception> retainPendingOnException) deduplicate(IdempotencyManager.IdempotencyKey key, Supplier<Message> supplier, Predicate<Exception> retainPendingOnException, boolean cpOperation) getStats()Returns current statistics about the idempotency manager.@Nullable MessageReturns a copy of the live cached response for the key, ornullif no completed response is currently available.voidrecordCompleted(IdempotencyManager.IdempotencyKey key, Message response) Record a completed idempotent response without executing the supplier again.voidrecordCompleted(IdempotencyManager.IdempotencyKey key, Message response, boolean cpOperation) voidrestoreCpSnapshotEntries(@Nullable Object rawSnapshotEntries) voidrestoreSnapshotEntries(@Nullable Object rawSnapshotEntries) snapshotCpEntries(long maxWindowMs)
-
Constructor Details
-
IdempotencyManager
public IdempotencyManager()Creates a new IdempotencyManager with default TTL (60s) and cache size (100,000). -
IdempotencyManager
public IdempotencyManager(long ttlMillis, int maxCacheSize) Creates a new IdempotencyManager with custom TTL and max cache size.- Parameters:
ttlMillis- TTL for cached responses in milliseconds (must be positive)maxCacheSize- maximum number of entries in the cache (must be positive)- Throws:
InvalidConfigurationException- if ttlMillis or maxCacheSize is zero or negative
-
IdempotencyManager
public IdempotencyManager(long ttlMillis, int maxCacheSize, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry)
-
-
Method Details
-
deduplicate
Deduplicates a request. If a cached response exists and hasn't expired, returns it. Otherwise, executes the supplier, caches the result, and returns it.- Parameters:
key- the idempotency key (client ID + request ID)supplier- the operation to execute if not cached- Returns:
- the cached or newly computed response message
-
deduplicate
-
deduplicate
-
recordCompleted
Record a completed idempotent response without executing the supplier again. Used when a committed result is learned through Raft state-machine apply on followers, so a later leader failover can still deduplicate the retried request. -
recordCompleted
public void recordCompleted(IdempotencyManager.IdempotencyKey key, Message response, boolean cpOperation) -
peekCompleted
Returns a copy of the live cached response for the key, ornullif no completed response is currently available. -
getStats
Returns current statistics about the idempotency manager. -
snapshotCpEntries
-
snapshotAllEntries
-
restoreSnapshotEntries
-
restoreCpSnapshotEntries
-
clear
public void clear()Clears all cached entries. -
clearExpired
public void clearExpired()Clears expired entries from the cache. -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-