Skip to content

Commit

Permalink
Add retry to make sure source is not shutdown when exceptions are thr…
Browse files Browse the repository at this point in the history
…own on the main thread (opensearch-project#5029)

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

Signed-off-by: Souvik Bose <souvbose@amazon.com>

* Address review comments

Signed-off-by: Souvik Bose <souvbose@amazon.com>

---------

Signed-off-by: Souvik Bose <souvbose@amazon.com>
Co-authored-by: Souvik Bose <souvbose@amazon.com>
  • Loading branch information
sb2k16 and sbose2k21 authored Oct 11, 2024
1 parent 37aaac8 commit a49e93f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
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!");
}
}
}
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 @@ -76,6 +76,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 @@ -105,6 +107,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 @@ -135,6 +139,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 @@ -160,4 +166,4 @@ void testSourceConfigWithInitialPosition() {
assertEquals(kinesisStreamConfig.getCompression(), CompressionOption.GZIP);
}
}
}
}

0 comments on commit a49e93f

Please sign in to comment.