|
39 | 39 | import java.util.concurrent.CompletableFuture; |
40 | 40 | import java.util.concurrent.CompletionException; |
41 | 41 | import java.util.concurrent.ExecutionException; |
| 42 | +import java.util.concurrent.Executor; |
42 | 43 | import java.util.concurrent.TimeUnit; |
43 | 44 | import java.util.concurrent.TimeoutException; |
44 | 45 | import java.util.function.Function; |
|
58 | 59 | import org.apache.pulsar.client.api.PulsarClientException; |
59 | 60 | import org.apache.pulsar.client.impl.auth.v5.AsyncHttpAuthenticationProvider; |
60 | 61 | import org.apache.pulsar.client.impl.auth.v5.HttpAuthenticationDriver; |
| 62 | +import org.apache.pulsar.client.impl.auth.v5.V5AuthContexts; |
61 | 63 | import org.apache.pulsar.common.policies.data.ErrorData; |
62 | 64 | import org.apache.pulsar.common.util.ObjectMapperFactory; |
63 | 65 |
|
@@ -69,12 +71,42 @@ public abstract class BaseResource { |
69 | 71 |
|
70 | 72 | protected final Authentication auth; |
71 | 73 | protected final long requestTimeoutMs; |
| 74 | + // PIP-478: the owning admin's bounded blocking executor, where the deprecated v4 credential composition |
| 75 | + // runs. Lent by PulsarAdminImpl right after construction rather than threaded through the constructors |
| 76 | + // of all 23 resource classes, and never mutated afterwards. Null for a resource built outside a |
| 77 | + // PulsarAdmin (tests, embedders), which has no admin pool to borrow and falls back to the framework's |
| 78 | + // shared one — still off the caller thread. |
| 79 | + private volatile Executor blockingAuthExecutor; |
72 | 80 |
|
73 | 81 | protected BaseResource(Authentication auth, long requestTimeoutMs) { |
74 | 82 | this.auth = auth; |
75 | 83 | this.requestTimeoutMs = requestTimeoutMs; |
76 | 84 | } |
77 | 85 |
|
| 86 | + /** |
| 87 | + * Lend this resource the owning admin's bounded blocking authentication executor (PIP-478), so a |
| 88 | + * stalled identity provider reached through this admin's plugin cannot occupy the process-wide shared |
| 89 | + * pool that every other client in the JVM depends on. Called once by {@link PulsarAdminImpl}, before |
| 90 | + * the resource is published. |
| 91 | + * |
| 92 | + * @param blockingAuthExecutor the owning admin's blocking authentication executor |
| 93 | + */ |
| 94 | + void setBlockingAuthExecutor(Executor blockingAuthExecutor) { |
| 95 | + this.blockingAuthExecutor = blockingAuthExecutor; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * The executor lent by {@link PulsarAdminImpl}, or {@code null} when none was (VisibleForTesting). The |
| 100 | + * lending is opt-in per construction site, and a resource that misses it still works — it just falls |
| 101 | + * back to the shared pool — so the only thing that can catch a resource added without it is a test that |
| 102 | + * reads this. |
| 103 | + * |
| 104 | + * @return the lent blocking authentication executor, or {@code null} |
| 105 | + */ |
| 106 | + Executor blockingAuthExecutorForTest() { |
| 107 | + return blockingAuthExecutor; |
| 108 | + } |
| 109 | + |
78 | 110 | public Builder request(final WebTarget target) throws PulsarAdminException { |
79 | 111 | try { |
80 | 112 | return requestAsync(target).get(); |
@@ -123,7 +155,7 @@ public CompletableFuture<Builder> requestAsync(final WebTarget target) { |
123 | 155 | * {@code GET} to the original URI each round, exactly what the v4 {@code authenticationStage(...)} does |
124 | 156 | * today — and yields the validated role-token headers. Otherwise the deprecated v4 |
125 | 157 | * {@code authenticationStage(...)} / {@code newRequestHeader(...)} hooks run verbatim, preserving |
126 | | - * behaviour for third-party plugins and single-pass built-ins. |
| 158 | + * behaviour for third-party plugins and single-pass built-ins — but off the calling thread. |
127 | 159 | */ |
128 | 160 | protected CompletableFuture<Map<String, String>> computeAuthHeaders(URI uri) { |
129 | 161 | try { |
@@ -137,13 +169,50 @@ protected CompletableFuture<Map<String, String>> computeAuthHeaders(URI uri) { |
137 | 169 | .thenApply(headers -> (headers == null || headers.isEmpty()) ? null : headers.asMap()); |
138 | 170 | } |
139 | 171 | } |
| 172 | + // The deprecated v4 hooks may block — an OAuth2 or Athenz shim's getAuthData() refreshes its |
| 173 | + // credential with a synchronous HTTP exchange — and this method runs on whatever thread issued the |
| 174 | + // admin call, which for a broker calling its own admin client is a request-handling thread. |
| 175 | + // HttpClient.computeAuthHeaders off-loads the identical composition for the lookup path and names |
| 176 | + // the self-deadlock it avoids; this was the last caller-thread credential resolution left, and the |
| 177 | + // one place PIP-478's "every synchronous v4 plugin call is off-loaded" was still an overstatement. |
| 178 | + // |
| 179 | + // It runs on the owning admin's own bounded pool, the same one a services-aware plugin is lent, so |
| 180 | + // the work that can actually stall — a KDC or IdP round trip inside the plugin — stays isolated |
| 181 | + // per admin: were this the framework's process-wide shared pool, one admin's stalled provider |
| 182 | + // would throttle authentication for every other client in the JVM. A resource built outside a |
| 183 | + // PulsarAdmin has no pool to borrow and falls back to that shared one, which is still off the |
| 184 | + // caller thread. |
| 185 | + Executor executor = V5AuthContexts.blockingExecutorOrShared(blockingAuthExecutor); |
| 186 | + return V5AuthContexts.supplyBlocking(executor, () -> v4AuthHeaders(uri, executor)) |
| 187 | + .thenCompose(headers -> headers); |
| 188 | + } catch (Throwable t) { |
| 189 | + return CompletableFuture.failedFuture(t); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * The deprecated v4 HTTP authentication composition, run on a blocking executor by |
| 195 | + * {@link #computeAuthHeaders(URI)}. |
| 196 | + * |
| 197 | + * @param uri the request URI |
| 198 | + * @param blockingExecutor the executor this composition runs on, for the continuation below |
| 199 | + * @return a future of the headers, or of {@code null} when the plugin contributes none |
| 200 | + */ |
| 201 | + private CompletableFuture<Map<String, String>> v4AuthHeaders(URI uri, Executor blockingExecutor) { |
| 202 | + try { |
140 | 203 | AuthenticationDataProvider authData = auth.getAuthData(uri.getHost()); |
141 | 204 | if (!authData.hasDataForHttp()) { |
142 | 205 | return CompletableFuture.completedFuture(null); |
143 | 206 | } |
144 | 207 | CompletableFuture<Map<String, String>> stage = new CompletableFuture<>(); |
145 | 208 | auth.authenticationStage(uri.toString(), authData, null, stage); |
146 | | - return stage.thenApply(respHeaders -> { |
| 209 | + // thenApplyAsync, not thenApply: newRequestHeader is the second synchronous v4 hook, and a plugin |
| 210 | + // that completes the stage asynchronously — the multi-round challenge shape — completes it from |
| 211 | + // its own HTTP callback thread. A plain continuation would run that hook there, which off-loading |
| 212 | + // the resolution alone would not cover. In-tree that path now belongs to the v5 driver above, so |
| 213 | + // this is for third-party v4 plugins; where the stage completes inline (the single-pass default) |
| 214 | + // the hop is to a sibling task on this same executor. |
| 215 | + return stage.thenApplyAsync(respHeaders -> { |
147 | 216 | try { |
148 | 217 | Set<Entry<String, String>> headers = auth.newRequestHeader(uri.toString(), authData, respHeaders); |
149 | 218 | if (headers == null) { |
@@ -155,7 +224,7 @@ protected CompletableFuture<Map<String, String>> computeAuthHeaders(URI uri) { |
155 | 224 | } catch (Exception e) { |
156 | 225 | throw new CompletionException(e); |
157 | 226 | } |
158 | | - }); |
| 227 | + }, blockingExecutor); |
159 | 228 | } catch (Throwable t) { |
160 | 229 | return CompletableFuture.failedFuture(t); |
161 | 230 | } |
|