Use human readable durations instead of milliseconds

This commit is contained in:
Guillaume Nodet
2020-11-12 11:41:59 +01:00
committed by Peter Palaga
parent 3e880ab870
commit 7d1c7c072c
8 changed files with 284 additions and 41 deletions

View File

@@ -94,7 +94,7 @@ public class DaemonClientConnection implements Closeable {
}
public List<Message> receive() throws ConnectException, StaleAddressException {
int maxKeepAliveMs = parameters.keepAliveMs() * parameters.maxLostKeepAlive();
long maxKeepAliveMs = parameters.keepAlive().toMillis() * parameters.maxLostKeepAlive();
while (true) {
try {
final Message m = queue.poll(maxKeepAliveMs, TimeUnit.MILLISECONDS);

View File

@@ -21,6 +21,7 @@ import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -41,6 +42,7 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.mvndaemon.mvnd.common.BuildProperties;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.Os;
import org.mvndaemon.mvnd.common.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -268,8 +270,8 @@ public class DaemonParameters {
.put(Environment.USER_DIR, newUserDir));
}
public int keepAliveMs() {
return property(Environment.DAEMON_KEEP_ALIVE_MS).orFail().asInt();
public Duration keepAlive() {
return property(Environment.DAEMON_KEEP_ALIVE).orFail().asDuration();
}
public int maxLostKeepAlive() {
@@ -579,5 +581,9 @@ public class DaemonParameters {
return function.applyAsInt(asInt());
}
public Duration asDuration() {
return TimeUtils.toDuration(get());
}
}
}

View File

@@ -17,11 +17,11 @@ package org.mvndaemon.mvnd.common;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Collection;
import java.util.Locale;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
/**
* Collects system properties and environment variables used by mvnd client or server.
@@ -74,8 +74,8 @@ public enum Environment {
DAEMON_REGISTRY("daemon.registry", null, null, false),
MVND_NO_DAEMON("mvnd.noDaemon", "MVND_NO_DAEMON", "false", true),
DAEMON_DEBUG("daemon.debug", null, false, true),
DAEMON_IDLE_TIMEOUT_MS("daemon.idleTimeoutMs", null, TimeUnit.HOURS.toMillis(3), true),
DAEMON_KEEP_ALIVE_MS("daemon.keepAliveMs", null, TimeUnit.SECONDS.toMillis(1), true),
DAEMON_IDLE_TIMEOUT("daemon.idleTimeout", null, "3 hours", true),
DAEMON_KEEP_ALIVE("daemon.keepAlive", null, "1 sec", true),
DAEMON_MAX_LOST_KEEP_ALIVE("daemon.maxLostKeepAlive", null, 3, false),
/**
* The minimum number of threads to use when constructing the default {@code -T} parameter for the daemon.
@@ -146,17 +146,13 @@ public enum Environment {
/**
* Interval to check if the daemon should expire
*/
DAEMON_EXPIRATION_CHECK_DELAY_MS("daemon.expirationCheckDelayMs", null, TimeUnit.SECONDS.toMillis(10), true),
DAEMON_EXPIRATION_CHECK_DELAY("daemon.expirationCheckDelay", null, "10 seconds", true),
/**
* Period after which idle daemons will shut down
*/
DAEMON_DUPLICATE_DAEMON_GRACE_PERIOD_MS("daemon.duplicateDaemonGracePeriodMs", null, TimeUnit.SECONDS.toMillis(10), true),
DAEMON_DUPLICATE_DAEMON_GRACE_PERIOD("daemon.duplicateDaemonGracePeriod", null, "10 seconds", true),
;
public static final int DEFAULT_IDLE_TIMEOUT = (int) TimeUnit.HOURS.toMillis(3);
public static final int DEFAULT_KEEP_ALIVE = (int) TimeUnit.SECONDS.toMillis(1);
static Properties properties = System.getProperties();
public static void setProperties(Properties properties) {
@@ -219,6 +215,10 @@ public enum Environment {
return Paths.get(result);
}
public Duration asDuration() {
return TimeUtils.toDuration(asString());
}
public String asCommandLineProperty(String value) {
return "-D" + property + "=" + value;
}

View File

@@ -0,0 +1,244 @@
/*
* 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.mvndaemon.mvnd.common;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.time.Duration;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Time utils.
*
* Origin file:
* https://github.com/apache/camel/blob/4ea9e6c357371682b855d2d79655b41120331b7a/core/camel-util/src/main/java/org/apache/camel/util/TimeUtils.java
*/
public final class TimeUtils {
private static final Pattern NUMBERS_ONLY_STRING_PATTERN = Pattern.compile("^[-]?(\\d)+$", Pattern.CASE_INSENSITIVE);
private static final Pattern WEEK_REGEX_PATTERN = Pattern.compile("((\\d)*(\\d))\\s*w(eek(s)?)?(?=\\b|\\d|$)",
Pattern.CASE_INSENSITIVE);
private static final Pattern DAY_REGEX_PATTERN = Pattern.compile("((\\d)*(\\d))\\s*d(ay(s)?)?(?=\\b|\\d|$)",
Pattern.CASE_INSENSITIVE);
private static final Pattern HOUR_REGEX_PATTERN = Pattern.compile("((\\d)*(\\d))\\s*h(our(s)?)?(?=\\b|\\d|$)",
Pattern.CASE_INSENSITIVE);
private static final Pattern MINUTES_REGEX_PATTERN = Pattern.compile("((\\d)*(\\d))\\s*m(in(ute(s)?)?)?(?=\\b|\\d|$)",
Pattern.CASE_INSENSITIVE);
private static final Pattern SECONDS_REGEX_PATTERN = Pattern
.compile("((\\d)(\\d)*)(\\.(\\d+))?\\s*s(ec(ond)?(s)?)?(?=\\b|\\d|$)", Pattern.CASE_INSENSITIVE);
private static final Pattern MILLIS_REGEX_PATTERN = Pattern
.compile("((\\d)(\\d)*)(\\.(\\d+))?\\s*m(illi)?s(ec(ond)?(s)?)?(?=\\b|\\d|$)", Pattern.CASE_INSENSITIVE);
private TimeUtils() {
}
public static boolean isPositive(Duration dur) {
return dur.getSeconds() > 0 || dur.getNano() != 0;
}
public static String printDuration(Duration uptime) {
return printDuration(uptime.toMillis());
}
/**
* Prints the duration in a human readable format as X days Y hours Z minutes etc.
*
* @param uptime the uptime in millis
* @return the time used for displaying on screen or in logs
*/
public static String printDuration(double uptime) {
// Code taken from Karaf
// https://svn.apache.org/repos/asf/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/InfoAction.java
NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH));
NumberFormat fmtD = new DecimalFormat("###,##0.000", new DecimalFormatSymbols(Locale.ENGLISH));
uptime /= 1000;
if (uptime < 60) {
return fmtD.format(uptime) + " seconds";
}
uptime /= 60;
if (uptime < 60) {
long minutes = (long) uptime;
String s = fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
return s;
}
uptime /= 60;
if (uptime < 24) {
long hours = (long) uptime;
long minutes = (long) ((uptime - hours) * 60);
String s = fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
if (minutes != 0) {
s += " " + fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
}
return s;
}
uptime /= 24;
long days = (long) uptime;
long hours = (long) ((uptime - days) * 24);
String s = fmtI.format(days) + (days > 1 ? " days" : " day");
if (hours != 0) {
s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
}
return s;
}
public static Duration toDuration(String source) throws IllegalArgumentException {
return Duration.ofMillis(toMilliSeconds(source));
}
public static long toMilliSeconds(String source) throws IllegalArgumentException {
// quick conversion if its only digits
boolean digit = true;
for (int i = 0; i < source.length(); i++) {
char ch = source.charAt(i);
// special for first as it can be negative number
if (i == 0 && ch == '-') {
continue;
}
// quick check if its 0..9
if (ch < '0' || ch > '9') {
digit = false;
break;
}
}
if (digit) {
return Long.parseLong(source);
}
long milliseconds = 0;
boolean foundFlag = false;
checkCorrectnessOfPattern(source);
Matcher matcher;
matcher = createMatcher(NUMBERS_ONLY_STRING_PATTERN, source);
if (matcher.find()) {
// Note: This will also be used for regular numeric strings.
// This String -> long converter will be used for all strings.
milliseconds = Long.parseLong(source);
} else {
matcher = createMatcher(WEEK_REGEX_PATTERN, source);
if (matcher.find()) {
milliseconds += 7 * TimeUnit.DAYS.toMillis(Long.parseLong(matcher.group(1)));
foundFlag = true;
}
matcher = createMatcher(DAY_REGEX_PATTERN, source);
if (matcher.find()) {
milliseconds += TimeUnit.DAYS.toMillis(Long.parseLong(matcher.group(1)));
foundFlag = true;
}
matcher = createMatcher(HOUR_REGEX_PATTERN, source);
if (matcher.find()) {
milliseconds += TimeUnit.HOURS.toMillis(Long.parseLong(matcher.group(1)));
foundFlag = true;
}
matcher = createMatcher(MINUTES_REGEX_PATTERN, source);
if (matcher.find()) {
milliseconds += TimeUnit.MINUTES.toMillis(Long.parseLong(matcher.group(1)));
foundFlag = true;
}
matcher = createMatcher(SECONDS_REGEX_PATTERN, source);
if (matcher.find()) {
milliseconds += TimeUnit.SECONDS.toMillis(Long.parseLong(matcher.group(1)));
if (matcher.group(5) != null && !matcher.group(5).isEmpty()) {
milliseconds += TimeUnit.MILLISECONDS.toMillis(Long.parseLong(matcher.group(5)));
}
foundFlag = true;
}
matcher = createMatcher(MILLIS_REGEX_PATTERN, source);
if (matcher.find()) {
milliseconds += TimeUnit.MILLISECONDS.toMillis(Long.parseLong(matcher.group(1)));
foundFlag = true;
}
// No pattern matched... initiating fallback check and conversion (if required).
// The source at this point may contain illegal values or special characters
if (!foundFlag) {
milliseconds = Long.parseLong(source);
}
}
return milliseconds;
}
private static void checkCorrectnessOfPattern(String source) {
//replace only numbers once
Matcher matcher = createMatcher(NUMBERS_ONLY_STRING_PATTERN, source);
String replaceSource = matcher.replaceFirst("");
//replace week string once
matcher = createMatcher(WEEK_REGEX_PATTERN, replaceSource);
if (matcher.find() && matcher.find()) {
throw new IllegalArgumentException("Weeks should not be specified more then once: " + source);
}
replaceSource = matcher.replaceFirst("");
//replace day string once
matcher = createMatcher(DAY_REGEX_PATTERN, replaceSource);
if (matcher.find() && matcher.find()) {
throw new IllegalArgumentException("Days should not be specified more then once: " + source);
}
replaceSource = matcher.replaceFirst("");
//replace hour string once
matcher = createMatcher(HOUR_REGEX_PATTERN, replaceSource);
if (matcher.find() && matcher.find()) {
throw new IllegalArgumentException("Hours should not be specified more then once: " + source);
}
replaceSource = matcher.replaceFirst("");
//replace minutes once
matcher = createMatcher(MINUTES_REGEX_PATTERN, replaceSource);
if (matcher.find() && matcher.find()) {
throw new IllegalArgumentException("Minutes should not be specified more then once: " + source);
}
replaceSource = matcher.replaceFirst("");
//replace seconds once
matcher = createMatcher(SECONDS_REGEX_PATTERN, replaceSource);
if (matcher.find() && matcher.find()) {
throw new IllegalArgumentException("Seconds should not be specified more then once: " + source);
}
replaceSource = matcher.replaceFirst("");
//replace millis once
matcher = createMatcher(MILLIS_REGEX_PATTERN, replaceSource);
if (matcher.find() && matcher.find()) {
throw new IllegalArgumentException("Milliseconds should not be specified more then once: " + source);
}
replaceSource = matcher.replaceFirst("");
if (replaceSource.length() > 0) {
throw new IllegalArgumentException("Illegal characters: " + source);
}
}
private static Matcher createMatcher(Pattern pattern, String source) {
return pattern.matcher(source);
}
}

View File

@@ -17,6 +17,8 @@ package org.mvndaemon.mvnd.daemon;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
@@ -28,6 +30,7 @@ import org.mvndaemon.mvnd.common.DaemonExpirationStatus;
import org.mvndaemon.mvnd.common.DaemonInfo;
import org.mvndaemon.mvnd.common.DaemonState;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.TimeUtils;
import static org.mvndaemon.mvnd.common.DaemonExpirationStatus.DO_NOT_EXPIRE;
import static org.mvndaemon.mvnd.common.DaemonExpirationStatus.GRACEFUL_EXPIRE;
@@ -41,8 +44,6 @@ import static org.mvndaemon.mvnd.daemon.DaemonExpiration.DaemonExpirationResult.
*/
public class DaemonExpiration {
public static final int DUPLICATE_DAEMON_GRACE_PERIOD_MS = 10000;
public interface DaemonExpirationStrategy {
DaemonExpirationResult checkExpiration(Server daemon);
@@ -53,7 +54,7 @@ public class DaemonExpiration {
return any(
any(gcTrashing(), lowHeapSpace(), lowNonHeap()),
all(compatible(), duplicateGracePeriod(), notMostRecentlyUsed()),
idleTimeout(Environment.DAEMON_IDLE_TIMEOUT_MS.asInt()),
idleTimeout(Environment.DAEMON_IDLE_TIMEOUT.asDuration()),
all(duplicateGracePeriod(), notMostRecentlyUsed(), lowMemory(0.05)),
registryUnavailable());
}
@@ -82,26 +83,14 @@ public class DaemonExpiration {
}
static DaemonExpirationStrategy duplicateGracePeriod() {
return idleTimeout(Environment.DAEMON_DUPLICATE_DAEMON_GRACE_PERIOD_MS.asInt());
return idleTimeout(Environment.DAEMON_DUPLICATE_DAEMON_GRACE_PERIOD.asDuration());
}
private static final long HOUR = 60 * 60 * 1000;
private static final long MINUTE = 60 * 1000;
private static final long SECOND = 1000;
static DaemonExpirationStrategy idleTimeout(long timeout) {
static DaemonExpirationStrategy idleTimeout(Duration timeout) {
return daemon -> {
long idl = System.currentTimeMillis() - daemon.getLastIdle();
if (daemon.getState() == DaemonState.Idle && idl > timeout) {
String str;
if (idl > HOUR) {
str = (idl / HOUR) + " hours";
} else if (idl > MINUTE) {
str = (idl / MINUTE) + " minutes";
} else {
str = (idl / SECOND) + " seconds";
}
return new DaemonExpirationResult(QUIET_EXPIRE, "after being idle for " + str);
Duration idl = Duration.between(Instant.ofEpochMilli(daemon.getLastIdle()), Instant.now());
if (daemon.getState() == DaemonState.Idle && idl.compareTo(timeout) > 0) {
return new DaemonExpirationResult(QUIET_EXPIRE, "after being idle for " + TimeUtils.printDuration(idl));
} else {
return NOT_TRIGGERED;
}

View File

@@ -21,6 +21,7 @@ import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
@@ -165,9 +166,9 @@ public class Server implements AutoCloseable, Runnable {
public void run() {
try {
int expirationCheckDelayMs = Environment.DAEMON_EXPIRATION_CHECK_DELAY_MS.asInt();
Duration expirationCheckDelay = Environment.DAEMON_EXPIRATION_CHECK_DELAY.asDuration();
executor.scheduleAtFixedRate(this::expirationCheck,
expirationCheckDelayMs, expirationCheckDelayMs, TimeUnit.MILLISECONDS);
expirationCheckDelay.toMillis(), expirationCheckDelay.toMillis(), TimeUnit.MILLISECONDS);
LOGGER.info("Daemon started");
if (noDaemon) {
try (SocketChannel socket = this.socket.accept()) {
@@ -405,7 +406,7 @@ public class Server implements AutoCloseable, Runnable {
private void handle(DaemonConnection connection, BuildRequest buildRequest) {
updateState(Busy);
try {
int keepAlive = Environment.DAEMON_KEEP_ALIVE_MS.asInt();
Duration keepAlive = Environment.DAEMON_KEEP_ALIVE.asDuration();
LOGGER.info("Executing request");
@@ -421,7 +422,7 @@ public class Server implements AutoCloseable, Runnable {
while (true) {
Message m;
if (flushed) {
m = sendQueue.poll(keepAlive, TimeUnit.MILLISECONDS);
m = sendQueue.poll(keepAlive.toMillis(), TimeUnit.MILLISECONDS);
if (m == null) {
m = Message.KEEP_ALIVE_SINGLETON;
}

View File

@@ -35,6 +35,7 @@ import org.mvndaemon.mvnd.client.DaemonParameters;
import org.mvndaemon.mvnd.client.DefaultClient;
import org.mvndaemon.mvnd.common.DaemonRegistry;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.TimeUtils;
import static org.mvndaemon.mvnd.junit.TestUtils.deleteDir;
@@ -205,8 +206,8 @@ public class MvndTestExtension implements BeforeAllCallback, BeforeEachCallback,
Paths.get(System.getProperty("java.home")).toAbsolutePath().normalize(),
localMavenRepository, settingsPath,
logback,
Environment.DEFAULT_IDLE_TIMEOUT,
Environment.DEFAULT_KEEP_ALIVE,
TimeUtils.toDuration(Environment.DAEMON_IDLE_TIMEOUT.getDef()),
TimeUtils.toDuration(Environment.DAEMON_KEEP_ALIVE.getDef()),
Integer.parseInt(Environment.DAEMON_MAX_LOST_KEEP_ALIVE.getDef()));
final TestRegistry registry = new TestRegistry(parameters.registry());

View File

@@ -16,8 +16,10 @@
package org.mvndaemon.mvnd.junit;
import java.nio.file.Path;
import java.time.Duration;
import org.mvndaemon.mvnd.client.DaemonParameters;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.TimeUtils;
public class TestParameters extends DaemonParameters {
static final int TEST_MIN_THREADS = 2;
@@ -26,7 +28,7 @@ public class TestParameters extends DaemonParameters {
public TestParameters(Path testDir, Path mvndPropertiesPath, Path mavenHome, Path userHome, Path userDir,
Path multiModuleProjectDirectory,
Path javaHome, Path localMavenRepository, Path settings, Path logbackConfigurationPath,
int idleTimeout, int keepAlive, int maxLostKeepAlive) {
Duration idleTimeout, Duration keepAlive, int maxLostKeepAlive) {
super(new PropertiesBuilder().put(Environment.MVND_PROPERTIES_PATH, mvndPropertiesPath)
.put(Environment.MVND_HOME, mavenHome)
.put(Environment.USER_HOME, userHome)
@@ -36,8 +38,8 @@ public class TestParameters extends DaemonParameters {
.put(Environment.MAVEN_REPO_LOCAL, localMavenRepository)
.put(Environment.MAVEN_SETTINGS, settings)
.put(Environment.LOGBACK_CONFIGURATION_FILE, logbackConfigurationPath)
.put(Environment.DAEMON_IDLE_TIMEOUT_MS, idleTimeout)
.put(Environment.DAEMON_KEEP_ALIVE_MS, keepAlive)
.put(Environment.DAEMON_IDLE_TIMEOUT, TimeUtils.printDuration(idleTimeout))
.put(Environment.DAEMON_KEEP_ALIVE, TimeUtils.printDuration(keepAlive))
.put(Environment.DAEMON_MAX_LOST_KEEP_ALIVE, maxLostKeepAlive)
.put(Environment.MVND_MIN_THREADS, TEST_MIN_THREADS));
this.testDir = testDir;