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