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.
(cherry picked from commit c5237ea)
(cherry picked from commit 3a2e593)
  • Loading branch information
poorbarcode authored and nicoloboschi committed Jul 17, 2023
1 parent b94990a commit fa43cf7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pulsar-io/kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
<artifactId>jjwt-jackson</artifactId>
</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 @@ -188,7 +188,16 @@ public void start() {
}
});
runnerThread.setUncaughtExceptionHandler(
(t, e) -> LOG.error("[{}] Error while consuming records", t.getName(), e));
(t, e) -> {
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 @@ -21,14 +21,19 @@


import com.google.common.collect.ImmutableMap;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.security.auth.SecurityProtocol;
import org.apache.pulsar.client.api.Schema;
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 @@ -154,6 +159,28 @@ public final void loadFromSaslYamlFileTest() throws IOException {
assertEquals(config.getSslTruststorePassword(), "cert_pwd");
}

@Test
public final void closeConnectorWhenUnexpectedExceptionThrownTest() throws Exception {
KafkaAbstractSource source = new DummySource();
Consumer consumer = mock(Consumer.class);
Mockito.doThrow(new RuntimeException("Uncaught exception")).when(consumer)
.subscribe(Mockito.any(Collection.class));

Field consumerField = KafkaAbstractSource.class.getDeclaredField("consumer");
consumerField.setAccessible(true);
consumerField.set(source, consumer);

source.start();

Field runningField = KafkaAbstractSource.class.getDeclaredField("running");
runningField.setAccessible(true);

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

private File getFile(String name) {
ClassLoader classLoader = getClass().getClassLoader();
return new File(classLoader.getResource(name).getFile());
Expand Down

0 comments on commit fa43cf7

Please sign in to comment.