Skip to content

Commit

Permalink
Better display for IPC sync contexts, apache#424
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 4, 2021
1 parent 6223486 commit 88e7060
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
30 changes: 16 additions & 14 deletions sync/src/main/java/org/mvndaemon/mvnd/sync/IpcClient.java
Expand Up @@ -197,8 +197,7 @@ void receive() {
f.complete(s);
}
} catch (Exception e) {
// ignore
close();
close(e);
}
}

Expand All @@ -224,12 +223,16 @@ List<String> send(List<String> request) throws IOException {
}
}

synchronized void close() {
void close() {
close(new IOException("Closing"));
}

synchronized void close(Throwable e) {
if (socket != null) {
try {
socket.close();
} catch (IOException t) {
// ignore
e.addSuppressed(t);
}
socket = null;
input = null;
Expand All @@ -239,12 +242,11 @@ synchronized void close() {
receiver.interrupt();
try {
receiver.join(1000);
} catch (InterruptedException e) {
// ignore
} catch (InterruptedException t) {
e.addSuppressed(t);
}
}
Throwable t = new IOException("Closing");
responses.values().forEach(f -> f.completeExceptionally(t));
responses.values().forEach(f -> f.completeExceptionally(e));
responses.clear();
}

Expand All @@ -257,8 +259,8 @@ String newContext(boolean shared) {
}
return response.get(1);
} catch (Exception e) {
close();
throw new RuntimeException("Unable to create new context", e);
close(e);
throw new RuntimeException("Unable to create new sync context", e);
}
}

Expand All @@ -273,8 +275,8 @@ void lock(String contextId, Collection<String> keys) {
throw new IOException("Unexpected response: " + response);
}
} catch (Exception e) {
close();
throw new RuntimeException("Unable to perform lock", e);
close(e);
throw new RuntimeException("Unable to perform lock (contextId = " + contextId + ")", e);
}
}

Expand All @@ -285,8 +287,8 @@ void unlock(String contextId) {
throw new IOException("Unexpected response: " + response);
}
} catch (Exception e) {
close();
throw new RuntimeException("Unable to perform lock", e);
close(e);
throw new RuntimeException("Unable to unlock (contextId = " + contextId + ")", e);
}
}

Expand Down
24 changes: 16 additions & 8 deletions sync/src/main/java/org/mvndaemon/mvnd/sync/IpcServer.java
Expand Up @@ -98,13 +98,17 @@ public static void main(String[] args) throws Exception {
}
}

private static void debug(String msg, Object... args) {
//System.out.printf("[ipc] [debug] " + msg + "\n", args);
}

private static void info(String msg, Object... args) {
System.out.printf(msg + "\n", args);
System.out.printf("[ipc] [info] " + msg + "\n", args);
}

private static void error(String msg, Throwable t) {
System.err.println(msg);
t.printStackTrace();
System.out.println("[ipc] [error] " + msg);
t.printStackTrace(System.out);
}

private static void run(Runnable runnable) {
Expand Down Expand Up @@ -133,8 +137,8 @@ public void run() {
}

private void client(Socket socket) {
info("Client connected");
clients.incrementAndGet();
int c = clients.incrementAndGet();
info("New client connected (%d connected)", c);
use();
Map<String, Context> clientContexts = new ConcurrentHashMap<>();
try {
Expand Down Expand Up @@ -163,6 +167,7 @@ private void client(Socket socket) {
contexts.put(context.id, context);
clientContexts.put(context.id, context);
synchronized (output) {
debug("Created context %s", context.id);
output.writeInt(requestId);
output.writeInt(2);
output.writeUTF(RESPONSE_CONTEXT);
Expand All @@ -177,11 +182,12 @@ private void client(Socket socket) {
contextId = request.remove(0);
context = contexts.get(contextId);
if (context == null) {
throw new IOException("Unknown context: " + contextId);
throw new IOException("Unknown context: " + contextId + ". Known contexts = " + contexts.keySet());
}
context.lock(request).thenRun(() -> {
try {
synchronized (output) {
debug("Locking in context %s", context.id);
output.writeInt(requestId);
output.writeInt(1);
output.writeUTF(RESPONSE_ACQUIRE);
Expand All @@ -191,8 +197,9 @@ private void client(Socket socket) {
try {
socket.close();
} catch (IOException ioException) {
// ignore
e.addSuppressed(ioException);
}
error("Error writing lock response", e);
}
});
break;
Expand All @@ -204,10 +211,11 @@ private void client(Socket socket) {
context = contexts.remove(contextId);
clientContexts.remove(contextId);
if (context == null) {
throw new IOException("Unknown context: " + contextId);
throw new IOException("Unknown context: " + contextId + ". Known contexts = " + contexts.keySet());
}
context.unlock();
synchronized (output) {
debug("Closing context %s", context.id);
output.writeInt(requestId);
output.writeInt(1);
output.writeUTF(RESPONSE_CLOSE);
Expand Down
16 changes: 11 additions & 5 deletions sync/src/main/java/org/mvndaemon/mvnd/sync/IpcSyncContext.java
Expand Up @@ -16,7 +16,9 @@
package org.mvndaemon.mvnd.sync;

import java.util.Collection;
import java.util.Objects;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;
import org.eclipse.aether.SyncContext;
import org.eclipse.aether.artifact.Artifact;
Expand All @@ -27,18 +29,22 @@
*/
class IpcSyncContext implements SyncContext {

IpcClient client;
boolean shared;
String contextId;
final IpcClient client;
final boolean shared;
final String contextId;
final AtomicBoolean closed = new AtomicBoolean();

IpcSyncContext(IpcClient client, boolean shared) {
this.client = client;
this.shared = shared;
this.contextId = client.newContext(shared);
this.contextId = Objects.requireNonNull(client.newContext(shared));
}

@Override
public void acquire(Collection<? extends Artifact> artifacts, Collection<? extends Metadata> metadatas) {
if (closed.get()) {
throw new IllegalStateException("Already closed");
}
Collection<String> keys = new TreeSet<>();
stream(artifacts).map(this::getKey).forEach(keys::add);
stream(metadatas).map(this::getKey).forEach(keys::add);
Expand All @@ -50,7 +56,7 @@ public void acquire(Collection<? extends Artifact> artifacts, Collection<? exten

@Override
public void close() {
if (contextId != null) {
if (closed.compareAndSet(false, true)) {
client.unlock(contextId);
}
}
Expand Down

0 comments on commit 88e7060

Please sign in to comment.