|
18 | 18 | package com.datastax.oss.driver.api.core; |
19 | 19 |
|
20 | 20 | import com.datastax.oss.driver.api.core.cql.ExecutionInfo; |
| 21 | +import com.datastax.oss.driver.api.core.metadata.EndPoint; |
21 | 22 | import edu.umd.cs.findbugs.annotations.NonNull; |
| 23 | +import edu.umd.cs.findbugs.annotations.Nullable; |
22 | 24 |
|
23 | | -/** Thrown when a driver request timed out. */ |
| 25 | +/** |
| 26 | + * Thrown when a driver request timed out. |
| 27 | + * |
| 28 | + * <p>When thrown from the request execution path the exception carries a per-node diagnostic |
| 29 | + * snapshot captured at the moment the timer fires (see {@link #getNodeDiagnostics()}). This |
| 30 | + * information is also embedded in the exception message for easy log-based diagnosis. |
| 31 | + */ |
24 | 32 | public class DriverTimeoutException extends DriverException { |
| 33 | + |
| 34 | + /** |
| 35 | + * Sentinel value used in {@link NodeDiagnostics} fields when the corresponding data was not |
| 36 | + * available at the time the exception was created (e.g. the pool had already been removed). |
| 37 | + */ |
| 38 | + public static final int UNAVAILABLE = -1; |
| 39 | + |
| 40 | + /** |
| 41 | + * Per-node diagnostic snapshot captured at timeout time. |
| 42 | + * |
| 43 | + * <p>Fields: |
| 44 | + * |
| 45 | + * <ul> |
| 46 | + * <li>{@link #getChannelInFlight()}: requests currently awaiting a response on the specific |
| 47 | + * connection used for this request. |
| 48 | + * <li>{@link #getPoolInFlight()}: total in-flight across all connections to this host ({@link |
| 49 | + * #UNAVAILABLE} if the pool was already removed). |
| 50 | + * <li>{@link #getPoolAvailableIds()}: remaining stream IDs available to send new requests; a |
| 51 | + * low value indicates pool contention ({@link #UNAVAILABLE} if pool was already removed). |
| 52 | + * <li>{@link #getPoolOrphanedIds()}: stream IDs from previously timed-out or cancelled requests |
| 53 | + * that cannot be released yet; a high value indicates stale stream ID accumulation ({@link |
| 54 | + * #UNAVAILABLE} if pool was already removed). |
| 55 | + * </ul> |
| 56 | + * |
| 57 | + * <p><b>Diagnosing failure modes:</b> |
| 58 | + * |
| 59 | + * <ul> |
| 60 | + * <li>{@code poolAvailableIds} near zero → pool contention; requests queuing inside the driver |
| 61 | + * before reaching the server. |
| 62 | + * <li>{@code poolAvailableIds} normal + high {@code channelInFlight} → server is slow; requests |
| 63 | + * were sent but not answered within the timeout. |
| 64 | + * <li>High {@code poolOrphanedIds} → previous timeouts consumed stream IDs that the driver is |
| 65 | + * still waiting to reclaim. |
| 66 | + * </ul> |
| 67 | + */ |
| 68 | + public static final class NodeDiagnostics { |
| 69 | + |
| 70 | + @NonNull private final EndPoint endPoint; |
| 71 | + private final int channelInFlight; |
| 72 | + private final int poolInFlight; |
| 73 | + private final int poolAvailableIds; |
| 74 | + private final int poolOrphanedIds; |
| 75 | + |
| 76 | + /** |
| 77 | + * Creates a full diagnostic snapshot (pool was available at timeout time). |
| 78 | + * |
| 79 | + * @param endPoint the endpoint of the node. |
| 80 | + * @param channelInFlight in-flight count on the specific channel. |
| 81 | + * @param poolInFlight total in-flight across the pool for this host. |
| 82 | + * @param poolAvailableIds remaining stream IDs available in the pool. |
| 83 | + * @param poolOrphanedIds orphaned stream IDs in the pool. |
| 84 | + */ |
| 85 | + public NodeDiagnostics( |
| 86 | + @NonNull EndPoint endPoint, |
| 87 | + int channelInFlight, |
| 88 | + int poolInFlight, |
| 89 | + int poolAvailableIds, |
| 90 | + int poolOrphanedIds) { |
| 91 | + this.endPoint = endPoint; |
| 92 | + this.channelInFlight = channelInFlight; |
| 93 | + this.poolInFlight = poolInFlight; |
| 94 | + this.poolAvailableIds = poolAvailableIds; |
| 95 | + this.poolOrphanedIds = poolOrphanedIds; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Creates a partial diagnostic snapshot for when the pool was unavailable at timeout time. The |
| 100 | + * pool-related fields ({@link #getPoolInFlight()}, {@link #getPoolAvailableIds()}, {@link |
| 101 | + * #getPoolOrphanedIds()}) will be {@link DriverTimeoutException#UNAVAILABLE}. |
| 102 | + * |
| 103 | + * @param endPoint the endpoint of the node. |
| 104 | + * @param channelInFlight in-flight count on the specific channel. |
| 105 | + */ |
| 106 | + public NodeDiagnostics(@NonNull EndPoint endPoint, int channelInFlight) { |
| 107 | + this(endPoint, channelInFlight, UNAVAILABLE, UNAVAILABLE, UNAVAILABLE); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Creates a diagnostic snapshot using pre-computed pool stats. Pass {@link |
| 112 | + * DriverTimeoutException#UNAVAILABLE} for pool fields when the pool was not available at |
| 113 | + * timeout time. |
| 114 | + * |
| 115 | + * @param endPoint the endpoint of the node. |
| 116 | + * @param channelInFlight in-flight count on the specific channel. |
| 117 | + * @param poolInFlight total in-flight across the pool, or {@link |
| 118 | + * DriverTimeoutException#UNAVAILABLE}. |
| 119 | + * @param poolAvailableIds remaining stream IDs in the pool, or {@link |
| 120 | + * DriverTimeoutException#UNAVAILABLE}. |
| 121 | + * @param poolOrphanedIds orphaned stream IDs in the pool, or {@link |
| 122 | + * DriverTimeoutException#UNAVAILABLE}. |
| 123 | + */ |
| 124 | + @NonNull |
| 125 | + public static NodeDiagnostics of( |
| 126 | + @NonNull EndPoint endPoint, |
| 127 | + int channelInFlight, |
| 128 | + int poolInFlight, |
| 129 | + int poolAvailableIds, |
| 130 | + int poolOrphanedIds) { |
| 131 | + return new NodeDiagnostics( |
| 132 | + endPoint, channelInFlight, poolInFlight, poolAvailableIds, poolOrphanedIds); |
| 133 | + } |
| 134 | + |
| 135 | + /** Returns the endpoint of the node that had in-flight requests at timeout time. */ |
| 136 | + @NonNull |
| 137 | + public EndPoint getEndPoint() { |
| 138 | + return endPoint; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Returns the number of in-flight requests on the specific connection at timeout time, or |
| 143 | + * {@link DriverTimeoutException#UNAVAILABLE} if not available. |
| 144 | + */ |
| 145 | + public int getChannelInFlight() { |
| 146 | + return channelInFlight; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Returns the total number of in-flight requests across all connections to this host at timeout |
| 151 | + * time, or {@link DriverTimeoutException#UNAVAILABLE} if the pool was no longer available. |
| 152 | + */ |
| 153 | + public int getPoolInFlight() { |
| 154 | + return poolInFlight; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Returns the number of remaining stream IDs available in the pool at timeout time, or {@link |
| 159 | + * DriverTimeoutException#UNAVAILABLE} if the pool was no longer available. A low value |
| 160 | + * indicates pool contention. |
| 161 | + */ |
| 162 | + public int getPoolAvailableIds() { |
| 163 | + return poolAvailableIds; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Returns the number of orphaned stream IDs in the pool at timeout time, or {@link |
| 168 | + * DriverTimeoutException#UNAVAILABLE} if the pool was no longer available. A high value |
| 169 | + * indicates stale stream ID accumulation from previous timeouts. |
| 170 | + */ |
| 171 | + public int getPoolOrphanedIds() { |
| 172 | + return poolOrphanedIds; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public String toString() { |
| 177 | + if (poolInFlight == UNAVAILABLE) { |
| 178 | + return String.format("%s [channel in-flight: %d, pool: n/a]", endPoint, channelInFlight); |
| 179 | + } |
| 180 | + return String.format( |
| 181 | + "%s [channel in-flight: %d, pool in-flight: %d, pool available ids: %d, pool orphaned ids: %d]", |
| 182 | + endPoint, channelInFlight, poolInFlight, poolAvailableIds, poolOrphanedIds); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + @Nullable private final NodeDiagnostics nodeDiagnostics; |
| 187 | + |
| 188 | + /** |
| 189 | + * Creates an exception with a plain message and no node diagnostics. Used for cases where the |
| 190 | + * diagnostic data is unavailable (e.g. no nodes were in-flight at timeout time). |
| 191 | + * |
| 192 | + * @param message the exception message. |
| 193 | + */ |
25 | 194 | public DriverTimeoutException(@NonNull String message) { |
26 | | - this(message, null); |
| 195 | + this(message, (NodeDiagnostics) null); |
27 | 196 | } |
28 | 197 |
|
29 | | - private DriverTimeoutException(String message, ExecutionInfo executionInfo) { |
| 198 | + /** |
| 199 | + * Creates an exception with per-node diagnostic context captured at timeout time. The message is |
| 200 | + * generated automatically from {@code baseMessage} and the diagnostic data. |
| 201 | + * |
| 202 | + * @param baseMessage the base timeout message (e.g. {@code "Query timed out after PT0.5S"}). |
| 203 | + * @param nodeDiagnostics per-node diagnostic snapshot; may be {@code null} if unavailable, in |
| 204 | + * which case no node information is appended to the message. |
| 205 | + */ |
| 206 | + public DriverTimeoutException( |
| 207 | + @NonNull String baseMessage, @Nullable NodeDiagnostics nodeDiagnostics) { |
| 208 | + this(buildMessage(baseMessage, nodeDiagnostics), nodeDiagnostics, null); |
| 209 | + } |
| 210 | + |
| 211 | + private DriverTimeoutException( |
| 212 | + String message, @Nullable NodeDiagnostics nodeDiagnostics, ExecutionInfo executionInfo) { |
30 | 213 | super(message, executionInfo, null, true); |
| 214 | + this.nodeDiagnostics = nodeDiagnostics; |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Returns the per-node diagnostic snapshot captured at timeout time, or {@code null} if not |
| 219 | + * available. |
| 220 | + */ |
| 221 | + @Nullable |
| 222 | + public NodeDiagnostics getNodeDiagnostics() { |
| 223 | + return nodeDiagnostics; |
31 | 224 | } |
32 | 225 |
|
33 | 226 | @NonNull |
34 | 227 | @Override |
35 | 228 | public DriverException copy() { |
36 | | - return new DriverTimeoutException(getMessage(), getExecutionInfo()); |
| 229 | + return new DriverTimeoutException(getMessage(), nodeDiagnostics, getExecutionInfo()); |
| 230 | + } |
| 231 | + |
| 232 | + private static String buildMessage( |
| 233 | + @NonNull String baseMessage, @Nullable NodeDiagnostics nodeDiagnostics) { |
| 234 | + if (nodeDiagnostics == null) { |
| 235 | + return baseMessage; |
| 236 | + } |
| 237 | + return baseMessage + " — node in flight: " + nodeDiagnostics; |
37 | 238 | } |
38 | 239 | } |
0 commit comments