Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.io.ByteArrayInputStream; |
| 19 | import java.io.ByteArrayOutputStream; |
| 20 | import java.io.DataInputStream; |
| 21 | import java.io.DataOutputStream; |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 24 | import java.util.Map; |
| 25 | import java.util.Objects; |
| 26 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 27 | import java.util.function.Consumer; |
| 28 | |
Madan Jampani | 6f74371 | 2016-03-26 11:20:25 -0700 | [diff] [blame] | 29 | |
| 30 | |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 31 | import org.apache.commons.io.IOUtils; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 32 | import org.onlab.util.Tools; |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 33 | import org.onosproject.cluster.PartitionId; |
Madan Jampani | 6f74371 | 2016-03-26 11:20:25 -0700 | [diff] [blame] | 34 | import org.onosproject.store.cluster.messaging.MessagingException; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 35 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 36 | |
| 37 | import com.google.common.base.MoreObjects; |
| 38 | import com.google.common.base.Throwables; |
| 39 | import com.google.common.collect.Maps; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | 0da01a4 | 2016-03-18 14:33:18 -0700 | [diff] [blame] | 42 | import io.atomix.catalyst.serializer.SerializationException; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 43 | import io.atomix.catalyst.transport.Address; |
| 44 | import io.atomix.catalyst.transport.Connection; |
| 45 | import io.atomix.catalyst.transport.MessageHandler; |
Madan Jampani | 6f74371 | 2016-03-26 11:20:25 -0700 | [diff] [blame] | 46 | import io.atomix.catalyst.transport.TransportException; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 47 | import io.atomix.catalyst.util.Assert; |
| 48 | import io.atomix.catalyst.util.Listener; |
| 49 | import io.atomix.catalyst.util.Listeners; |
| 50 | import io.atomix.catalyst.util.ReferenceCounted; |
| 51 | import io.atomix.catalyst.util.concurrent.ThreadContext; |
| 52 | |
| 53 | /** |
| 54 | * {@link Connection} implementation for CopycatTransport. |
| 55 | */ |
| 56 | public class CopycatTransportConnection implements Connection { |
| 57 | |
| 58 | private final Listeners<Throwable> exceptionListeners = new Listeners<>(); |
| 59 | private final Listeners<Connection> closeListeners = new Listeners<>(); |
| 60 | |
| 61 | static final byte SUCCESS = 0x03; |
| 62 | static final byte FAILURE = 0x04; |
| 63 | |
| 64 | private final long connectionId; |
Madan Jampani | 71d13e1 | 2016-01-13 17:14:35 -0800 | [diff] [blame] | 65 | private final CopycatTransport.Mode mode; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 66 | private final Address remoteAddress; |
| 67 | private final MessagingService messagingService; |
| 68 | private final String outboundMessageSubject; |
| 69 | private final String inboundMessageSubject; |
| 70 | private final ThreadContext context; |
| 71 | private final Map<Class<?>, InternalHandler> handlers = Maps.newConcurrentMap(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 72 | |
| 73 | CopycatTransportConnection(long connectionId, |
| 74 | CopycatTransport.Mode mode, |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 75 | PartitionId partitionId, |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 76 | Address address, |
| 77 | MessagingService messagingService, |
| 78 | ThreadContext context) { |
| 79 | this.connectionId = connectionId; |
| 80 | this.mode = checkNotNull(mode); |
| 81 | this.remoteAddress = checkNotNull(address); |
| 82 | this.messagingService = checkNotNull(messagingService); |
| 83 | if (mode == CopycatTransport.Mode.CLIENT) { |
Madan Jampani | 3a9911c | 2016-02-21 11:25:45 -0800 | [diff] [blame] | 84 | this.outboundMessageSubject = String.format("onos-copycat-%s", partitionId); |
| 85 | this.inboundMessageSubject = String.format("onos-copycat-%s-%d", partitionId, connectionId); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 86 | } else { |
Madan Jampani | 3a9911c | 2016-02-21 11:25:45 -0800 | [diff] [blame] | 87 | this.outboundMessageSubject = String.format("onos-copycat-%s-%d", partitionId, connectionId); |
| 88 | this.inboundMessageSubject = String.format("onos-copycat-%s", partitionId); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 89 | } |
| 90 | this.context = checkNotNull(context); |
| 91 | } |
| 92 | |
| 93 | public void setBidirectional() { |
| 94 | messagingService.registerHandler(inboundMessageSubject, (sender, payload) -> { |
| 95 | try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(payload))) { |
| 96 | if (input.readLong() != connectionId) { |
| 97 | throw new IllegalStateException("Invalid connection Id"); |
| 98 | } |
| 99 | return handle(IOUtils.toByteArray(input)); |
| 100 | } catch (IOException e) { |
| 101 | Throwables.propagate(e); |
| 102 | return null; |
| 103 | } |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public <T, U> CompletableFuture<U> send(T message) { |
| 109 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
| 110 | CompletableFuture<U> result = new CompletableFuture<>(); |
| 111 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 112 | new DataOutputStream(baos).writeLong(connectionId); |
| 113 | context.serializer().writeObject(message, baos); |
| 114 | if (message instanceof ReferenceCounted) { |
| 115 | ((ReferenceCounted<?>) message).release(); |
| 116 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 117 | messagingService.sendAndReceive(CopycatTransport.toEndpoint(remoteAddress), |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 118 | outboundMessageSubject, |
| 119 | baos.toByteArray(), |
| 120 | context.executor()) |
| 121 | .whenComplete((r, e) -> { |
Madan Jampani | 6f74371 | 2016-03-26 11:20:25 -0700 | [diff] [blame] | 122 | Throwable wrappedError = e; |
| 123 | if (e != null) { |
| 124 | Throwable rootCause = Throwables.getRootCause(e); |
| 125 | if (MessagingException.class.isAssignableFrom(rootCause.getClass())) { |
| 126 | wrappedError = new TransportException(e); |
| 127 | } |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 128 | } |
Madan Jampani | 6f74371 | 2016-03-26 11:20:25 -0700 | [diff] [blame] | 129 | handleResponse(r, wrappedError, result, context); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 130 | }); |
Madan Jampani | 0da01a4 | 2016-03-18 14:33:18 -0700 | [diff] [blame] | 131 | } catch (SerializationException | IOException e) { |
| 132 | result.completeExceptionally(e); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 133 | } |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | private <T> void handleResponse(byte[] response, |
| 138 | Throwable error, |
| 139 | CompletableFuture<T> future, |
| 140 | ThreadContext context) { |
| 141 | if (error != null) { |
| 142 | context.execute(() -> future.completeExceptionally(error)); |
| 143 | return; |
| 144 | } |
| 145 | checkNotNull(response); |
| 146 | InputStream input = new ByteArrayInputStream(response); |
| 147 | try { |
| 148 | byte status = (byte) input.read(); |
| 149 | if (status == FAILURE) { |
| 150 | Throwable t = context.serializer().readObject(input); |
| 151 | context.execute(() -> future.completeExceptionally(t)); |
| 152 | } else { |
| 153 | context.execute(() -> future.complete(context.serializer().readObject(input))); |
| 154 | } |
| 155 | } catch (IOException e) { |
| 156 | context.execute(() -> future.completeExceptionally(e)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public <T, U> Connection handler(Class<T> type, MessageHandler<T, U> handler) { |
| 162 | Assert.notNull(type, "type"); |
| 163 | handlers.put(type, new InternalHandler(handler, ThreadContext.currentContextOrThrow())); |
| 164 | return null; |
| 165 | } |
| 166 | |
| 167 | public CompletableFuture<byte[]> handle(byte[] message) { |
| 168 | try { |
| 169 | Object request = context.serializer().readObject(new ByteArrayInputStream(message)); |
| 170 | InternalHandler handler = handlers.get(request.getClass()); |
| 171 | if (handler == null) { |
| 172 | return Tools.exceptionalFuture(new IllegalStateException( |
| 173 | "No handler registered for " + request.getClass())); |
| 174 | } |
| 175 | return handler.handle(request).handle((result, error) -> { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 176 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 177 | baos.write(error != null ? FAILURE : SUCCESS); |
| 178 | context.serializer().writeObject(error != null ? error : result, baos); |
| 179 | return baos.toByteArray(); |
| 180 | } catch (IOException e) { |
| 181 | Throwables.propagate(e); |
| 182 | return null; |
| 183 | } |
| 184 | }); |
| 185 | } catch (Exception e) { |
| 186 | return Tools.exceptionalFuture(e); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public Listener<Throwable> exceptionListener(Consumer<Throwable> listener) { |
| 192 | return exceptionListeners.add(listener); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public Listener<Connection> closeListener(Consumer<Connection> listener) { |
| 197 | return closeListeners.add(listener); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public CompletableFuture<Void> close() { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 202 | closeListeners.forEach(listener -> listener.accept(this)); |
| 203 | if (mode == CopycatTransport.Mode.CLIENT) { |
| 204 | messagingService.unregisterHandler(inboundMessageSubject); |
| 205 | } |
| 206 | return CompletableFuture.completedFuture(null); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public int hashCode() { |
| 211 | return Objects.hash(connectionId); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public boolean equals(Object other) { |
| 216 | if (!(other instanceof CopycatTransportConnection)) { |
| 217 | return false; |
| 218 | } |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 219 | return connectionId == ((CopycatTransportConnection) other).connectionId; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public String toString() { |
| 224 | return MoreObjects.toStringHelper(getClass()) |
| 225 | .add("id", connectionId) |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 226 | .toString(); |
| 227 | } |
| 228 | |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 229 | @SuppressWarnings("rawtypes") |
| 230 | private final class InternalHandler { |
| 231 | |
| 232 | private final MessageHandler handler; |
| 233 | private final ThreadContext context; |
| 234 | |
| 235 | private InternalHandler(MessageHandler handler, ThreadContext context) { |
| 236 | this.handler = handler; |
| 237 | this.context = context; |
| 238 | } |
| 239 | |
| 240 | @SuppressWarnings("unchecked") |
| 241 | public CompletableFuture<Object> handle(Object message) { |
| 242 | CompletableFuture<Object> answer = new CompletableFuture<>(); |
| 243 | context.execute(() -> handler.handle(message).whenComplete((r, e) -> { |
| 244 | if (e != null) { |
| 245 | answer.completeExceptionally((Throwable) e); |
| 246 | } else { |
| 247 | answer.complete(r); |
| 248 | } |
| 249 | })); |
| 250 | return answer; |
| 251 | } |
| 252 | } |
| 253 | } |