Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix display showing more projects than the ones actually active #367

Merged
merged 1 commit into from Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions common/src/main/java/org/mvndaemon/mvnd/common/Message.java
Expand Up @@ -23,6 +23,7 @@
import java.io.StringWriter;
import java.io.UTFDataFormatException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -108,6 +109,51 @@ public static Message read(DataInputStream input) throws IOException {

final long timestamp = System.nanoTime();

public static Comparator<Message> getMessageComparator() {
return Comparator.comparingInt(Message::getClassOrder).thenComparingLong(Message::timestamp);
}

public static int getClassOrder(Message m) {
switch (m.getType()) {
case KEEP_ALIVE:
case BUILD_REQUEST:
return 0;
case BUILD_STARTED:
return 1;
case PROMPT:
case PROMPT_RESPONSE:
case DISPLAY:
return 2;
case PROJECT_STARTED:
return 3;
case MOJO_STARTED:
return 4;
case TRANSFER_INITIATED:
case TRANSFER_STARTED:
return 40;
case TRANSFER_PROGRESSED:
return 41;
case TRANSFER_CORRUPTED:
case TRANSFER_SUCCEEDED:
case TRANSFER_FAILED:
return 42;
case PROJECT_LOG_MESSAGE:
return 50;
case BUILD_LOG_MESSAGE:
return 51;
case PROJECT_STOPPED:
return 95;
case BUILD_FINISHED:
return 96;
case BUILD_EXCEPTION:
return 97;
case STOP:
return 99;
default:
throw new IllegalStateException("Unexpected message type " + m.getType() + ": " + m);
}
}

public long timestamp() {
return timestamp;
}
Expand Down
Expand Up @@ -355,8 +355,10 @@ private boolean doAccept(Message entry) {
}
case Message.PROJECT_LOG_MESSAGE: {
final ProjectEvent bm = (ProjectEvent) entry;
final Project prj = projects.computeIfAbsent(bm.getProjectId(), Project::new);
if (noBuffering || dumb) {
final Project prj = projects.get(bm.getProjectId());
if (prj == null) {
log.accept(bm.getMessage());
} else if (noBuffering || dumb) {
String msg;
if (maxThreads > 1) {
msg = String.format("[%s] %s", bm.getProjectId(), bm.getMessage());
Expand Down
46 changes: 1 addition & 45 deletions daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java
Expand Up @@ -24,7 +24,6 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -429,8 +428,7 @@ private void cancelNow() {

private void handle(DaemonConnection connection, BuildRequest buildRequest) {
updateState(Busy);
final BlockingQueue<Message> sendQueue = new PriorityBlockingQueue<>(64,
Comparator.comparingInt(this::getClassOrder).thenComparingLong(Message::timestamp));
Copy link
Contributor

@ppalaga ppalaga Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you really wanted to remove getClassOrder from the comparator, then the getClassOrder() method can be removed too.
And we perhaps also do not need a PriorityBlockingQueue anymore.

final BlockingQueue<Message> sendQueue = new PriorityBlockingQueue<>(64, Message.getMessageComparator());
final BlockingQueue<Message> recvQueue = new LinkedBlockingDeque<>();
final BuildEventListener buildEventListener = new ClientDispatcher(sendQueue);
try (ProjectBuildLogAppender logAppender = new ProjectBuildLogAppender(buildEventListener)) {
Expand Down Expand Up @@ -558,48 +556,6 @@ public <T extends Message> T request(Message request, Class<T> responseType, Pre
}
}

int getClassOrder(Message m) {
switch (m.getType()) {
case Message.BUILD_REQUEST:
return 0;
case Message.BUILD_STARTED:
return 1;
case Message.PROMPT:
case Message.PROMPT_RESPONSE:
case Message.DISPLAY:
return 2;
case Message.PROJECT_STARTED:
return 3;
case Message.MOJO_STARTED:
return 4;
case Message.TRANSFER_INITIATED:
case Message.TRANSFER_STARTED:
return 40;
case Message.TRANSFER_PROGRESSED:
return 41;
case Message.TRANSFER_CORRUPTED:
case Message.TRANSFER_SUCCEEDED:
case Message.TRANSFER_FAILED:
return 42;
case Message.PROJECT_LOG_MESSAGE:
return 50;
case Message.BUILD_LOG_MESSAGE:
return 51;
case Message.PROJECT_STOPPED:
return 95;
case Message.BUILD_FINISHED:
return 96;
case Message.BUILD_EXCEPTION:
return 97;
case Message.STOP:
return 99;
case Message.KEEP_ALIVE:
return 100;
default:
throw new IllegalStateException("Unexpected message type " + m.getType() + ": " + m);
}
}

private void updateState(DaemonState state) {
if (getState() != state) {
LOGGER.info("Updating state to: " + state);
Expand Down
42 changes: 42 additions & 0 deletions daemon/src/test/java/org/mvndaemon/mvnd/daemon/ServerTest.java
@@ -0,0 +1,42 @@
/*
* Copyright 2021 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.daemon;

import java.util.Arrays;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

import org.junit.jupiter.api.Test;
import org.mvndaemon.mvnd.common.Message;

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

public class ServerTest {

@Test
void testMessageOrdering() {
BlockingQueue<Message> messages = new PriorityBlockingQueue<>(64, Message.getMessageComparator());
messages.addAll(Arrays.asList(
Message.projectStopped("projectId"),
Message.projectStarted("projectId"),
Message.log("projectId", "message"))
);

assertEquals(Message.PROJECT_STARTED, messages.remove().getType());
assertEquals(Message.PROJECT_LOG_MESSAGE, messages.remove().getType());
assertEquals(Message.PROJECT_STOPPED, messages.remove().getType());
}
}