Class KafkaProtocolFaultProxy

java.lang.Object
org.apache.kafka.test.faultproxy.KafkaProtocolFaultProxy
All Implemented Interfaces:
AutoCloseable

public final class KafkaProtocolFaultProxy extends Object implements AutoCloseable
A lightweight, client-agnostic Kafka wire-protocol fault-injection proxy for fast integration tests.

Sit it in front of a real (embedded) broker and point any client's bootstrap.servers at it; it decodes requests/responses with Kafka's own protocol classes (RequestHeader, AbstractResponse, RequestUtils.serialize(Message, short, Message, short)) — so it is correct across every wire version, including flexible/tagged-field ones, with no hand-rolled byte offsets.

Usage

try (var broker = new EmbeddedKafkaCluster(1)) {
    broker.start();
    try (var proxy = KafkaProtocolFaultProxy.inFrontOf(broker.bootstrapServers())) {
        // point clients here:
        props.put(BOOTSTRAP_SERVERS_CONFIG, proxy.bootstrapServers());

        proxy.injectError(ApiKeys.END_TXN, Errors.CONCURRENT_TRANSACTIONS).once();
        proxy.injectError(ApiKeys.PRODUCE, Errors.NOT_ENOUGH_REPLICAS).onCall(2);
        proxy.disconnectOn(ApiKeys.END_TXN).once();      // the EOS "commit gap"
        proxy.delayOn(ApiKeys.FETCH, Duration.ofSeconds(2)).everyTime();  // slow broker
    }
}

Routing is transparent: the proxy rewrites Metadata/FindCoordinator responses so every advertised address points back at itself, so a single-broker embedded cluster needs no special config (its own ephemeral port is discovered from bootstrapServers()).

Single broker only. Because every advertised broker and coordinator address is rewritten to this one proxy — which forwards to a single upstream broker — the proxy supports exactly one broker. A multi-broker bootstrap would silently funnel every partition and coordinator to the first broker, producing NOT_LEADER_OR_FOLLOWER/NOT_COORDINATOR retry loops, so inFrontOf(String) rejects a bootstrap that lists more than one server. Front a single-broker EmbeddedKafkaCluster, or pass one broker's address (e.g. cluster.bootstrapServers().split(",")[0]).

Determinism: once()/onCall(n)/times(n) are deterministic and safe for assertions; withProbability(p) is chaos-mode only. The proxy never closes sockets unless a disconnectOn(...) rule fires, so it is not itself a source of flakiness.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    blackholeClient(String clientIdSubstring)
    Blackhole every request from clients whose clientId contains clientIdSubstring: drop the request before it reaches the broker and close the connection.
    The address to hand to a client's bootstrap.servers.
    void
    Remove all registered faults and client blackholes (routing rewrites are always on and unaffected).
    void
     
    delayOn(org.apache.kafka.common.protocol.ApiKeys apiKey, Duration delay)
    Hold back a response of apiKey by delay before forwarding it to the client, modelling a slow broker.
    disconnectOn(org.apache.kafka.common.protocol.ApiKeys apiKey)
    Drop the connection when a response of apiKey would be returned (models the EOS commit gap).
    inFrontOf(String targetBootstrap)
    Create and start a proxy in front of the given broker bootstrap address.
    injectError(org.apache.kafka.common.protocol.ApiKeys apiKey, org.apache.kafka.common.protocol.Errors error)
    Rewrite responses of apiKey to carry error.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • inFrontOf

      public static KafkaProtocolFaultProxy inFrontOf(String targetBootstrap)
      Create and start a proxy in front of the given broker bootstrap address.
      Parameters:
      targetBootstrap - a single broker's host:port — the proxy supports one broker only
      Throws:
      IllegalArgumentException - if targetBootstrap lists more than one server
    • bootstrapServers

      public String bootstrapServers()
      The address to hand to a client's bootstrap.servers.
    • injectError

      public FaultRule.Builder injectError(org.apache.kafka.common.protocol.ApiKeys apiKey, org.apache.kafka.common.protocol.Errors error)
      Rewrite responses of apiKey to carry error. Follow with a trigger, e.g. .once().
    • disconnectOn

      public FaultRule.Builder disconnectOn(org.apache.kafka.common.protocol.ApiKeys apiKey)
      Drop the connection when a response of apiKey would be returned (models the EOS commit gap).
    • delayOn

      public FaultRule.Builder delayOn(org.apache.kafka.common.protocol.ApiKeys apiKey, Duration delay)
      Hold back a response of apiKey by delay before forwarding it to the client, modelling a slow broker. Only the matching response on its own connection is delayed (each connection/direction runs on its own thread), so other clients and the request path are unaffected. Follow with a trigger, e.g. .everyTime(). A delay longer than the client's request.timeout.ms will be seen by the client as a timeout (and handled like a disconnect), so size the delay against the timeout under test.
    • blackholeClient

      public void blackholeClient(String clientIdSubstring)
      Blackhole every request from clients whose clientId contains clientIdSubstring: drop the request before it reaches the broker and close the connection. The broker stops hearing that instance's heartbeats and evicts it by session timeout — an ungraceful, reversible one-node network partition. Set a distinct client.id per Streams instance to target one instance. Undo via clearFaults().
    • clearFaults

      public void clearFaults()
      Remove all registered faults and client blackholes (routing rewrites are always on and unaffected).
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable