
com.zendesk.maxwell.producer.KafkaProducerDiagnostic Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maxwell Show documentation
Show all versions of maxwell Show documentation
Maxwell's daemon. Watches mysql, outputs to JSON.
package com.zendesk.maxwell.producer;
import com.zendesk.maxwell.MaxwellConfig;
import com.zendesk.maxwell.monitoring.MaxwellDiagnostic;
import com.zendesk.maxwell.monitoring.MaxwellDiagnosticResult;
import com.zendesk.maxwell.row.RowMap;
import com.zendesk.maxwell.schema.PositionStoreThread;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
public class KafkaProducerDiagnostic implements MaxwellDiagnostic {
private final MaxwellKafkaProducerWorker producer;
private final MaxwellConfig config;
private final PositionStoreThread positionStoreThread;
public KafkaProducerDiagnostic(MaxwellKafkaProducerWorker producer, MaxwellConfig config, PositionStoreThread positionStoreThread) {
this.producer = producer;
this.config = config;
this.positionStoreThread = positionStoreThread;
}
@Override
public String getName() {
return "kafka-producer";
}
@Override
public CompletableFuture check() {
return getLatency().thenApply(this::normalResult).exceptionally(this::exceptionResult);
}
@Override
public boolean isMandatory() {
return true;
}
@Override
public String getResource() {
return config.getKafkaProperties().getProperty("bootstrap.servers");
}
public CompletableFuture getLatency() {
DiagnosticCallback callback = new DiagnosticCallback();
try {
RowMap rowMap = new RowMap("insert", config.databaseName, "dummy", System.currentTimeMillis(),
new ArrayList<>(), positionStoreThread.getPosition());
rowMap.setTXCommit();
ProducerRecord record = producer.makeProducerRecord(rowMap);
producer.sendAsync(record, callback);
} catch (Exception e) {
callback.latency.completeExceptionally(e);
}
return callback.latency;
}
private MaxwellDiagnosticResult.Check normalResult(Long latency) {
Map info = new HashMap<>();
info.put("message", "Kafka producer acknowledgement lag is " + latency.toString() + "ms");
return new MaxwellDiagnosticResult.Check(this, true, info);
}
private MaxwellDiagnosticResult.Check exceptionResult(Throwable e) {
Map info = new HashMap<>();
info.put("error", e.getCause().toString());
return new MaxwellDiagnosticResult.Check(this, false, info);
}
static class DiagnosticCallback implements Callback {
final CompletableFuture latency;
final long sendTime;
DiagnosticCallback() {
latency = new CompletableFuture<>();
sendTime = System.currentTimeMillis();
}
@Override
public void onCompletion(final RecordMetadata metadata, final Exception exception) {
if (exception == null) {
latency.complete(System.currentTimeMillis() - sendTime);
} else {
latency.completeExceptionally(exception);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy