001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.util.concurrency;
020
021import java.util.concurrent.Executors;
022
023import org.eclipse.aether.Keys;
024import org.eclipse.aether.RepositorySystemSession;
025
026import static java.util.Objects.requireNonNull;
027
028/**
029 * Utilities for executors and sizing them.
030 * <em>Big fat note:</em> Do not use this class outside of resolver. This and related classes are not meant as "drop
031 * in replacement" for Jave Executors, is used in very controlled fashion only.
032 *
033 * @since 2.0.11
034 */
035public final class SmartExecutorUtils {
036    private static final SmartExecutor DIRECT = new SmartExecutor.Direct();
037
038    private SmartExecutorUtils() {}
039
040    /**
041     * Returns a smart executor for given parameters. If {@code tasks} is known (non-null), it must be greater than 0.
042     * The {@code maxConcurrentTasks} also must be greater than 0. The {@code namePrefix} must be non-null.
043     * <p>
044     * If {@code tasks} is set (is known), and equals to 1 (one), or {@code maxConcurrentTasks} equals to 1, the
045     * {@link #DIRECT} executor is returned, otherwise pooled one.
046     * <p>
047     * If @code tasks} is not set (is null), pooled one is returned with pool size of {@code maxConcurrentTasks}. In
048     * this case caller is advised to <em>reuse created executor across session</em>.
049     * <p>
050     * The returned instance must be closed out, ideally in try-with-resources construct. Returned instances when
051     * {@code tasks} parameter is given should not be cached (like in a session) as they may return {@link #DIRECT}
052     * executor for one call and a pool for subsequent call, based on value of tasks.
053     *
054     * @param tasks the amount of tasks, if known, {@code null} otherwise
055     * @param maxConcurrentTasks the maximum concurrency caller wants
056     * @param namePrefix the thread name prefixes, must not be {@code null).}
057     */
058    public static SmartExecutor newSmartExecutor(Integer tasks, int maxConcurrentTasks, String namePrefix) {
059        if (maxConcurrentTasks < 1) {
060            throw new IllegalArgumentException("maxConcurrentTasks must be > 0");
061        }
062        requireNonNull(namePrefix);
063        int poolSize;
064        if (tasks != null) {
065            if (tasks < 1) {
066                throw new IllegalArgumentException("tasks must be > 0");
067            }
068            if (tasks == 1 || maxConcurrentTasks == 1) {
069                return DIRECT;
070            }
071            poolSize = Math.min(tasks, maxConcurrentTasks);
072        } else {
073            if (maxConcurrentTasks == 1) {
074                return DIRECT;
075            }
076            poolSize = maxConcurrentTasks;
077        }
078        return new SmartExecutor.Pooled(Executors.newFixedThreadPool(poolSize, new WorkerThreadFactory(namePrefix)));
079    }
080
081    /**
082     * Returns a smart executor, bound to session if tasks to execute are not known ahead of time. The returned
083     * instance should be handled transparently, so preferably in try-with-resource even if underlying executor is
084     * probably tied to session lifecycle, if applicable.
085     * <p>
086     * Implementation note: by this change, the caller "concurrency" is made deterministic and global(!). If you consider
087     * collector example, it is called from project builder that in Maven 4 is already multithreaded, and before this
088     * change the actual threads doing IO (HTTP) was {@code callerThreadCount x maxConcurrentTask} per JVM/Maven process.
089     * Now, the {@code maxConcurrentTask} becomes global limit, and hence can be upped without unexpected "explosion"
090     * in increasing build threading or anything.
091     */
092    public static SmartExecutor smartExecutor(
093            RepositorySystemSession session, Integer tasks, int maxConcurrentTasks, String namePrefix) {
094        if (tasks == null && maxConcurrentTasks > 1) {
095            return (SmartExecutor) session.getData().computeIfAbsent(Keys.of(SmartExecutor.class, namePrefix), () -> {
096                SmartExecutor smartExecutor = newSmartExecutor(null, maxConcurrentTasks, namePrefix);
097                session.addOnSessionEndedHandler(smartExecutor::close);
098                return new SmartExecutor.NonClosing(smartExecutor);
099            });
100        } else {
101            return newSmartExecutor(tasks, maxConcurrentTasks, namePrefix);
102        }
103    }
104}