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: review fixes for the admin authentication executor · apache/pulsar@5cf761e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • 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
Expand file treeCollapse file tree
Open diff view settings
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
+37-9Lines changed: 37 additions & 9 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;
@@ -70,12 +71,30 @@ public abstract class BaseResource {
7071

7172
protected final Authentication auth;
7273
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;
7380

7481
protected BaseResource(Authentication auth, long requestTimeoutMs) {
7582
this.auth = auth;
7683
this.requestTimeoutMs = requestTimeoutMs;
7784
}
7885

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+
7998
public Builder request(final WebTarget target) throws PulsarAdminException {
8099
try {
81100
return requestAsync(target).get();
@@ -145,13 +164,15 @@ protected CompletableFuture<Map<String, String>> computeAuthHeaders(URI uri) {
145164
// the self-deadlock it avoids; this was the last caller-thread credential resolution left, and the
146165
// one place PIP-478's "every synchronous v4 plugin call is off-loaded" was still an overstatement.
147166
//
148-
// A null executor sends the work to the framework's shared blocking pool rather than running it
149-
// inline. That is deliberate: BaseResource is constructed by 30 subclasses that would each have to
150-
// thread an executor through, and the shared pool is exactly what PIP-478 provides for a component
151-
// with none to lend. The admin's own bounded pool is still what a services-aware plugin gets, so
167+
// It runs on the owning admin's own bounded pool, the same one a services-aware plugin is lent, so
152168
// the work that can actually stall — a KDC or IdP round trip inside the plugin — stays isolated
153-
// per admin.
154-
return V5AuthContexts.supplyBlocking(null, () -> v4AuthHeaders(uri)).thenCompose(headers -> headers);
169+
// per admin: were this the framework's process-wide shared pool, one admin's stalled provider
170+
// would throttle authentication for every other client in the JVM. A resource built outside a
171+
// PulsarAdmin has no pool to borrow and falls back to that shared one, which is still off the
172+
// caller thread.
173+
Executor executor = V5AuthContexts.blockingExecutorOrShared(blockingAuthExecutor);
174+
return V5AuthContexts.supplyBlocking(executor, () -> v4AuthHeaders(uri, executor))
175+
.thenCompose(headers -> headers);
155176
} catch (Throwable t) {
156177
return CompletableFuture.failedFuture(t);
157178
}
@@ -162,17 +183,24 @@ protected CompletableFuture<Map<String, String>> computeAuthHeaders(URI uri) {
162183
* {@link #computeAuthHeaders(URI)}.
163184
*
164185
* @param uri the request URI
186+
* @param blockingExecutor the executor this composition runs on, for the continuation below
165187
* @return a future of the headers, or of {@code null} when the plugin contributes none
166188
*/
167-
private CompletableFuture<Map<String, String>> v4AuthHeaders(URI uri) {
189+
private CompletableFuture<Map<String, String>> v4AuthHeaders(URI uri, Executor blockingExecutor) {
168190
try {
169191
AuthenticationDataProvider authData = auth.getAuthData(uri.getHost());
170192
if (!authData.hasDataForHttp()) {
171193
return CompletableFuture.completedFuture(null);
172194
}
173195
CompletableFuture<Map<String, String>> stage = new CompletableFuture<>();
174196
auth.authenticationStage(uri.toString(), authData, null, stage);
175-
return stage.thenApply(respHeaders -> {
197+
// thenApplyAsync, not thenApply: newRequestHeader is the second synchronous v4 hook, and a plugin
198+
// that completes the stage asynchronously — the multi-round challenge shape — completes it from
199+
// its own HTTP callback thread. A plain continuation would run that hook there, which off-loading
200+
// the resolution alone would not cover. In-tree that path now belongs to the v5 driver above, so
201+
// this is for third-party v4 plugins; where the stage completes inline (the single-pass default)
202+
// the hop is to a sibling task on this same executor.
203+
return stage.thenApplyAsync(respHeaders -> {
176204
try {
177205
Set<Entry<String, String>> headers = auth.newRequestHeader(uri.toString(), authData, respHeaders);
178206
if (headers == null) {
@@ -184,7 +212,7 @@ private CompletableFuture<Map<String, String>> v4AuthHeaders(URI uri) {
184212
} catch (Exception e) {
185213
throw new CompletionException(e);
186214
}
187-
});
215+
}, blockingExecutor);
188216
} catch (Throwable t) {
189217
return CompletableFuture.failedFuture(t);
190218
}
Collapse file

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

Copy file name to clipboardExpand all lines: pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/PulsarAdminImpl.java
+70-55Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.time.Clock;
3030
import java.util.LinkedHashMap;
3131
import java.util.Map;
32-
import java.util.concurrent.Executor;
3332
import java.util.concurrent.LinkedBlockingQueue;
3433
import java.util.concurrent.ThreadPoolExecutor;
3534
import java.util.concurrent.TimeUnit;
@@ -132,9 +131,11 @@ public class PulsarAdminImpl implements PulsarAdmin {
132131
// plugin does not implement ClientAuthenticationServicesAware. Closed with this admin.
133132
private FrameworkHttpClientFactory authHttpClientFactory;
134133
// PIP-478: the admin's own bounded executor for potentially-blocking authentication work — the v4
135-
// credential composition on every request, and whatever a services-aware plugin off-loads. Created
136-
// lazily so an admin whose plugin never needs one pays nothing, and shut down with this admin.
137-
private volatile ThreadPoolExecutor blockingAuthExecutor;
134+
// credential composition on every request, and whatever a services-aware plugin off-loads. Shut down
135+
// with this admin. Constructing it costs one object: a ThreadPoolExecutor starts no threads until a
136+
// task arrives, and with core threads timing out an admin that issues no authenticated request keeps
137+
// none.
138+
private final ThreadPoolExecutor blockingAuthExecutor;
138139
@Getter
139140
private AsyncHttpConnectorProvider asyncConnectorProvider;
140141

@@ -166,6 +167,8 @@ public PulsarAdminImpl(String serviceUrl, ClientConfigurationData clientConfigDa
166167
asyncConnectorProvider = new AsyncHttpConnectorProvider(clientConfigData,
167168
clientConfigData.getAutoCertRefreshSeconds(), acceptGzipCompression);
168169

170+
this.blockingAuthExecutor = newBlockingAuthExecutor();
171+
169172
boolean constructed = false;
170173
try {
171174
bindAuthenticationServices(clientConfigData);
@@ -204,29 +207,29 @@ public PulsarAdminImpl(String serviceUrl, ClientConfigurationData clientConfigDa
204207
clientConfigData.getAutoCertRefreshSeconds(), sharedResources);
205208

206209
long requestTimeoutMs = clientConfigData.getRequestTimeoutMs();
207-
this.clusters = new ClustersImpl(root, auth, requestTimeoutMs);
208-
this.brokers = new BrokersImpl(root, auth, requestTimeoutMs);
209-
this.brokerStats = new BrokerStatsImpl(root, auth, requestTimeoutMs);
210-
this.proxyStats = new ProxyStatsImpl(root, auth, requestTimeoutMs);
211-
this.tenants = new TenantsImpl(root, auth, requestTimeoutMs);
212-
this.resourcegroups = new ResourceGroupsImpl(root, auth, requestTimeoutMs);
213-
this.namespaces = new NamespacesImpl(root, auth, requestTimeoutMs);
214-
this.topics = new TopicsImpl(root, auth, requestTimeoutMs);
215-
this.localTopicPolicies = new TopicPoliciesImpl(root, auth, requestTimeoutMs, false);
216-
this.globalTopicPolicies = new TopicPoliciesImpl(root, auth, requestTimeoutMs, true);
217-
this.nonPersistentTopics = new NonPersistentTopicsImpl(root, auth, requestTimeoutMs);
218-
this.resourceQuotas = new ResourceQuotasImpl(root, auth, requestTimeoutMs);
219-
this.lookups = new LookupImpl(root, auth, useTls, requestTimeoutMs, topics);
220-
this.functions = new FunctionsImpl(root, auth, asyncHttpConnector, requestTimeoutMs);
221-
this.sources = new SourcesImpl(root, auth, asyncHttpConnector, requestTimeoutMs);
222-
this.sinks = new SinksImpl(root, auth, asyncHttpConnector, requestTimeoutMs);
223-
this.worker = new WorkerImpl(root, auth, requestTimeoutMs);
224-
this.schemas = new SchemasImpl(root, auth, requestTimeoutMs);
225-
this.bookies = new BookiesImpl(root, auth, requestTimeoutMs);
226-
this.packages = new PackagesImpl(root, auth, asyncHttpConnector, requestTimeoutMs);
227-
this.transactions = new TransactionsImpl(root, auth, requestTimeoutMs);
228-
this.metadataMigration = new MetadataMigrationImpl(root, auth, requestTimeoutMs);
229-
this.scalableTopics = new ScalableTopicsImpl(root, auth, requestTimeoutMs);
210+
this.clusters = lendAuthExecutor(new ClustersImpl(root, auth, requestTimeoutMs));
211+
this.brokers = lendAuthExecutor(new BrokersImpl(root, auth, requestTimeoutMs));
212+
this.brokerStats = lendAuthExecutor(new BrokerStatsImpl(root, auth, requestTimeoutMs));
213+
this.proxyStats = lendAuthExecutor(new ProxyStatsImpl(root, auth, requestTimeoutMs));
214+
this.tenants = lendAuthExecutor(new TenantsImpl(root, auth, requestTimeoutMs));
215+
this.resourcegroups = lendAuthExecutor(new ResourceGroupsImpl(root, auth, requestTimeoutMs));
216+
this.namespaces = lendAuthExecutor(new NamespacesImpl(root, auth, requestTimeoutMs));
217+
this.topics = lendAuthExecutor(new TopicsImpl(root, auth, requestTimeoutMs));
218+
this.localTopicPolicies = lendAuthExecutor(new TopicPoliciesImpl(root, auth, requestTimeoutMs, false));
219+
this.globalTopicPolicies = lendAuthExecutor(new TopicPoliciesImpl(root, auth, requestTimeoutMs, true));
220+
this.nonPersistentTopics = lendAuthExecutor(new NonPersistentTopicsImpl(root, auth, requestTimeoutMs));
221+
this.resourceQuotas = lendAuthExecutor(new ResourceQuotasImpl(root, auth, requestTimeoutMs));
222+
this.lookups = lendAuthExecutor(new LookupImpl(root, auth, useTls, requestTimeoutMs, topics));
223+
this.functions = lendAuthExecutor(new FunctionsImpl(root, auth, asyncHttpConnector, requestTimeoutMs));
224+
this.sources = lendAuthExecutor(new SourcesImpl(root, auth, asyncHttpConnector, requestTimeoutMs));
225+
this.sinks = lendAuthExecutor(new SinksImpl(root, auth, asyncHttpConnector, requestTimeoutMs));
226+
this.worker = lendAuthExecutor(new WorkerImpl(root, auth, requestTimeoutMs));
227+
this.schemas = lendAuthExecutor(new SchemasImpl(root, auth, requestTimeoutMs));
228+
this.bookies = lendAuthExecutor(new BookiesImpl(root, auth, requestTimeoutMs));
229+
this.packages = lendAuthExecutor(new PackagesImpl(root, auth, asyncHttpConnector, requestTimeoutMs));
230+
this.transactions = lendAuthExecutor(new TransactionsImpl(root, auth, requestTimeoutMs));
231+
this.metadataMigration = lendAuthExecutor(new MetadataMigrationImpl(root, auth, requestTimeoutMs));
232+
this.scalableTopics = lendAuthExecutor(new ScalableTopicsImpl(root, auth, requestTimeoutMs));
230233

231234
if (originalCtxLoader != null) {
232235
Thread.currentThread().setContextClassLoader(originalCtxLoader);
@@ -240,6 +243,8 @@ public PulsarAdminImpl(String serviceUrl, ClientConfigurationData clientConfigDa
240243
// trustCertsFilePath being the common case) leaks one of each. The connectors release only
241244
// their own borrowed handles, which is exactly why this has to happen here.
242245
asyncConnectorProvider.close();
246+
// A services-aware plugin's start() may already have run work here before the failure.
247+
blockingAuthExecutor.shutdown();
243248
}
244249
}
245250
}
@@ -610,40 +615,53 @@ private void bindAuthenticationServices(ClientConfigurationData conf) {
610615
// does schedule some gets the framework's shared one — binding a scheduled pool per admin to sit idle
611616
// would cost more than it buys.
612617
//
613-
// The blocking executor *is* the admin's own. This is where the SASL-over-HTTP challenge rounds put
614-
// their GSSAPI work, so it must never be the admin's request threads — a slow KDC would consume them
615-
// — but leaving it unbound is not right either: the plugin then shares one process-wide pool with
616-
// every other client in the JVM, so a stalled identity provider reached by one admin throttles
617-
// authentication for all of them. A small bounded pool per admin keeps that blast radius inside the
618-
// admin that owns the plugin, and is shut down with it.
618+
// The blocking executor *is* the admin's own — the same pool BaseResource runs the deprecated v4
619+
// composition on. This is where the SASL-over-HTTP challenge rounds put their GSSAPI work, so it must
620+
// never be the admin's request threads — a slow KDC would consume them — but leaving it unbound is
621+
// not right either: the plugin then shares one process-wide pool with every other client in the JVM,
622+
// so a stalled identity provider reached by one admin throttles authentication for all of them. A
623+
// small bounded pool per admin keeps that blast radius inside the admin that owns the plugin, and is
624+
// shut down with it.
619625
ClientAuthenticationServices services = new DefaultClientAuthenticationServices(
620-
authHttpClientFactory, null, blockingAuthExecutor(), Clock.systemDefaultZone(), openTelemetry,
626+
authHttpClientFactory, null, blockingAuthExecutor, Clock.systemDefaultZone(), openTelemetry,
621627
clientInstanceId);
622628
aware.bindClientAuthenticationServices(services);
623629
}
624630

625631
/**
626-
* The admin's bounded executor for potentially-blocking authentication work (PIP-478).
632+
* Lend a resource this admin's blocking authentication executor, so the deprecated v4 credential
633+
* composition it runs per request stays on the admin's own pool (PIP-478).
634+
*
635+
* @param resource the freshly constructed resource
636+
* @param <T> the resource type
637+
* @return the same resource
638+
*/
639+
private <T extends BaseResource> T lendAuthExecutor(T resource) {
640+
resource.setBlockingAuthExecutor(blockingAuthExecutor);
641+
return resource;
642+
}
643+
644+
/**
645+
* Build the admin's bounded executor for potentially-blocking authentication work (PIP-478).
627646
*
628-
* <p>Queues rather than rejects: every caller is an authenticated request, so a saturated pool must slow
629-
* requests down rather than fail them. Core threads time out, so an admin whose plugin never blocks pays
630-
* for nothing. Created lazily under this admin's monitor and shut down in {@link #close()}.
647+
* <p>Queues rather than rejects, matching the framework's shared pool: every caller is an authenticated
648+
* request, so a saturated pool must slow requests down rather than fail them — and a queued task is far
649+
* smaller than what the caller already retains to produce it (a synchronous admin call is a parked
650+
* thread, bounded by its own {@code requestTimeoutMs}). Core threads time out, so an admin whose plugin
651+
* never blocks holds no threads. Shut down in {@link #close()}.
631652
*
632653
* @return the blocking authentication executor
633654
*/
634-
private synchronized Executor blockingAuthExecutor() {
635-
if (blockingAuthExecutor == null) {
636-
ThreadPoolExecutor executor = new ThreadPoolExecutor(AUTH_BLOCKING_MAX_THREADS,
637-
AUTH_BLOCKING_MAX_THREADS, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
638-
runnable -> {
639-
Thread thread = new Thread(runnable, "pulsar-admin-auth-blocking");
640-
thread.setDaemon(true);
641-
return thread;
642-
});
643-
executor.allowCoreThreadTimeOut(true);
644-
blockingAuthExecutor = executor;
645-
}
646-
return blockingAuthExecutor;
655+
private static ThreadPoolExecutor newBlockingAuthExecutor() {
656+
ThreadPoolExecutor executor = new ThreadPoolExecutor(AUTH_BLOCKING_MAX_THREADS,
657+
AUTH_BLOCKING_MAX_THREADS, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
658+
runnable -> {
659+
Thread thread = new Thread(runnable, "pulsar-admin-auth-blocking");
660+
thread.setDaemon(true);
661+
return thread;
662+
});
663+
executor.allowCoreThreadTimeOut(true);
664+
return executor;
647665
}
648666

649667
@Override
@@ -667,10 +685,7 @@ public void close() {
667685
// PIP-478: after auth.close(), so a plugin shutting down over this executor still has it. shutdown()
668686
// rather than shutdownNow(): queued work is a credential call for a request already in flight, and
669687
// the threads are daemons, so a straggler cannot hold the JVM up.
670-
ThreadPoolExecutor authExecutor = blockingAuthExecutor;
671-
if (authExecutor != null) {
672-
authExecutor.shutdown();
673-
}
688+
blockingAuthExecutor.shutdown();
674689
}
675690

676691
@VisibleForTesting

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.