Skip to content

Commit b9220f9

Browse files
authored
example/kafka-mesh: Add docs/test for batching and high volume handling (#1034)
Signed-off-by: Ryan Northey <[email protected]>
1 parent dafb747 commit b9220f9

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

kafka-mesh/example.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,63 @@ produce and fetch request counts:
146146
kafka.kafka_mesh.request.metadata_request: 8
147147
148148
149+
Step 8: Test high-volume producing with batched records
150+
*******************************************************
151+
152+
In production, Kafka producers often batch multiple records into a single
153+
``ProduceRequest`` to improve throughput. The mesh filter must correctly handle
154+
these batched requests and route records to the appropriate upstream cluster.
155+
156+
Send multiple messages rapidly to the ``apricots`` topic (which routes to
157+
``cluster1`` based on the ``a`` prefix). The producer will automatically batch
158+
these into fewer requests:
159+
160+
.. code-block:: console
161+
162+
$ docker compose run --rm kafka-client /bin/bash -c " \
163+
for i in {1..20}; do \
164+
echo \"apricot message \$i\"; \
165+
done | kafka-console-producer --request-required-acks 1 --producer-property enable.idempotence=false --broker-list proxy:10000 --topic apricots"
166+
167+
Now verify that all 20 messages arrived at ``cluster1`` by consuming directly
168+
from the upstream:
169+
170+
.. code-block:: console
171+
172+
$ docker compose run --rm kafka-client \
173+
kafka-console-consumer --bootstrap-server kafka-cluster1:9092 --topic apricots --from-beginning --max-messages 20 | wc -l
174+
20
175+
176+
This confirms that even though the producer may have batched the records into
177+
multiple ``ProduceRequest``, the mesh filter correctly routed all messages to the
178+
appropriate cluster. This is critical for high-throughput production workloads.
179+
180+
Next, send 20 messages rapidly to the ``blueberries`` topic (which routes to
181+
cluster2 based on the ``b`` prefix). This demonstrates that the mesh filter
182+
correctly splits batched ProduceRequests when they need to go to different
183+
upstream clusters:
184+
185+
.. code-block:: console
186+
187+
$ docker compose run --rm kafka-client /bin/bash -c " \
188+
for i in {1..20}; do \
189+
echo \"blueberry message \$i\"; \
190+
done | kafka-console-producer --request-required-acks 1 --producer-property enable.idempotence=false --broker-list proxy:10000 --topic blueberries"
191+
192+
Verify that all 20 messages arrived at cluster2:
193+
194+
.. code-block:: console
195+
196+
$ docker compose run --rm kafka-client \
197+
kafka-console-consumer --bootstrap-server kafka-cluster2:9092 --topic blueberries --from-beginning --max-messages 20 | wc -l
198+
20
199+
200+
This demonstrates the mesh filter's ability to handle batched records destined
201+
for different clusters. In production environments where a single producer
202+
sends to multiple topics across different clusters, this ensures records are
203+
correctly routed based on their destination.
204+
205+
149206
.. seealso::
150207

151208
:ref:`Envoy Kafka mesh filter <config_network_filters_kafka_mesh>`

kafka-mesh/verify.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,57 @@ stats_output=$(_curl "http://localhost:${PORT_ADMIN}/stats?filter=kafka")
3636
echo "$stats_output" | grep "produce_request" | grep -v ": 0"
3737
echo "$stats_output" | grep "fetch_request" | grep -v ": 0"
3838
echo "$stats_output" | grep "metadata_request" | grep -v ": 0"
39+
40+
run_log "Test high-volume producing with batched records"
41+
# Send 20 messages rapidly to trigger producer batching
42+
kafka_client /bin/bash -c " \
43+
for i in {1..20}; do \
44+
echo \"apricot message \$i\"; \
45+
done | kafka-console-producer --request-required-acks 1 --producer-property enable.idempotence=false --broker-list proxy:10000 --topic apricots"
46+
47+
run_log "Verify all 20 messages arrived at cluster1"
48+
# Consume all messages and count them
49+
message_count=$(kafka_client kafka-console-consumer --bootstrap-server kafka-cluster1:9092 --topic apricots --from-beginning --max-messages 20 2>/dev/null | wc -l)
50+
message_count=${message_count:-0}
51+
run_log "Received $message_count messages from apricots topic"
52+
53+
if [[ "$message_count" -eq 20 ]]; then
54+
run_log "SUCCESS: All 20 messages arrived at cluster1"
55+
else
56+
echo "ERROR: Expected 20 messages but received $message_count" >&2
57+
exit 1
58+
fi
59+
60+
run_log "Verify produce metrics reflect the batched requests"
61+
# Get the produce_request count - it should be greater than 0 and likely less than 20 (due to batching)
62+
stats_output=$(_curl "http://localhost:${PORT_ADMIN}/stats?filter=kafka.kafka_mesh.request.produce_request")
63+
produce_count=$(echo "$stats_output" | grep "produce_request:" | cut -f2 -d':' | tr -d ' ')
64+
produce_count=${produce_count:-0}
65+
run_log "Total produce_request count: $produce_count"
66+
67+
if [[ "$produce_count" -gt 0 ]]; then
68+
run_log "SUCCESS: Produce requests tracked correctly (count: $produce_count)"
69+
else
70+
echo "ERROR: No produce requests tracked" >&2
71+
exit 1
72+
fi
73+
74+
run_log "Test high-volume producing to second cluster with batched records"
75+
# Send 20 messages rapidly to blueberries topic (routes to cluster2)
76+
kafka_client /bin/bash -c " \
77+
for i in {1..20}; do \
78+
echo \"blueberry message \$i\"; \
79+
done | kafka-console-producer --request-required-acks 1 --producer-property enable.idempotence=false --broker-list proxy:10000 --topic blueberries"
80+
81+
run_log "Verify all 20 messages arrived at cluster2"
82+
# Consume all messages and count them
83+
message_count=$(kafka_client kafka-console-consumer --bootstrap-server kafka-cluster2:9092 --topic blueberries --from-beginning --max-messages 20 2>/dev/null | wc -l)
84+
message_count=${message_count:-0}
85+
run_log "Received $message_count messages from blueberries topic"
86+
87+
if [[ "$message_count" -eq 20 ]]; then
88+
run_log "SUCCESS: All 20 messages arrived at cluster2"
89+
else
90+
echo "ERROR: Expected 20 messages but received $message_count" >&2
91+
exit 1
92+
fi

0 commit comments

Comments
 (0)