Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move blocking send operation to executor thread #251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

import java.time.Duration;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Kafka write stream implementation
Expand All @@ -50,6 +52,7 @@ public class KafkaWriteStreamImpl<K, V> implements KafkaWriteStream<K, V> {
private final ProducerTracer tracer;
private final TaskQueue taskQueue;

private final ExecutorService executor = Executors.newSingleThreadExecutor();
public KafkaWriteStreamImpl(Vertx vertx, Producer<K, V> producer, KafkaClientOptions options) {
ContextInternal ctxInt = ((ContextInternal) vertx.getOrCreateContext()).unwrap();
this.producer = producer;
Expand Down Expand Up @@ -77,42 +80,44 @@ public Future<RecordMetadata> send(ProducerRecord<K, V> record) {
return ctx.executeBlocking(() -> {
Promise<RecordMetadata> prom = ctx.promise();
try {
this.producer.send(record, (metadata, err) -> {
executor.execute(() -> {
this.producer.send(record, (metadata, err) -> {

// callback from Kafka IO thread
ctx.runOnContext(v1 -> {
synchronized (KafkaWriteStreamImpl.this) {
// callback from Kafka IO thread
ctx.runOnContext(v1 -> {
synchronized (KafkaWriteStreamImpl.this) {

// if exception happens, no record written
if (err != null) {
// if exception happens, no record written
if (err != null) {

if (this.exceptionHandler != null) {
Handler<Throwable> exceptionHandler = this.exceptionHandler;
ctx.runOnContext(v2 -> exceptionHandler.handle(err));
if (this.exceptionHandler != null) {
Handler<Throwable> exceptionHandler = this.exceptionHandler;
ctx.runOnContext(v2 -> exceptionHandler.handle(err));
}
}

long lowWaterMark = this.maxSize / 2;
this.pending -= len;
if (this.pending < lowWaterMark && this.drainHandler != null) {
Handler<Void> drainHandler = this.drainHandler;
this.drainHandler = null;
ctx.runOnContext(drainHandler);
}
}
});

long lowWaterMark = this.maxSize / 2;
this.pending -= len;
if (this.pending < lowWaterMark && this.drainHandler != null) {
Handler<Void> drainHandler = this.drainHandler;
this.drainHandler = null;
ctx.runOnContext(drainHandler);
if (err != null) {
if (startedSpan != null) {
startedSpan.fail(ctx, err);
}
prom.fail(err);
} else {
if (startedSpan != null) {
startedSpan.finish(ctx);
}
prom.complete(metadata);
}
});

if (err != null) {
if (startedSpan != null) {
startedSpan.fail(ctx, err);
}
prom.fail(err);
} else {
if (startedSpan != null) {
startedSpan.finish(ctx);
}
prom.complete(metadata);
}
});
} catch (Throwable e) {
synchronized (KafkaWriteStreamImpl.this) {
Expand Down