Copilot commented on code in PR #24790: URL: https://github.com/apache/pulsar/pull/24790#discussion_r2383334495
########## pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientSharedResourcesImpl.java: ########## @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.client.impl; + +import static org.apache.pulsar.client.impl.PulsarClientResourcesConfigurer.createDnsResolverGroupWithResourceConfig; +import static org.apache.pulsar.client.impl.PulsarClientResourcesConfigurer.createEventLoopGroupWithResourceConfig; +import static org.apache.pulsar.client.impl.PulsarClientResourcesConfigurer.createExternalExecutorProviderWithResourceConfig; +import static org.apache.pulsar.client.impl.PulsarClientResourcesConfigurer.createInternalExecutorProviderWithResourceConfig; +import static org.apache.pulsar.client.impl.PulsarClientResourcesConfigurer.createLookupExecutorProviderWithResourceConfig; +import static org.apache.pulsar.client.impl.PulsarClientResourcesConfigurer.createScheduledExecutorProviderWithResourceConfig; +import static org.apache.pulsar.client.impl.PulsarClientResourcesConfigurer.createTimer; +import io.netty.channel.EventLoopGroup; +import io.netty.util.Timer; +import java.util.Collection; +import java.util.EnumSet; +import java.util.Map; +import java.util.Set; +import lombok.Getter; +import org.apache.pulsar.client.api.PulsarClientException; +import org.apache.pulsar.client.api.PulsarClientSharedResources; +import org.apache.pulsar.client.util.ExecutorProvider; +import org.apache.pulsar.client.util.ScheduledExecutorProvider; +import org.apache.pulsar.common.util.netty.EventLoopUtil; + +@Getter +public class PulsarClientSharedResourcesImpl implements PulsarClientSharedResources { + Set<SharedResource> sharedResources; + protected final EventLoopGroup ioEventLoopGroup; + private final ExecutorProvider internalExecutorProvider; + private final ExecutorProvider externalExecutorProvider; + private final ScheduledExecutorProvider scheduledExecutorProvider; + private final Timer timer; + private final ExecutorProvider lookupExecutorProvider; + private final DnsResolverGroupImpl dnsResolverGroup; + + public PulsarClientSharedResourcesImpl(Set<SharedResource> sharedResources, + Map<SharedResource, PulsarClientSharedResourcesBuilderImpl.ResourceConfig> + resourceConfigs) { + if (sharedResources.isEmpty()) { + this.sharedResources = EnumSet.allOf(SharedResource.class); + } else { + this.sharedResources = Set.copyOf(sharedResources); + } + if (this.sharedResources.contains(SharedResource.DnsResolver) + && !this.sharedResources.contains(SharedResource.EventLoopGroup)) { + throw new IllegalArgumentException( + "It's necessary to enable ResourceType.eventLoopGroup when using ResourceType.dnsResolverGroup"); + } + this.ioEventLoopGroup = this.sharedResources.contains(SharedResource.EventLoopGroup) + ? createEventLoopGroupWithResourceConfig( + getResourceConfig(resourceConfigs, SharedResource.EventLoopGroup)) + : null; + this.externalExecutorProvider = this.sharedResources.contains(SharedResource.ListenerExecutor) + ? createExternalExecutorProviderWithResourceConfig( + getResourceConfig(resourceConfigs, SharedResource.ListenerExecutor)) + : null; + this.internalExecutorProvider = this.sharedResources.contains(SharedResource.InternalExecutor) + ? createInternalExecutorProviderWithResourceConfig( + getResourceConfig(resourceConfigs, SharedResource.InternalExecutor)) + : null; + this.scheduledExecutorProvider = this.sharedResources.contains(SharedResource.ScheduledExecutor) + ? createScheduledExecutorProviderWithResourceConfig( + getResourceConfig(resourceConfigs, SharedResource.ScheduledExecutor)) + : null; + this.lookupExecutorProvider = this.sharedResources.contains(SharedResource.LookupExecutor) + ? createLookupExecutorProviderWithResourceConfig( + getResourceConfig(resourceConfigs, SharedResource.LookupExecutor)) + : null; + this.timer = this.sharedResources.contains(SharedResource.Timer) + ? createTimer(getResourceConfig(resourceConfigs, SharedResource.Timer)) + : null; + this.dnsResolverGroup = this.sharedResources.contains(SharedResource.DnsResolver) + ? createDnsResolverGroupWithResourceConfig( + getResourceConfig(resourceConfigs, SharedResource.DnsResolver), + ioEventLoopGroup) + : null; + } + + private <T extends PulsarClientSharedResourcesBuilderImpl.ResourceConfig> T getResourceConfig( + Map<SharedResource, PulsarClientSharedResourcesBuilderImpl.ResourceConfig> resourceConfigs, + SharedResource resource) { + return (T) resourceConfigs.get(resource); + } + + + @Override + public boolean contains(SharedResource sharedResource) { + return sharedResources.contains(sharedResource); + } + + public Collection<SharedResource> getSharedResources() { + return sharedResources; + } + + @Override + public void close() throws PulsarClientException { + externalExecutorProvider.shutdownNow(); + internalExecutorProvider.shutdownNow(); + scheduledExecutorProvider.shutdownNow(); + lookupExecutorProvider.shutdownNow(); + dnsResolverGroup.close(); + timer.stop(); + EventLoopUtil.shutdownGracefully(ioEventLoopGroup); Review Comment: The close method does not handle null values for executor providers, which could cause NullPointerException when only some resources are shared. Add null checks before calling shutdown methods. ```suggestion if (externalExecutorProvider != null) { externalExecutorProvider.shutdownNow(); } if (internalExecutorProvider != null) { internalExecutorProvider.shutdownNow(); } if (scheduledExecutorProvider != null) { scheduledExecutorProvider.shutdownNow(); } if (lookupExecutorProvider != null) { lookupExecutorProvider.shutdownNow(); } if (dnsResolverGroup != null) { dnsResolverGroup.close(); } if (timer != null) { timer.stop(); } if (ioEventLoopGroup != null) { EventLoopUtil.shutdownGracefully(ioEventLoopGroup); } ``` ########## pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientSharedResourcesBuilderImpl.java: ########## @@ -0,0 +1,192 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.client.impl; + +import java.net.InetSocketAddress; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; +import org.apache.pulsar.client.api.PulsarClientSharedResources; +import org.apache.pulsar.client.api.PulsarClientSharedResourcesBuilder; + +public class PulsarClientSharedResourcesBuilderImpl implements PulsarClientSharedResourcesBuilder { + Set<PulsarClientSharedResources.SharedResource> sharedResources = new HashSet<>(); + Map<PulsarClientSharedResources.SharedResource, ResourceConfig> resourceConfigs = new HashMap<>(); + + interface ResourceConfig { + + } + + abstract static class NamedResourceConfig<T> implements ResourceConfig { + String name; + + public T name(String name) { + this.name = name; + return (T) this; + } + } + + static class ThreadPoolResourceConfig extends NamedResourceConfig<ThreadPoolConfig> implements ThreadPoolConfig { + int numberOfThreads = Runtime.getRuntime().availableProcessors(); + boolean daemon = Thread.currentThread().isDaemon(); + + @Override + public ThreadPoolConfig numberOfThreads(int numberOfThreads) { + this.numberOfThreads = numberOfThreads; + return this; + } + + @Override + public ThreadPoolConfig daemon(boolean daemon) { + this.daemon = daemon; + return this; + } + } + + static class EventLoopResourceConfig extends NamedResourceConfig<EventLoopGroupConfig> + implements EventLoopGroupConfig { + int numberOfThreads = Runtime.getRuntime().availableProcessors(); + boolean daemon = Thread.currentThread().isDaemon(); + boolean enableBusyWait; + + EventLoopResourceConfig() { + name = PulsarClientResourcesConfigurer.POOL_NAME_EVENT_LOOP_GROUP; + } + + @Override + public EventLoopGroupConfig numberOfThreads(int numberOfThreads) { + this.numberOfThreads = numberOfThreads; + return this; + } + + @Override + public EventLoopGroupConfig daemon(boolean daemon) { + this.daemon = daemon; + return this; + } + + @Override + public EventLoopGroupConfig enableBusyWait(boolean enableBusyWait) { + this.enableBusyWait = enableBusyWait; + return this; + } + } + + static class TimerResourceConfig extends NamedResourceConfig<TimerConfig> implements TimerConfig { + long tickDuration = 1L; + TimeUnit tickDurationTimeUnit = TimeUnit.MILLISECONDS; Review Comment: Extra space in variable name 'tickDurationTimeUnit' - should be 'tickDurationTimeUnit' without the extra space. ```suggestion TimeUnit tickDurationTimeUnit = TimeUnit.MILLISECONDS; ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
