|
| 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.Duration; |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.concurrent.ConcurrentHashMap; |
| 23 | +import java.util.concurrent.atomic.AtomicInteger; |
| 24 | + |
| 25 | +/** Shared process-local latency scores for routed Spanner endpoints. */ |
| 26 | +final class EndpointLatencyRegistry { |
| 27 | + |
| 28 | + static final Duration DEFAULT_ERROR_PENALTY = Duration.ofSeconds(10); |
| 29 | + static final Duration DEFAULT_RTT = Duration.ofMillis(10); |
| 30 | + static final double DEFAULT_PENALTY_VALUE = 1_000_000.0; |
| 31 | + |
| 32 | + private static final ConcurrentHashMap<TrackerKey, LatencyTracker> TRACKERS = |
| 33 | + new ConcurrentHashMap<>(); |
| 34 | + private static final ConcurrentHashMap<String, AtomicInteger> INFLIGHT_REQUESTS = |
| 35 | + new ConcurrentHashMap<>(); |
| 36 | + |
| 37 | + private EndpointLatencyRegistry() {} |
| 38 | + |
| 39 | + static boolean hasScore(long operationUid, String endpointLabelOrAddress) { |
| 40 | + TrackerKey trackerKey = trackerKey(operationUid, endpointLabelOrAddress); |
| 41 | + return trackerKey != null && TRACKERS.containsKey(trackerKey); |
| 42 | + } |
| 43 | + |
| 44 | + static double getSelectionCost(long operationUid, String endpointLabelOrAddress) { |
| 45 | + TrackerKey trackerKey = trackerKey(operationUid, endpointLabelOrAddress); |
| 46 | + if (trackerKey == null) { |
| 47 | + return Double.MAX_VALUE; |
| 48 | + } |
| 49 | + double activeRequests = getInflight(endpointLabelOrAddress); |
| 50 | + LatencyTracker tracker = TRACKERS.get(trackerKey); |
| 51 | + if (tracker != null) { |
| 52 | + return tracker.getScore() * (activeRequests + 1.0); |
| 53 | + } |
| 54 | + if (activeRequests > 0.0) { |
| 55 | + return DEFAULT_PENALTY_VALUE + activeRequests; |
| 56 | + } |
| 57 | + return defaultRttMicros() * (activeRequests + 1.0); |
| 58 | + } |
| 59 | + |
| 60 | + static void recordLatency(long operationUid, String endpointLabelOrAddress, Duration latency) { |
| 61 | + TrackerKey trackerKey = trackerKey(operationUid, endpointLabelOrAddress); |
| 62 | + if (trackerKey == null || latency == null) { |
| 63 | + return; |
| 64 | + } |
| 65 | + TRACKERS.computeIfAbsent(trackerKey, ignored -> new EwmaLatencyTracker()).update(latency); |
| 66 | + } |
| 67 | + |
| 68 | + static void recordError(long operationUid, String endpointLabelOrAddress) { |
| 69 | + recordError(operationUid, endpointLabelOrAddress, DEFAULT_ERROR_PENALTY); |
| 70 | + } |
| 71 | + |
| 72 | + static void recordError(long operationUid, String endpointLabelOrAddress, Duration penalty) { |
| 73 | + TrackerKey trackerKey = trackerKey(operationUid, endpointLabelOrAddress); |
| 74 | + if (trackerKey == null || penalty == null) { |
| 75 | + return; |
| 76 | + } |
| 77 | + TRACKERS.computeIfAbsent(trackerKey, ignored -> new EwmaLatencyTracker()).recordError(penalty); |
| 78 | + } |
| 79 | + |
| 80 | + static void beginRequest(String endpointLabelOrAddress) { |
| 81 | + String address = normalizeAddress(endpointLabelOrAddress); |
| 82 | + if (address == null) { |
| 83 | + return; |
| 84 | + } |
| 85 | + INFLIGHT_REQUESTS.computeIfAbsent(address, ignored -> new AtomicInteger()).incrementAndGet(); |
| 86 | + } |
| 87 | + |
| 88 | + static void finishRequest(String endpointLabelOrAddress) { |
| 89 | + String address = normalizeAddress(endpointLabelOrAddress); |
| 90 | + if (address == null) { |
| 91 | + return; |
| 92 | + } |
| 93 | + AtomicInteger counter = INFLIGHT_REQUESTS.get(address); |
| 94 | + if (counter == null) { |
| 95 | + return; |
| 96 | + } |
| 97 | + int updated = counter.decrementAndGet(); |
| 98 | + if (updated <= 0) { |
| 99 | + INFLIGHT_REQUESTS.remove(address, counter); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + static int getInflight(String endpointLabelOrAddress) { |
| 104 | + String address = normalizeAddress(endpointLabelOrAddress); |
| 105 | + if (address == null) { |
| 106 | + return 0; |
| 107 | + } |
| 108 | + AtomicInteger counter = INFLIGHT_REQUESTS.get(address); |
| 109 | + return counter == null ? 0 : Math.max(0, counter.get()); |
| 110 | + } |
| 111 | + |
| 112 | + @VisibleForTesting |
| 113 | + static void clear() { |
| 114 | + TRACKERS.clear(); |
| 115 | + INFLIGHT_REQUESTS.clear(); |
| 116 | + } |
| 117 | + |
| 118 | + @VisibleForTesting |
| 119 | + static String normalizeAddress(String endpointLabelOrAddress) { |
| 120 | + if (endpointLabelOrAddress == null || endpointLabelOrAddress.isEmpty()) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + return endpointLabelOrAddress; |
| 124 | + } |
| 125 | + |
| 126 | + @VisibleForTesting |
| 127 | + static TrackerKey trackerKey(long operationUid, String endpointLabelOrAddress) { |
| 128 | + String address = normalizeAddress(endpointLabelOrAddress); |
| 129 | + if (operationUid <= 0 || address == null) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + return new TrackerKey(operationUid, address); |
| 133 | + } |
| 134 | + |
| 135 | + private static long defaultRttMicros() { |
| 136 | + return DEFAULT_RTT.toNanos() / 1_000L; |
| 137 | + } |
| 138 | + |
| 139 | + @VisibleForTesting |
| 140 | + static final class TrackerKey { |
| 141 | + private final long operationUid; |
| 142 | + private final String address; |
| 143 | + |
| 144 | + private TrackerKey(long operationUid, String address) { |
| 145 | + this.operationUid = operationUid; |
| 146 | + this.address = address; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public boolean equals(Object other) { |
| 151 | + if (this == other) { |
| 152 | + return true; |
| 153 | + } |
| 154 | + if (!(other instanceof TrackerKey)) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + TrackerKey that = (TrackerKey) other; |
| 158 | + return operationUid == that.operationUid && Objects.equals(address, that.address); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public int hashCode() { |
| 163 | + return Objects.hash(operationUid, address); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public String toString() { |
| 168 | + return operationUid + "@" + address; |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments