|
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 | } |
@@ -604,40 +609,53 @@ private void bindAuthenticationServices(ClientConfigurationData conf) { |
604 | 609 | // does schedule some gets the framework's shared one — binding a scheduled pool per admin to sit idle |
605 | 610 | // would cost more than it buys. |
606 | 611 | // |
607 | | - // The blocking executor *is* the admin's own. This is where the SASL-over-HTTP challenge rounds put |
608 | | - // their GSSAPI work, so it must never be the admin's request threads — a slow KDC would consume them |
609 | | - // — but leaving it unbound is not right either: the plugin then shares one process-wide pool with |
610 | | - // every other client in the JVM, so a stalled identity provider reached by one admin throttles |
611 | | - // authentication for all of them. A small bounded pool per admin keeps that blast radius inside the |
612 | | - // admin that owns the plugin, and is shut down with it. |
| 612 | + // The blocking executor *is* the admin's own — the same pool BaseResource runs the deprecated v4 |
| 613 | + // composition on. This is where the SASL-over-HTTP challenge rounds put their GSSAPI work, so it must |
| 614 | + // never be the admin's request threads — a slow KDC would consume them — but leaving it unbound is |
| 615 | + // not right either: the plugin then shares one process-wide pool with every other client in the JVM, |
| 616 | + // so a stalled identity provider reached by one admin throttles authentication for all of them. A |
| 617 | + // small bounded pool per admin keeps that blast radius inside the admin that owns the plugin, and is |
| 618 | + // shut down with it. |
613 | 619 | ClientAuthenticationServices services = new DefaultClientAuthenticationServices( |
614 | | - authHttpClientFactory, null, blockingAuthExecutor(), Clock.systemDefaultZone(), openTelemetry, |
| 620 | + authHttpClientFactory, null, blockingAuthExecutor, Clock.systemDefaultZone(), openTelemetry, |
615 | 621 | clientInstanceId); |
616 | 622 | aware.bindClientAuthenticationServices(services); |
617 | 623 | } |
618 | 624 |
|
619 | 625 | /** |
620 | | - * The admin's bounded executor for potentially-blocking authentication work (PIP-478). |
| 626 | + * Lend a resource this admin's blocking authentication executor, so the deprecated v4 credential |
| 627 | + * composition it runs per request stays on the admin's own pool (PIP-478). |
| 628 | + * |
| 629 | + * @param resource the freshly constructed resource |
| 630 | + * @param <T> the resource type |
| 631 | + * @return the same resource |
| 632 | + */ |
| 633 | + private <T extends BaseResource> T lendAuthExecutor(T resource) { |
| 634 | + resource.setBlockingAuthExecutor(blockingAuthExecutor); |
| 635 | + return resource; |
| 636 | + } |
| 637 | + |
| 638 | + /** |
| 639 | + * Build the admin's bounded executor for potentially-blocking authentication work (PIP-478). |
621 | 640 | * |
622 | | - * <p>Queues rather than rejects: every caller is an authenticated request, so a saturated pool must slow |
623 | | - * requests down rather than fail them. Core threads time out, so an admin whose plugin never blocks pays |
624 | | - * for nothing. Created lazily under this admin's monitor and shut down in {@link #close()}. |
| 641 | + * <p>Queues rather than rejects, matching the framework's shared pool: every caller is an authenticated |
| 642 | + * request, so a saturated pool must slow requests down rather than fail them — and a queued task is far |
| 643 | + * smaller than what the caller already retains to produce it (a synchronous admin call is a parked |
| 644 | + * thread, bounded by its own {@code requestTimeoutMs}). Core threads time out, so an admin whose plugin |
| 645 | + * never blocks holds no threads. Shut down in {@link #close()}. |
625 | 646 | * |
626 | 647 | * @return the blocking authentication executor |
627 | 648 | */ |
628 | | - private synchronized Executor blockingAuthExecutor() { |
629 | | - if (blockingAuthExecutor == null) { |
630 | | - ThreadPoolExecutor executor = new ThreadPoolExecutor(AUTH_BLOCKING_MAX_THREADS, |
631 | | - AUTH_BLOCKING_MAX_THREADS, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), |
632 | | - runnable -> { |
633 | | - Thread thread = new Thread(runnable, "pulsar-admin-auth-blocking"); |
634 | | - thread.setDaemon(true); |
635 | | - return thread; |
636 | | - }); |
637 | | - executor.allowCoreThreadTimeOut(true); |
638 | | - blockingAuthExecutor = executor; |
639 | | - } |
640 | | - return blockingAuthExecutor; |
| 649 | + private static ThreadPoolExecutor newBlockingAuthExecutor() { |
| 650 | + ThreadPoolExecutor executor = new ThreadPoolExecutor(AUTH_BLOCKING_MAX_THREADS, |
| 651 | + AUTH_BLOCKING_MAX_THREADS, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), |
| 652 | + runnable -> { |
| 653 | + Thread thread = new Thread(runnable, "pulsar-admin-auth-blocking"); |
| 654 | + thread.setDaemon(true); |
| 655 | + return thread; |
| 656 | + }); |
| 657 | + executor.allowCoreThreadTimeOut(true); |
| 658 | + return executor; |
641 | 659 | } |
642 | 660 |
|
643 | 661 | @Override |
@@ -661,10 +679,7 @@ public void close() { |
661 | 679 | // PIP-478: after auth.close(), so a plugin shutting down over this executor still has it. shutdown() |
662 | 680 | // rather than shutdownNow(): queued work is a credential call for a request already in flight, and |
663 | 681 | // the threads are daemons, so a straggler cannot hold the JVM up. |
664 | | - ThreadPoolExecutor authExecutor = blockingAuthExecutor; |
665 | | - if (authExecutor != null) { |
666 | | - authExecutor.shutdown(); |
667 | | - } |
| 682 | + blockingAuthExecutor.shutdown(); |
668 | 683 | } |
669 | 684 |
|
670 | 685 | @VisibleForTesting |
|