blob: 2f6993444416832dbd16fbdb6ad217f1f0239197 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.service.impl;
Madan Jampani9b19a822014-11-04 21:37:13 -080017
Yuta HIGUCHIa3982c12014-11-10 09:47:08 -080018import static com.google.common.base.Verify.verifyNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import static org.onosproject.store.service.impl.ClusterMessagingProtocol.DB_SERIALIZER;
Yuta HIGUCHI2b75f1a2014-11-14 16:07:34 -080020import static org.onlab.util.Tools.namedThreads;
Madan Jampani2b6ca912014-11-21 14:44:45 -080021import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI03e77e92014-11-22 17:53:01 -080022import static java.util.concurrent.Executors.newCachedThreadPool;
Madan Jampani9b19a822014-11-04 21:37:13 -080023
24import java.io.IOException;
25import java.util.concurrent.CompletableFuture;
26import java.util.concurrent.ExecutionException;
Yuta HIGUCHI2b75f1a2014-11-14 16:07:34 -080027import java.util.concurrent.ExecutorService;
Madan Jampani9b19a822014-11-04 21:37:13 -080028import java.util.concurrent.TimeUnit;
29import java.util.concurrent.TimeoutException;
Madan Jampani6234fd42014-11-21 22:34:28 -080030import java.util.concurrent.atomic.AtomicBoolean;
Madan Jampani2b6ca912014-11-21 14:44:45 -080031
Yuta HIGUCHI71b9d092014-11-12 13:31:11 -080032import net.kuujo.copycat.cluster.TcpMember;
Madan Jampani9b19a822014-11-04 21:37:13 -080033import net.kuujo.copycat.protocol.PingRequest;
34import net.kuujo.copycat.protocol.PingResponse;
35import net.kuujo.copycat.protocol.PollRequest;
36import net.kuujo.copycat.protocol.PollResponse;
37import net.kuujo.copycat.protocol.SubmitRequest;
38import net.kuujo.copycat.protocol.SubmitResponse;
39import net.kuujo.copycat.protocol.SyncRequest;
40import net.kuujo.copycat.protocol.SyncResponse;
41import net.kuujo.copycat.spi.protocol.ProtocolClient;
42
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.cluster.ClusterService;
44import org.onosproject.cluster.ControllerNode;
45import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
46import org.onosproject.store.cluster.messaging.ClusterMessage;
47import org.onosproject.store.cluster.messaging.MessageSubject;
Madan Jampani9b19a822014-11-04 21:37:13 -080048import org.slf4j.Logger;
49
Madan Jampani9b19a822014-11-04 21:37:13 -080050/**
Madan Jampanidfbfa182014-11-04 22:06:41 -080051 * ONOS Cluster messaging based Copycat protocol client.
Madan Jampani9b19a822014-11-04 21:37:13 -080052 */
Madan Jampani9b19a822014-11-04 21:37:13 -080053public class ClusterMessagingProtocolClient implements ProtocolClient {
54
55 private final Logger log = getLogger(getClass());
56
Madan Jampani9b19a822014-11-04 21:37:13 -080057 public static final long RETRY_INTERVAL_MILLIS = 2000;
58
Yuta HIGUCHI71b9d092014-11-12 13:31:11 -080059 private final ClusterService clusterService;
Madan Jampani9b19a822014-11-04 21:37:13 -080060 private final ClusterCommunicationService clusterCommunicator;
Madan Jampani515865d2014-11-09 22:29:34 -080061 private final ControllerNode localNode;
Yuta HIGUCHI71b9d092014-11-12 13:31:11 -080062 private final TcpMember remoteMember;
Yuta HIGUCHId2499432014-11-20 12:07:43 -080063
Madan Jampani6234fd42014-11-21 22:34:28 -080064 private ControllerNode remoteNode;
65 private final AtomicBoolean connectionOK = new AtomicBoolean(true);
Madan Jampani9b19a822014-11-04 21:37:13 -080066
Yuta HIGUCHI03e77e92014-11-22 17:53:01 -080067 private ExecutorService pool;
Madan Jampani9b19a822014-11-04 21:37:13 -080068
69 public ClusterMessagingProtocolClient(
Yuta HIGUCHI71b9d092014-11-12 13:31:11 -080070 ClusterService clusterService,
Madan Jampani9b19a822014-11-04 21:37:13 -080071 ClusterCommunicationService clusterCommunicator,
Madan Jampani515865d2014-11-09 22:29:34 -080072 ControllerNode localNode,
Yuta HIGUCHI71b9d092014-11-12 13:31:11 -080073 TcpMember remoteMember) {
74
75 this.clusterService = clusterService;
Madan Jampani9b19a822014-11-04 21:37:13 -080076 this.clusterCommunicator = clusterCommunicator;
Madan Jampani515865d2014-11-09 22:29:34 -080077 this.localNode = localNode;
Yuta HIGUCHI71b9d092014-11-12 13:31:11 -080078 this.remoteMember = remoteMember;
Madan Jampani9b19a822014-11-04 21:37:13 -080079 }
80
81 @Override
82 public CompletableFuture<PingResponse> ping(PingRequest request) {
Madan Jampani6234fd42014-11-21 22:34:28 -080083 return requestReply(request);
Madan Jampani9b19a822014-11-04 21:37:13 -080084 }
85
86 @Override
87 public CompletableFuture<SyncResponse> sync(SyncRequest request) {
Madan Jampani6234fd42014-11-21 22:34:28 -080088 return requestReply(request);
Madan Jampani9b19a822014-11-04 21:37:13 -080089 }
90
91 @Override
92 public CompletableFuture<PollResponse> poll(PollRequest request) {
Madan Jampani6234fd42014-11-21 22:34:28 -080093 return requestReply(request);
Madan Jampani9b19a822014-11-04 21:37:13 -080094 }
95
96 @Override
97 public CompletableFuture<SubmitResponse> submit(SubmitRequest request) {
Madan Jampani6234fd42014-11-21 22:34:28 -080098 return requestReply(request);
Madan Jampani9b19a822014-11-04 21:37:13 -080099 }
100
101 @Override
Yuta HIGUCHI71b9d092014-11-12 13:31:11 -0800102 public synchronized CompletableFuture<Void> connect() {
Yuta HIGUCHI03e77e92014-11-22 17:53:01 -0800103 if (pool == null || pool.isShutdown()) {
104 // TODO include remote name?
105 pool = newCachedThreadPool(namedThreads("copycat-netty-messaging-client-%d"));
106 }
Madan Jampani6234fd42014-11-21 22:34:28 -0800107 return CompletableFuture.completedFuture(null);
Madan Jampani9b19a822014-11-04 21:37:13 -0800108 }
109
110 @Override
Yuta HIGUCHI71b9d092014-11-12 13:31:11 -0800111 public synchronized CompletableFuture<Void> close() {
Yuta HIGUCHI03e77e92014-11-22 17:53:01 -0800112 if (pool != null) {
113 pool.shutdownNow();
114 pool = null;
115 }
Madan Jampani9b19a822014-11-04 21:37:13 -0800116 return CompletableFuture.completedFuture(null);
117 }
118
Yuta HIGUCHI3b016732014-11-07 15:32:18 -0800119 private <I> MessageSubject messageType(I input) {
Madan Jampani9b19a822014-11-04 21:37:13 -0800120 Class<?> clazz = input.getClass();
121 if (clazz.equals(PollRequest.class)) {
122 return ClusterMessagingProtocol.COPYCAT_POLL;
123 } else if (clazz.equals(SyncRequest.class)) {
124 return ClusterMessagingProtocol.COPYCAT_SYNC;
125 } else if (clazz.equals(SubmitRequest.class)) {
126 return ClusterMessagingProtocol.COPYCAT_SUBMIT;
127 } else if (clazz.equals(PingRequest.class)) {
128 return ClusterMessagingProtocol.COPYCAT_PING;
129 } else {
130 throw new IllegalArgumentException("Unknown class " + clazz.getName());
131 }
Madan Jampani9b19a822014-11-04 21:37:13 -0800132 }
133
134 private <I, O> CompletableFuture<O> requestReply(I request) {
135 CompletableFuture<O> future = new CompletableFuture<>();
Yuta HIGUCHI03e77e92014-11-22 17:53:01 -0800136 if (pool == null) {
137 log.info("Attempted to use closed client, connecting now. {}", request);
138 connect();
139 }
140 pool.submit(new RPCTask<I, O>(request, future));
Madan Jampani9b19a822014-11-04 21:37:13 -0800141 return future;
142 }
143
Yuta HIGUCHI71b9d092014-11-12 13:31:11 -0800144 private ControllerNode getControllerNode(TcpMember remoteMember) {
145 final String host = remoteMember.host();
146 final int port = remoteMember.port();
147 for (ControllerNode node : clusterService.getNodes()) {
148 if (node.ip().toString().equals(host) && node.tcpPort() == port) {
149 return node;
150 }
151 }
152 return null;
153 }
154
Madan Jampani9b19a822014-11-04 21:37:13 -0800155 private class RPCTask<I, O> implements Runnable {
156
Yuta HIGUCHIe59656d2014-11-07 01:57:32 -0800157 private final I request;
Madan Jampani9b19a822014-11-04 21:37:13 -0800158 private final ClusterMessage message;
159 private final CompletableFuture<O> future;
160
161 public RPCTask(I request, CompletableFuture<O> future) {
Yuta HIGUCHIe59656d2014-11-07 01:57:32 -0800162 this.request = request;
Madan Jampani9b19a822014-11-04 21:37:13 -0800163 this.message =
164 new ClusterMessage(
Madan Jampani515865d2014-11-09 22:29:34 -0800165 localNode.id(),
Madan Jampani9b19a822014-11-04 21:37:13 -0800166 messageType(request),
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800167 verifyNotNull(DB_SERIALIZER.encode(request)));
Madan Jampani9b19a822014-11-04 21:37:13 -0800168 this.future = future;
169 }
170
171 @Override
172 public void run() {
173 try {
Madan Jampani6234fd42014-11-21 22:34:28 -0800174 if (remoteNode == null) {
175 remoteNode = getControllerNode(remoteMember);
176 if (remoteNode == null) {
177 throw new IOException("Remote node is offline!");
178 }
Yuta HIGUCHId2499432014-11-20 12:07:43 -0800179 }
Madan Jampani9b19a822014-11-04 21:37:13 -0800180 byte[] response = clusterCommunicator
Madan Jampani6234fd42014-11-21 22:34:28 -0800181 .sendAndReceive(message, remoteNode.id())
Madan Jampani9b19a822014-11-04 21:37:13 -0800182 .get(RETRY_INTERVAL_MILLIS, TimeUnit.MILLISECONDS);
Madan Jampani6234fd42014-11-21 22:34:28 -0800183 if (!connectionOK.getAndSet(true)) {
184 log.info("Connectivity to {} restored", remoteNode);
185 }
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800186 future.complete(verifyNotNull(DB_SERIALIZER.decode(response)));
187
Yuta HIGUCHId2499432014-11-20 12:07:43 -0800188 } catch (IOException | TimeoutException e) {
Madan Jampani6234fd42014-11-21 22:34:28 -0800189 if (connectionOK.getAndSet(false)) {
190 log.warn("Detected connectivity issues with {}. Reason: {}", remoteNode, e.getMessage());
191 }
Yuta HIGUCHI45207162014-11-17 15:25:13 -0800192 log.debug("RPCTask for {} failed.", request, e);
Madan Jampani515865d2014-11-09 22:29:34 -0800193 future.completeExceptionally(e);
Yuta HIGUCHId2499432014-11-20 12:07:43 -0800194 } catch (ExecutionException e) {
195 log.warn("RPCTask execution for {} failed: {}", request, e.getMessage());
196 log.debug("RPCTask execution for {} failed.", request, e);
197 future.completeExceptionally(e);
Yuta HIGUCHI2b75f1a2014-11-14 16:07:34 -0800198 } catch (InterruptedException e) {
Yuta HIGUCHI45207162014-11-17 15:25:13 -0800199 log.warn("RPCTask for {} was interrupted: {}", request, e.getMessage());
200 log.debug("RPCTask for {} was interrupted.", request, e);
Yuta HIGUCHI2b75f1a2014-11-14 16:07:34 -0800201 future.completeExceptionally(e);
202 Thread.currentThread().interrupt();
Madan Jampani9b19a822014-11-04 21:37:13 -0800203 } catch (Exception e) {
Yuta HIGUCHI9bbaca32014-11-07 13:08:31 -0800204 log.warn("RPCTask for {} terribly failed.", request, e);
Madan Jampani9b19a822014-11-04 21:37:13 -0800205 future.completeExceptionally(e);
206 }
207 }
208 }
Madan Jampani515865d2014-11-09 22:29:34 -0800209}