Skip to content

Commit

Permalink
Separate BuildStarted message to avoid serializing via Propertries.[l…
Browse files Browse the repository at this point in the history
…oad|write]()
  • Loading branch information
ppalaga committed Nov 1, 2020
1 parent 8452440 commit 96b8730
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 37 deletions.
20 changes: 4 additions & 16 deletions client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
*/
package org.jboss.fuse.mvnd.client;

import java.io.StringReader;
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.List;
import java.util.Properties;
import java.util.function.Supplier;
import org.fusesource.jansi.Ansi;
import org.jboss.fuse.mvnd.common.BuildProperties;
Expand All @@ -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;
Expand Down Expand Up @@ -219,22 +218,12 @@ public ExecutionResult execute(ClientOutput output, List<String> 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:
Expand All @@ -254,7 +243,6 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
}
}
}

}

static void setDefaultArgs(List<String> args, ClientLayout layout) {
Expand Down
54 changes: 53 additions & 1 deletion common/src/main/java/org/jboss/fuse/mvnd/common/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 4 additions & 20 deletions daemon/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -452,16 +451,14 @@ 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;
} else if (m instanceof BuildEvent && ((BuildEvent) m).getType() == Type.MojoStarted) {
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) {
Expand Down Expand Up @@ -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
Expand All @@ -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));
Expand Down

0 comments on commit 96b8730

Please sign in to comment.