Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.cluster.messaging.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
| 20 | import java.io.IOException; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 21 | import java.util.Set; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 22 | |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
Madan Jampani | a5d0d78 | 2014-10-07 14:36:00 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | 4e729af | 2014-11-19 11:11:37 -0800 | [diff] [blame] | 29 | import org.onlab.netty.Endpoint; |
| 30 | import org.onlab.netty.Message; |
| 31 | import org.onlab.netty.MessageHandler; |
| 32 | import org.onlab.netty.MessagingService; |
| 33 | import org.onlab.netty.NettyMessagingService; |
Madan Jampani | a5d0d78 | 2014-10-07 14:36:00 -0700 | [diff] [blame] | 34 | import org.onlab.onos.cluster.ClusterService; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 35 | import org.onlab.onos.cluster.ControllerNode; |
| 36 | import org.onlab.onos.cluster.NodeId; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 37 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 38 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
| 39 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
| 40 | import org.onlab.onos.store.cluster.messaging.MessageSubject; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 41 | import org.onlab.onos.store.serializers.KryoNamespaces; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 42 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 60a190b | 2014-11-07 16:24:47 -0800 | [diff] [blame] | 43 | import org.onlab.onos.store.serializers.impl.ClusterMessageSerializer; |
| 44 | import org.onlab.onos.store.serializers.impl.MessageSubjectSerializer; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 45 | import org.onlab.util.KryoNamespace; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 49 | import com.google.common.util.concurrent.ListenableFuture; |
| 50 | |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 51 | @Component(immediate = true) |
| 52 | @Service |
Madan Jampani | 3b0dfd5 | 2014-10-02 16:48:13 -0700 | [diff] [blame] | 53 | public class ClusterCommunicationManager |
Yuta HIGUCHI | bbfc96a | 2014-10-13 18:05:44 -0700 | [diff] [blame] | 54 | implements ClusterCommunicationService { |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 55 | |
| 56 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 57 | |
Madan Jampani | a5d0d78 | 2014-10-07 14:36:00 -0700 | [diff] [blame] | 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | private ClusterService clusterService; |
| 60 | |
Yuta HIGUCHI | 993d7aa | 2014-10-06 22:54:38 -0700 | [diff] [blame] | 61 | // TODO: This probably should not be a OSGi service. |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 62 | private MessagingService messagingService; |
| 63 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 64 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
Yuta HIGUCHI | 7a8d4aa | 2014-10-07 17:37:11 -0700 | [diff] [blame] | 65 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 66 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 67 | serializerPool = KryoNamespace.newBuilder() |
| 68 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 69 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 70 | .register(new ClusterMessageSerializer(), ClusterMessage.class) |
| 71 | .register(new MessageSubjectSerializer(), MessageSubject.class) |
| 72 | .build(); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | }; |
| 76 | |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 77 | @Activate |
| 78 | public void activate() { |
Yuta HIGUCHI | bbfc96a | 2014-10-13 18:05:44 -0700 | [diff] [blame] | 79 | ControllerNode localNode = clusterService.getLocalNode(); |
Madan Jampani | 8710093 | 2014-10-21 16:46:12 -0700 | [diff] [blame] | 80 | NettyMessagingService netty = new NettyMessagingService(localNode.ip().toString(), localNode.tcpPort()); |
Yuta HIGUCHI | 7a8d4aa | 2014-10-07 17:37:11 -0700 | [diff] [blame] | 81 | // FIXME: workaround until it becomes a service. |
| 82 | try { |
| 83 | netty.activate(); |
| 84 | } catch (Exception e) { |
Yuta HIGUCHI | 7a8d4aa | 2014-10-07 17:37:11 -0700 | [diff] [blame] | 85 | log.error("NettyMessagingService#activate", e); |
| 86 | } |
| 87 | messagingService = netty; |
Yuta HIGUCHI | 1f8cd5f | 2014-11-04 23:48:55 -0800 | [diff] [blame] | 88 | log.info("Started on {}:{}", localNode.ip(), localNode.tcpPort()); |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Deactivate |
| 92 | public void deactivate() { |
Yuta HIGUCHI | 993d7aa | 2014-10-06 22:54:38 -0700 | [diff] [blame] | 93 | // TODO: cleanup messageingService if needed. |
Yuta HIGUCHI | 6c6fb9c | 2014-10-31 20:21:15 -0700 | [diff] [blame] | 94 | // FIXME: workaround until it becomes a service. |
| 95 | try { |
| 96 | ((NettyMessagingService) messagingService).deactivate(); |
| 97 | } catch (Exception e) { |
| 98 | log.error("NettyMessagingService#deactivate", e); |
| 99 | } |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 100 | log.info("Stopped"); |
| 101 | } |
| 102 | |
| 103 | @Override |
Yuta HIGUCHI | 9cff53b | 2014-10-13 15:21:16 -0700 | [diff] [blame] | 104 | public boolean broadcast(ClusterMessage message) throws IOException { |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 105 | boolean ok = true; |
Yuta HIGUCHI | bbfc96a | 2014-10-13 18:05:44 -0700 | [diff] [blame] | 106 | final ControllerNode localNode = clusterService.getLocalNode(); |
Yuta HIGUCHI | 7a8d4aa | 2014-10-07 17:37:11 -0700 | [diff] [blame] | 107 | for (ControllerNode node : clusterService.getNodes()) { |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 108 | if (!node.equals(localNode)) { |
Madan Jampani | 4e729af | 2014-11-19 11:11:37 -0800 | [diff] [blame] | 109 | ok = unicastUnchecked(message, node.id()) && ok; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | return ok; |
| 113 | } |
| 114 | |
| 115 | @Override |
Madan Jampani | f5d263b | 2014-11-13 10:04:40 -0800 | [diff] [blame] | 116 | public boolean broadcastIncludeSelf(ClusterMessage message) throws IOException { |
| 117 | boolean ok = true; |
| 118 | for (ControllerNode node : clusterService.getNodes()) { |
Madan Jampani | 4e729af | 2014-11-19 11:11:37 -0800 | [diff] [blame] | 119 | ok = unicastUnchecked(message, node.id()) && ok; |
Madan Jampani | f5d263b | 2014-11-13 10:04:40 -0800 | [diff] [blame] | 120 | } |
| 121 | return ok; |
| 122 | } |
| 123 | |
| 124 | @Override |
Yuta HIGUCHI | 9cff53b | 2014-10-13 15:21:16 -0700 | [diff] [blame] | 125 | public boolean multicast(ClusterMessage message, Set<NodeId> nodes) throws IOException { |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 126 | boolean ok = true; |
Yuta HIGUCHI | bbfc96a | 2014-10-13 18:05:44 -0700 | [diff] [blame] | 127 | final ControllerNode localNode = clusterService.getLocalNode(); |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 128 | for (NodeId nodeId : nodes) { |
| 129 | if (!nodeId.equals(localNode.id())) { |
Madan Jampani | 98c1760 | 2014-10-23 15:33:23 -0700 | [diff] [blame] | 130 | ok = unicastUnchecked(message, nodeId) && ok; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | return ok; |
| 134 | } |
| 135 | |
| 136 | @Override |
Yuta HIGUCHI | 9cff53b | 2014-10-13 15:21:16 -0700 | [diff] [blame] | 137 | public boolean unicast(ClusterMessage message, NodeId toNodeId) throws IOException { |
Yuta HIGUCHI | 7a8d4aa | 2014-10-07 17:37:11 -0700 | [diff] [blame] | 138 | ControllerNode node = clusterService.getNode(toNodeId); |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 139 | checkArgument(node != null, "Unknown nodeId: %s", toNodeId); |
| 140 | Endpoint nodeEp = new Endpoint(node.ip().toString(), node.tcpPort()); |
| 141 | try { |
Yuta HIGUCHI | 053b7d7 | 2014-10-07 23:03:20 -0700 | [diff] [blame] | 142 | messagingService.sendAsync(nodeEp, |
Yuta HIGUCHI | 68d9047 | 2014-10-07 17:55:50 -0700 | [diff] [blame] | 143 | message.subject().value(), SERIALIZER.encode(message)); |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 144 | return true; |
Yuta HIGUCHI | 053b7d7 | 2014-10-07 23:03:20 -0700 | [diff] [blame] | 145 | } catch (IOException e) { |
Yuta HIGUCHI | a58451f | 2014-10-16 15:44:59 -0700 | [diff] [blame] | 146 | log.trace("Failed to send cluster message to nodeId: " + toNodeId, e); |
Yuta HIGUCHI | 9cff53b | 2014-10-13 15:21:16 -0700 | [diff] [blame] | 147 | throw e; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 148 | } |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Madan Jampani | 98c1760 | 2014-10-23 15:33:23 -0700 | [diff] [blame] | 151 | private boolean unicastUnchecked(ClusterMessage message, NodeId toNodeId) throws IOException { |
| 152 | try { |
| 153 | return unicast(message, toNodeId); |
| 154 | } catch (IOException e) { |
| 155 | return false; |
| 156 | } |
| 157 | } |
| 158 | |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 159 | @Override |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 160 | public ListenableFuture<byte[]> sendAndReceive(ClusterMessage message, NodeId toNodeId) throws IOException { |
Madan Jampani | 4a9cb6d | 2014-10-17 10:48:50 -0700 | [diff] [blame] | 161 | ControllerNode node = clusterService.getNode(toNodeId); |
| 162 | checkArgument(node != null, "Unknown nodeId: %s", toNodeId); |
| 163 | Endpoint nodeEp = new Endpoint(node.ip().toString(), node.tcpPort()); |
| 164 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 165 | return messagingService.sendAndReceive(nodeEp, message.subject().value(), SERIALIZER.encode(message)); |
Madan Jampani | 4a9cb6d | 2014-10-17 10:48:50 -0700 | [diff] [blame] | 166 | |
| 167 | } catch (IOException e) { |
Yuta HIGUCHI | 39ae550 | 2014-11-05 16:42:12 -0800 | [diff] [blame] | 168 | log.trace("Failed interaction with remote nodeId: " + toNodeId, e); |
Madan Jampani | 4a9cb6d | 2014-10-17 10:48:50 -0700 | [diff] [blame] | 169 | throw e; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | @Override |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 174 | public void addSubscriber(MessageSubject subject, |
Yuta HIGUCHI | 76b54bf | 2014-11-07 01:56:55 -0800 | [diff] [blame] | 175 | ClusterMessageHandler subscriber) { |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 176 | messagingService.registerHandler(subject.value(), new InternalClusterMessageHandler(subscriber)); |
| 177 | } |
| 178 | |
Yuta HIGUCHI | 76b54bf | 2014-11-07 01:56:55 -0800 | [diff] [blame] | 179 | @Override |
| 180 | public void removeSubscriber(MessageSubject subject) { |
| 181 | messagingService.unregisterHandler(subject.value()); |
| 182 | } |
| 183 | |
Yuta HIGUCHI | 053b7d7 | 2014-10-07 23:03:20 -0700 | [diff] [blame] | 184 | private final class InternalClusterMessageHandler implements MessageHandler { |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 185 | |
| 186 | private final ClusterMessageHandler handler; |
| 187 | |
| 188 | public InternalClusterMessageHandler(ClusterMessageHandler handler) { |
| 189 | this.handler = handler; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void handle(Message message) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 194 | final ClusterMessage clusterMessage; |
Yuta HIGUCHI | 38bd145 | 2014-10-07 17:37:25 -0700 | [diff] [blame] | 195 | try { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 196 | clusterMessage = SERIALIZER.decode(message.payload()); |
| 197 | } catch (Exception e) { |
| 198 | log.error("Failed decoding ClusterMessage", e); |
| 199 | throw e; |
| 200 | } |
| 201 | try { |
Madan Jampani | 8a89509 | 2014-10-17 16:55:50 -0700 | [diff] [blame] | 202 | handler.handle(new InternalClusterMessage(clusterMessage, message)); |
Yuta HIGUCHI | 38bd145 | 2014-10-07 17:37:25 -0700 | [diff] [blame] | 203 | } catch (Exception e) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 204 | log.error("Exception caught handling {}", clusterMessage, e); |
Yuta HIGUCHI | 053b7d7 | 2014-10-07 23:03:20 -0700 | [diff] [blame] | 205 | throw e; |
Yuta HIGUCHI | 38bd145 | 2014-10-07 17:37:25 -0700 | [diff] [blame] | 206 | } |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
Madan Jampani | 4a9cb6d | 2014-10-17 10:48:50 -0700 | [diff] [blame] | 209 | |
Madan Jampani | 8a89509 | 2014-10-17 16:55:50 -0700 | [diff] [blame] | 210 | public static final class InternalClusterMessage extends ClusterMessage { |
| 211 | |
| 212 | private final Message rawMessage; |
| 213 | |
| 214 | public InternalClusterMessage(ClusterMessage clusterMessage, Message rawMessage) { |
| 215 | super(clusterMessage.sender(), clusterMessage.subject(), clusterMessage.payload()); |
| 216 | this.rawMessage = rawMessage; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void respond(byte[] response) throws IOException { |
| 221 | rawMessage.respond(response); |
| 222 | } |
| 223 | } |
Madan Jampani | f5d263b | 2014-11-13 10:04:40 -0800 | [diff] [blame] | 224 | } |