Add share-groups module: Kafka 4.3.1 share groups with Spring for Apache Kafka 4.1.1
Six consumers on two partitions under a consumer group, a share group with the default share.acquire.mode and a share group with record_limit; the three acknowledgement types and a record that is released until Kafka stops redelivering it; a new share group starting at latest; the ordering a share group gives up; and Spring Kafka's share containers in implicit and manual acknowledgement modes. Tests run against a real broker (scripts/broker-up.sh) and write output/. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Lm655hdgDo5xsSLNomgdsy
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package com.ankurm.sharegroups;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||
|
||||
import org.springframework.kafka.annotation.KafkaListener;
|
||||
import org.springframework.kafka.support.ShareAcknowledgment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/** Two listeners on share containers. They only record what they saw; the tests read the lists. */
|
||||
@Component
|
||||
public class PaymentListeners {
|
||||
|
||||
public final List<String> implicitSeen = new CopyOnWriteArrayList<>();
|
||||
|
||||
public final List<String> manualSeen = new CopyOnWriteArrayList<>();
|
||||
|
||||
/** Implicit mode: returning normally acknowledges (ACCEPT); throwing makes the container reject or release. */
|
||||
@KafkaListener(id = "implicit", topics = "${share.topic.implicit}", groupId = "${share.group.implicit}",
|
||||
containerFactory = "implicitShareFactory")
|
||||
void implicit(ConsumerRecord<String, String> record) throws InterruptedException {
|
||||
Thread.sleep(20);
|
||||
implicitSeen.add(Thread.currentThread().getName() + " <- " + record.value());
|
||||
}
|
||||
|
||||
/** Manual mode: the listener decides per record. "bad" is rejected, "again" is released once, the rest accepted. */
|
||||
@KafkaListener(id = "manual", topics = "${share.topic.manual}", groupId = "${share.group.manual}",
|
||||
containerFactory = "explicitShareFactory")
|
||||
void manual(ConsumerRecord<String, String> record, ShareAcknowledgment ack) {
|
||||
int attempt = record.deliveryCount().orElse((short) 1);
|
||||
switch (record.value()) {
|
||||
case "bad" -> {
|
||||
manualSeen.add("bad delivery #" + attempt + " -> reject");
|
||||
ack.reject();
|
||||
}
|
||||
case "again" -> {
|
||||
if (attempt == 1) {
|
||||
manualSeen.add("again delivery #1 -> release");
|
||||
ack.release();
|
||||
}
|
||||
else {
|
||||
manualSeen.add("again delivery #" + attempt + " -> acknowledge");
|
||||
ack.acknowledge();
|
||||
}
|
||||
}
|
||||
default -> {
|
||||
manualSeen.add(record.value() + " delivery #" + attempt + " -> acknowledge");
|
||||
ack.acknowledge();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.ankurm.sharegroups;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.kafka.clients.consumer.ConsumerConfig;
|
||||
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.kafka.annotation.EnableKafka;
|
||||
import org.springframework.kafka.config.ShareKafkaListenerContainerFactory;
|
||||
import org.springframework.kafka.core.DefaultShareConsumerFactory;
|
||||
import org.springframework.kafka.core.ShareConsumerFactory;
|
||||
import org.springframework.kafka.listener.ContainerProperties.ShareAckMode;
|
||||
|
||||
/**
|
||||
* Spring Boot 4.1.1 does not auto-configure share consumers, so the factory and the listener container
|
||||
* factories are plain beans. Two container factories: implicit (the container acknowledges for you) and
|
||||
* explicit (the listener must call ShareAcknowledgment).
|
||||
*/
|
||||
@Configuration
|
||||
@EnableKafka
|
||||
public class ShareConfig {
|
||||
|
||||
@Bean
|
||||
ShareConsumerFactory<String, String> shareConsumerFactory(@Value("${broker:localhost:9092}") String bootstrap) {
|
||||
Map<String, Object> props = new HashMap<>();
|
||||
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap);
|
||||
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
|
||||
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
|
||||
props.put("share.acquire.mode", "record_limit");
|
||||
props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, "3");
|
||||
return new DefaultShareConsumerFactory<>(props);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ShareKafkaListenerContainerFactory<String, String> implicitShareFactory(ShareConsumerFactory<String, String> factory) {
|
||||
ShareKafkaListenerContainerFactory<String, String> f = new ShareKafkaListenerContainerFactory<>(factory);
|
||||
f.setConcurrency(4);
|
||||
f.getContainerProperties().setShareAckMode(ShareAckMode.IMPLICIT);
|
||||
return f;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ShareKafkaListenerContainerFactory<String, String> explicitShareFactory(ShareConsumerFactory<String, String> factory) {
|
||||
ShareKafkaListenerContainerFactory<String, String> f = new ShareKafkaListenerContainerFactory<>(factory);
|
||||
f.setConcurrency(2);
|
||||
f.getContainerProperties().setShareAckMode(ShareAckMode.MANUAL);
|
||||
return f;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.ankurm.sharegroups;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/** Marker application class so the Spring tests have something to bootstrap. Nothing here starts a broker. */
|
||||
@SpringBootApplication
|
||||
public class ShareGroupsApplication {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.ankurm.sharegroups;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.kafka.clients.consumer.AcknowledgeType;
|
||||
import org.apache.kafka.clients.consumer.ConsumerConfig;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||
import org.apache.kafka.clients.consumer.KafkaShareConsumer;
|
||||
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/** What ACCEPT, RELEASE and REJECT do to a record, and what happens to a record that is released forever. */
|
||||
class AckTypesTest {
|
||||
|
||||
@Test
|
||||
void acknowledgementTypes() throws Exception {
|
||||
Transcript t = new Transcript("02-ack-types.txt");
|
||||
String topic = Broker.freshTopic("acks", 1);
|
||||
String group = topic + "-g";
|
||||
Broker.shareGroupFromEarliest(group);
|
||||
Broker.produce(topic, List.of(new String[] { "k", "ok" }, new String[] { "k", "retry-once" },
|
||||
new String[] { "k", "reject-me" }, new String[] { "k", "always-release" }));
|
||||
|
||||
Properties p = new Properties();
|
||||
p.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, Broker.BOOTSTRAP);
|
||||
p.put(ConsumerConfig.GROUP_ID_CONFIG, group);
|
||||
p.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
|
||||
p.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
|
||||
p.put("share.acknowledgement.mode", "explicit");
|
||||
|
||||
List<String> deliveries = new ArrayList<>();
|
||||
long end = System.nanoTime() + TimeUnit.SECONDS.toNanos(40);
|
||||
long lastRecord = System.nanoTime();
|
||||
try (KafkaShareConsumer<String, String> c = new KafkaShareConsumer<>(p)) {
|
||||
c.subscribe(List.of(topic));
|
||||
while (System.nanoTime() < end && System.nanoTime() - lastRecord < TimeUnit.SECONDS.toNanos(15)) {
|
||||
for (ConsumerRecord<String, String> r : c.poll(Duration.ofMillis(500))) {
|
||||
lastRecord = System.nanoTime();
|
||||
int attempt = r.deliveryCount().orElse((short) -1);
|
||||
AcknowledgeType type = switch (r.value()) {
|
||||
case "ok" -> AcknowledgeType.ACCEPT;
|
||||
case "retry-once" -> attempt == 1 ? AcknowledgeType.RELEASE : AcknowledgeType.ACCEPT;
|
||||
case "reject-me" -> AcknowledgeType.REJECT;
|
||||
default -> AcknowledgeType.RELEASE;
|
||||
};
|
||||
c.acknowledge(r, type);
|
||||
String line = String.format("%-15s delivery #%d -> %s", r.value(), attempt, type);
|
||||
deliveries.add(line);
|
||||
t.line("%s", line);
|
||||
}
|
||||
c.commitSync();
|
||||
}
|
||||
}
|
||||
t.line("deliveries of ok = %d", count(deliveries, "ok "));
|
||||
t.line("deliveries of retry-once = %d", count(deliveries, "retry-once"));
|
||||
t.line("deliveries of reject-me = %d", count(deliveries, "reject-me"));
|
||||
t.line("deliveries of always-release = %d", count(deliveries, "always-release"));
|
||||
t.write();
|
||||
|
||||
assertThat(count(deliveries, "ok ")).isEqualTo(1);
|
||||
assertThat(count(deliveries, "retry-once")).isEqualTo(2);
|
||||
assertThat(count(deliveries, "reject-me")).isEqualTo(1);
|
||||
assertThat(count(deliveries, "always-release")).isGreaterThan(1);
|
||||
}
|
||||
|
||||
private long count(List<String> lines, String prefix) {
|
||||
return lines.stream().filter(l -> l.startsWith(prefix)).count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.ankurm.sharegroups;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.kafka.clients.admin.AdminClient;
|
||||
import org.apache.kafka.clients.admin.AdminClientConfig;
|
||||
import org.apache.kafka.clients.admin.AlterConfigOp;
|
||||
import org.apache.kafka.clients.admin.ConfigEntry;
|
||||
import org.apache.kafka.clients.admin.NewTopic;
|
||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||
import org.apache.kafka.common.config.ConfigResource;
|
||||
import org.apache.kafka.common.serialization.StringSerializer;
|
||||
|
||||
/**
|
||||
* Helpers for the real broker. The tests do not start a broker: run scripts/broker-up.sh first
|
||||
* (Kafka 4.3.1, KRaft, localhost:9092). Override with -Dbroker=host:port.
|
||||
*/
|
||||
final class Broker {
|
||||
|
||||
static final String BOOTSTRAP = System.getProperty("broker", "localhost:9092");
|
||||
|
||||
private Broker() {
|
||||
}
|
||||
|
||||
static Properties admin() {
|
||||
Properties p = new Properties();
|
||||
p.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP);
|
||||
return p;
|
||||
}
|
||||
|
||||
/** A fresh topic per test run (unique suffix), so reruns never see old records. */
|
||||
static String freshTopic(String base, int partitions) throws Exception {
|
||||
String name = base + "-" + System.currentTimeMillis();
|
||||
try (AdminClient admin = AdminClient.create(admin())) {
|
||||
admin.createTopics(List.of(new NewTopic(name, partitions, (short) 1))).all().get(30, TimeUnit.SECONDS);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
/** Share groups start at the latest offset by default; flip a group to earliest before it first subscribes. */
|
||||
static void shareGroupFromEarliest(String group) throws Exception {
|
||||
ConfigResource resource = new ConfigResource(ConfigResource.Type.GROUP, group);
|
||||
try (AdminClient admin = AdminClient.create(admin())) {
|
||||
admin.incrementalAlterConfigs(Map.of(resource,
|
||||
List.of(new AlterConfigOp(new ConfigEntry("share.auto.offset.reset", "earliest"), AlterConfigOp.OpType.SET))))
|
||||
.all().get(30, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
static void produce(String topic, List<String[]> keyValues) {
|
||||
Properties p = new Properties();
|
||||
p.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP);
|
||||
p.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
|
||||
p.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
|
||||
try (KafkaProducer<String, String> producer = new KafkaProducer<>(p)) {
|
||||
for (String[] kv : keyValues) {
|
||||
producer.send(new ProducerRecord<>(topic, kv[0], kv[1]));
|
||||
}
|
||||
producer.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.ankurm.sharegroups;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.kafka.clients.consumer.AcknowledgeType;
|
||||
import org.apache.kafka.clients.consumer.ConsumerConfig;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||
import org.apache.kafka.clients.consumer.KafkaShareConsumer;
|
||||
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/** The first surprise: a brand-new share group starts at the end of the topic, not the beginning. */
|
||||
class OffsetResetTest {
|
||||
|
||||
@Test
|
||||
void newShareGroupSeesNothingUntilItIsToldToStartEarly() throws Exception {
|
||||
Transcript t = new Transcript("03-share-group-starts-at-latest.txt");
|
||||
String topic = Broker.freshTopic("reset", 1);
|
||||
Broker.produce(topic, List.of(new String[] { "k", "a" }, new String[] { "k", "b" }, new String[] { "k", "c" }));
|
||||
t.line("produced 3 records before any group existed");
|
||||
|
||||
int byDefault = drain(topic, topic + "-default", t);
|
||||
t.line("new group, default share.auto.offset.reset -> received %d records", byDefault);
|
||||
|
||||
String early = topic + "-early";
|
||||
Broker.shareGroupFromEarliest(early);
|
||||
int withEarliest = drain(topic, early, t);
|
||||
t.line("new group, share.auto.offset.reset=earliest -> received %d records", withEarliest);
|
||||
t.write();
|
||||
|
||||
assertThat(byDefault).isZero();
|
||||
assertThat(withEarliest).isEqualTo(3);
|
||||
}
|
||||
|
||||
private int drain(String topic, String group, Transcript t) {
|
||||
Properties p = new Properties();
|
||||
p.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, Broker.BOOTSTRAP);
|
||||
p.put(ConsumerConfig.GROUP_ID_CONFIG, group);
|
||||
p.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
|
||||
p.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
|
||||
p.put("share.acknowledgement.mode", "explicit");
|
||||
int received = 0;
|
||||
try (KafkaShareConsumer<String, String> c = new KafkaShareConsumer<>(p)) {
|
||||
c.subscribe(List.of(topic));
|
||||
long end = System.currentTimeMillis() + 12_000;
|
||||
while (System.currentTimeMillis() < end) {
|
||||
for (ConsumerRecord<String, String> r : c.poll(Duration.ofMillis(500))) {
|
||||
c.acknowledge(r, AcknowledgeType.ACCEPT);
|
||||
received++;
|
||||
}
|
||||
c.commitSync();
|
||||
}
|
||||
}
|
||||
return received;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.ankurm.sharegroups;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.apache.kafka.clients.consumer.AcknowledgeType;
|
||||
import org.apache.kafka.clients.consumer.ConsumerConfig;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||
import org.apache.kafka.clients.consumer.KafkaShareConsumer;
|
||||
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/** 40 records, one key, one partition, three consumers with uneven speed: arrival order is not the produce order. */
|
||||
class OrderingTest {
|
||||
|
||||
@Test
|
||||
void sameKeyDoesNotMeanSameOrder() throws Exception {
|
||||
Transcript t = new Transcript("04-no-ordering-guarantee.txt");
|
||||
String topic = Broker.freshTopic("order", 1);
|
||||
String group = topic + "-g";
|
||||
Broker.shareGroupFromEarliest(group);
|
||||
List<String[]> records = new ArrayList<>();
|
||||
for (int i = 0; i < 40; i++) {
|
||||
records.add(new String[] { "same-key", String.valueOf(i) });
|
||||
}
|
||||
|
||||
List<Integer> arrival = Collections.synchronizedList(new ArrayList<>());
|
||||
AtomicInteger done = new AtomicInteger();
|
||||
ExecutorService pool = Executors.newFixedThreadPool(3);
|
||||
int[] workMillis = { 5, 40, 90 };
|
||||
for (int id = 0; id < 3; id++) {
|
||||
int work = workMillis[id];
|
||||
pool.submit(() -> {
|
||||
Properties p = new Properties();
|
||||
p.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, Broker.BOOTSTRAP);
|
||||
p.put(ConsumerConfig.GROUP_ID_CONFIG, group);
|
||||
p.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
|
||||
p.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
|
||||
p.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, "4");
|
||||
p.put("share.acknowledgement.mode", "explicit");
|
||||
p.put("share.acquire.mode", "record_limit");
|
||||
try (KafkaShareConsumer<String, String> c = new KafkaShareConsumer<>(p)) {
|
||||
c.subscribe(List.of(topic));
|
||||
while (done.get() < 40 && !Thread.currentThread().isInterrupted()) {
|
||||
for (ConsumerRecord<String, String> r : c.poll(Duration.ofMillis(300))) {
|
||||
Thread.sleep(work);
|
||||
arrival.add(Integer.valueOf(r.value()));
|
||||
c.acknowledge(r, AcknowledgeType.ACCEPT);
|
||||
done.incrementAndGet();
|
||||
}
|
||||
c.commitSync();
|
||||
}
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
}
|
||||
Thread.sleep(6000); // let all three consumers join the group before any record exists
|
||||
Broker.produce(topic, records);
|
||||
long end = System.nanoTime() + TimeUnit.SECONDS.toNanos(60);
|
||||
while (done.get() < 40 && System.nanoTime() < end) {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
pool.shutdownNow();
|
||||
pool.awaitTermination(15, TimeUnit.SECONDS);
|
||||
|
||||
List<Integer> snapshot = new ArrayList<>(arrival);
|
||||
long inversions = 0;
|
||||
for (int i = 1; i < snapshot.size(); i++) {
|
||||
if (snapshot.get(i) < snapshot.get(i - 1)) {
|
||||
inversions++;
|
||||
}
|
||||
}
|
||||
t.line("produced : 0..39 in order, one key, one partition");
|
||||
t.line("processed : %s", snapshot);
|
||||
t.line("adjacent pairs that arrived out of order = %d", inversions);
|
||||
t.write();
|
||||
|
||||
assertThat(snapshot).hasSize(40);
|
||||
assertThat(inversions).isGreaterThan(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.ankurm.sharegroups;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
|
||||
/** The same ideas through Spring Kafka 4.1.1's share containers instead of the raw client. */
|
||||
@SpringBootTest(classes = { ShareGroupsApplication.class })
|
||||
class SpringShareListenerTest {
|
||||
|
||||
static String implicitTopic;
|
||||
static String manualTopic;
|
||||
|
||||
@DynamicPropertySource
|
||||
static void props(DynamicPropertyRegistry r) throws Exception {
|
||||
implicitTopic = Broker.freshTopic("spring-implicit", 1);
|
||||
manualTopic = Broker.freshTopic("spring-manual", 1);
|
||||
Broker.shareGroupFromEarliest(implicitTopic + "-g");
|
||||
Broker.shareGroupFromEarliest(manualTopic + "-g");
|
||||
r.add("share.topic.implicit", () -> implicitTopic);
|
||||
r.add("share.group.implicit", () -> implicitTopic + "-g");
|
||||
r.add("share.topic.manual", () -> manualTopic);
|
||||
r.add("share.group.manual", () -> manualTopic + "-g");
|
||||
r.add("spring.kafka.listener.auto-startup", () -> "true");
|
||||
}
|
||||
|
||||
@Autowired
|
||||
PaymentListeners listeners;
|
||||
|
||||
@Test
|
||||
void implicitAndManualAcknowledgement() throws Exception {
|
||||
Transcript t = new Transcript("05-spring-share-listeners.txt");
|
||||
|
||||
List<String[]> batch = new ArrayList<>();
|
||||
for (int i = 0; i < 24; i++) {
|
||||
batch.add(new String[] { "k" + i, "p" + i });
|
||||
}
|
||||
Broker.produce(implicitTopic, batch);
|
||||
await().atMost(Duration.ofSeconds(60)).until(() -> listeners.implicitSeen.size() >= 24);
|
||||
|
||||
Set<String> threads = new TreeSet<>();
|
||||
listeners.implicitSeen.forEach(s -> threads.add(s.substring(0, s.indexOf(" <- "))));
|
||||
t.line("== implicit mode, concurrency 4, 24 records on one topic partition");
|
||||
t.line("records handled = %d", listeners.implicitSeen.size());
|
||||
t.line("distinct listener threads that handled records = %d", threads.size());
|
||||
threads.forEach(n -> t.line(" %s", n));
|
||||
|
||||
Broker.produce(manualTopic, List.of(new String[] { "k", "ok" }, new String[] { "k", "again" }, new String[] { "k", "bad" }));
|
||||
await().atMost(Duration.ofSeconds(60)).until(() -> listeners.manualSeen.size() >= 4);
|
||||
Thread.sleep(8000);
|
||||
t.line("== manual mode (ShareAckMode.MANUAL), listener takes a ShareAcknowledgment");
|
||||
listeners.manualSeen.stream().sorted().forEach(s -> t.line("%s", s));
|
||||
t.write();
|
||||
|
||||
assertThat(listeners.implicitSeen).hasSize(24);
|
||||
assertThat(listeners.manualSeen).anyMatch(s -> s.startsWith("again delivery #2"));
|
||||
assertThat(listeners.manualSeen.stream().filter(s -> s.startsWith("bad")).count()).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.ankurm.sharegroups;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Collects the lines a test prints and writes them to output/NN-name.txt, so the article quotes real output. */
|
||||
final class Transcript {
|
||||
|
||||
private final String file;
|
||||
private final List<String> lines = new ArrayList<>();
|
||||
|
||||
Transcript(String file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
synchronized void line(String format, Object... args) {
|
||||
String l = String.format(format, args);
|
||||
lines.add(l);
|
||||
System.out.println(l);
|
||||
}
|
||||
|
||||
void write() throws IOException {
|
||||
Path dir = Path.of("output");
|
||||
Files.createDirectories(dir);
|
||||
Files.write(dir.resolve(file), lines);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package com.ankurm.sharegroups;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.apache.kafka.clients.consumer.AcknowledgeType;
|
||||
import org.apache.kafka.clients.consumer.ConsumerConfig;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||
import org.apache.kafka.clients.consumer.KafkaShareConsumer;
|
||||
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Six consumers, a two-partition topic, 60 records that each take 50 ms to process.
|
||||
* A consumer group can keep at most two of the six busy; a share group can keep more busy, but only when
|
||||
* share.acquire.mode lets max.poll.records bound what one consumer takes (see the default-mode result).
|
||||
*/
|
||||
class WorkSpreadTest {
|
||||
|
||||
static final int CONSUMERS = 6;
|
||||
static final int RECORDS = 60;
|
||||
static final int PARTITIONS = 2;
|
||||
|
||||
record Result(ConcurrentHashMap<Integer, AtomicInteger> perConsumer, long millis) {
|
||||
}
|
||||
|
||||
@Test
|
||||
void consumerGroupVersusShareGroup() throws Exception {
|
||||
Transcript t = new Transcript("01-work-spread.txt");
|
||||
t.line("topic partitions = %d, consumers = %d, records = %d, work per record = 50 ms", PARTITIONS, CONSUMERS, RECORDS);
|
||||
|
||||
Result classic = run("classic", t);
|
||||
Result shareDefault = run("share-default", t);
|
||||
Result share = run("share-record-limit", t);
|
||||
|
||||
long activeClassic = classic.perConsumer().values().stream().filter(c -> c.get() > 0).count();
|
||||
long activeShare = share.perConsumer().values().stream().filter(c -> c.get() > 0).count();
|
||||
long activeDefault = shareDefault.perConsumer().values().stream().filter(c -> c.get() > 0).count();
|
||||
t.line("consumer group : consumers that received records = %d", activeClassic);
|
||||
t.line("share group, default share.acquire.mode: consumers that received records = %d", activeDefault);
|
||||
t.line("share group, acquire.mode=record_limit : consumers that received records = %d", activeShare);
|
||||
t.line("(elapsed times vary from run to run; the shape, not the milliseconds, is the result)");
|
||||
t.write();
|
||||
|
||||
assertThat(activeClassic).isEqualTo(PARTITIONS);
|
||||
assertThat(activeShare).isGreaterThan(PARTITIONS);
|
||||
assertThat(activeDefault).isGreaterThanOrEqualTo(1);
|
||||
}
|
||||
|
||||
private Result run(String variant, Transcript t) throws Exception {
|
||||
boolean share = variant.startsWith("share");
|
||||
String topic = Broker.freshTopic("spread-" + variant, PARTITIONS);
|
||||
String group = topic + "-g";
|
||||
if (share) {
|
||||
Broker.shareGroupFromEarliest(group);
|
||||
}
|
||||
ConcurrentHashMap<Integer, AtomicInteger> counts = new ConcurrentHashMap<>();
|
||||
AtomicInteger total = new AtomicInteger();
|
||||
ExecutorService pool = Executors.newFixedThreadPool(CONSUMERS);
|
||||
for (int i = 0; i < CONSUMERS; i++) {
|
||||
int id = i;
|
||||
counts.put(id, new AtomicInteger());
|
||||
pool.submit(() -> consume(variant, topic, group, id, counts, total));
|
||||
}
|
||||
Thread.sleep(6000); // let every consumer join before the records arrive
|
||||
List<String[]> records = new ArrayList<>();
|
||||
for (int i = 0; i < RECORDS; i++) {
|
||||
records.add(new String[] { "key-" + i, "r" + i });
|
||||
}
|
||||
long start = System.nanoTime();
|
||||
Broker.produce(topic, records);
|
||||
while (total.get() < RECORDS && System.nanoTime() - start < TimeUnit.SECONDS.toNanos(60)) {
|
||||
Thread.sleep(50);
|
||||
}
|
||||
long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
|
||||
pool.shutdownNow();
|
||||
pool.awaitTermination(15, TimeUnit.SECONDS);
|
||||
|
||||
t.line("== %s", switch (variant) {
|
||||
case "classic" -> "consumer group (6 KafkaConsumers, same group.id)";
|
||||
case "share-default" -> "share group (6 ShareConsumers, default share.acquire.mode)";
|
||||
default -> "share group (6 ShareConsumers, share.acquire.mode=record_limit)";
|
||||
});
|
||||
counts.forEach((id, c) -> t.line("consumer-%d processed %d records", id, c.get()));
|
||||
t.line("total processed = %d in %d ms", total.get(), millis);
|
||||
return new Result(counts, millis);
|
||||
}
|
||||
|
||||
private void consume(String variant, String topic, String group, int id, ConcurrentHashMap<Integer, AtomicInteger> counts,
|
||||
AtomicInteger total) {
|
||||
Properties p = new Properties();
|
||||
p.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, Broker.BOOTSTRAP);
|
||||
p.put(ConsumerConfig.GROUP_ID_CONFIG, group);
|
||||
p.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
|
||||
p.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
|
||||
p.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, "3");
|
||||
boolean share = variant.startsWith("share");
|
||||
if (variant.equals("share-record-limit")) {
|
||||
p.put("share.acquire.mode", "record_limit");
|
||||
}
|
||||
try {
|
||||
if (share) {
|
||||
p.put("share.acknowledgement.mode", "explicit");
|
||||
try (KafkaShareConsumer<String, String> c = new KafkaShareConsumer<>(p)) {
|
||||
c.subscribe(List.of(topic));
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
for (ConsumerRecord<String, String> r : c.poll(Duration.ofMillis(300))) {
|
||||
Thread.sleep(50);
|
||||
c.acknowledge(r, AcknowledgeType.ACCEPT);
|
||||
counts.get(id).incrementAndGet();
|
||||
total.incrementAndGet();
|
||||
}
|
||||
c.commitSync();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
p.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
|
||||
try (KafkaConsumer<String, String> c = new KafkaConsumer<>(p)) {
|
||||
c.subscribe(List.of(topic));
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
for (ConsumerRecord<String, String> r : c.poll(Duration.ofMillis(300))) {
|
||||
Thread.sleep(50);
|
||||
counts.get(id).incrementAndGet();
|
||||
total.incrementAndGet();
|
||||
}
|
||||
c.commitSync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (InterruptedException | org.apache.kafka.common.errors.InterruptException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder><pattern>%d{HH:mm:ss.SSS} %-5level %logger{20} - %msg%n</pattern></encoder>
|
||||
</appender>
|
||||
<logger name="org.apache.kafka" level="OFF"/>
|
||||
<logger name="org.springframework.kafka" level="WARN"/>
|
||||
<root level="WARN"><appender-ref ref="STDOUT"/></root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user