Skip to content

Commit

Permalink
Fixup 143f4f1 Display the daemon id and shorten it a bit #314
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Jan 8, 2021
1 parent 143f4f1 commit ddea5d8
Show file tree
Hide file tree
Showing 22 changed files with 114 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public List<Message> receive() throws ConnectException, StaleAddressException {
}
return result;
} catch (Exception e) {
DaemonDiagnostics diag = new DaemonDiagnostics(daemon.getUid(), parameters);
DaemonDiagnostics diag = new DaemonDiagnostics(daemon.getId(), parameters);
LOG.debug("Problem receiving message to the daemon. Performing 'on failure' operation...");
if (!hasReceived && newDaemon) {
throw new ConnectException("Could not receive a message from the daemon.\n" + diag.describe(), e);
Expand Down
52 changes: 26 additions & 26 deletions client/src/main/java/org/mvndaemon/mvnd/client/DaemonConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ public DaemonClientConnection connect(ClientOutput output) {
}

// No compatible daemons available - start a new daemon
String message = handleStopEvents(idleDaemons, busyDaemons);
final String daemonId = newId();
String message = handleStopEvents(daemonId, idleDaemons, busyDaemons);
output.accept(Message.buildStatus(message));
return startDaemon();
return startDaemon(daemonId);
}

private DaemonClientConnection connectNoDaemon() {
Expand All @@ -128,7 +129,7 @@ private DaemonClientConnection connectNoDaemon() {
properties.put(Environment.USER_DIR.getProperty(), parameters.userDir().toString());
properties.put(Environment.USER_HOME.getProperty(), parameters.userHome().toString());
properties.put(Environment.MVND_HOME.getProperty(), parameters.mvndHome().toString());
properties.put(Environment.MVND_UID.getProperty(), daemon);
properties.put(Environment.MVND_ID.getProperty(), daemon);
properties.put(Environment.MVND_DAEMON_STORAGE.getProperty(), parameters.daemonStorage().toString());
properties.put(Environment.MVND_REGISTRY.getProperty(), parameters.registry().toString());
properties.putAll(parameters.getDaemonOptsMap());
Expand Down Expand Up @@ -160,7 +161,7 @@ private DaemonClientConnection connectNoDaemon() {
throw new RuntimeException("Unable to connect to internal daemon", throwable.get());
}

private String handleStopEvents(Collection<DaemonInfo> idleDaemons, Collection<DaemonInfo> busyDaemons) {
private String handleStopEvents(String daemonId, Collection<DaemonInfo> idleDaemons, Collection<DaemonInfo> busyDaemons) {
final List<DaemonStopEvent> stopEvents = registry.getStopEvents();

// Clean up old stop events
Expand All @@ -173,21 +174,21 @@ private String handleStopEvents(Collection<DaemonInfo> idleDaemons, Collection<D

final List<DaemonStopEvent> recentStopEvents = stopEvents.stream()
.filter(e -> e.getTimestamp() >= time)
.collect(Collectors.groupingBy(DaemonStopEvent::getUid,
.collect(Collectors.groupingBy(DaemonStopEvent::getDaemonId,
Collectors.minBy(this::compare)))
.values()
.stream()
.map(Optional::get)
.collect(Collectors.toList());
for (DaemonStopEvent stopEvent : recentStopEvents) {
LOGGER.debug("Previous Daemon ({}) stopped at {} {}",
stopEvent.getUid(), stopEvent.getTimestamp(), stopEvent.getReason());
stopEvent.getDaemonId(), stopEvent.getTimestamp(), stopEvent.getReason());
}

return generate(busyDaemons.size(), idleDaemons.size(), recentStopEvents.size());
return generate(daemonId, busyDaemons.size(), idleDaemons.size(), recentStopEvents.size());
}

public static String generate(final int numBusy, final int numIncompatible, final int numStopped) {
public static String generate(final String daemonId, final int numBusy, final int numIncompatible, final int numStopped) {
final int totalUnavailableDaemons = numBusy + numIncompatible + numStopped;
if (totalUnavailableDaemons > 0) {
final List<String> reasons = new ArrayList<>();
Expand All @@ -200,11 +201,11 @@ public static String generate(final int numBusy, final int numIncompatible, fina
if (numStopped > 0) {
reasons.add(numStopped + " stopped");
}
return "Starting new daemon, "
return "Starting new daemon " + daemonId + ", "
+ String.join(" and ", reasons) + " daemon" + (totalUnavailableDaemons > 1 ? "s" : "")
+ " could not be reused, use --status for details";
} else {
return "Starting new daemon (subsequent builds will be faster)...";
return "Starting new daemon " + daemonId + " (subsequent builds will be faster)...";
}
}

Expand Down Expand Up @@ -254,7 +255,7 @@ private List<DaemonInfo> getCompatibleDaemons(Iterable<DaemonInfo> daemons, Daem
compatibleDaemons.add(daemon);
} else {
LOGGER.debug("{} daemon {} does not match the desired criteria: "
+ result.getWhy(), daemon.getState(), daemon.getUid());
+ result.getWhy(), daemon.getState(), daemon.getId());
}
}
return compatibleDaemons;
Expand All @@ -271,13 +272,12 @@ private DaemonClientConnection findConnection(List<DaemonInfo> compatibleDaemons
return null;
}

public DaemonClientConnection startDaemon() {
final String daemon = newUid();
final Process process = startDaemon(daemon);
LOGGER.debug("Started Maven daemon {}", daemon);
public DaemonClientConnection startDaemon(String daemonId) {
final Process process = startDaemonProcess(daemonId);
LOGGER.debug("Started Maven daemon {}", daemonId);
long start = System.currentTimeMillis();
do {
DaemonClientConnection daemonConnection = connectToDaemonWithId(daemon, true);
DaemonClientConnection daemonConnection = connectToDaemonWithId(daemonId, true);
if (daemonConnection != null) {
return daemonConnection;
}
Expand All @@ -287,15 +287,15 @@ public DaemonClientConnection startDaemon() {
throw new DaemonException.InterruptedException(e);
}
} while (process.isAlive() && System.currentTimeMillis() - start < DEFAULT_CONNECT_TIMEOUT);
DaemonDiagnostics diag = new DaemonDiagnostics(daemon, parameters);
DaemonDiagnostics diag = new DaemonDiagnostics(daemonId, parameters);
throw new DaemonException.ConnectException("Timeout waiting to connect to the Maven daemon.\n" + diag.describe());
}

static String newUid() {
static String newId() {
return String.format("%08x", new Random().nextInt());
}

private Process startDaemon(String uid) {
private Process startDaemonProcess(String daemonId) {
final Path mvndHome = parameters.mvndHome();
final Path workingDir = parameters.userDir();
String command = "";
Expand Down Expand Up @@ -353,15 +353,15 @@ private Process startDaemon(String uid) {
Environment.MVND_JAVA_HOME.addCommandLineOption(args, parameters.javaHome().toString());
Environment.LOGBACK_CONFIGURATION_FILE
.addCommandLineOption(args, parameters.logbackConfigurationPath().toString());
Environment.MVND_UID.addCommandLineOption(args, uid);
Environment.MVND_ID.addCommandLineOption(args, daemonId);
Environment.MVND_DAEMON_STORAGE.addCommandLineOption(args, parameters.daemonStorage().toString());
Environment.MVND_REGISTRY.addCommandLineOption(args, parameters.registry().toString());
parameters.discriminatingCommandLineOptions(args);
args.add(MavenDaemon.class.getName());
command = String.join(" ", args);

LOGGER.debug("Starting daemon process: uid = {}, workingDir = {}, daemonArgs: {}", uid, workingDir, command);
ProcessBuilder.Redirect redirect = ProcessBuilder.Redirect.appendTo(parameters.daemonOutLog(uid).toFile());
LOGGER.debug("Starting daemon process: id = {}, workingDir = {}, daemonArgs: {}", daemonId, workingDir, command);
ProcessBuilder.Redirect redirect = ProcessBuilder.Redirect.appendTo(parameters.daemonOutLog(daemonId).toFile());
Process process = new ProcessBuilder()
.directory(workingDir.toFile())
.command(args)
Expand All @@ -371,8 +371,8 @@ private Process startDaemon(String uid) {
return process;
} catch (Exception e) {
throw new DaemonException.StartException(
String.format("Error starting daemon: uid = %s, workingDir = %s, daemonArgs: %s",
uid, workingDir, command),
String.format("Error starting daemon: id = %s, workingDir = %s, daemonArgs: %s",
daemonId, workingDir, command),
e);
}
}
Expand Down Expand Up @@ -419,10 +419,10 @@ public CleanupOnStaleAddress(DaemonInfo daemon) {
public boolean maybeStaleAddress(Exception failure) {
LOGGER.debug("Removing daemon from the registry due to communication failure. Daemon information: {}", daemon);
final long timestamp = System.currentTimeMillis();
final DaemonStopEvent stopEvent = new DaemonStopEvent(daemon.getUid(), timestamp, null,
final DaemonStopEvent stopEvent = new DaemonStopEvent(daemon.getId(), timestamp, null,
"by user or operating system");
registry.storeStopEvent(stopEvent);
registry.remove(daemon.getUid());
registry.remove(daemon.getId());
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ public class DaemonDiagnostics {

private final static int TAIL_SIZE = 200;

private final String uid;
private final String id;
private final DaemonParameters parameters;

public DaemonDiagnostics(String uid, DaemonParameters parameters) {
this.uid = uid;
public DaemonDiagnostics(String id, DaemonParameters parameters) {
this.id = id;
this.parameters = parameters;
}

@Override
public String toString() {
return "{"
+ "uid=" + uid
+ "id=" + id
+ ", parameters=" + parameters
+ '}';
}

public String describe() {
StringBuilder sb = new StringBuilder();
sb.append("Daemon uid: ").append(uid).append("\n");
tail(sb, "log file", parameters.daemonLog(uid));
tail(sb, "output", parameters.daemonOutLog(uid));
sb.append("Daemon id: ").append(id).append("\n");
tail(sb, "log file", parameters.daemonLog(id));
tail(sb, "output", parameters.daemonOutLog(id));
return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,16 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {

try (DaemonRegistry registry = new DaemonRegistry(parameters.registry())) {
if (Environment.STATUS.removeCommandLineOption(args) != null) {
final String template = " %36s %7s %5s %7s %5s %23s %s";
final String template = "%8s %7s %5s %7s %5s %23s %s";
output.accept(Message.log(String.format(template,
"UUID", "PID", "Port", "Status", "RSS", "Last activity", "Java home")));
"ID", "PID", "Port", "Status", "RSS", "Last activity", "Java home")));
for (DaemonInfo d : registry.getAll()) {
if (ProcessHandle.of(d.getPid()).isEmpty()) {
/* The process does not exist anymore - remove it from the registry */
registry.remove(d.getUid());
registry.remove(d.getId());
} else {
output.accept(Message.log(String.format(template,
d.getUid(), d.getPid(), d.getAddress(), d.getState(),
d.getId(), d.getPid(), d.getAddress(), d.getState(),
OsUtils.kbTohumanReadable(OsUtils.findProcessRssInKb(d.getPid())),
LocalDateTime.ofInstant(
Instant.ofEpochMilli(Math.max(d.getLastIdle(), d.getLastBusy())),
Expand All @@ -197,9 +197,9 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
try {
ProcessHandle.of(di.getPid()).ifPresent(ProcessHandle::destroyForcibly);
} catch (Exception t) {
System.out.println("Daemon " + di.getUid() + ": " + t);
System.out.println("Daemon " + di.getId() + ": " + t);
} finally {
registry.remove(di.getUid());
registry.remove(di.getId());
}
}
}
Expand Down Expand Up @@ -229,7 +229,7 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {

final DaemonConnector connector = new DaemonConnector(parameters, registry);
try (DaemonClientConnection daemon = connector.connect(output)) {
output.setDaemonId(daemon.getDaemon().getUid());
output.setDaemonId(daemon.getDaemon().getId());
output.setDaemonDispatch(daemon::dispatch);
output.setDaemonReceive(daemon::enqueue);

Expand All @@ -239,7 +239,8 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
parameters.multiModuleProjectDirectory().toString(),
System.getenv()));

output.accept(Message.buildStatus("Daemon started, scanning for projects..."));
output.accept(Message
.buildStatus("Connected to daemon " + daemon.getDaemon().getId() + ", scanning for projects..."));

// We've sent the request, so it gives us a bit of time to purge the logs
AtomicReference<String> purgeMessage = new AtomicReference<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
public class DaemonConnectorTest {

@Test
public void testUid() {
String uid = DaemonConnector.newUid();
assertNotNull(uid);
assertEquals(8, uid.length());
public void newId() {
String id = DaemonConnector.newId();
assertNotNull(id);
assertEquals(8, id.length());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ 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');
context.appendNonKeyFields(sb).append("id=").append(context.getId()).append('\n');
return sb.toString();
}

Expand Down
14 changes: 7 additions & 7 deletions common/src/main/java/org/mvndaemon/mvnd/common/DaemonInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
public class DaemonInfo {

private final String uid;
private final String id;
private final String javaHome;
private final String mvndHome;
private final int pid;
Expand All @@ -37,11 +37,11 @@ public class DaemonInfo {
private final long lastIdle;
private final long lastBusy;

public DaemonInfo(String uid, String javaHome, String mavenHome,
public DaemonInfo(String id, String javaHome, String mavenHome,
int pid, int address,
String locale, List<String> options,
DaemonState state, long lastIdle, long lastBusy) {
this.uid = uid;
this.id = id;
this.javaHome = javaHome;
this.mvndHome = mavenHome;
this.pid = pid;
Expand All @@ -53,8 +53,8 @@ public DaemonInfo(String uid, String javaHome, String mavenHome,
this.lastBusy = lastBusy;
}

public String getUid() {
return uid;
public String getId() {
return id;
}

public String getJavaHome() {
Expand Down Expand Up @@ -105,13 +105,13 @@ public DaemonInfo withState(DaemonState state) {
li = lastIdle;
lb = lastBusy;
}
return new DaemonInfo(uid, javaHome, mvndHome, pid, address,
return new DaemonInfo(id, javaHome, mvndHome, pid, address,
locale, options, state, li, lb);
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("DaemonInfo{uid=").append(uid);
final StringBuilder sb = new StringBuilder("DaemonInfo{id=").append(id);
appendNonKeyFields(sb);
return sb.append('}').toString();
}
Expand Down
Loading

0 comments on commit ddea5d8

Please sign in to comment.