-
Notifications
You must be signed in to change notification settings - Fork 467
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
Get latest counter before attempting leaseSteal to ensure leaseSteal succeeds #765
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. | ||
* Licensed under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package software.amazon.kinesis.common; | ||
|
||
|
||
public class CommonCalculations { | ||
|
||
|
||
/** | ||
* Convenience method for calculating renewer intervals in milliseconds. | ||
* | ||
* @param leaseDurationMillis Duration of a lease | ||
* @param epsilonMillis Allow for some variance when calculating lease expirations | ||
*/ | ||
public static long getRenewerTakerIntervalMillis(long leaseDurationMillis, long epsilonMillis) { | ||
return leaseDurationMillis / 3 - epsilonMillis; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
import software.amazon.kinesis.metrics.MetricsLevel; | ||
import software.amazon.kinesis.metrics.MetricsScope; | ||
import software.amazon.kinesis.metrics.MetricsUtil; | ||
import static software.amazon.kinesis.common.CommonCalculations.*; | ||
|
||
/** | ||
* An implementation of {@link LeaseTaker} that uses DynamoDB via {@link LeaseRefresher}. | ||
|
@@ -58,8 +59,11 @@ public class DynamoDBLeaseTaker implements LeaseTaker { | |
private final LeaseRefresher leaseRefresher; | ||
private final String workerIdentifier; | ||
private final long leaseDurationNanos; | ||
private final long leaseRenewalIntervalMillis; | ||
private final MetricsFactory metricsFactory; | ||
|
||
private final double RENEWAL_SLACK_PERCENTAGE = 0.5; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. static final? |
||
|
||
private final Map<String, Lease> allLeases = new HashMap<>(); | ||
// TODO: Remove these defaults and use the defaults in the config | ||
private int maxLeasesForWorker = Integer.MAX_VALUE; | ||
|
@@ -71,6 +75,7 @@ public DynamoDBLeaseTaker(LeaseRefresher leaseRefresher, String workerIdentifier | |
final MetricsFactory metricsFactory) { | ||
this.leaseRefresher = leaseRefresher; | ||
this.workerIdentifier = workerIdentifier; | ||
this.leaseRenewalIntervalMillis = getRenewerTakerIntervalMillis(leaseDurationMillis, 0); | ||
this.leaseDurationNanos = TimeUnit.MILLISECONDS.toNanos(leaseDurationMillis); | ||
this.metricsFactory = metricsFactory; | ||
} | ||
|
@@ -153,6 +158,7 @@ synchronized Map<String, Lease> takeLeases(Callable<Long> timeProvider) | |
final MetricsScope scope = MetricsUtil.createMetricsWithOperation(metricsFactory, TAKE_LEASES_DIMENSION); | ||
|
||
long startTime = System.currentTimeMillis(); | ||
long updateAllLeasesTotalTimeMillis; | ||
boolean success = false; | ||
|
||
ProvisionedThroughputException lastException = null; | ||
|
@@ -170,19 +176,23 @@ synchronized Map<String, Lease> takeLeases(Callable<Long> timeProvider) | |
} | ||
} | ||
} finally { | ||
updateAllLeasesTotalTimeMillis = System.currentTimeMillis() - startTime; | ||
MetricsUtil.addWorkerIdentifier(scope, workerIdentifier); | ||
MetricsUtil.addSuccessAndLatency(scope, "ListLeases", success, startTime, MetricsLevel.DETAILED); | ||
} | ||
|
||
|
||
if (lastException != null) { | ||
log.error("Worker {} could not scan leases table, aborting TAKE_LEASES_DIMENSION. Exception caught by" | ||
+ " last retry:", workerIdentifier, lastException); | ||
+ " last retry:", workerIdentifier, lastException); | ||
return takenLeases; | ||
} | ||
|
||
List<Lease> expiredLeases = getExpiredLeases(); | ||
|
||
Set<Lease> leasesToTake = computeLeasesToTake(expiredLeases); | ||
leasesToTake = updateStaleLeasesWithLatestState(updateAllLeasesTotalTimeMillis, leasesToTake); | ||
|
||
Set<String> untakenLeaseKeys = new HashSet<>(); | ||
|
||
for (Lease lease : leasesToTake) { | ||
|
@@ -230,6 +240,32 @@ synchronized Map<String, Lease> takeLeases(Callable<Long> timeProvider) | |
return takenLeases; | ||
} | ||
|
||
/** | ||
* If update all leases takes longer than the lease renewal time, | ||
* we fetch the latest lease info for the given leases that are marked for lease steal. | ||
* If nothing is found (or any transient network error occurs), | ||
* we default to the last known state of the lease | ||
* | ||
* @param updateAllLeasesEndTime How long it takes for update all leases to complete | ||
* @return set of leases to take. | ||
*/ | ||
private Set<Lease> updateStaleLeasesWithLatestState(long updateAllLeasesEndTime, | ||
Set<Lease> leasesToTake) { | ||
if (updateAllLeasesEndTime > leaseRenewalIntervalMillis * RENEWAL_SLACK_PERCENTAGE) { | ||
leasesToTake = leasesToTake.stream().map(lease -> { | ||
if (lease.isMarkedForLeaseSteal()) { | ||
try { | ||
return leaseRefresher.getLease(lease.leaseKey()); | ||
} catch (DependencyException | InvalidStateException | ProvisionedThroughputException e) { | ||
log.debug("Unable to retrieve the current lease, defaulting to existing lease", e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make this warn There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also give more context on when this failed. Like while we tried to update the stale leases |
||
} | ||
} | ||
return lease; | ||
}).collect(Collectors.toSet()); | ||
} | ||
return leasesToTake; | ||
} | ||
|
||
/** Package access for testing purposes. | ||
* | ||
* @param strings | ||
|
@@ -526,8 +562,9 @@ private List<Lease> chooseLeasesToSteal(Map<String, Integer> leaseCounts, int ne | |
// Return random ones | ||
Collections.shuffle(candidates); | ||
int toIndex = Math.min(candidates.size(), numLeasesToSteal); | ||
leasesToSteal.addAll(candidates.subList(0, toIndex)); | ||
|
||
leasesToSteal.addAll(candidates.subList(0, toIndex).stream() | ||
.map(lease -> lease.isMarkedForLeaseSteal(true)) | ||
.collect(Collectors.toList())); | ||
return leasesToSteal; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid wildcard in packages