[ 
https://issues.apache.org/jira/browse/MRESOLVER-283?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17629718#comment-17629718
 ] 

ASF GitHub Bot commented on MRESOLVER-283:
------------------------------------------

cstamas commented on code in PR #213:
URL: https://github.com/apache/maven-resolver/pull/213#discussion_r1015200916


##########
maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/concurrency/DefaultResolverExecutorService.java:
##########
@@ -0,0 +1,92 @@
+package org.eclipse.aether.internal.impl.concurrency;
+
+/*
+ * 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.
+ */
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.eclipse.aether.RepositorySystemSession;
+import org.eclipse.aether.spi.concurrency.ResolverExecutor;
+import org.eclipse.aether.spi.concurrency.ResolverExecutorService;
+import org.eclipse.aether.util.concurrency.WorkerThreadFactory;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Default implementation of {@link ResolverExecutor}.
+ * <p>
+ * This implementation uses {@link RepositorySystemSession#getData()} to store 
created {@link ExecutorService}
+ * instances. It creates instances that may be eventually garbage collected, 
so no explicit shutdown happens on
+ * them. When {@code maxThreads} parameter is 1 (accepted values are greater 
than zero), this implementation assumes
+ * caller wants "direct execution" (on caller thread) and creates {@link 
ResolverExecutor} instances accordingly.
+ */
+@Singleton
+@Named
+public final class DefaultResolverExecutorService implements 
ResolverExecutorService
+{
+    @Override
+    public ResolverExecutor getResolverExecutor( RepositorySystemSession 
session,
+                                                 Class<?> service,
+                                                 int maxThreads )
+    {
+        requireNonNull( session );
+        requireNonNull( service );
+        if ( maxThreads < 1 )
+        {
+            throw new IllegalArgumentException( "threads must be greater than 
zero" );
+        }
+
+        final ExecutorService executorService;
+        if ( maxThreads == 1 ) // direct
+        {
+            executorService = null;
+        }
+        else // shared && pooled
+        {
+            String key = DefaultResolverExecutorService.class.getName() + "." 
+ service.getSimpleName();
+            executorService = (ExecutorService) session.getData()
+                    .computeIfAbsent( key, () -> createExecutorService( 
service, maxThreads ) );
+        }
+        return new DefaultResolverExecutor( executorService );
+    }
+
+    /**
+     * Creates am {@link ExecutorService} that allows its core threads to die 
off in case of inactivity, and allows
+     * for proper garbage collection. This is important detail, as these 
instances are kept within session data, and
+     * currently there is no way to shut down them.
+     */
+    private ExecutorService createExecutorService( Class<?> service, int 
maxThreads )
+    {
+        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
+                maxThreads,
+                maxThreads,
+                3L, TimeUnit.SECONDS,
+                new LinkedBlockingQueue<>(),
+                new WorkerThreadFactory( getClass().getSimpleName() + "-" + 
service.getSimpleName() + "-" )
+        );
+        threadPoolExecutor.allowCoreThreadTimeOut( true );

Review Comment:
   To let the pool become garbage collected, otherwise (if have living threads) 
it will NOT be GC-ed. OTOH, for performance reasons, we do want to create pool 
(and keep the pool) with live threads for some....



##########
maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/concurrency/DefaultResolverExecutorService.java:
##########
@@ -0,0 +1,92 @@
+package org.eclipse.aether.internal.impl.concurrency;
+
+/*
+ * 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.
+ */
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.eclipse.aether.RepositorySystemSession;
+import org.eclipse.aether.spi.concurrency.ResolverExecutor;
+import org.eclipse.aether.spi.concurrency.ResolverExecutorService;
+import org.eclipse.aether.util.concurrency.WorkerThreadFactory;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Default implementation of {@link ResolverExecutor}.
+ * <p>
+ * This implementation uses {@link RepositorySystemSession#getData()} to store 
created {@link ExecutorService}
+ * instances. It creates instances that may be eventually garbage collected, 
so no explicit shutdown happens on
+ * them. When {@code maxThreads} parameter is 1 (accepted values are greater 
than zero), this implementation assumes
+ * caller wants "direct execution" (on caller thread) and creates {@link 
ResolverExecutor} instances accordingly.
+ */
+@Singleton
+@Named
+public final class DefaultResolverExecutorService implements 
ResolverExecutorService
+{
+    @Override
+    public ResolverExecutor getResolverExecutor( RepositorySystemSession 
session,
+                                                 Class<?> service,
+                                                 int maxThreads )
+    {
+        requireNonNull( session );
+        requireNonNull( service );
+        if ( maxThreads < 1 )
+        {
+            throw new IllegalArgumentException( "threads must be greater than 
zero" );
+        }
+
+        final ExecutorService executorService;
+        if ( maxThreads == 1 ) // direct
+        {
+            executorService = null;
+        }
+        else // shared && pooled
+        {
+            String key = DefaultResolverExecutorService.class.getName() + "." 
+ service.getSimpleName();
+            executorService = (ExecutorService) session.getData()
+                    .computeIfAbsent( key, () -> createExecutorService( 
service, maxThreads ) );
+        }
+        return new DefaultResolverExecutor( executorService );
+    }
+
+    /**
+     * Creates am {@link ExecutorService} that allows its core threads to die 
off in case of inactivity, and allows
+     * for proper garbage collection. This is important detail, as these 
instances are kept within session data, and
+     * currently there is no way to shut down them.
+     */
+    private ExecutorService createExecutorService( Class<?> service, int 
maxThreads )
+    {
+        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
+                maxThreads,
+                maxThreads,
+                3L, TimeUnit.SECONDS,
+                new LinkedBlockingQueue<>(),
+                new WorkerThreadFactory( getClass().getSimpleName() + "-" + 
service.getSimpleName() + "-" )

Review Comment:
   Good point, fixing 





> Introduce resolver wide "shared" executor service
> -------------------------------------------------
>
>                 Key: MRESOLVER-283
>                 URL: https://issues.apache.org/jira/browse/MRESOLVER-283
>             Project: Maven Resolver
>          Issue Type: Improvement
>          Components: Resolver
>            Reporter: Tamas Cservenak
>            Assignee: Tamas Cservenak
>            Priority: Major
>             Fix For: 1.9.0
>
>
> More and more component in resolver does parallel processing (BF collector, 
> MD resolver, basic connector), and they all create, maintain their own 
> executor instance.
> Instead of this, create one shared service component and just reuse it 
> accross resolver.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to