Class GrpcChaosRegistry

java.lang.Object
org.mockserver.mock.action.http.GrpcChaosRegistry

public class GrpcChaosRegistry extends Object
Process-wide registry of gRPC chaos profiles, keyed by gRPC service name. These profiles are applied by the GrpcToHttpRequestHandler Netty handler to probabilistically return gRPC error statuses (UNAVAILABLE, DEADLINE_EXCEEDED, RESOURCE_EXHAUSTED, INTERNAL, etc.) on matched gRPC method calls.

The design mirrors TcpChaosRegistry — a singleton backed by a ConcurrentHashMap, with optional TTL-based auto-expiry, service name normalisation (lower-cased, trimmed), and lazy eviction on lookup.

An empty-string key ("") serves as a default profile that applies to all services unless overridden by a service-specific registration, following the same pattern as GrpcHealthRegistry.

State is cleared on server reset (see HttpState.reset()).

Fleet-awareness (G11): when a clustered StateBackend is wired via setStateBackend(StateBackend), mutations are replicated via the backend's crudEntities("chaos-grpc") store, and an InvalidationListener rebuilds the node-local map on remote writes. The get(String) path remains purely node-local. When no backend is set or the backend is not clustered, behaviour is identical to the pre-G11 node-local-only registry.

  • Field Details

  • Constructor Details

    • GrpcChaosRegistry

      public GrpcChaosRegistry(LongSupplier clock)
  • Method Details

    • getInstance

      public static GrpcChaosRegistry getInstance()
    • setStateBackend

      public void setStateBackend(StateBackend backend)
      Wires the clustered state backend for fleet-wide chaos replication. When the backend isClustered(), mutations are replicated via the backend's CRUD entity store, and an InvalidationListener is registered to rebuild the node-local map on remote writes. When the backend is not clustered, this method is a no-op — the registry stays purely node-local.
    • put

      public void put(String service, GrpcChaosProfile profile)
      Register (or replace) the gRPC chaos profile for the given service with no expiry.
    • put

      public void put(String service, GrpcChaosProfile profile, long ttlMillis)
      Register (or replace) the gRPC chaos profile for the given service, optionally with a time-to-live after which it auto-expires.
      Parameters:
      ttlMillis - milliseconds until the profile auto-expires; <= 0 means no expiry
    • get

      public GrpcChaosProfile get(String service)
      Returns the gRPC chaos profile for the given service, falling back to the default ("") profile if no service-specific one exists. Returns null if neither exists (or both have expired). Expired entries are lazily removed.
    • patch

      public GrpcChaosProfile patch(String service, GrpcChaosProfile partial)
      Applies JSON Merge Patch semantics to the gRPC chaos profile for the given service. Only non-null fields from partial are applied to the existing profile; unset fields in the partial are left unchanged.
    • remove

      public void remove(String service)
      Removes the gRPC chaos profile for the given service (no-op if absent).
    • entries

      public Map<String,GrpcChaosProfile> entries()
      Returns a snapshot copy of the current, non-expired service to profile mappings.
    • ttlRemainingMillis

      public Map<String,Long> ttlRemainingMillis()
      Returns, for each currently-active registration that carries a TTL, the remaining milliseconds until it auto-reverts.
    • activeCountByFaultType

      public Map<String,Integer> activeCountByFaultType()
      For each fault type, the number of currently-active (non-expired) registrations whose profile includes that fault.
    • activeCount

      public int activeCount()
      Returns the number of non-expired entries.
    • incrementMatchCount

      public int incrementMatchCount(String service)
      Atomically increments and returns the per-service match counter. Used by the fault decision logic to track how many times a given service has been matched for the count-window check.
    • reset

      public void reset()
      Clear all gRPC chaos profiles and match counters. Called on server reset.
    • reconcileFromBackend

      public void reconcileFromBackend()
      Rebuilds the node-local map from the backend store. Called by the InvalidationListener when a remote write is detected.