Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
f7ca5f1
test(cdc): require acknowledged Kafka publication before offsets
seonghobae Aug 8, 2026
b378c98
test(cdc): cover bounded Kafka publication failures
seonghobae Aug 8, 2026
15153d0
fix(cdc): await Kafka acknowledgements before offset progress
seonghobae Aug 8, 2026
254ea3f
fix(cdc): require durable Kafka producer acknowledgements
seonghobae Aug 8, 2026
1ae9d75
test(cdc): compile bounded publication failure contract
seonghobae Aug 8, 2026
ffa3dd6
docs(cdc): record acknowledged-delivery evidence
seonghobae Aug 8, 2026
d35e9e2
docs(cdc): document acknowledged source publish operations
seonghobae Aug 8, 2026
56c2671
docs(architecture): bind CDC offsets to Kafka acknowledgements
seonghobae Aug 8, 2026
968e62a
docs(changelog): record CDC acknowledged delivery
seonghobae Aug 8, 2026
3a5f37c
refactor(cdc): simplify bounded Kafka publish retry
seonghobae Aug 8, 2026
54ba5c1
test(cdc): require public API documentation
seonghobae Aug 8, 2026
61f18f2
docs(cdc): complete public service API documentation
seonghobae Aug 8, 2026
85e9f8a
test(cdc): bound hung Kafka acknowledgement waits
seonghobae Aug 8, 2026
c56dce4
fix(cdc): bound Kafka acknowledgement waits
seonghobae Aug 8, 2026
6e89ee2
docs(cdc): record bounded acknowledgement timeout
seonghobae Aug 8, 2026
4848e7b
docs(cdc): document finite future wait
seonghobae Aug 8, 2026
279a29e
docs(changelog): record bounded Kafka future wait
seonghobae Aug 8, 2026
db78668
Merge develop into automation/opencode-20260808T1303Z-cdc-kafka-ack
seonghobae Sep 11, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 91 additions & 15 deletions cdc-service/src/main/java/com/xtrmetl/cdc/service/CdcService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@
import io.debezium.engine.ChangeEvent;
import io.debezium.engine.DebeziumEngine;
import io.debezium.engine.format.Json;
import org.apache.kafka.common.KafkaException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand All @@ -33,6 +38,7 @@
public class CdcService implements DisposableBean {

private static final Logger log = LoggerFactory.getLogger(CdcService.class);
private static final int KAFKA_PUBLISH_ATTEMPTS = 2;

private ExecutorService executor;
private final KafkaTemplate<String, String> kafkaTemplate;
Expand All @@ -42,6 +48,8 @@ public class CdcService implements DisposableBean {
private final DebeziumChangeRecordMapper changeRecordMapper;
private final AtomicLong canonicalMapSuccess = new AtomicLong();
private final AtomicLong canonicalMapFailure = new AtomicLong();
private final AtomicLong kafkaPublishSuccess = new AtomicLong();
private final AtomicLong kafkaPublishFailure = new AtomicLong();

private DebeziumEngine<ChangeEvent<String, String>> debeziumEngine;
private Future<?> engineTask;
Expand Down Expand Up @@ -177,6 +185,8 @@ public synchronized Map<String, Object> getStatus() {
status.put("canonicalMapEnabled", canonicalMapEnabled);
status.put("canonicalMapSuccess", canonicalMapSuccess.get());
status.put("canonicalMapFailure", canonicalMapFailure.get());
status.put("kafkaPublishSuccess", kafkaPublishSuccess.get());
status.put("kafkaPublishFailure", kafkaPublishFailure.get());
status.put("configPrefixes", "mightyetl.* (preferred) or xtrmetl.* (legacy); dual-read via EnvironmentPostProcessor");
status.put("notes", "Capture is PostgreSQL→Kafka only; see docs/cdc/any-to-any-cdc.md");
return status;
Expand Down Expand Up @@ -221,7 +231,7 @@ public synchronized void shutdown() {
private void initializeDebeziumEngine() {
this.debeziumEngine = DebeziumEngine.create(Json.class)
.using(getCdcConfiguration().asProperties())
.notifying(this::handleChangeEvent)
.notifying(this::handleChangeBatch)
.build();
}

Expand All @@ -231,33 +241,99 @@ public void destroy() {
}

/**
* Publishes Debezium key/value JSON to the destination Kafka topic.
* Publishes one Debezium change event without waiting for acknowledgement.
*
* Debezium에서 받은 CDC 변경 이벤트의 JSON key/value를 해당 topic으로 전송한다.
* <p>This method is retained for direct callers and compatibility tests. The live Debezium engine uses
* {@link #handleChangeBatch(List, DebeziumEngine.RecordCommitter)} so source offsets are advanced only after
* Kafka acknowledges publication.</p>
*
* Uses a key-less send overload when the key is absent, following Spring Kafka conventions.
*
* key가 없으면 Spring Kafka 계약에 맞게 key-less send 오버로드를 사용한다.
*
* @param changeEvent Debezium의 변경 이벤트로부터 key/value와 destination을 포함하는 이벤트 객체
* @param changeEvent Debezium change event containing destination, key, and JSON value
*/
protected void handleChangeEvent(ChangeEvent<String, String> changeEvent) {
String topic = changeEvent.destination();
if (topic == null) {
return;
}

String key = changeEvent.key();
String value = changeEvent.value();
maybeMapCanonical(topic, changeEvent.key(), changeEvent.value());
sendChangeEvent(changeEvent);
}

maybeMapCanonical(topic, key, value);
/**
* Publishes a Debezium batch to Kafka and advances source offsets only after broker acknowledgement.
*
* <p>Each event with a destination is sent as raw Debezium JSON, awaited, and retried once after a terminal
* publication failure. A record is marked processed only after an acknowledged send. The batch is marked
* finished only after every record has been processed. An interrupt propagates immediately without advancing
* the current record or batch.</p>
*
* <p>An event without a destination is treated as non-publishable engine metadata and is marked processed
* without touching Kafka or the publication counters.</p>
*
* @param records ordered Debezium change events for the current engine batch
* @param committer Debezium offset committer controlling record and batch progress
* @throws InterruptedException when the worker is interrupted while awaiting Kafka acknowledgement
* @throws KafkaException when Kafka does not acknowledge an event within two application-level attempts
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
*/
protected void handleChangeBatch(
List<ChangeEvent<String, String>> records,
DebeziumEngine.RecordCommitter<ChangeEvent<String, String>> committer
) throws InterruptedException {
for (ChangeEvent<String, String> changeEvent : records) {
String topic = changeEvent.destination();
if (topic != null) {
maybeMapCanonical(topic, changeEvent.key(), changeEvent.value());
publishWithAcknowledgement(changeEvent);
}
committer.markProcessed(changeEvent);
}
committer.markBatchFinished();
}

// Live path: raw Debezium JSON (not canonical) for consumer compatibility.
/**
* Sends an event and waits for Kafka acknowledgement, retrying once after a terminal failure.
*/
private void publishWithAcknowledgement(ChangeEvent<String, String> changeEvent) throws InterruptedException {
for (int attempt = 1; attempt <= KAFKA_PUBLISH_ATTEMPTS; attempt++) {
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
try {
awaitAcknowledgement(changeEvent);
kafkaPublishSuccess.incrementAndGet();
return;
} catch (ExecutionException executionException) {
kafkaPublishFailure.incrementAndGet();
if (attempt == KAFKA_PUBLISH_ATTEMPTS) {
throw new KafkaException(
"Kafka publication failed after " + KAFKA_PUBLISH_ATTEMPTS + " attempts",
executionException.getCause()
);
}
}
}
}

/**
* Performs one Kafka send attempt and blocks interruptibly until that attempt completes.
*/
private void awaitAcknowledgement(ChangeEvent<String, String> changeEvent)
throws InterruptedException, ExecutionException {
try {
sendChangeEvent(changeEvent).get();
} catch (RuntimeException runtimeException) {
throw new ExecutionException(runtimeException);
}
}

/**
* Selects the keyed or keyless Spring Kafka send overload for one raw Debezium JSON event.
*/
private CompletableFuture<SendResult<String, String>> sendChangeEvent(ChangeEvent<String, String> changeEvent) {
String topic = changeEvent.destination();
String key = changeEvent.key();
String value = changeEvent.value();
if (key != null) {
kafkaTemplate.send(topic, key, value);
} else {
kafkaTemplate.send(topic, value);
return kafkaTemplate.send(topic, key, value);
}
return kafkaTemplate.send(topic, value);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions cdc-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ spring:
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
acks: all
properties:
"[enable.idempotence]": true
"[delivery.timeout.ms]": ${CDC_KAFKA_DELIVERY_TIMEOUT_MS:60000}
"[max.block.ms]": ${CDC_KAFKA_MAX_BLOCK_MS:30000}
"[max.in.flight.requests.per.connection]": 5
consumer:
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
Expand Down
Loading
Loading