Class RequestCoalescer

java.lang.Object
com.loomcache.server.handler.RequestCoalescer

public class RequestCoalescer extends Object
Coalesces identical concurrent read requests to reduce redundant work.

When multiple clients request the same key simultaneously, only the first (leader) request executes the operation. Subsequent identical requests join the group and wait for the leader's result, sharing the CompletableFuture.

Groups auto-expire after a configurable timeout window. Only read operations (GET, CONTAINS_KEY, SIZE) are coalesced; write operations bypass coalescing.

Thread-safe: Uses ConcurrentHashMap and ReentrantLock for synchronization.

Since:
1.0
  • Constructor Details

    • RequestCoalescer

      public RequestCoalescer()
      Create a new RequestCoalescer with default settings.
    • RequestCoalescer

      public RequestCoalescer(int timeoutMs, int maxGroupSize)
      Create a new RequestCoalescer with custom timeout and max group size.
      Parameters:
      timeoutMs - timeout in milliseconds for group expiration
      maxGroupSize - maximum size of a coalescing group
  • Method Details

    • coalesce

      public CompletableFuture<@Nullable Message> coalesce(RequestCoalescer.CoalescingKey key, Supplier<@Nullable Message> supplier)
      Coalesce a read request or execute it if no group exists.

      For the first request (leader): - Creates a new group - Executes the supplier - Completes the group's future - Returns the future

      For subsequent identical requests (followers): - Joins the existing group (if under max size) - Returns the group's future (shared)

      Groups auto-expire after timeout. If the group has expired, it is removed and a new group is created for the new request (which becomes the leader).

      Parameters:
      key - the coalescing key (operation, mapName, key)
      supplier - the operation to execute (called only for the leader)
      Returns:
      a CompletableFuture containing the result (shared across group members)
      Throws:
      NullPointerException - if key or supplier is null
    • getStats

      Get current statistics.
      Returns:
      statistics snapshot
    • clear

      public void clear()
      Clear all groups and reset statistics. Useful for testing.
    • getActiveGroupCount

      public int getActiveGroupCount()
      Get the number of active groups.
      Returns:
      count of active coalescing groups