mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-10-15 14:50:54 +00:00
Improve status and daemon selection output
This commit is contained in:
@@ -20,6 +20,9 @@ import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
@@ -105,11 +108,13 @@ public class Client {
|
||||
try (DaemonRegistry registry = new DaemonRegistry(layout.registry())) {
|
||||
boolean status = args.remove("--status");
|
||||
if (status) {
|
||||
output.log(String.format(" %36s %5s %5s %7s %s",
|
||||
"UUID", "PID", "Port", "Status", "Timestamp"));
|
||||
registry.getAll().forEach(d -> output.log(String.format(" %36s %5s %5s %7s %s",
|
||||
output.log(String.format(" %36s %7s %5s %7s %s",
|
||||
"UUID", "PID", "Port", "Status", "Last activity"));
|
||||
registry.getAll().forEach(d -> output.log(String.format(" %36s %7s %5s %7s %s",
|
||||
d.getUid(), d.getPid(), d.getAddress(), d.getState(),
|
||||
new Date(Math.max(d.getLastIdle(), d.getLastBusy())).toString())));
|
||||
LocalDateTime.ofInstant(
|
||||
Instant.ofEpochMilli(Math.max(d.getLastIdle(), d.getLastBusy())),
|
||||
ZoneId.systemDefault()))));
|
||||
return new ClientResult<O>(argv, true, output);
|
||||
}
|
||||
boolean stop = args.remove("--stop");
|
||||
@@ -146,7 +151,7 @@ public class Client {
|
||||
|
||||
DaemonConnector connector = new DaemonConnector(layout, registry, this::startDaemon, new MessageSerializer());
|
||||
List<String> opts = new ArrayList<>();
|
||||
DaemonClientConnection daemon = connector.connect(new DaemonCompatibilitySpec(javaHome.toString(), opts));
|
||||
DaemonClientConnection daemon = connector.connect(new DaemonCompatibilitySpec(javaHome, opts));
|
||||
|
||||
daemon.dispatch(new Message.BuildRequest(
|
||||
args,
|
||||
|
@@ -16,36 +16,42 @@
|
||||
package org.jboss.fuse.mvnd.client;
|
||||
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class DaemonCompatibilitySpec {
|
||||
|
||||
private final String javaHome;
|
||||
private final Path javaHome;
|
||||
private final List<String> options;
|
||||
|
||||
public DaemonCompatibilitySpec(String javaHome, List<String> options) {
|
||||
this.javaHome = javaHome;
|
||||
this.options = options;
|
||||
/**
|
||||
* @param javaHome make sure the Path is a result of {@link Path#toRealPath(java.nio.file.LinkOption...)}
|
||||
* @param options the options
|
||||
*/
|
||||
public DaemonCompatibilitySpec(Path javaHome, List<String> options) {
|
||||
this.javaHome = Objects.requireNonNull(javaHome, "javaHome");
|
||||
this.options = Objects.requireNonNull(options, "options");
|
||||
}
|
||||
|
||||
public boolean isSatisfiedBy(DaemonInfo daemon) {
|
||||
return whyUnsatisfied(daemon) == null;
|
||||
}
|
||||
|
||||
public String whyUnsatisfied(DaemonInfo daemon) {
|
||||
public Result isSatisfiedBy(DaemonInfo daemon) {
|
||||
if (!javaHomeMatches(daemon)) {
|
||||
return "Java home is different.\n" + description(daemon);
|
||||
} else if (!daemonOptsMatch(daemon)) {
|
||||
return "At least one daemon option is different.\n" + description(daemon);
|
||||
return new Result(false, () -> "Java home is different.\n" + diff(daemon));
|
||||
}
|
||||
return null;
|
||||
if (!daemonOptsMatch(daemon)) {
|
||||
return new Result(false, () -> "At least one daemon option is different.\n" + diff(daemon));
|
||||
}
|
||||
return new Result(true, () -> {throw new RuntimeException("No reason if DaemonCompatibilityResult.compatible == true");});
|
||||
}
|
||||
|
||||
private String description(DaemonInfo context) {
|
||||
return "Wanted: " + this + "\n"
|
||||
+ "Actual: " + context + "\n";
|
||||
private String diff(DaemonInfo context) {
|
||||
final StringBuilder sb = new StringBuilder("Wanted: ");
|
||||
appendFields(sb);
|
||||
sb.append("\nActual: ");
|
||||
context.appendNonKeyFields(sb).append("uid=").append(context.getUid()).append('\n');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private boolean daemonOptsMatch(DaemonInfo daemon) {
|
||||
@@ -54,16 +60,35 @@ public class DaemonCompatibilitySpec {
|
||||
}
|
||||
|
||||
private boolean javaHomeMatches(DaemonInfo daemon) {
|
||||
return Objects.equals(
|
||||
Paths.get(daemon.getJavaHome()).normalize(),
|
||||
Paths.get(javaHome).normalize());
|
||||
return javaHome.equals(Paths.get(daemon.getJavaHome()));
|
||||
}
|
||||
|
||||
StringBuilder appendFields(StringBuilder sb) {
|
||||
return sb.append("javaHome=").append(javaHome)
|
||||
.append(", options=").append(options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DaemonCompatibilitySpec{" +
|
||||
"javaHome='" + javaHome + '\'' +
|
||||
", daemonOpts=" + options +
|
||||
'}';
|
||||
return appendFields(new StringBuilder("DaemonCompatibilitySpec{")).append('}').toString();
|
||||
}
|
||||
|
||||
public static class Result {
|
||||
private final boolean compatible;
|
||||
private final Supplier<String> why;
|
||||
|
||||
Result(boolean compatible, Supplier<String> why) {
|
||||
super();
|
||||
this.compatible = compatible;
|
||||
this.why = why;
|
||||
}
|
||||
|
||||
public boolean isCompatible() {
|
||||
return compatible;
|
||||
}
|
||||
|
||||
public String getWhy() {
|
||||
return why.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -28,6 +28,7 @@ import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.jboss.fuse.mvnd.client.DaemonCompatibilitySpec.Result;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -179,12 +180,12 @@ public class DaemonConnector {
|
||||
private List<DaemonInfo> getCompatibleDaemons(Iterable<DaemonInfo> daemons, DaemonCompatibilitySpec constraint) {
|
||||
List<DaemonInfo> compatibleDaemons = new LinkedList<>();
|
||||
for (DaemonInfo daemon : daemons) {
|
||||
if (constraint.isSatisfiedBy(daemon)) {
|
||||
final Result result = constraint.isSatisfiedBy(daemon);
|
||||
if (result.isCompatible()) {
|
||||
compatibleDaemons.add(daemon);
|
||||
} else {
|
||||
LOGGER.info("Found daemon {} however it does not match the desired criteria.\n"
|
||||
+ constraint.whyUnsatisfied(daemon) + "\n"
|
||||
+ " Looking for a different daemon...", daemon);
|
||||
LOGGER.info("{} daemon {} does not match the desired criteria: "
|
||||
+ result.getWhy(), daemon.getState(), daemon.getUid());
|
||||
}
|
||||
}
|
||||
return compatibleDaemons;
|
||||
|
@@ -18,6 +18,7 @@ package org.jboss.fuse.mvnd.client;
|
||||
import static org.jboss.fuse.mvnd.client.DaemonState.Busy;
|
||||
import static org.jboss.fuse.mvnd.client.DaemonState.Idle;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
public class DaemonInfo {
|
||||
@@ -113,18 +114,20 @@ public class DaemonInfo {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DaemonInfo{" +
|
||||
"uid='" + uid + '\'' +
|
||||
", javaHome='" + javaHome + '\'' +
|
||||
", mavenHome='" + mavenHome + '\'' +
|
||||
", pid=" + pid +
|
||||
", address=" + address +
|
||||
", idleTimeout=" + idleTimeout +
|
||||
", locale='" + locale + '\'' +
|
||||
", options=" + options +
|
||||
", state=" + state +
|
||||
", lastIdle=" + lastIdle +
|
||||
", lastBusy=" + lastBusy +
|
||||
'}';
|
||||
final StringBuilder sb = new StringBuilder("DaemonInfo{uid=").append(uid);
|
||||
appendNonKeyFields(sb);
|
||||
return sb.append('}').toString();
|
||||
}
|
||||
public StringBuilder appendNonKeyFields(StringBuilder sb) {
|
||||
return sb.append("javaHome=").append(javaHome)
|
||||
.append(", options=").append(options)
|
||||
.append(", mavenHome=").append(mavenHome)
|
||||
.append(", pid=").append(pid)
|
||||
.append(", address=").append(address)
|
||||
.append(", idleTimeout=").append(idleTimeout)
|
||||
.append(", locale=").append(locale)
|
||||
.append(", state=").append(state)
|
||||
.append(", lastIdle=").append(lastIdle)
|
||||
.append(", lastBusy=").append(lastBusy);
|
||||
}
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@
|
||||
package org.jboss.fuse.mvnd.daemon;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
@@ -24,6 +25,7 @@ import java.util.function.ToLongFunction;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.jboss.fuse.mvnd.client.DaemonCompatibilitySpec;
|
||||
import org.jboss.fuse.mvnd.client.DaemonCompatibilitySpec.Result;
|
||||
import org.jboss.fuse.mvnd.client.DaemonExpirationStatus;
|
||||
import org.jboss.fuse.mvnd.client.DaemonInfo;
|
||||
import org.jboss.fuse.mvnd.client.DaemonState;
|
||||
@@ -129,9 +131,10 @@ public class DaemonExpiration {
|
||||
static DaemonExpirationStrategy compatible() {
|
||||
return daemon -> {
|
||||
DaemonCompatibilitySpec constraint = new DaemonCompatibilitySpec(
|
||||
daemon.getInfo().getJavaHome(), daemon.getInfo().getOptions());
|
||||
Paths.get(daemon.getInfo().getJavaHome()), daemon.getInfo().getOptions());
|
||||
long compatible = daemon.getRegistry().getAll().stream()
|
||||
.filter(constraint::isSatisfiedBy)
|
||||
.map(constraint::isSatisfiedBy)
|
||||
.filter(Result::isCompatible)
|
||||
.count();
|
||||
if (compatible > 1) {
|
||||
return new DaemonExpirationResult(GRACEFUL_EXPIRE, "other compatible daemons were started");
|
||||
|
@@ -24,6 +24,7 @@ import java.lang.reflect.Field;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
@@ -101,7 +102,8 @@ public class Server implements AutoCloseable, Runnable {
|
||||
|
||||
List<String> opts = new ArrayList<>();
|
||||
long cur = System.currentTimeMillis();
|
||||
info = new DaemonInfo(uid, System.getProperty("java.home"), layout.mavenHome().toString(),
|
||||
final Path javaHome = Paths.get(System.getProperty("java.home")).toRealPath();
|
||||
info = new DaemonInfo(uid, javaHome.toString(), layout.mavenHome().toString(),
|
||||
DaemonRegistry.getProcessId(), socket.socket().getLocalPort(),
|
||||
idleTimeout, Locale.getDefault().toLanguageTag(), opts,
|
||||
Busy, cur, cur);
|
||||
|
Reference in New Issue
Block a user