|
29 | 29 | import java.time.Clock; |
30 | 30 | import java.util.LinkedHashMap; |
31 | 31 | import java.util.Map; |
32 | | -import java.util.concurrent.Executor; |
33 | 32 | import java.util.concurrent.LinkedBlockingQueue; |
34 | 33 | import java.util.concurrent.ThreadPoolExecutor; |
35 | 34 | import java.util.concurrent.TimeUnit; |
@@ -132,9 +131,11 @@ public class PulsarAdminImpl implements PulsarAdmin { |
132 | 131 | // plugin does not implement ClientAuthenticationServicesAware. Closed with this admin. |
133 | 132 | private FrameworkHttpClientFactory authHttpClientFactory; |
134 | 133 | // 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; |
138 | 139 | @Getter |
139 | 140 | private AsyncHttpConnectorProvider asyncConnectorProvider; |
140 | 141 |
|
@@ -166,6 +167,8 @@ public PulsarAdminImpl(String serviceUrl, ClientConfigurationData clientConfigDa |
166 | 167 | asyncConnectorProvider = new AsyncHttpConnectorProvider(clientConfigData, |
167 | 168 | clientConfigData.getAutoCertRefreshSeconds(), acceptGzipCompression); |
168 | 169 |
|
| 170 | + this.blockingAuthExecutor = newBlockingAuthExecutor(); |
| 171 | + |
169 | 172 | boolean constructed = false; |
170 | 173 | try { |
171 | 174 | bindAuthenticationServices(clientConfigData); |
@@ -204,29 +207,29 @@ public PulsarAdminImpl(String serviceUrl, ClientConfigurationData clientConfigDa |
204 | 207 | clientConfigData.getAutoCertRefreshSeconds(), sharedResources); |
205 | 208 |
|
206 | 209 | 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)); |
230 | 233 |
|
231 | 234 | if (originalCtxLoader != null) { |
232 | 235 | Thread.currentThread().setContextClassLoader(originalCtxLoader); |
@@ -240,6 +243,8 @@ public PulsarAdminImpl(String serviceUrl, ClientConfigurationData clientConfigDa |
240 | 243 | // trustCertsFilePath being the common case) leaks one of each. The connectors release only |
241 | 244 | // their own borrowed handles, which is exactly why this has to happen here. |
242 | 245 | asyncConnectorProvider.close(); |
| 246 | + // A services-aware plugin's start() may already have run work here before the failure. |
| 247 | + blockingAuthExecutor.shutdown(); |
243 | 248 | } |
244 | 249 | } |
245 | 250 | } |
@@ -610,40 +615,53 @@ private void bindAuthenticationServices(ClientConfigurationData conf) { |
610 | 615 | // does schedule some gets the framework's shared one — binding a scheduled pool per admin to sit idle |
611 | 616 | // would cost more than it buys. |
612 | 617 | // |
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. |
619 | 625 | ClientAuthenticationServices services = new DefaultClientAuthenticationServices( |
620 | | - authHttpClientFactory, null, blockingAuthExecutor(), Clock.systemDefaultZone(), openTelemetry, |
| 626 | + authHttpClientFactory, null, blockingAuthExecutor, Clock.systemDefaultZone(), openTelemetry, |
621 | 627 | clientInstanceId); |
622 | 628 | aware.bindClientAuthenticationServices(services); |
623 | 629 | } |
624 | 630 |
|
625 | 631 | /** |
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). |
627 | 646 | * |
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()}. |
631 | 652 | * |
632 | 653 | * @return the blocking authentication executor |
633 | 654 | */ |
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; |
647 | 665 | } |
648 | 666 |
|
649 | 667 | @Override |
@@ -667,10 +685,7 @@ public void close() { |
667 | 685 | // PIP-478: after auth.close(), so a plugin shutting down over this executor still has it. shutdown() |
668 | 686 | // rather than shutdownNow(): queued work is a credential call for a request already in flight, and |
669 | 687 | // 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(); |
674 | 689 | } |
675 | 690 |
|
676 | 691 | @VisibleForTesting |
|