Skip to content

Commit

Permalink
Display the daemon id and shorten it a bit, fixes apache#314
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jan 8, 2021
1 parent b958a91 commit 5eaf924
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.UUID;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -272,7 +272,7 @@ private DaemonClientConnection findConnection(List<DaemonInfo> compatibleDaemons
}

public DaemonClientConnection startDaemon() {
final String daemon = UUID.randomUUID().toString();
final String daemon = newUid();
final Process process = startDaemon(daemon);
LOGGER.debug("Started Maven daemon {}", daemon);
long start = System.currentTimeMillis();
Expand All @@ -291,6 +291,10 @@ public DaemonClientConnection startDaemon() {
throw new DaemonException.ConnectException("Timeout waiting to connect to the Maven daemon.\n" + diag.describe());
}

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

private Process startDaemon(String uid) {
final Path mvndHome = parameters.mvndHome();
final Path workingDir = parameters.userDir();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed 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.client;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class DaemonConnectorTest {

@Test
public void testUid() {
String uid = DaemonConnector.newUid();
assertNotNull(uid);
assertEquals(8, uid.length());
}
}
12 changes: 10 additions & 2 deletions common/src/main/java/org/mvndaemon/mvnd/common/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -518,21 +518,24 @@ public void write(DataOutputStream output) throws IOException {
public static class BuildStarted extends Message {

final String projectId;
final String daemonId;
final int projectCount;
final int maxThreads;
final int artifactIdDisplayLength;

public static BuildStarted read(DataInputStream input) throws IOException {
final String projectId = readUTF(input);
final String daemonId = readUTF(input);
final int projectCount = input.readInt();
final int maxThreads = input.readInt();
final int artifactIdDisplayLength = input.readInt();
return new BuildStarted(projectId, projectCount, maxThreads, artifactIdDisplayLength);
return new BuildStarted(projectId, daemonId, projectCount, maxThreads, artifactIdDisplayLength);
}

public BuildStarted(String projectId, int projectCount, int maxThreads, int artifactIdDisplayLength) {
public BuildStarted(String projectId, String daemonId, int projectCount, int maxThreads, int artifactIdDisplayLength) {
super(BUILD_STARTED);
this.projectId = projectId;
this.daemonId = daemonId;
this.projectCount = projectCount;
this.maxThreads = maxThreads;
this.artifactIdDisplayLength = artifactIdDisplayLength;
Expand All @@ -542,6 +545,10 @@ public String getProjectId() {
return projectId;
}

public String getDaemonId() {
return daemonId;
}

public int getProjectCount() {
return projectCount;
}
Expand All @@ -565,6 +572,7 @@ public String toString() {
public void write(DataOutputStream output) throws IOException {
super.write(output);
writeUTF(output, projectId);
writeUTF(output, daemonId);
output.writeInt(projectCount);
output.writeInt(maxThreads);
output.writeInt(artifactIdDisplayLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class TerminalOutput implements ClientOutput {
* Therefore, these fields do not need to be volatile
*/
private String name;
private String daemonId;
private int totalProjects;
/** String format for formatting the number of projects done with padding based on {@link #totalProjects} */
private String projectsDoneFomat;
Expand Down Expand Up @@ -171,6 +172,7 @@ private boolean doAccept(Message entry) {
case Message.BUILD_STARTED: {
BuildStarted bs = (BuildStarted) entry;
this.name = bs.getProjectId();
this.daemonId = bs.getDaemonId();
this.totalProjects = bs.getProjectCount();
final int totalProjectsDigits = (int) (Math.log10(totalProjects) + 1);
this.projectsDoneFomat = "%" + totalProjectsDigits + "d";
Expand Down Expand Up @@ -526,6 +528,13 @@ private void addStatusLine(final List<AttributedString> lines, int dispLines, fi
.append('%')
.style(AttributedStyle.DEFAULT);

/* DaemonId */
asb
.append(" daemon: ")
.style(AttributedStyle.BOLD)
.append(daemonId)
.style(AttributedStyle.DEFAULT);

} else if (buildStatus != null) {
asb
.style(AttributedStyle.BOLD)
Expand Down
3 changes: 3 additions & 0 deletions daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,9 @@ private int execute(CliRequest cliRequest)

eventSpyDispatcher.onEvent(request);

slf4jLogger.info(buffer().a("Processing build on daemon ")
.strong(Environment.MVND_UID.asString()).toString());

MavenExecutionResult result = maven.execute(request);

eventSpyDispatcher.onEvent(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.project.MavenProject;
import org.mvndaemon.mvnd.builder.DependencyGraph;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.Message;
import org.mvndaemon.mvnd.common.Message.BuildException;
import org.mvndaemon.mvnd.common.Message.BuildStarted;
Expand All @@ -51,7 +52,8 @@ public void sessionStarted(ExecutionEvent event) {
final int maxThreads = degreeOfConcurrency == 1 ? 1 : dependencyGraph.computeMaxWidth(degreeOfConcurrency, 1000);
final List<MavenProject> projects = session.getProjects();
final int _90thArtifactIdLengthPercentile = artifactIdLength90thPercentile(projects);
queue.add(new BuildStarted(getCurrentProject(session).getArtifactId(), projects.size(), maxThreads,
queue.add(new BuildStarted(getCurrentProject(session).getArtifactId(),
Environment.MVND_UID.asString(), projects.size(), maxThreads,
_90thArtifactIdLengthPercentile));
}

Expand Down

0 comments on commit 5eaf924

Please sign in to comment.