Skip to content

Commit

Permalink
Make use of Java 8 to simplify computeLeaseCounts() (#847)
Browse files Browse the repository at this point in the history
It's possible to remove quite a few lines of code from the
method using Java 8 features (a `groupingBy` Collector, etc).

Java 8 is the minimum supported version of the KCL since
#176 was
merged in July 2017.

See also:

https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#package.description
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html
  • Loading branch information
rtyley authored Oct 4, 2021
1 parent 4fe29ff commit e638c17
Showing 1 changed file with 8 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import software.amazon.kinesis.metrics.MetricsScope;
import software.amazon.kinesis.metrics.MetricsUtil;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.summingInt;

/**
* An implementation of {@link LeaseTaker} that uses DynamoDB via {@link LeaseRefresher}.
*/
Expand Down Expand Up @@ -539,30 +542,16 @@ private List<Lease> chooseLeasesToSteal(Map<String, Integer> leaseCounts, int ne
* @return map of workerIdentifier to lease count
*/
private Map<String, Integer> computeLeaseCounts(List<Lease> expiredLeases) {
Map<String, Integer> leaseCounts = new HashMap<>();
// The set will give much faster lookup than the original list, an
// important optimization when the list is large
Set<Lease> expiredLeasesSet = new HashSet<>(expiredLeases);

// Compute the number of leases per worker by looking through allLeases and ignoring leases that have expired.
for (Lease lease : allLeases.values()) {
if (!expiredLeasesSet.contains(lease)) {
String leaseOwner = lease.leaseOwner();
Integer oldCount = leaseCounts.get(leaseOwner);
if (oldCount == null) {
leaseCounts.put(leaseOwner, 1);
} else {
leaseCounts.put(leaseOwner, oldCount + 1);
}
}
}
Map<String, Integer> leaseCounts = allLeases.values().stream()
.filter(lease -> !expiredLeasesSet.contains(lease))
.collect(groupingBy(Lease::leaseOwner, summingInt(lease -> 1)));

// If I have no leases, I wasn't represented in leaseCounts. Let's fix that.
Integer myCount = leaseCounts.get(workerIdentifier);
if (myCount == null) {
myCount = 0;
leaseCounts.put(workerIdentifier, myCount);
}
// If I have no leases, I won't be represented in leaseCounts. Let's fix that.
leaseCounts.putIfAbsent(workerIdentifier, 0);

return leaseCounts;
}
Expand Down

0 comments on commit e638c17

Please sign in to comment.