Skip to content

Commit

Permalink
Rework HPA scaledown lock to be more fair (#1018)
Browse files Browse the repository at this point in the history
Co-authored-by: Bryan Burkholder <[email protected]>
  • Loading branch information
bryanlb and bryanlb authored Aug 5, 2024
1 parent e1be082 commit e0cf0b9
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.slf4j.Logger;
Expand Down Expand Up @@ -102,10 +103,12 @@ protected synchronized void runOneIteration() {
* </pre>
*/
private void publishCacheHpaMetrics() {
Set<String> replicaSets =
List<String> replicaSets =
replicaMetadataStore.listSync().stream()
.map(ReplicaMetadata::getReplicaSet)
.collect(Collectors.toSet());
.distinct()
.collect(Collectors.toList());
Collections.shuffle(replicaSets);

for (String replicaSet : replicaSets) {
long totalCacheSlotCapacity =
Expand Down Expand Up @@ -261,8 +264,17 @@ protected boolean tryCacheReplicasetLock(String replicaset) {
}
}

// update the last-updated lock time to now
cacheScalingLock.put(replicaset, Instant.now());
// only refresh the lock if it doesn't exist, or is expired
if (cacheScalingLock.containsKey(replicaset)) {
if (cacheScalingLock.get(replicaset).isBefore(Instant.now().minus(CACHE_SCALEDOWN_LOCK))) {
// update the last-acquired lock time to now (ie, refresh the lock for another
// CACHE_SCALEDOWN_LOCK mins
cacheScalingLock.put(replicaset, Instant.now());
}
} else {
// set the last-updated lock time to now
cacheScalingLock.put(replicaset, Instant.now());
}
return true;
}
}

0 comments on commit e0cf0b9

Please sign in to comment.