Class KafkaProtocolFaultProxy
- All Implemented Interfaces:
AutoCloseable
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 TypeMethodDescriptionvoidblackholeClient(String clientIdSubstring) Blackhole every request from clients whose clientId containsclientIdSubstring: drop the request before it reaches the broker and close the connection.The address to hand to a client'sbootstrap.servers.voidRemove all registered faults and client blackholes (routing rewrites are always on and unaffected).voidclose()Hold back a response ofapiKeybydelaybefore forwarding it to the client, modelling a slow broker.disconnectOn(org.apache.kafka.common.protocol.ApiKeys apiKey) Drop the connection when a response ofapiKeywould be returned (models the EOS commit gap).static KafkaProtocolFaultProxyCreate 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 ofapiKeyto carryerror.
-
Method Details
-
inFrontOf
Create and start a proxy in front of the given broker bootstrap address.- Parameters:
targetBootstrap- a single broker'shost:port— the proxy supports one broker only- Throws:
IllegalArgumentException- iftargetBootstraplists more than one server
-
bootstrapServers
The address to hand to a client'sbootstrap.servers. -
injectError
public FaultRule.Builder injectError(org.apache.kafka.common.protocol.ApiKeys apiKey, org.apache.kafka.common.protocol.Errors error) Rewrite responses ofapiKeyto carryerror. Follow with a trigger, e.g..once(). -
disconnectOn
Drop the connection when a response ofapiKeywould be returned (models the EOS commit gap). -
delayOn
Hold back a response ofapiKeybydelaybefore 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'srequest.timeout.mswill be seen by the client as a timeout (and handled like a disconnect), so size the delay against the timeout under test. -
blackholeClient
Blackhole every request from clients whose clientId containsclientIdSubstring: 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 distinctclient.idper Streams instance to target one instance. Undo viaclearFaults(). -
clearFaults
public void clearFaults()Remove all registered faults and client blackholes (routing rewrites are always on and unaffected). -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-