GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][admin] PIP-478: off-load the admin's v4 credential and lend it … · apache/pulsar@71c4109

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pip
    • pip-478.md
  • pulsar-client-admin/src
    • main/java/org/apache/pulsar/client/admin/internal
      • BaseResource.java
      • PulsarAdminImpl.java
    • test/java/org/apache/pulsar/client/admin/internal
      • AdminAuthOffloadTest.java
  • pulsar-client/src
    • main/java/org/apache/pulsar/client/impl
      • HttpClient.java
      • auth/v5
        • V5AuthContexts.java
    • test/java/org/apache/pulsar/client/impl/auth/v5
      • BoundInitContextFallbackTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pip/pip-478.md‎

Copy file name to clipboardExpand all lines: pip/pip-478.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ The style is selected purely by which capability the plugin exposes — today on
10231023

10241024
**Who owns the future a budget is applied to (normative).** The framework bounds each stage of the exchange by timing out a defensive **copy** of the stage future, never the future the plugin or the transport returned. `CompletableFuture.orTimeout` returns — and completes — *the receiver*, so bounding an SPI-supplied future in place would permanently fail a plugin that legitimately memoizes one shared credential future (the natural shape of an OAuth2 access-token cache): one slow request would turn into a client-wide authentication outage. A corollary follows from bounding the copy: the framework does **not** cancel the underlying work on timeout — deliberately, since cancelling a shared plugin future is the same hazard — so cleanup on timeout belongs to the stage itself, and both HTTP transports self-bound against the `Duration` they are handed (request timeout on the AsyncHttpClient one; the JAX-RS one times out its own future and cancels the in-flight request).
10251025

1026-
**The budget covers the v4 branch too.** Both HTTP transports hand the driver an explicit budget — the lookup client's `lookupTimeoutMs` and the admin client's `requestTimeoutMs`, each falling back to 60 s — and the driver bounds each stage against the remaining budget, surfacing exhaustion as an authentication failure naming the round. On the HTTP-lookup client the v4 branch — taken when the plugin exposes no HTTP challenge capability — is not driven inline either: the whole v4 header composition is off-loaded to the blocking auth executor and composed with `thenCompose`, never joined, so a plugin whose `getAuthData()` refreshes a token cannot deadlock against the event loop that would serve that refresh. **The admin client's `BaseResource` is the exception**: it still composes v4 headers on the calling thread. That thread is the application's own, not an event loop, so it is a latency rather than a liveness problem — but it is the one place the "every synchronous v4 plugin call is off-loaded" rule does not yet hold, and it is tracked as a follow-up.
1026+
**The budget covers the v4 branch too.** Both HTTP transports hand the driver an explicit budget — the lookup client's `lookupTimeoutMs` and the admin client's `requestTimeoutMs`, each falling back to 60 s — and the driver bounds each stage against the remaining budget, surfacing exhaustion as an authentication failure naming the round. On the HTTP-lookup client the v4 branch — taken when the plugin exposes no HTTP challenge capability — is not driven inline either: the whole v4 header composition is off-loaded to the blocking auth executor and composed with `thenCompose`, never joined, so a plugin whose `getAuthData()` refreshes a token cannot deadlock against the event loop that would serve that refresh. The admin client's `BaseResource` composes its v4 headers the same way, on a bounded authentication executor owned by the `PulsarAdmin` that lent it (falling back to the shared pool for a resource built outside one), so the "every synchronous v4 plugin call is off-loaded" rule holds on every transport.
10271027

10281028
### Class-name compatibility and the v4 internal migration
10291029

Collapse file

‎pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/BaseResource.java‎

Copy file name to clipboardExpand all lines: pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/BaseResource.java
+72-3Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.concurrent.CompletableFuture;
4040
import java.util.concurrent.CompletionException;
4141
import java.util.concurrent.ExecutionException;
42+
import java.util.concurrent.Executor;
4243
import java.util.concurrent.TimeUnit;
4344
import java.util.concurrent.TimeoutException;
4445
import java.util.function.Function;
@@ -58,6 +59,7 @@
5859
import org.apache.pulsar.client.api.PulsarClientException;
5960
import org.apache.pulsar.client.impl.auth.v5.AsyncHttpAuthenticationProvider;
6061
import org.apache.pulsar.client.impl.auth.v5.HttpAuthenticationDriver;
62+
import org.apache.pulsar.client.impl.auth.v5.V5AuthContexts;
6163
import org.apache.pulsar.common.policies.data.ErrorData;
6264
import org.apache.pulsar.common.util.ObjectMapperFactory;
6365

@@ -69,12 +71,42 @@ public abstract class BaseResource {
6971

7072
protected final Authentication auth;
7173
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;
7280

7381
protected BaseResource(Authentication auth, long requestTimeoutMs) {
7482
this.auth = auth;
7583
this.requestTimeoutMs = requestTimeoutMs;
7684
}
7785

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+
78110
public Builder request(final WebTarget target) throws PulsarAdminException {
79111
try {
80112
return requestAsync(target).get();
@@ -123,7 +155,7 @@ public CompletableFuture<Builder> requestAsync(final WebTarget target) {
123155
* {@code GET} to the original URI each round, exactly what the v4 {@code authenticationStage(...)} does
124156
* today — and yields the validated role-token headers. Otherwise the deprecated v4
125157
* {@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.
127159
*/
128160
protected CompletableFuture<Map<String, String>> computeAuthHeaders(URI uri) {
129161
try {
@@ -137,13 +169,50 @@ protected CompletableFuture<Map<String, String>> computeAuthHeaders(URI uri) {
137169
.thenApply(headers -> (headers == null || headers.isEmpty()) ? null : headers.asMap());
138170
}
139171
}
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 {
140203
AuthenticationDataProvider authData = auth.getAuthData(uri.getHost());
141204
if (!authData.hasDataForHttp()) {
142205
return CompletableFuture.completedFuture(null);
143206
}
144207
CompletableFuture<Map<String, String>> stage = new CompletableFuture<>();
145208
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 -> {
147216
try {
148217
Set<Entry<String, String>> headers = auth.newRequestHeader(uri.toString(), authData, respHeaders);
149218
if (headers == null) {
@@ -155,7 +224,7 @@ protected CompletableFuture<Map<String, String>> computeAuthHeaders(URI uri) {
155224
} catch (Exception e) {
156225
throw new CompletionException(e);
157226
}
158-
});
227+
}, blockingExecutor);
159228
} catch (Throwable t) {
160229
return CompletableFuture.failedFuture(t);
161230
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.