alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.service.impl; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 17 | |
Yuta HIGUCHI | a3982c1 | 2014-11-10 09:47:08 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Verify.verifyNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import static org.onosproject.store.service.impl.ClusterMessagingProtocol.DB_SERIALIZER; |
Yuta HIGUCHI | 2b75f1a | 2014-11-14 16:07:34 -0800 | [diff] [blame] | 20 | import static org.onlab.util.Tools.namedThreads; |
Madan Jampani | 2b6ca91 | 2014-11-21 14:44:45 -0800 | [diff] [blame] | 21 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 22 | import static java.util.concurrent.Executors.newCachedThreadPool; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 23 | |
| 24 | import java.io.IOException; |
Sho SHIMIZU | 0908275 | 2014-12-04 12:21:56 -0800 | [diff] [blame] | 25 | import java.time.Duration; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 26 | import java.util.concurrent.CompletableFuture; |
| 27 | import java.util.concurrent.ExecutionException; |
Yuta HIGUCHI | 2b75f1a | 2014-11-14 16:07:34 -0800 | [diff] [blame] | 28 | import java.util.concurrent.ExecutorService; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 29 | import java.util.concurrent.TimeUnit; |
| 30 | import java.util.concurrent.TimeoutException; |
Madan Jampani | 6234fd4 | 2014-11-21 22:34:28 -0800 | [diff] [blame] | 31 | import java.util.concurrent.atomic.AtomicBoolean; |
Madan Jampani | 2b6ca91 | 2014-11-21 14:44:45 -0800 | [diff] [blame] | 32 | |
Yuta HIGUCHI | 71b9d09 | 2014-11-12 13:31:11 -0800 | [diff] [blame] | 33 | import net.kuujo.copycat.cluster.TcpMember; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 34 | import net.kuujo.copycat.protocol.PingRequest; |
| 35 | import net.kuujo.copycat.protocol.PingResponse; |
| 36 | import net.kuujo.copycat.protocol.PollRequest; |
| 37 | import net.kuujo.copycat.protocol.PollResponse; |
| 38 | import net.kuujo.copycat.protocol.SubmitRequest; |
| 39 | import net.kuujo.copycat.protocol.SubmitResponse; |
| 40 | import net.kuujo.copycat.protocol.SyncRequest; |
| 41 | import net.kuujo.copycat.protocol.SyncResponse; |
| 42 | import net.kuujo.copycat.spi.protocol.ProtocolClient; |
| 43 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.cluster.ClusterService; |
| 45 | import org.onosproject.cluster.ControllerNode; |
| 46 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 47 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 48 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 51 | /** |
Madan Jampani | dfbfa18 | 2014-11-04 22:06:41 -0800 | [diff] [blame] | 52 | * ONOS Cluster messaging based Copycat protocol client. |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 53 | */ |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 54 | public class ClusterMessagingProtocolClient implements ProtocolClient { |
| 55 | |
| 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
Sho SHIMIZU | 0908275 | 2014-12-04 12:21:56 -0800 | [diff] [blame] | 58 | public static final Duration RETRY_INTERVAL = Duration.ofMillis(2000); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 59 | |
Yuta HIGUCHI | 71b9d09 | 2014-11-12 13:31:11 -0800 | [diff] [blame] | 60 | private final ClusterService clusterService; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 61 | private final ClusterCommunicationService clusterCommunicator; |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 62 | private final ControllerNode localNode; |
Yuta HIGUCHI | 71b9d09 | 2014-11-12 13:31:11 -0800 | [diff] [blame] | 63 | private final TcpMember remoteMember; |
Yuta HIGUCHI | d249943 | 2014-11-20 12:07:43 -0800 | [diff] [blame] | 64 | |
Madan Jampani | 6234fd4 | 2014-11-21 22:34:28 -0800 | [diff] [blame] | 65 | private ControllerNode remoteNode; |
| 66 | private final AtomicBoolean connectionOK = new AtomicBoolean(true); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 67 | |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 68 | private ExecutorService pool; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 69 | |
| 70 | public ClusterMessagingProtocolClient( |
Yuta HIGUCHI | 71b9d09 | 2014-11-12 13:31:11 -0800 | [diff] [blame] | 71 | ClusterService clusterService, |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 72 | ClusterCommunicationService clusterCommunicator, |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 73 | ControllerNode localNode, |
Yuta HIGUCHI | 71b9d09 | 2014-11-12 13:31:11 -0800 | [diff] [blame] | 74 | TcpMember remoteMember) { |
| 75 | |
| 76 | this.clusterService = clusterService; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 77 | this.clusterCommunicator = clusterCommunicator; |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 78 | this.localNode = localNode; |
Yuta HIGUCHI | 71b9d09 | 2014-11-12 13:31:11 -0800 | [diff] [blame] | 79 | this.remoteMember = remoteMember; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public CompletableFuture<PingResponse> ping(PingRequest request) { |
Madan Jampani | 6234fd4 | 2014-11-21 22:34:28 -0800 | [diff] [blame] | 84 | return requestReply(request); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public CompletableFuture<SyncResponse> sync(SyncRequest request) { |
Madan Jampani | 6234fd4 | 2014-11-21 22:34:28 -0800 | [diff] [blame] | 89 | return requestReply(request); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public CompletableFuture<PollResponse> poll(PollRequest request) { |
Madan Jampani | 6234fd4 | 2014-11-21 22:34:28 -0800 | [diff] [blame] | 94 | return requestReply(request); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public CompletableFuture<SubmitResponse> submit(SubmitRequest request) { |
Madan Jampani | 6234fd4 | 2014-11-21 22:34:28 -0800 | [diff] [blame] | 99 | return requestReply(request); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
Yuta HIGUCHI | 71b9d09 | 2014-11-12 13:31:11 -0800 | [diff] [blame] | 103 | public synchronized CompletableFuture<Void> connect() { |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 104 | if (pool == null || pool.isShutdown()) { |
| 105 | // TODO include remote name? |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 106 | pool = newCachedThreadPool(namedThreads("onos-copycat-netty-messaging-client-%d")); |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 107 | } |
Madan Jampani | 6234fd4 | 2014-11-21 22:34:28 -0800 | [diff] [blame] | 108 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
Yuta HIGUCHI | 71b9d09 | 2014-11-12 13:31:11 -0800 | [diff] [blame] | 112 | public synchronized CompletableFuture<Void> close() { |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 113 | if (pool != null) { |
| 114 | pool.shutdownNow(); |
| 115 | pool = null; |
| 116 | } |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 117 | return CompletableFuture.completedFuture(null); |
| 118 | } |
| 119 | |
Yuta HIGUCHI | 3b01673 | 2014-11-07 15:32:18 -0800 | [diff] [blame] | 120 | private <I> MessageSubject messageType(I input) { |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 121 | Class<?> clazz = input.getClass(); |
| 122 | if (clazz.equals(PollRequest.class)) { |
| 123 | return ClusterMessagingProtocol.COPYCAT_POLL; |
| 124 | } else if (clazz.equals(SyncRequest.class)) { |
| 125 | return ClusterMessagingProtocol.COPYCAT_SYNC; |
| 126 | } else if (clazz.equals(SubmitRequest.class)) { |
| 127 | return ClusterMessagingProtocol.COPYCAT_SUBMIT; |
| 128 | } else if (clazz.equals(PingRequest.class)) { |
| 129 | return ClusterMessagingProtocol.COPYCAT_PING; |
| 130 | } else { |
| 131 | throw new IllegalArgumentException("Unknown class " + clazz.getName()); |
| 132 | } |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private <I, O> CompletableFuture<O> requestReply(I request) { |
| 136 | CompletableFuture<O> future = new CompletableFuture<>(); |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 137 | if (pool == null) { |
| 138 | log.info("Attempted to use closed client, connecting now. {}", request); |
| 139 | connect(); |
| 140 | } |
| 141 | pool.submit(new RPCTask<I, O>(request, future)); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 142 | return future; |
| 143 | } |
| 144 | |
Yuta HIGUCHI | 71b9d09 | 2014-11-12 13:31:11 -0800 | [diff] [blame] | 145 | private ControllerNode getControllerNode(TcpMember remoteMember) { |
| 146 | final String host = remoteMember.host(); |
| 147 | final int port = remoteMember.port(); |
| 148 | for (ControllerNode node : clusterService.getNodes()) { |
| 149 | if (node.ip().toString().equals(host) && node.tcpPort() == port) { |
| 150 | return node; |
| 151 | } |
| 152 | } |
| 153 | return null; |
| 154 | } |
| 155 | |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 156 | private class RPCTask<I, O> implements Runnable { |
| 157 | |
Yuta HIGUCHI | e59656d | 2014-11-07 01:57:32 -0800 | [diff] [blame] | 158 | private final I request; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 159 | private final ClusterMessage message; |
| 160 | private final CompletableFuture<O> future; |
| 161 | |
| 162 | public RPCTask(I request, CompletableFuture<O> future) { |
Yuta HIGUCHI | e59656d | 2014-11-07 01:57:32 -0800 | [diff] [blame] | 163 | this.request = request; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 164 | this.message = |
| 165 | new ClusterMessage( |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 166 | localNode.id(), |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 167 | messageType(request), |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 168 | verifyNotNull(DB_SERIALIZER.encode(request))); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 169 | this.future = future; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void run() { |
| 174 | try { |
Madan Jampani | 6234fd4 | 2014-11-21 22:34:28 -0800 | [diff] [blame] | 175 | if (remoteNode == null) { |
| 176 | remoteNode = getControllerNode(remoteMember); |
| 177 | if (remoteNode == null) { |
| 178 | throw new IOException("Remote node is offline!"); |
| 179 | } |
Yuta HIGUCHI | d249943 | 2014-11-20 12:07:43 -0800 | [diff] [blame] | 180 | } |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 181 | byte[] response = clusterCommunicator |
Madan Jampani | 6234fd4 | 2014-11-21 22:34:28 -0800 | [diff] [blame] | 182 | .sendAndReceive(message, remoteNode.id()) |
Sho SHIMIZU | 0908275 | 2014-12-04 12:21:56 -0800 | [diff] [blame] | 183 | .get(RETRY_INTERVAL.toMillis(), TimeUnit.MILLISECONDS); |
Madan Jampani | 6234fd4 | 2014-11-21 22:34:28 -0800 | [diff] [blame] | 184 | if (!connectionOK.getAndSet(true)) { |
| 185 | log.info("Connectivity to {} restored", remoteNode); |
| 186 | } |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 187 | future.complete(verifyNotNull(DB_SERIALIZER.decode(response))); |
| 188 | |
Yuta HIGUCHI | d249943 | 2014-11-20 12:07:43 -0800 | [diff] [blame] | 189 | } catch (IOException | TimeoutException e) { |
Madan Jampani | 6234fd4 | 2014-11-21 22:34:28 -0800 | [diff] [blame] | 190 | if (connectionOK.getAndSet(false)) { |
| 191 | log.warn("Detected connectivity issues with {}. Reason: {}", remoteNode, e.getMessage()); |
| 192 | } |
Yuta HIGUCHI | 4520716 | 2014-11-17 15:25:13 -0800 | [diff] [blame] | 193 | log.debug("RPCTask for {} failed.", request, e); |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 194 | future.completeExceptionally(e); |
Yuta HIGUCHI | d249943 | 2014-11-20 12:07:43 -0800 | [diff] [blame] | 195 | } catch (ExecutionException e) { |
| 196 | log.warn("RPCTask execution for {} failed: {}", request, e.getMessage()); |
| 197 | log.debug("RPCTask execution for {} failed.", request, e); |
| 198 | future.completeExceptionally(e); |
Yuta HIGUCHI | 2b75f1a | 2014-11-14 16:07:34 -0800 | [diff] [blame] | 199 | } catch (InterruptedException e) { |
Yuta HIGUCHI | 4520716 | 2014-11-17 15:25:13 -0800 | [diff] [blame] | 200 | log.warn("RPCTask for {} was interrupted: {}", request, e.getMessage()); |
| 201 | log.debug("RPCTask for {} was interrupted.", request, e); |
Yuta HIGUCHI | 2b75f1a | 2014-11-14 16:07:34 -0800 | [diff] [blame] | 202 | future.completeExceptionally(e); |
| 203 | Thread.currentThread().interrupt(); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 204 | } catch (Exception e) { |
Yuta HIGUCHI | 9bbaca3 | 2014-11-07 13:08:31 -0800 | [diff] [blame] | 205 | log.warn("RPCTask for {} terribly failed.", request, e); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 206 | future.completeExceptionally(e); |
| 207 | } |
| 208 | } |
| 209 | } |
Madan Jampani | 515865d | 2014-11-09 22:29:34 -0800 | [diff] [blame] | 210 | } |