User's preference for -T can be stored as mvnd.threads in ~/.m2/mvnd.properties #132

This commit is contained in:
Peter Palaga
2020-10-31 23:44:44 +01:00
parent 52da0110f3
commit fcce52cbd4
4 changed files with 35 additions and 15 deletions

View File

@@ -41,7 +41,7 @@ public class ClientLayout extends Layout {
private final int idleTimeoutMs;
private final int keepAliveMs;
private final int maxLostKeepAlive;
private final int minThreads;
private final String threads;
public static ClientLayout getEnvInstance() {
if (ENV_INSTANCE == null) {
@@ -87,11 +87,20 @@ public class ClientLayout extends Layout {
.orLocalProperty(mvndProperties, mvndPropertiesPath)
.orDefault(() -> Integer.toString(Environment.DEFAULT_MAX_LOST_KEEP_ALIVE))
.asInt();
final int minThreads = Environment.MVND_MIN_THREADS
final String threads = Environment.MVND_THREADS
.systemProperty()
.orLocalProperty(mvndProperties, mvndPropertiesPath)
.orDefault(() -> Integer.toString(Environment.DEFAULT_MIN_THREADS))
.asInt();
.orDefault(() -> {
final int minThreads = Environment.MVND_MIN_THREADS
.systemProperty()
.orLocalProperty(mvndProperties, mvndPropertiesPath)
.orDefault(() -> Integer.toString(Environment.DEFAULT_MIN_THREADS))
.asInt();
return String
.valueOf(Math.max(Runtime.getRuntime().availableProcessors() - 1, minThreads));
})
.asString();
ENV_INSTANCE = new ClientLayout(
mvndPropertiesPath,
mvndHome,
@@ -101,14 +110,14 @@ public class ClientLayout extends Layout {
findLocalRepo(),
null,
Environment.findLogbackConfigurationPath(mvndProperties, mvndPropertiesPath, mvndHome),
idleTimeoutMs, keepAliveMs, maxLostKeepAlive, minThreads);
idleTimeoutMs, keepAliveMs, maxLostKeepAlive, threads);
}
return ENV_INSTANCE;
}
public ClientLayout(Path mvndPropertiesPath, Path mavenHome, Path userDir, Path multiModuleProjectDirectory, Path javaHome,
Path localMavenRepository, Path settings, Path logbackConfigurationPath, int idleTimeoutMs, int keepAliveMs,
int maxLostKeepAlive, int minThreads) {
int maxLostKeepAlive, String threads) {
super(mvndPropertiesPath, mavenHome, userDir, multiModuleProjectDirectory);
this.localMavenRepository = localMavenRepository;
this.settings = settings;
@@ -117,7 +126,7 @@ public class ClientLayout extends Layout {
this.idleTimeoutMs = idleTimeoutMs;
this.keepAliveMs = keepAliveMs;
this.maxLostKeepAlive = maxLostKeepAlive;
this.minThreads = minThreads;
this.threads = threads;
}
/**
@@ -127,7 +136,7 @@ public class ClientLayout extends Layout {
public ClientLayout cd(Path newUserDir) {
return new ClientLayout(mvndPropertiesPath, mavenHome, newUserDir, multiModuleProjectDirectory, javaHome,
localMavenRepository, settings, logbackConfigurationPath, idleTimeoutMs, keepAliveMs, maxLostKeepAlive,
minThreads);
threads);
}
/**
@@ -166,11 +175,11 @@ public class ClientLayout extends Layout {
}
/**
* @return the minimum number of threads to use when constructing the default {@code -T} parameter for the daemon.
* This value is ignored if the user passes his own `-T` or `--threads`.
* @return the number of threads (same syntax as Maven's {@code -T}/{@code --threads} option) to pass to the daemon
* unless the user passes his own `-T` or `--threads`.
*/
public int getMinThreads() {
return minThreads;
public String getThreads() {
return threads;
}
static Path findLocalRepo() {

View File

@@ -257,8 +257,7 @@ public class DefaultClient implements Client {
static void setDefaultArgs(List<String> args, ClientLayout layout) {
if (args.stream().noneMatch(arg -> arg.startsWith("-T") || arg.equals("--threads"))) {
final int threads = Math.max(Runtime.getRuntime().availableProcessors() - 1, layout.getMinThreads());
args.add("-T" + threads);
args.add("-T" + layout.getThreads());
}
if (args.stream().noneMatch(arg -> arg.startsWith("-b") || arg.equals("--builder"))) {
args.add("-bsmart");

View File

@@ -45,7 +45,18 @@ public enum Environment {
DAEMON_IDLE_TIMEOUT_MS("daemon.idleTimeoutMs", null),
DAEMON_KEEP_ALIVE_MS("daemon.keepAliveMs", null),
DAEMON_MAX_LOST_KEEP_ALIVE("daemon.maxLostKeepAlive", null),
/**
* The minimum number of threads to use when constructing the default {@code -T} parameter for the daemon.
* This value is ignored if the user passes @{@code-T}, @{@code --threads} or {@code -Dmvnd.threads} on the command
* line or if he sets {@code mvnd.threads} in {@code ~/.m2/mvnd.properties}.
*/
MVND_MIN_THREADS("mvnd.minThreads", null),
/**
* The number of threads to pass to the daemon; same syntax as Maven's {@code -T}/{@code --threads} option. Ignored
* if the user passes @{@code-T}, @{@code --threads} or {@code -Dmvnd.threads} on the command
* line.
*/
MVND_THREADS("mvnd.threads", null),
DAEMON_UID("daemon.uid", null);
public static final int DEFAULT_IDLE_TIMEOUT = (int) TimeUnit.HOURS.toMillis(3);

View File

@@ -26,7 +26,8 @@ public class TestLayout extends ClientLayout {
Path javaHome, Path localMavenRepository, Path settings, Path logbackConfigurationPath,
int idleTimeout, int keepAlive, int maxLostKeepAlive) {
super(mvndPropertiesPath, mavenHome, userDir, multiModuleProjectDirectory, javaHome, localMavenRepository,
settings, logbackConfigurationPath, idleTimeout, keepAlive, maxLostKeepAlive, TEST_MIN_THREADS);
settings, logbackConfigurationPath, idleTimeout, keepAlive, maxLostKeepAlive,
String.valueOf(Math.max(Runtime.getRuntime().availableProcessors() - 1, TEST_MIN_THREADS)));
this.testDir = testDir;
}