Madan Jampani
Committed by Gerrit Code Review

use maven shade plugin version defined in root pom + Minor fixes to CopycatTrans…

…port based on review comments

Change-Id: Iac2bd2e7eca99208930eb319e2f3996fbf043f88
......@@ -62,7 +62,7 @@ public class CopycatTransportConnection implements Connection {
static final byte FAILURE = 0x04;
private final long connectionId;
private CopycatTransport.Mode mode;
private final CopycatTransport.Mode mode;
private final Address remoteAddress;
private final MessagingService messagingService;
private final String outboundMessageSubject;
......@@ -73,6 +73,7 @@ public class CopycatTransportConnection implements Connection {
private final AtomicInteger sendFailures = new AtomicInteger(0);
private final AtomicInteger messagesReceived = new AtomicInteger(0);
private final AtomicInteger receiveFailures = new AtomicInteger(0);
private final Map<Address, Endpoint> endpointLookupCache = Maps.newConcurrentMap();
CopycatTransportConnection(long connectionId,
CopycatTransport.Mode mode,
......@@ -206,7 +207,6 @@ public class CopycatTransportConnection implements Connection {
@Override
public CompletableFuture<Void> close() {
// TODO: need to unregister message handler
closeListeners.forEach(listener -> listener.accept(this));
if (mode == CopycatTransport.Mode.CLIENT) {
messagingService.unregisterHandler(inboundMessageSubject);
......@@ -240,12 +240,14 @@ public class CopycatTransportConnection implements Connection {
}
private Endpoint toEndpoint(Address address) {
try {
return new Endpoint(IpAddress.valueOf(InetAddress.getByName(address.host())), address.port());
} catch (UnknownHostException e) {
Throwables.propagate(e);
return null;
}
return endpointLookupCache.computeIfAbsent(address, a -> {
try {
return new Endpoint(IpAddress.valueOf(InetAddress.getByName(a.host())), a.port());
} catch (UnknownHostException e) {
Throwables.propagate(e);
return null;
}
});
}
@SuppressWarnings("rawtypes")
......
......@@ -45,7 +45,7 @@ import io.atomix.catalyst.util.concurrent.ThreadContext;
public class CopycatTransportServer implements Server {
private final AtomicBoolean listening = new AtomicBoolean(false);
private CompletableFuture<Void> listenFuture;
private CompletableFuture<Void> listenFuture = new CompletableFuture<>();
private final String clusterName;
private final MessagingService messagingService;
private final String messageSubject;
......@@ -59,20 +59,14 @@ public class CopycatTransportServer implements Server {
@Override
public CompletableFuture<Void> listen(Address address, Consumer<Connection> listener) {
if (listening.get()) {
return CompletableFuture.completedFuture(null);
}
ThreadContext context = ThreadContext.currentContextOrThrow();
synchronized (this) {
if (listenFuture == null) {
listenFuture = new CompletableFuture<>();
listen(address, listener, context);
}
if (listening.compareAndSet(false, true)) {
ThreadContext context = ThreadContext.currentContextOrThrow();
listen(address, listener, context);
}
return listenFuture;
}
public void listen(Address address, Consumer<Connection> listener, ThreadContext context) {
private void listen(Address address, Consumer<Connection> listener, ThreadContext context) {
messagingService.registerHandler(messageSubject, (sender, payload) -> {
try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(payload))) {
long connectionId = input.readLong();
......@@ -101,7 +95,6 @@ public class CopycatTransportServer implements Server {
return Tools.exceptionalFuture(e);
}
});
listening.set(true);
context.execute(() -> {
listenFuture.complete(null);
});
......
......@@ -69,7 +69,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<createSourcesJar>true</createSourcesJar>
......