From 669432fbab844fca69ddfc3f1ce92fba1065cb8d Mon Sep 17 00:00:00 2001 From: Peter Palaga Date: Sun, 1 Nov 2020 22:26:26 +0100 Subject: [PATCH] Separate BuildStarted message to avoid serializing via Propertries.[load|write]() --- .../jboss/fuse/mvnd/client/DefaultClient.java | 20 ++----- .../org/jboss/fuse/mvnd/common/Message.java | 54 ++++++++++++++++++- .../org/jboss/fuse/mvnd/daemon/Server.java | 24 ++------- 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java b/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java index 00b64d7d1..410bd895f 100644 --- a/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java +++ b/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java @@ -15,7 +15,6 @@ */ package org.jboss.fuse.mvnd.client; -import java.io.StringReader; import java.nio.file.Path; import java.nio.file.Paths; import java.time.Instant; @@ -23,7 +22,6 @@ import java.time.ZoneId; import java.util.ArrayList; import java.util.List; -import java.util.Properties; import java.util.function.Supplier; import org.fusesource.jansi.Ansi; import org.jboss.fuse.mvnd.common.BuildProperties; @@ -35,6 +33,7 @@ import org.jboss.fuse.mvnd.common.Message.BuildEvent; import org.jboss.fuse.mvnd.common.Message.BuildException; import org.jboss.fuse.mvnd.common.Message.BuildMessage; +import org.jboss.fuse.mvnd.common.Message.BuildStarted; import org.jboss.fuse.mvnd.common.OsUtils; import org.jboss.fuse.mvnd.common.logging.ClientOutput; import org.jboss.fuse.mvnd.common.logging.TerminalOutput; @@ -219,22 +218,12 @@ public ExecutionResult execute(ClientOutput output, List argv) { output.error(e.getMessage(), e.getClassName(), e.getStackTrace()); return new DefaultResult(argv, new Exception(e.getClassName() + ": " + e.getMessage() + "\n" + e.getStackTrace())); + } else if (m instanceof BuildStarted) { + final BuildStarted bs = (BuildStarted) m; + output.startBuild(bs.getProjectId(), bs.getProjectCount(), bs.getMaxThreads()); } else if (m instanceof BuildEvent) { BuildEvent be = (BuildEvent) m; switch (be.getType()) { - case BuildStarted: - int projects = 0; - int cores = 0; - Properties props = new Properties(); - try { - props.load(new StringReader(be.getDisplay())); - projects = Integer.parseInt(props.getProperty("projects")); - cores = Integer.parseInt(props.getProperty("cores")); - } catch (Exception e) { - // Ignore - } - output.startBuild(be.getProjectId(), projects, cores); - break; case BuildStopped: return new DefaultResult(argv, null); case ProjectStarted: @@ -254,7 +243,6 @@ public ExecutionResult execute(ClientOutput output, List argv) { } } } - } static void setDefaultArgs(List args, ClientLayout layout) { diff --git a/common/src/main/java/org/jboss/fuse/mvnd/common/Message.java b/common/src/main/java/org/jboss/fuse/mvnd/common/Message.java index e5be9551c..f842428ed 100644 --- a/common/src/main/java/org/jboss/fuse/mvnd/common/Message.java +++ b/common/src/main/java/org/jboss/fuse/mvnd/common/Message.java @@ -34,6 +34,7 @@ public abstract class Message { static final int BUILD_EXCEPTION = 3; static final int KEEP_ALIVE = 4; static final int STOP = 5; + static final int BUILD_STARTED = 6; public static final SimpleMessage KEEP_ALIVE_SINGLETON = new SimpleMessage(Message.KEEP_ALIVE, "KEEP_ALIVE"); public static final SimpleMessage STOP_SINGLETON = new SimpleMessage(Message.STOP, "STOP"); @@ -46,6 +47,8 @@ public static Message read(DataInputStream input) throws IOException { switch (type) { case BUILD_REQUEST: return BuildRequest.read(input); + case BUILD_STARTED: + return BuildStarted.read(input); case BUILD_EVENT: return BuildEvent.read(input); case BUILD_MESSAGE: @@ -315,7 +318,7 @@ public void write(DataOutputStream output) throws IOException { public static class BuildEvent extends Message { public enum Type { - BuildStarted, BuildStopped, ProjectStarted, ProjectStopped, MojoStarted, MojoStopped + BuildStopped, ProjectStarted, ProjectStopped, MojoStarted } final Type type; @@ -365,6 +368,55 @@ public void write(DataOutputStream output) throws IOException { } } + public static class BuildStarted extends Message { + + final String projectId; + final int projectCount; + final int maxThreads; + + public static BuildStarted read(DataInputStream input) throws IOException { + final String projectId = readUTF(input); + final int projectCount = input.readInt(); + final int maxThreads = input.readInt(); + return new BuildStarted(projectId, projectCount, maxThreads); + } + + public BuildStarted(String projectId, int projectCount, int maxThreads) { + this.projectId = projectId; + this.projectCount = projectCount; + this.maxThreads = maxThreads; + } + + public String getProjectId() { + return projectId; + } + + public int getProjectCount() { + return projectCount; + } + + public int getMaxThreads() { + return maxThreads; + } + + @Override + public String toString() { + return "BuildEvent{" + + "projectId='" + projectId + '\'' + + ", projectCount=" + projectCount + + ", maxThreads='" + maxThreads + '\'' + + '}'; + } + + @Override + public void write(DataOutputStream output) throws IOException { + output.write(BUILD_STARTED); + writeUTF(output, projectId); + output.writeInt(projectCount); + output.writeInt(maxThreads); + } + } + public static class BuildMessage extends Message { final String projectId; final String message; diff --git a/daemon/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java b/daemon/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java index 0e54fc8db..6682f09c5 100644 --- a/daemon/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java +++ b/daemon/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java @@ -16,7 +16,6 @@ package org.jboss.fuse.mvnd.daemon; import java.io.IOException; -import java.io.StringWriter; import java.lang.reflect.Field; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -29,7 +28,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Properties; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executors; import java.util.concurrent.PriorityBlockingQueue; @@ -57,6 +55,7 @@ import org.jboss.fuse.mvnd.common.Message.BuildException; import org.jboss.fuse.mvnd.common.Message.BuildMessage; import org.jboss.fuse.mvnd.common.Message.BuildRequest; +import org.jboss.fuse.mvnd.common.Message.BuildStarted; import org.jboss.fuse.mvnd.daemon.DaemonExpiration.DaemonExpirationResult; import org.jboss.fuse.mvnd.daemon.DaemonExpiration.DaemonExpirationStrategy; import org.jboss.fuse.mvnd.logging.smart.AbstractLoggingSpy; @@ -452,7 +451,7 @@ private void handle(DaemonConnection connection, BuildRequest buildRequest) { int getClassOrder(Message m) { if (m instanceof BuildRequest) { return 0; - } else if (m instanceof BuildEvent && ((BuildEvent) m).getType() == Type.BuildStarted) { + } else if (m instanceof BuildStarted) { return 1; } else if (m instanceof BuildEvent && ((BuildEvent) m).getType() == Type.ProjectStarted) { return 2; @@ -460,8 +459,6 @@ int getClassOrder(Message m) { return 3; } else if (m instanceof BuildMessage) { return 50; - } else if (m instanceof BuildEvent && ((BuildEvent) m).getType() == Type.MojoStopped) { - return 94; } else if (m instanceof BuildEvent && ((BuildEvent) m).getType() == Type.ProjectStopped) { return 95; } else if (m instanceof BuildEvent && ((BuildEvent) m).getType() == Type.BuildStopped) { @@ -542,16 +539,8 @@ public void fail(Throwable t) throws Exception { @Override protected void onStartSession(MavenSession session) { - Properties props = new Properties(); - props.setProperty("projects", Integer.toString(session.getAllProjects().size())); - props.setProperty("cores", Integer.toString(session.getRequest().getDegreeOfConcurrency())); - StringWriter sw = new StringWriter(); - try { - props.store(sw, null); - } catch (IOException e) { - throw new IllegalStateException(e); - } - queue.add(new BuildEvent(Type.BuildStarted, session.getTopLevelProject().getName(), sw.toString())); + queue.add(new BuildStarted(session.getTopLevelProject().getName(), session.getAllProjects().size(), + session.getRequest().getDegreeOfConcurrency())); } @Override @@ -569,11 +558,6 @@ protected void onStartMojo(String projectId, String display) { sendEvent(Type.MojoStarted, projectId, display); } - @Override - protected void onStopMojo(String projectId, String display) { - sendEvent(Type.MojoStopped, projectId, display); - } - @Override protected void onProjectLog(String projectId, String message) { queue.add(new BuildMessage(projectId, message));