Also display diagnostics when the client can not connect to the daemon

This commit is contained in:
Guillaume Nodet
2020-10-27 15:27:32 +01:00
parent a2a9d5eedf
commit e24562c343
3 changed files with 17 additions and 15 deletions

View File

@@ -25,10 +25,12 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.jboss.fuse.mvnd.common.DaemonConnection;
import org.jboss.fuse.mvnd.common.DaemonDiagnostics;
import org.jboss.fuse.mvnd.common.DaemonException;
import org.jboss.fuse.mvnd.common.DaemonException.ConnectException;
import org.jboss.fuse.mvnd.common.DaemonException.StaleAddressException;
import org.jboss.fuse.mvnd.common.DaemonInfo;
import org.jboss.fuse.mvnd.common.Layout;
import org.jboss.fuse.mvnd.common.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -52,9 +54,10 @@ public class DaemonClientConnection implements Closeable {
private final Thread receiver;
private final AtomicBoolean running = new AtomicBoolean(true);
private final AtomicReference<Exception> exception = new AtomicReference<>();
private final Layout layout;
public DaemonClientConnection(DaemonConnection connection, DaemonInfo daemon,
StaleAddressDetector staleAddressDetector, boolean newDaemon, int maxKeepAliveMs) {
StaleAddressDetector staleAddressDetector, boolean newDaemon, int maxKeepAliveMs, Layout layout) {
this.connection = connection;
this.daemon = daemon;
this.staleAddressDetector = staleAddressDetector;
@@ -62,6 +65,7 @@ public class DaemonClientConnection implements Closeable {
this.maxKeepAliveMs = maxKeepAliveMs;
this.receiver = new Thread(this::doReceive);
this.receiver.start();
this.layout = layout;
}
public DaemonInfo getDaemon() {
@@ -101,11 +105,12 @@ public class DaemonClientConnection implements Closeable {
+ "ms, daemon may have crashed. You may want to check its status using mvnd --status");
}
} catch (Exception e) {
DaemonDiagnostics diag = new DaemonDiagnostics(daemon.getUid(), layout);
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.", e);
throw new ConnectException("Could not receive a message from the daemon.\n" + diag.describe(), e);
} else if (staleAddressDetector.maybeStaleAddress(e)) {
throw new StaleAddressException("Could not receive a message from the daemon.", e);
throw new StaleAddressException("Could not receive a message from the daemon.\n" + diag.describe(), e);
}
} finally {
hasReceived = true;

View File

@@ -237,7 +237,7 @@ public class DaemonConnector {
throw new DaemonException.InterruptedException(e);
}
} while (System.currentTimeMillis() - start < DEFAULT_CONNECT_TIMEOUT);
DaemonDiagnostics diag = new DaemonDiagnostics(daemon, layout.daemonLog(daemon), layout.daemonOutLog(daemon));
DaemonDiagnostics diag = new DaemonDiagnostics(daemon, layout);
throw new DaemonException.ConnectException("Timeout waiting to connect to the Maven daemon.\n" + diag.describe());
}
@@ -297,7 +297,7 @@ public class DaemonConnector {
try {
return connectToDaemon(daemonInfo, new CleanupOnStaleAddress(daemonInfo), newDaemon);
} catch (DaemonException.ConnectException e) {
DaemonDiagnostics diag = new DaemonDiagnostics(daemon, layout.daemonLog(daemon), layout.daemonOutLog(daemon));
DaemonDiagnostics diag = new DaemonDiagnostics(daemon, layout);
throw new DaemonException.ConnectException("Could not connect to the Maven daemon.\n" + diag.describe(), e);
}
}
@@ -311,7 +311,7 @@ public class DaemonConnector {
try {
int maxKeepAliveMs = layout.getKeepAliveMs() * layout.getMaxLostKeepAlive();
DaemonConnection connection = connect(daemon.getAddress());
return new DaemonClientConnection(connection, daemon, staleAddressDetector, newDaemon, maxKeepAliveMs);
return new DaemonClientConnection(connection, daemon, staleAddressDetector, newDaemon, maxKeepAliveMs, layout);
} catch (DaemonException.ConnectException e) {
staleAddressDetector.maybeStaleAddress(e);
throw e;

View File

@@ -35,29 +35,26 @@ public class DaemonDiagnostics {
private final static int TAIL_SIZE = 20;
private final String uid;
private final Path daemonLog;
private final Path daemonOutLog;
private final Layout layout;
public DaemonDiagnostics(String uid, Path daemonLog, Path daemonOutLog) {
public DaemonDiagnostics(String uid, Layout layout) {
this.uid = uid;
this.daemonLog = daemonLog;
this.daemonOutLog = daemonOutLog;
this.layout = layout;
}
@Override
public String toString() {
return "{"
+ "uid=" + uid
+ ", daemonLog=" + daemonLog
+ ", daemonOutLog=" + daemonOutLog
+ ", layout=" + layout
+ '}';
}
public String describe() {
StringBuilder sb = new StringBuilder();
sb.append("Daemon uid: ").append(uid).append("\n");
tail(sb, "log file", daemonLog);
tail(sb, "output", daemonOutLog);
tail(sb, "log file", layout.daemonLog(uid));
tail(sb, "output", layout.daemonOutLog(uid));
return sb.toString();
}