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