|
3 | 3 |
|
4 | 4 | package com.azure.messaging.servicebus; |
5 | 5 |
|
| 6 | +import com.azure.core.amqp.AmqpClientOptions; |
| 7 | +import com.azure.core.amqp.AmqpRetryOptions; |
6 | 8 | import com.azure.core.amqp.exception.AmqpErrorCondition; |
7 | 9 | import com.azure.core.amqp.exception.AmqpException; |
8 | 10 | import com.azure.core.amqp.implementation.StringUtil; |
| 11 | +import com.azure.core.amqp.implementation.handler.ReceiveLinkHandler2; |
9 | 12 | import com.azure.core.util.logging.ClientLogger; |
10 | 13 | import com.azure.messaging.servicebus.implementation.MessagingEntityType; |
11 | 14 | import com.azure.messaging.servicebus.implementation.ServiceBusManagementNode; |
|
26 | 29 |
|
27 | 30 | import static com.azure.core.amqp.implementation.ClientConstants.ENTITY_PATH_KEY; |
28 | 31 |
|
| 32 | +/** |
| 33 | + * A type to acquire a session from a session enabled Service Bus entity. If the broker cannot find a session within |
| 34 | + * the timeout, it returns a timeout error. The acquirer retries on timeout unless disabled via {@code timeoutRetryDisabled}. |
| 35 | + * <p> |
| 36 | + * The {@code timeoutRetryDisabled} is true when the session acquirer is used for synchronous {@link ServiceBusSessionReceiverClient}. |
| 37 | + * This allows the synchronous 'acceptNextSession()' API to propagate the broker timeout error if no session is available. |
| 38 | + * The 'acceptNextSession()' has a client-side timeout that is set slightly longer than the broker's timeout, ensuring |
| 39 | + * the broker's timeout usually triggers first (the client-side timeout still helps in case of unexpected hanging). |
| 40 | + * For ServiceBusSessionReceiverClient, if the library retries session-acquire on broker timeout, the client-side sync |
| 41 | + * timeout might expire while waiting. When client-side timeout expires like this, library cannot cancel the outstanding |
| 42 | + * acquire request to the broker, which means, the broker may still lock a session for an acquire request that nobody |
| 43 | + * is waiting on, resulting that session to be unavailable for any other 'acceptNextSession()' until initial broker |
| 44 | + * lock expires. Hence, library propagate the broker timeout error in ServiceBusSessionReceiverClient case. |
| 45 | + * </p> |
| 46 | + * <p> |
| 47 | + * For session enabled {@link ServiceBusProcessorClient} and {@link ServiceBusSessionReceiverAsyncClient}, |
| 48 | + * the {@code timeoutRetryDisabled} is false, hence session acquirer retries on broker timeout. |
| 49 | + * </p> |
| 50 | + */ |
29 | 51 | final class ServiceBusSessionAcquirer { |
30 | 52 | private static final String TRACKING_ID_KEY = "trackingId"; |
31 | 53 | private final ClientLogger logger; |
32 | 54 | private final String identifier; |
33 | 55 | private final String entityPath; |
34 | 56 | private final MessagingEntityType entityType; |
35 | | - private final Duration sessionActiveTimeout; |
| 57 | + private final Duration tryTimeout; |
| 58 | + private final boolean timeoutRetryDisabled; |
36 | 59 | private final ServiceBusReceiveMode receiveMode; |
37 | 60 | private final ConnectionCacheWrapper connectionCacheWrapper; |
38 | 61 | private final Mono<ServiceBusManagementNode> sessionManagement; |
39 | 62 |
|
| 63 | + /** |
| 64 | + * Creates ServiceBusSessionAcquirer to acquire session from a session enabled entity. |
| 65 | + * |
| 66 | + * @param logger the logger to use. |
| 67 | + * @param identifier the client identifier, currently callsites uses {@link AmqpClientOptions#getIdentifier()}. |
| 68 | + * @param entityPath path to the session enabled entity. |
| 69 | + * @param entityType the entity type (e.g., queue, topic subscription) |
| 70 | + * @param receiveMode the mode of receiving messages from the acquired session. |
| 71 | + * @param tryTimeout the try timeout, currently callsites uses {@link AmqpRetryOptions#getTryTimeout()}}. |
| 72 | + * @param timeoutRetryDisabled if session acquire retry should be disabled when broker timeout on no session. |
| 73 | + * @param connectionCacheWrapper the connection cache. |
| 74 | + */ |
40 | 75 | ServiceBusSessionAcquirer(ClientLogger logger, String identifier, String entityPath, MessagingEntityType entityType, |
41 | | - ServiceBusReceiveMode receiveMode, Duration sessionActiveTimeout, |
| 76 | + ServiceBusReceiveMode receiveMode, Duration tryTimeout, boolean timeoutRetryDisabled, |
42 | 77 | ConnectionCacheWrapper connectionCacheWrapper) { |
43 | 78 | assert connectionCacheWrapper.isV2(); |
44 | 79 | this.logger = logger; |
45 | 80 | this.identifier = identifier; |
46 | 81 | this.entityPath = entityPath; |
47 | 82 | this.entityType = entityType; |
48 | | - this.sessionActiveTimeout = sessionActiveTimeout; |
| 83 | + this.tryTimeout = tryTimeout; |
| 84 | + this.timeoutRetryDisabled = timeoutRetryDisabled; |
49 | 85 | this.receiveMode = receiveMode; |
50 | 86 | this.connectionCacheWrapper = connectionCacheWrapper; |
51 | 87 | this.sessionManagement = connectionCacheWrapper.getConnection() |
@@ -78,53 +114,135 @@ Mono<Session> acquire(String sessionId) { |
78 | 114 | return acquireIntern(sessionId); |
79 | 115 | } |
80 | 116 |
|
| 117 | + /** |
| 118 | + * Tries to acquire a session from the broker by opening an AMQP receive link. When the acquire attempt timeout then |
| 119 | + * the api will retry if {@code timeoutRetryDisabled} is set to {@code false}. In case an error needs to be propagated, |
| 120 | + * api publishes the error using bounded-elastic Thread. |
| 121 | + * |
| 122 | + * @param sessionId the unique id of the specific session to acquire, a value {@code null} means acquire any free session. |
| 123 | + * @return A Mono that completes with the acquired session, the Mono can emit {@link AmqpException} if the acquirer |
| 124 | + * is already disposed or {@link TimeoutException} if session acquire timeouts and {@code timeoutRetryDisabled} set to true. |
| 125 | + */ |
81 | 126 | private Mono<Session> acquireIntern(String sessionId) { |
82 | | - return Mono |
83 | | - .defer(() -> createSessionReceiveLink(sessionId).flatMap(sessionLink -> sessionLink.getSessionProperties() // Await for sessionLink to "ACTIVE" then reads its properties |
84 | | - .flatMap(sessionProperties -> { |
85 | | - return Mono.just(new Session(sessionLink, sessionProperties, sessionManagement)); |
86 | | - }))) |
87 | | - .timeout(sessionActiveTimeout) |
88 | | - .retryWhen(Retry.from(retrySignals -> retrySignals.flatMap(signal -> { |
89 | | - final Throwable failure = signal.failure(); |
90 | | - logger.atInfo() |
91 | | - .addKeyValue(ENTITY_PATH_KEY, entityPath) |
92 | | - .addKeyValue("attempt", signal.totalRetriesInARow()) |
93 | | - .log(sessionId == null |
94 | | - ? "Error occurred while getting unnamed session." |
95 | | - : "Error occurred while getting session " + sessionId, failure); |
96 | | - |
97 | | - if (failure instanceof TimeoutException) { |
98 | | - return Mono.delay(Duration.ZERO); |
99 | | - } else if (failure instanceof AmqpException |
100 | | - && ((AmqpException) failure).getErrorCondition() == AmqpErrorCondition.TIMEOUT_ERROR) { |
101 | | - // The link closed remotely with 'Detach {errorCondition:com.microsoft:timeout}' frame because |
102 | | - // the broker waited for N seconds (60 sec hard limit today) but there was no free or new session. |
103 | | - // |
104 | | - // Given N seconds elapsed since the last session acquire attempt, request for a session on |
105 | | - // the 'parallel' Scheduler and free the QPid thread for other IO. |
106 | | - // |
107 | | - return Mono.delay(Duration.ZERO); |
108 | | - } else { |
109 | | - final long id = System.nanoTime(); |
110 | | - logger.atInfo().addKeyValue(TRACKING_ID_KEY, id).log("Unable to acquire a session.", failure); |
111 | | - // The link-endpoint-state publisher will emit error on the QPid Thread, that is a non-blocking Thread, |
112 | | - // publish the error on the (blockable) bounded-elastic thread to free QPid thread and to allow |
113 | | - // any blocking operation that downstream may do. |
114 | | - return Mono.<Long>error(failure) |
115 | | - .publishOn(Schedulers.boundedElastic()) |
116 | | - .doOnError(e -> logger.atInfo() |
117 | | - .addKeyValue(TRACKING_ID_KEY, id) |
118 | | - .log("Emitting the error signal received for session acquire attempt.", e)); |
| 127 | + if (timeoutRetryDisabled) { |
| 128 | + return acquireSession(sessionId).onErrorResume(t -> { |
| 129 | + if (isBrokerTimeoutError(t)) { |
| 130 | + // map the broker timeout to application-friendly TimeoutException. |
| 131 | + final Throwable e = new TimeoutException("com.microsoft:timeout").initCause(t); |
| 132 | + return publishError(sessionId, e, false); |
119 | 133 | } |
120 | | - }))); |
| 134 | + return publishError(sessionId, t, true); |
| 135 | + }); |
| 136 | + } else { |
| 137 | + return acquireSession(sessionId).timeout(tryTimeout) |
| 138 | + .retryWhen(Retry.from(signals -> signals.flatMap(signal -> { |
| 139 | + final Throwable t = signal.failure(); |
| 140 | + if (isTimeoutError(t)) { |
| 141 | + logger.atVerbose() |
| 142 | + .addKeyValue(ENTITY_PATH_KEY, entityPath) |
| 143 | + .addKeyValue("attempt", signal.totalRetriesInARow()) |
| 144 | + .log("Timeout while acquiring session '{}'.", sessionName(sessionId), t); |
| 145 | + // retry session acquire using Schedulers.parallel() and free the QPid thread. |
| 146 | + return Mono.delay(Duration.ZERO); |
| 147 | + } |
| 148 | + return publishError(sessionId, t, true); |
| 149 | + }))); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Tries to acquire a session from the broker by opening an AMQP receive link. |
| 155 | + * |
| 156 | + * @param sessionId the unique id of the session to acquire, a value {@code null} means acquire any free session. |
| 157 | + * |
| 158 | + * @return the acquired session. |
| 159 | + */ |
| 160 | + private Mono<Session> acquireSession(String sessionId) { |
| 161 | + return Mono.defer(() -> { |
| 162 | + final Mono<ServiceBusReceiveLink> createLink = connectionCacheWrapper.getConnection() |
| 163 | + .flatMap(connection -> connection.createReceiveLink(linkName(sessionId), entityPath, receiveMode, null, |
| 164 | + entityType, identifier, sessionId)); |
| 165 | + return createLink.flatMap(link -> { |
| 166 | + // ServiceBusReceiveLink::getSessionProperties() await for link to "ACTIVE" then reads its properties. |
| 167 | + return link.getSessionProperties() |
| 168 | + .flatMap(sessionProperties -> Mono.just(new Session(link, sessionProperties, sessionManagement))); |
| 169 | + }); |
| 170 | + }); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Publish the session acquire error using a bounded-elastic Thread. |
| 175 | + * <p> |
| 176 | + * The link-endpoint-state publisher ({@link ReceiveLinkHandler2#getEndpointStates()}) will emit error on the QPid |
| 177 | + * Thread, which is a non-block-able Thread. Publishing the error on the (block-able) bounded-elastic Thread will free |
| 178 | + * QPid Thread and to allow any blocking operation that downstream may do. If library do not publish in bounded-elastic |
| 179 | + * Thread and downstream happens to make a blocking call on non-block-able QPid Thread then reactor-core will error |
| 180 | + * - 'IllegalStateException(..*operation* are blocking, which is not supported in thread ...'). |
| 181 | + * </p> |
| 182 | + * |
| 183 | + * @param sessionId the session id. |
| 184 | + * @param t the error to publish. |
| 185 | + * @param logAtInfo indicates if session acquire error should be logged at "info" level from the current thread, most of |
| 186 | + * the time, the broker timeout is the reason for session acquisition failure, in case, the acquire fails |
| 187 | + * due to any other reasons, that least expected error is logged in the "info" level. |
| 188 | + * @return a Mono that publishes the given error using a bounded-elastic Thread. |
| 189 | + * @param <T> the type |
| 190 | + */ |
| 191 | + private <T> Mono<T> publishError(String sessionId, Throwable t, boolean logAtInfo) { |
| 192 | + final long id = System.nanoTime(); |
| 193 | + if (logAtInfo) { |
| 194 | + logger.atInfo() |
| 195 | + .addKeyValue(ENTITY_PATH_KEY, entityPath) |
| 196 | + .addKeyValue(TRACKING_ID_KEY, id) |
| 197 | + .log("Unable to acquire session '{}'.", sessionName(sessionId), t); |
| 198 | + } |
| 199 | + return Mono.<T>error(t) |
| 200 | + .publishOn(Schedulers.boundedElastic()) |
| 201 | + .doOnError(ignored -> logger.atVerbose() |
| 202 | + .addKeyValue(TRACKING_ID_KEY, id) |
| 203 | + .log("Emitting session acquire error" + (logAtInfo ? "." : ": " + t.getMessage()))); |
121 | 204 | } |
122 | 205 |
|
123 | | - private Mono<ServiceBusReceiveLink> createSessionReceiveLink(String sessionId) { |
124 | | - final String linkName = (sessionId != null) ? sessionId : StringUtil.getRandomString("session-"); |
125 | | - return connectionCacheWrapper.getConnection() |
126 | | - .flatMap(connection -> connection.createReceiveLink(linkName, entityPath, receiveMode, null, entityType, |
127 | | - identifier, sessionId)); |
| 206 | + /** |
| 207 | + * Check if the given error is a remote link detach with '{errorCondition:com.microsoft:timeout}' indicating the broker |
| 208 | + * waited for N seconds (60 sec default) but there was no free or new session. |
| 209 | + * |
| 210 | + * @param t the error to test. |
| 211 | + * @return {@code true} if the error represents broker timeout. |
| 212 | + */ |
| 213 | + private static boolean isBrokerTimeoutError(Throwable t) { |
| 214 | + return t instanceof AmqpException |
| 215 | + && ((AmqpException) t).getErrorCondition() == AmqpErrorCondition.TIMEOUT_ERROR; |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Checks if the given error is a timeout error. |
| 220 | + * |
| 221 | + * @param t the error to test. |
| 222 | + * @return {@code true} if the error represents timeout. |
| 223 | + */ |
| 224 | + private static boolean isTimeoutError(Throwable t) { |
| 225 | + return t instanceof TimeoutException || isBrokerTimeoutError(t); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Obtain the name for the AMQP link that channels messages from a session. |
| 230 | + * |
| 231 | + * @param sessionId the session to channel messages from. |
| 232 | + * @return name for the AMQP link. |
| 233 | + */ |
| 234 | + private static String linkName(String sessionId) { |
| 235 | + return (sessionId != null) ? sessionId : StringUtil.getRandomString("session-"); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Get the session name for simple local logging purpose. |
| 240 | + * |
| 241 | + * @param sessionId the unique id of the session or {@code null}, if session id is unknown. |
| 242 | + * @return the session name. |
| 243 | + */ |
| 244 | + private static String sessionName(String sessionId) { |
| 245 | + return sessionId == null ? "unnamed" : sessionId; |
128 | 246 | } |
129 | 247 |
|
130 | 248 | /** |
|
0 commit comments