Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 1 | package org.onlab.onos.store.service.impl; |
| 2 | |
Yuta HIGUCHI | a3982c1 | 2014-11-10 09:47:08 -0800 | [diff] [blame^] | 3 | import static com.google.common.base.Verify.verifyNotNull; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 4 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | a3982c1 | 2014-11-10 09:47:08 -0800 | [diff] [blame^] | 5 | import static org.onlab.onos.store.service.impl.ClusterMessagingProtocol.SERIALIZER; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 6 | |
| 7 | import java.io.IOException; |
| 8 | import java.util.concurrent.CompletableFuture; |
| 9 | import java.util.concurrent.ExecutionException; |
| 10 | import java.util.concurrent.ScheduledExecutorService; |
| 11 | import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 12 | import java.util.concurrent.ThreadFactory; |
| 13 | import java.util.concurrent.TimeUnit; |
| 14 | import java.util.concurrent.TimeoutException; |
| 15 | |
| 16 | import net.kuujo.copycat.protocol.PingRequest; |
| 17 | import net.kuujo.copycat.protocol.PingResponse; |
| 18 | import net.kuujo.copycat.protocol.PollRequest; |
| 19 | import net.kuujo.copycat.protocol.PollResponse; |
| 20 | import net.kuujo.copycat.protocol.SubmitRequest; |
| 21 | import net.kuujo.copycat.protocol.SubmitResponse; |
| 22 | import net.kuujo.copycat.protocol.SyncRequest; |
| 23 | import net.kuujo.copycat.protocol.SyncResponse; |
| 24 | import net.kuujo.copycat.spi.protocol.ProtocolClient; |
| 25 | |
| 26 | import org.onlab.onos.cluster.ControllerNode; |
| 27 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 28 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
| 29 | import org.onlab.onos.store.cluster.messaging.MessageSubject; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 33 | |
| 34 | /** |
Madan Jampani | dfbfa18 | 2014-11-04 22:06:41 -0800 | [diff] [blame] | 35 | * ONOS Cluster messaging based Copycat protocol client. |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 36 | */ |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 37 | public class ClusterMessagingProtocolClient implements ProtocolClient { |
| 38 | |
| 39 | private final Logger log = getLogger(getClass()); |
| 40 | |
| 41 | private static final ThreadFactory THREAD_FACTORY = |
| 42 | new ThreadFactoryBuilder().setNameFormat("copycat-netty-messaging-%d").build(); |
| 43 | |
| 44 | public static final long RETRY_INTERVAL_MILLIS = 2000; |
| 45 | |
| 46 | private final ClusterCommunicationService clusterCommunicator; |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 47 | private final ControllerNode localNode; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 48 | private final ControllerNode remoteNode; |
| 49 | |
| 50 | // FIXME: Thread pool sizing. |
| 51 | private static final ScheduledExecutorService THREAD_POOL = |
| 52 | new ScheduledThreadPoolExecutor(10, THREAD_FACTORY); |
| 53 | |
| 54 | public ClusterMessagingProtocolClient( |
| 55 | ClusterCommunicationService clusterCommunicator, |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 56 | ControllerNode localNode, |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 57 | ControllerNode remoteNode) { |
| 58 | this.clusterCommunicator = clusterCommunicator; |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 59 | this.localNode = localNode; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 60 | this.remoteNode = remoteNode; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public CompletableFuture<PingResponse> ping(PingRequest request) { |
| 65 | return requestReply(request); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public CompletableFuture<SyncResponse> sync(SyncRequest request) { |
| 70 | return requestReply(request); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public CompletableFuture<PollResponse> poll(PollRequest request) { |
| 75 | return requestReply(request); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public CompletableFuture<SubmitResponse> submit(SubmitRequest request) { |
| 80 | return requestReply(request); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public CompletableFuture<Void> connect() { |
| 85 | return CompletableFuture.completedFuture(null); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public CompletableFuture<Void> close() { |
| 90 | return CompletableFuture.completedFuture(null); |
| 91 | } |
| 92 | |
Yuta HIGUCHI | 3b01673 | 2014-11-07 15:32:18 -0800 | [diff] [blame] | 93 | private <I> MessageSubject messageType(I input) { |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 94 | Class<?> clazz = input.getClass(); |
| 95 | if (clazz.equals(PollRequest.class)) { |
| 96 | return ClusterMessagingProtocol.COPYCAT_POLL; |
| 97 | } else if (clazz.equals(SyncRequest.class)) { |
| 98 | return ClusterMessagingProtocol.COPYCAT_SYNC; |
| 99 | } else if (clazz.equals(SubmitRequest.class)) { |
| 100 | return ClusterMessagingProtocol.COPYCAT_SUBMIT; |
| 101 | } else if (clazz.equals(PingRequest.class)) { |
| 102 | return ClusterMessagingProtocol.COPYCAT_PING; |
| 103 | } else { |
| 104 | throw new IllegalArgumentException("Unknown class " + clazz.getName()); |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |
| 109 | private <I, O> CompletableFuture<O> requestReply(I request) { |
| 110 | CompletableFuture<O> future = new CompletableFuture<>(); |
| 111 | THREAD_POOL.schedule(new RPCTask<I, O>(request, future), 0, TimeUnit.MILLISECONDS); |
| 112 | return future; |
| 113 | } |
| 114 | |
| 115 | private class RPCTask<I, O> implements Runnable { |
| 116 | |
Yuta HIGUCHI | e59656d | 2014-11-07 01:57:32 -0800 | [diff] [blame] | 117 | private final I request; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 118 | private final ClusterMessage message; |
| 119 | private final CompletableFuture<O> future; |
| 120 | |
| 121 | public RPCTask(I request, CompletableFuture<O> future) { |
Yuta HIGUCHI | e59656d | 2014-11-07 01:57:32 -0800 | [diff] [blame] | 122 | this.request = request; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 123 | this.message = |
| 124 | new ClusterMessage( |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 125 | localNode.id(), |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 126 | messageType(request), |
Yuta HIGUCHI | a3982c1 | 2014-11-10 09:47:08 -0800 | [diff] [blame^] | 127 | verifyNotNull(SERIALIZER.encode(request))); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 128 | this.future = future; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void run() { |
| 133 | try { |
| 134 | byte[] response = clusterCommunicator |
| 135 | .sendAndReceive(message, remoteNode.id()) |
| 136 | .get(RETRY_INTERVAL_MILLIS, TimeUnit.MILLISECONDS); |
Yuta HIGUCHI | a3982c1 | 2014-11-10 09:47:08 -0800 | [diff] [blame^] | 137 | future.complete(verifyNotNull(SERIALIZER.decode(response))); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 138 | |
| 139 | } catch (IOException | InterruptedException | ExecutionException | TimeoutException e) { |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 140 | log.warn("RPCTask for {} failed.", request, e); |
| 141 | future.completeExceptionally(e); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 142 | } catch (Exception e) { |
Yuta HIGUCHI | 9bbaca3 | 2014-11-07 13:08:31 -0800 | [diff] [blame] | 143 | log.warn("RPCTask for {} terribly failed.", request, e); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 144 | future.completeExceptionally(e); |
| 145 | } |
| 146 | } |
| 147 | } |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 148 | } |