|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.spanner.spi.v1; |
| 18 | + |
| 19 | +import com.google.common.annotations.VisibleForTesting; |
| 20 | +import java.time.Clock; |
| 21 | +import java.time.Duration; |
| 22 | +import java.time.Instant; |
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
| 24 | +import java.util.concurrent.ThreadLocalRandom; |
| 25 | +import java.util.function.LongUnaryOperator; |
| 26 | + |
| 27 | +/** |
| 28 | + * Tracks short-lived endpoint cooldowns after routed {@code RESOURCE_EXHAUSTED} failures. |
| 29 | + * |
| 30 | + * <p>This allows later requests to try a different replica instead of immediately routing back to |
| 31 | + * the same overloaded endpoint. |
| 32 | + */ |
| 33 | +final class EndpointOverloadCooldownTracker { |
| 34 | + |
| 35 | + @VisibleForTesting static final Duration DEFAULT_INITIAL_COOLDOWN = Duration.ofSeconds(10); |
| 36 | + @VisibleForTesting static final Duration DEFAULT_MAX_COOLDOWN = Duration.ofMinutes(1); |
| 37 | + @VisibleForTesting static final Duration DEFAULT_RESET_AFTER = Duration.ofMinutes(10); |
| 38 | + |
| 39 | + @VisibleForTesting |
| 40 | + static final class CooldownState { |
| 41 | + private final int consecutiveFailures; |
| 42 | + private final Instant cooldownUntil; |
| 43 | + private final Instant lastFailureAt; |
| 44 | + |
| 45 | + private CooldownState(int consecutiveFailures, Instant cooldownUntil, Instant lastFailureAt) { |
| 46 | + this.consecutiveFailures = consecutiveFailures; |
| 47 | + this.cooldownUntil = cooldownUntil; |
| 48 | + this.lastFailureAt = lastFailureAt; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private final ConcurrentHashMap<String, CooldownState> entries = new ConcurrentHashMap<>(); |
| 53 | + private final Duration initialCooldown; |
| 54 | + private final Duration maxCooldown; |
| 55 | + private final Duration resetAfter; |
| 56 | + private final Clock clock; |
| 57 | + private final LongUnaryOperator randomLong; |
| 58 | + |
| 59 | + EndpointOverloadCooldownTracker() { |
| 60 | + this( |
| 61 | + DEFAULT_INITIAL_COOLDOWN, |
| 62 | + DEFAULT_MAX_COOLDOWN, |
| 63 | + DEFAULT_RESET_AFTER, |
| 64 | + Clock.systemUTC(), |
| 65 | + bound -> ThreadLocalRandom.current().nextLong(bound)); |
| 66 | + } |
| 67 | + |
| 68 | + @VisibleForTesting |
| 69 | + EndpointOverloadCooldownTracker( |
| 70 | + Duration initialCooldown, |
| 71 | + Duration maxCooldown, |
| 72 | + Duration resetAfter, |
| 73 | + Clock clock, |
| 74 | + LongUnaryOperator randomLong) { |
| 75 | + Duration resolvedInitial = |
| 76 | + (initialCooldown == null || initialCooldown.isZero() || initialCooldown.isNegative()) |
| 77 | + ? DEFAULT_INITIAL_COOLDOWN |
| 78 | + : initialCooldown; |
| 79 | + Duration resolvedMax = |
| 80 | + (maxCooldown == null || maxCooldown.isZero() || maxCooldown.isNegative()) |
| 81 | + ? DEFAULT_MAX_COOLDOWN |
| 82 | + : maxCooldown; |
| 83 | + if (resolvedMax.compareTo(resolvedInitial) < 0) { |
| 84 | + resolvedMax = resolvedInitial; |
| 85 | + } |
| 86 | + this.initialCooldown = resolvedInitial; |
| 87 | + this.maxCooldown = resolvedMax; |
| 88 | + this.resetAfter = |
| 89 | + (resetAfter == null || resetAfter.isZero() || resetAfter.isNegative()) |
| 90 | + ? DEFAULT_RESET_AFTER |
| 91 | + : resetAfter; |
| 92 | + this.clock = clock == null ? Clock.systemUTC() : clock; |
| 93 | + this.randomLong = |
| 94 | + randomLong == null ? bound -> ThreadLocalRandom.current().nextLong(bound) : randomLong; |
| 95 | + } |
| 96 | + |
| 97 | + boolean isCoolingDown(String address) { |
| 98 | + if (address == null || address.isEmpty()) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + Instant now = clock.instant(); |
| 102 | + CooldownState state = entries.get(address); |
| 103 | + if (state == null) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + if (state.cooldownUntil.isAfter(now)) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + if (Duration.between(state.lastFailureAt, now).compareTo(resetAfter) < 0) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + entries.remove(address, state); |
| 113 | + CooldownState current = entries.get(address); |
| 114 | + return current != null && current.cooldownUntil.isAfter(now); |
| 115 | + } |
| 116 | + |
| 117 | + void recordFailure(String address) { |
| 118 | + if (address == null || address.isEmpty()) { |
| 119 | + return; |
| 120 | + } |
| 121 | + Instant now = clock.instant(); |
| 122 | + entries.compute( |
| 123 | + address, |
| 124 | + (ignored, state) -> { |
| 125 | + int consecutiveFailures = 1; |
| 126 | + if (state != null |
| 127 | + && Duration.between(state.lastFailureAt, now).compareTo(resetAfter) < 0) { |
| 128 | + consecutiveFailures = state.consecutiveFailures + 1; |
| 129 | + } |
| 130 | + Duration cooldown = cooldownForFailures(consecutiveFailures); |
| 131 | + return new CooldownState(consecutiveFailures, now.plus(cooldown), now); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + private Duration cooldownForFailures(int failures) { |
| 136 | + Duration cooldown = initialCooldown; |
| 137 | + for (int i = 1; i < failures; i++) { |
| 138 | + if (cooldown.compareTo(maxCooldown.dividedBy(2)) > 0) { |
| 139 | + cooldown = maxCooldown; |
| 140 | + break; |
| 141 | + } |
| 142 | + cooldown = cooldown.multipliedBy(2); |
| 143 | + } |
| 144 | + long bound = Math.max(1L, cooldown.toMillis() + 1L); |
| 145 | + return Duration.ofMillis(randomLong.applyAsLong(bound)); |
| 146 | + } |
| 147 | + |
| 148 | + @VisibleForTesting |
| 149 | + CooldownState getState(String address) { |
| 150 | + return entries.get(address); |
| 151 | + } |
| 152 | +} |
0 commit comments