Skip to content

Commit

Permalink
[fix][io] Close the kafka source connector got stuck (apache#20698)
Browse files Browse the repository at this point in the history
Motivation: apache#19880 (comment) When Kafka connector is closing, it waits for the `runnerThread` to stop, but the task-close is running at the same thread, so it will be stuck.

Modifications: run `close` in another thread.
  • Loading branch information
poorbarcode authored Jun 30, 2023
1 parent d83c7a6 commit c5237ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
6 changes: 6 additions & 0 deletions pulsar-io/kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,14 @@ public void start() {
});
runnerThread.setUncaughtExceptionHandler(
(t, e) -> {
LOG.error("[{}] Error while consuming records", t.getName(), e);
try {
this.close();
} catch (InterruptedException ex) {
// The interrupted exception is thrown by the runnerThread itself. Ignore it.
}
new Thread(() -> {
LOG.error("[{}] Error while consuming records", t.getName(), e);
try {
this.close();
} catch (Exception ex) {
LOG.error("[{}] Close kafka source error", t.getName(), e);
}
}, "Kafka Source Close Task Thread").start();
});
runnerThread.setName("Kafka Source Thread");
runnerThread.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.pulsar.io.core.SourceContext;
import org.apache.pulsar.io.kafka.KafkaAbstractSource;
import org.apache.pulsar.io.kafka.KafkaSourceConfig;
import org.awaitility.Awaitility;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -173,7 +174,10 @@ public final void closeConnectorWhenUnexpectedExceptionThrownTest() throws Excep
Field runningField = KafkaAbstractSource.class.getDeclaredField("running");
runningField.setAccessible(true);

Assert.assertFalse((boolean) runningField.get(source));
Awaitility.await().untilAsserted(() -> {
Assert.assertFalse((boolean) runningField.get(source));
Assert.assertNull(consumerField.get(source));
});
}

private File getFile(String name) {
Expand Down

0 comments on commit c5237ea

Please sign in to comment.