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