refactor(Worker): remove exits after initial lease completion code & default time limit
This commit is contained in:
parent
0a4fc8bef0
commit
913f2e9377
1 changed files with 4 additions and 64 deletions
|
|
@ -85,8 +85,6 @@ public class Worker implements Runnable {
|
||||||
private static final int MAX_RETRIES = 4;
|
private static final int MAX_RETRIES = 4;
|
||||||
private static final WorkerStateChangeListener DEFAULT_WORKER_STATE_CHANGE_LISTENER = new NoOpWorkerStateChangeListener();
|
private static final WorkerStateChangeListener DEFAULT_WORKER_STATE_CHANGE_LISTENER = new NoOpWorkerStateChangeListener();
|
||||||
private static final boolean DEFAULT_EXITS_ON_FAILURE = false;
|
private static final boolean DEFAULT_EXITS_ON_FAILURE = false;
|
||||||
private static final boolean DEFAULT_EXITS_AFTER_INITIAL_LEASES_COMPLETE = false;
|
|
||||||
private static final Optional<Duration> DEFAULT_TIME_LIMIT = Optional.empty();
|
|
||||||
|
|
||||||
private WorkerLog wlog = new WorkerLog();
|
private WorkerLog wlog = new WorkerLog();
|
||||||
|
|
||||||
|
|
@ -128,8 +126,6 @@ public class Worker implements Runnable {
|
||||||
|
|
||||||
// fivetran configurables
|
// fivetran configurables
|
||||||
private final boolean exitOnFailure;
|
private final boolean exitOnFailure;
|
||||||
private final boolean exitAfterInitialLeasesComplete;
|
|
||||||
private final Optional<Duration> timeLimit;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -361,9 +357,7 @@ public class Worker implements Runnable {
|
||||||
config.getRetryGetRecordsInSeconds(),
|
config.getRetryGetRecordsInSeconds(),
|
||||||
config.getMaxGetRecordsThreadPool(),
|
config.getMaxGetRecordsThreadPool(),
|
||||||
DEFAULT_WORKER_STATE_CHANGE_LISTENER,
|
DEFAULT_WORKER_STATE_CHANGE_LISTENER,
|
||||||
DEFAULT_EXITS_ON_FAILURE,
|
DEFAULT_EXITS_ON_FAILURE);
|
||||||
DEFAULT_EXITS_AFTER_INITIAL_LEASES_COMPLETE,
|
|
||||||
DEFAULT_TIME_LIMIT);
|
|
||||||
|
|
||||||
// If a region name was explicitly specified, use it as the region for Amazon Kinesis and Amazon DynamoDB.
|
// If a region name was explicitly specified, use it as the region for Amazon Kinesis and Amazon DynamoDB.
|
||||||
if (config.getRegionName() != null) {
|
if (config.getRegionName() != null) {
|
||||||
|
|
@ -411,7 +405,7 @@ public class Worker implements Runnable {
|
||||||
shardSyncIdleTimeMillis, cleanupLeasesUponShardCompletion, checkpoint, leaseCoordinator, execService,
|
shardSyncIdleTimeMillis, cleanupLeasesUponShardCompletion, checkpoint, leaseCoordinator, execService,
|
||||||
metricsFactory, taskBackoffTimeMillis, failoverTimeMillis, skipShardSyncAtWorkerInitializationIfLeasesExist,
|
metricsFactory, taskBackoffTimeMillis, failoverTimeMillis, skipShardSyncAtWorkerInitializationIfLeasesExist,
|
||||||
shardPrioritization, Optional.empty(), Optional.empty(), DEFAULT_WORKER_STATE_CHANGE_LISTENER,
|
shardPrioritization, Optional.empty(), Optional.empty(), DEFAULT_WORKER_STATE_CHANGE_LISTENER,
|
||||||
DEFAULT_EXITS_ON_FAILURE, DEFAULT_EXITS_AFTER_INITIAL_LEASES_COMPLETE, DEFAULT_TIME_LIMIT);
|
DEFAULT_EXITS_ON_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -443,7 +437,7 @@ public class Worker implements Runnable {
|
||||||
IMetricsFactory metricsFactory, long taskBackoffTimeMillis, long failoverTimeMillis,
|
IMetricsFactory metricsFactory, long taskBackoffTimeMillis, long failoverTimeMillis,
|
||||||
boolean skipShardSyncAtWorkerInitializationIfLeasesExist, ShardPrioritization shardPrioritization,
|
boolean skipShardSyncAtWorkerInitializationIfLeasesExist, ShardPrioritization shardPrioritization,
|
||||||
Optional<Integer> retryGetRecordsInSeconds, Optional<Integer> maxGetRecordsThreadPool, WorkerStateChangeListener workerStateChangeListener,
|
Optional<Integer> retryGetRecordsInSeconds, Optional<Integer> maxGetRecordsThreadPool, WorkerStateChangeListener workerStateChangeListener,
|
||||||
boolean exitOnFailure, boolean exitAfterInitialLeasesComplete, Optional<Duration> timeLimit) {
|
boolean exitOnFailure) {
|
||||||
this.applicationName = applicationName;
|
this.applicationName = applicationName;
|
||||||
this.recordProcessorFactory = recordProcessorFactory;
|
this.recordProcessorFactory = recordProcessorFactory;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
|
@ -467,8 +461,6 @@ public class Worker implements Runnable {
|
||||||
this.maxGetRecordsThreadPool = maxGetRecordsThreadPool;
|
this.maxGetRecordsThreadPool = maxGetRecordsThreadPool;
|
||||||
this.workerStateChangeListener = workerStateChangeListener;
|
this.workerStateChangeListener = workerStateChangeListener;
|
||||||
this.exitOnFailure = exitOnFailure;
|
this.exitOnFailure = exitOnFailure;
|
||||||
this.exitAfterInitialLeasesComplete = exitAfterInitialLeasesComplete;
|
|
||||||
this.timeLimit = timeLimit;
|
|
||||||
workerStateChangeListener.onWorkerStateChange(WorkerStateChangeListener.WorkerState.CREATED);
|
workerStateChangeListener.onWorkerStateChange(WorkerStateChangeListener.WorkerState.CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -503,52 +495,14 @@ public class Worker implements Runnable {
|
||||||
shutdown();
|
shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
Instant start = Instant.now();
|
|
||||||
List<String> initialLeaseKeys = getExistingLeaseKeys();
|
|
||||||
|
|
||||||
while (!shouldShutdown()) {
|
while (!shouldShutdown()) {
|
||||||
runProcessLoop();
|
runProcessLoop();
|
||||||
shutdownIfFivetranReady(start, initialLeaseKeys);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
finalShutdown();
|
finalShutdown();
|
||||||
LOG.info("Worker loop is complete. Exiting from worker.");
|
LOG.info("Worker loop is complete. Exiting from worker.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getExistingLeaseKeys() {
|
|
||||||
try {
|
|
||||||
return leaseCoordinator.getLeaseManager().listLeases().stream().map(Lease::getLeaseKey).collect(Collectors.toList());
|
|
||||||
} catch (DependencyException | InvalidStateException | ProvisionedThroughputException e) {
|
|
||||||
throw new RuntimeException("Unable to list leases to determine when to stop worker");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void shutdownIfFivetranReady(Instant start, List<String> initialLeases) {
|
|
||||||
boolean initiateShutdown = false;
|
|
||||||
|
|
||||||
List<String> currentLeases = getExistingLeaseKeys();
|
|
||||||
boolean initialLeasesComplete = currentLeases.stream().noneMatch(initialLeases::contains);
|
|
||||||
|
|
||||||
if (exitAfterInitialLeasesComplete && initialLeasesComplete) {
|
|
||||||
LOG.info("Requesting shutdown after finishing with all initial leases: " + initialLeases);
|
|
||||||
initiateShutdown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timeLimit.isPresent() && Duration.between(start, Instant.now()).compareTo(timeLimit.get()) > 0) {
|
|
||||||
LOG.info("Requesting shutdown after time limit of " + timeLimit.get() + " reached");
|
|
||||||
initiateShutdown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (initiateShutdown) {
|
|
||||||
try {
|
|
||||||
startGracefulShutdown().get(5, TimeUnit.MINUTES);
|
|
||||||
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
|
||||||
LOG.error("Unable to gracefully shutdown: " + e.getMessage());
|
|
||||||
shutdown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void runProcessLoop() {
|
void runProcessLoop() {
|
||||||
try {
|
try {
|
||||||
|
|
@ -1126,8 +1080,6 @@ public class Worker implements Runnable {
|
||||||
|
|
||||||
// fivetran additions
|
// fivetran additions
|
||||||
boolean exitOnFailure = DEFAULT_EXITS_ON_FAILURE;
|
boolean exitOnFailure = DEFAULT_EXITS_ON_FAILURE;
|
||||||
boolean exitAfterInitialLeasesComplete = DEFAULT_EXITS_AFTER_INITIAL_LEASES_COMPLETE;
|
|
||||||
Optional<Duration> timeLimit = DEFAULT_TIME_LIMIT;
|
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
AmazonKinesis getKinesisClient() {
|
AmazonKinesis getKinesisClient() {
|
||||||
|
|
@ -1273,9 +1225,7 @@ public class Worker implements Runnable {
|
||||||
config.getRetryGetRecordsInSeconds(),
|
config.getRetryGetRecordsInSeconds(),
|
||||||
config.getMaxGetRecordsThreadPool(),
|
config.getMaxGetRecordsThreadPool(),
|
||||||
workerStateChangeListener,
|
workerStateChangeListener,
|
||||||
exitOnFailure,
|
exitOnFailure);
|
||||||
exitAfterInitialLeasesComplete,
|
|
||||||
timeLimit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
<R, T extends AwsClientBuilder<T, R>> R createClient(final T builder,
|
<R, T extends AwsClientBuilder<T, R>> R createClient(final T builder,
|
||||||
|
|
@ -1357,15 +1307,5 @@ public class Worker implements Runnable {
|
||||||
this.exitOnFailure = true;
|
this.exitOnFailure = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder exitAfterInitialLeasesComplete() {
|
|
||||||
this.exitAfterInitialLeasesComplete = true;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder timeLimit(Duration timeLimit) {
|
|
||||||
this.timeLimit = Optional.ofNullable(timeLimit);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue