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

Add retry to make sure source is not shutdown when exceptions are thrown on the main thread #5029

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
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 @@ -116,20 +116,38 @@ public void start(final Buffer<Record<Event>> buffer) {
public void shutDown() {
LOG.info("Stop request received for Kinesis Source");

Future<Boolean> gracefulShutdownFuture = scheduler.startGracefulShutdown();
LOG.info("Waiting up to {} seconds for shutdown to complete.", GRACEFUL_SHUTDOWN_WAIT_INTERVAL_SECONDS);
try {
gracefulShutdownFuture.get(GRACEFUL_SHUTDOWN_WAIT_INTERVAL_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
LOG.error("Exception while executing kinesis consumer graceful shutdown, doing force shutdown", ex);
scheduler.shutdown();
if (scheduler != null) {
Future<Boolean> gracefulShutdownFuture = scheduler.startGracefulShutdown();
LOG.info("Waiting up to {} seconds for shutdown to complete.", GRACEFUL_SHUTDOWN_WAIT_INTERVAL_SECONDS);
try {
gracefulShutdownFuture.get(GRACEFUL_SHUTDOWN_WAIT_INTERVAL_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
LOG.error("Exception while executing kinesis consumer graceful shutdown, doing force shutdown", ex);
scheduler.shutdown();
}
LOG.info("Completed, shutting down now.");
} else {
LOG.info("The Kinesis Scheduler was not initialized.");
}
LOG.info("Completed, shutting down now.");
}

public Scheduler getScheduler(final Buffer<Record<Event>> buffer) {
if (scheduler == null) {
return createScheduler(buffer);

int numRetries = 0;
while (scheduler == null && numRetries++ < kinesisSourceConfig.getMaxInitializationAttempts()) {
try {
scheduler = createScheduler(buffer);
} catch (Exception ex) {
LOG.error("Caught exception when initializing KCL Scheduler. Will retry");
}

if (scheduler == null) {
try {
Thread.sleep(kinesisSourceConfig.getInitializationBackoffTime().toMillis());
} catch (InterruptedException e){
LOG.debug("Interrupted exception!");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LOG.debug("Interrupted exception!");
LOG.debug("Interrupted exception.");

}
}
}
return scheduler;
}
Expand Down Expand Up @@ -158,7 +176,9 @@ public Scheduler createScheduler(final Buffer<Record<Event>> buffer) {

return new Scheduler(
configsBuilder.checkpointConfig(),
configsBuilder.coordinatorConfig(),
configsBuilder.coordinatorConfig()
.schedulerInitializationBackoffTimeMillis(kinesisSourceConfig.getInitializationBackoffTime().toMillis())
.maxInitializationAttempts(kinesisSourceConfig.getMaxInitializationAttempts()),
configsBuilder.leaseManagementConfig().billingMode(BillingMode.PAY_PER_REQUEST),
configsBuilder.lifecycleConfig(),
configsBuilder.metricsConfig(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class KinesisSourceConfig {
static final Duration DEFAULT_TIME_OUT_IN_MILLIS = Duration.ofMillis(1000);
static final int DEFAULT_NUMBER_OF_RECORDS_TO_ACCUMULATE = 100;
static final Duration DEFAULT_SHARD_ACKNOWLEDGEMENT_TIMEOUT = Duration.ofMinutes(10);
static final Duration DEFAULT_INITIALIZATION_BACKOFF_TIME = Duration.ofMillis(1000);
static final int DEFAULT_MAX_INITIALIZATION_ATTEMPTS = Integer.MAX_VALUE;

@Getter
@JsonProperty("streams")
Expand Down Expand Up @@ -69,6 +71,14 @@ public class KinesisSourceConfig {
public Duration getShardAcknowledgmentTimeout() {
return shardAcknowledgmentTimeout;
}

@Getter
@JsonProperty("max_initialization_attempts")
private int maxInitializationAttempts = DEFAULT_MAX_INITIALIZATION_ATTEMPTS;

@Getter
@JsonProperty("initialization_backoff_time")
private Duration initializationBackoffTime = DEFAULT_INITIALIZATION_BACKOFF_TIME;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class KinesisServiceTest {
private static final int NUMBER_OF_RECORDS_TO_ACCUMULATE = 10;
private static final int DEFAULT_MAX_RECORDS = 10000;
private static final int IDLE_TIME_BETWEEN_READS_IN_MILLIS = 250;
private static final int DEFAULT_INITIALIZATION_ATTEMPTS = 10;
private static final String awsAccountId = "123456789012";
private static final String streamArnFormat = "arn:aws:kinesis:us-east-1:%s:stream/%s";
private static final Instant streamCreationTime = Instant.now();
Expand Down Expand Up @@ -189,6 +190,7 @@ void setup() {
streamConfigs.add(kinesisStreamConfig);
when(kinesisSourceConfig.getStreams()).thenReturn(streamConfigs);
when(kinesisSourceConfig.getNumberOfRecordsToAccumulate()).thenReturn(NUMBER_OF_RECORDS_TO_ACCUMULATE);
when(kinesisSourceConfig.getMaxInitializationAttempts()).thenReturn(DEFAULT_INITIALIZATION_ATTEMPTS);

PluginModel pluginModel = mock(PluginModel.class);
when(pluginModel.getPluginName()).thenReturn(codec_plugin_name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ void testSourceConfig() {
assertThat(kinesisSourceConfig, notNullValue());
assertEquals(KinesisSourceConfig.DEFAULT_NUMBER_OF_RECORDS_TO_ACCUMULATE, kinesisSourceConfig.getNumberOfRecordsToAccumulate());
assertEquals(KinesisSourceConfig.DEFAULT_TIME_OUT_IN_MILLIS, kinesisSourceConfig.getBufferTimeout());
assertEquals(KinesisSourceConfig.DEFAULT_MAX_INITIALIZATION_ATTEMPTS, kinesisSourceConfig.getMaxInitializationAttempts());
assertEquals(KinesisSourceConfig.DEFAULT_INITIALIZATION_BACKOFF_TIME, kinesisSourceConfig.getInitializationBackoffTime());
assertTrue(kinesisSourceConfig.isAcknowledgments());
assertEquals(KinesisSourceConfig.DEFAULT_SHARD_ACKNOWLEDGEMENT_TIMEOUT, kinesisSourceConfig.getShardAcknowledgmentTimeout());
assertThat(kinesisSourceConfig.getAwsAuthenticationConfig(), notNullValue());
Expand Down Expand Up @@ -104,6 +106,8 @@ void testSourceConfigWithStreamCodec() {
assertThat(kinesisSourceConfig, notNullValue());
assertEquals(KinesisSourceConfig.DEFAULT_NUMBER_OF_RECORDS_TO_ACCUMULATE, kinesisSourceConfig.getNumberOfRecordsToAccumulate());
assertEquals(KinesisSourceConfig.DEFAULT_TIME_OUT_IN_MILLIS, kinesisSourceConfig.getBufferTimeout());
assertEquals(KinesisSourceConfig.DEFAULT_MAX_INITIALIZATION_ATTEMPTS, kinesisSourceConfig.getMaxInitializationAttempts());
assertEquals(KinesisSourceConfig.DEFAULT_INITIALIZATION_BACKOFF_TIME, kinesisSourceConfig.getInitializationBackoffTime());
assertFalse(kinesisSourceConfig.isAcknowledgments());
assertEquals(KinesisSourceConfig.DEFAULT_SHARD_ACKNOWLEDGEMENT_TIMEOUT, kinesisSourceConfig.getShardAcknowledgmentTimeout());
assertThat(kinesisSourceConfig.getAwsAuthenticationConfig(), notNullValue());
Expand Down Expand Up @@ -134,6 +138,8 @@ void testSourceConfigWithInitialPosition() {
assertThat(kinesisSourceConfig, notNullValue());
assertEquals(KinesisSourceConfig.DEFAULT_NUMBER_OF_RECORDS_TO_ACCUMULATE, kinesisSourceConfig.getNumberOfRecordsToAccumulate());
assertEquals(KinesisSourceConfig.DEFAULT_TIME_OUT_IN_MILLIS, kinesisSourceConfig.getBufferTimeout());
assertEquals(KinesisSourceConfig.DEFAULT_MAX_INITIALIZATION_ATTEMPTS, kinesisSourceConfig.getMaxInitializationAttempts());
assertEquals(KinesisSourceConfig.DEFAULT_INITIALIZATION_BACKOFF_TIME, kinesisSourceConfig.getInitializationBackoffTime());
assertFalse(kinesisSourceConfig.isAcknowledgments());
assertEquals(KinesisSourceConfig.DEFAULT_SHARD_ACKNOWLEDGEMENT_TIMEOUT, kinesisSourceConfig.getShardAcknowledgmentTimeout());
assertThat(kinesisSourceConfig.getAwsAuthenticationConfig(), notNullValue());
Expand All @@ -158,4 +164,4 @@ void testSourceConfigWithInitialPosition() {
assertEquals(kinesisStreamConfig.getCheckPointInterval(), expectedCheckpointIntervals.get(kinesisStreamConfig.getName()));
}
}
}
}
Loading