Skip to content

Commit aee6fba

Browse files
committed
Fix a race condition in the split reader by retrying the subscription after a 2-second delay
1 parent 4bacee3 commit aee6fba

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/reader/PulsarPartitionSplitReader.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,32 @@ public void handleSplitsChanges(SplitsChange<PulsarPartitionSplit> splitsChanges
234234
}
235235
}
236236

237-
// Create pulsar consumer.
237+
// Create pulsar consumer with retry.
238238
try {
239239
this.pulsarConsumer = createPulsarConsumer(registeredSplit.getPartition());
240240
} catch (PulsarClientException e) {
241-
throw new FlinkRuntimeException(String.format("Failed to create consumer on partition %s", registeredSplit.getPartition()), e);
241+
LOG.warn("Failed to create consumer on partition {} on first attempt, will retry after 2 seconds",
242+
registeredSplit.getPartition(), e);
243+
244+
try {
245+
Thread.sleep(2000);
246+
} catch (InterruptedException ie) {
247+
Thread.currentThread().interrupt();
248+
throw new FlinkRuntimeException(
249+
String.format("Interrupted while waiting to retry consumer creation on partition %s",
250+
registeredSplit.getPartition()), ie);
251+
}
252+
253+
// Retry consumer creation
254+
try {
255+
this.pulsarConsumer = createPulsarConsumer(registeredSplit.getPartition());
256+
LOG.info("Successfully created consumer on partition {} on second attempt",
257+
registeredSplit.getPartition());
258+
} catch (PulsarClientException retryException) {
259+
throw new FlinkRuntimeException(
260+
String.format("Failed to create consumer on partition %s after retry",
261+
registeredSplit.getPartition()), retryException);
262+
}
242263
}
243264

244265
LOG.info("Register split {} consumer for current reader.", registeredSplit);

0 commit comments

Comments
 (0)