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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.cluster.impl; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.hazelcast.util.AddressUtil; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 25 | import org.joda.time.DateTime; |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 26 | import org.onlab.netty.Endpoint; |
| 27 | import org.onlab.netty.Message; |
| 28 | import org.onlab.netty.MessageHandler; |
| 29 | import org.onlab.netty.NettyMessagingService; |
| 30 | import org.onlab.packet.IpAddress; |
| 31 | import org.onlab.util.KryoNamespace; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.cluster.ClusterEvent; |
| 33 | import org.onosproject.cluster.ClusterStore; |
| 34 | import org.onosproject.cluster.ClusterStoreDelegate; |
| 35 | import org.onosproject.cluster.ControllerNode; |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 36 | import org.onosproject.cluster.ControllerNode.State; |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 37 | import org.onosproject.cluster.DefaultControllerNode; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.cluster.NodeId; |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 39 | import org.onosproject.store.AbstractStore; |
| 40 | import org.onosproject.store.serializers.KryoNamespaces; |
| 41 | import org.onosproject.store.serializers.KryoSerializer; |
| 42 | import org.slf4j.Logger; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 43 | |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 44 | import java.io.File; |
| 45 | import java.io.IOException; |
| 46 | import java.net.InetAddress; |
| 47 | import java.net.NetworkInterface; |
| 48 | import java.net.SocketException; |
| 49 | import java.util.Enumeration; |
| 50 | import java.util.Map; |
| 51 | import java.util.Set; |
| 52 | import java.util.concurrent.ExecutorService; |
| 53 | import java.util.concurrent.Executors; |
| 54 | import java.util.concurrent.ScheduledExecutorService; |
| 55 | import java.util.concurrent.TimeUnit; |
| 56 | import java.util.stream.Collectors; |
| 57 | |
| 58 | import static com.google.common.base.Preconditions.checkArgument; |
| 59 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame^] | 60 | import static java.net.NetworkInterface.getNetworkInterfaces; |
| 61 | import static java.util.Collections.list; |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 62 | import static org.onlab.util.Tools.groupedThreads; |
| 63 | import static org.slf4j.LoggerFactory.getLogger; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 64 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 65 | @Component(immediate = true) |
| 66 | @Service |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Distributed cluster nodes store that employs an accrual failure |
| 69 | * detector to identify cluster member up/down status. |
| 70 | */ |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 71 | public class DistributedClusterStore |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 72 | extends AbstractStore<ClusterEvent, ClusterStoreDelegate> |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 73 | implements ClusterStore { |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 74 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame^] | 75 | private static final Logger log = getLogger(DistributedClusterStore.class); |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 76 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 77 | // TODO: make these configurable. |
| 78 | private static final int HEARTBEAT_FD_PORT = 2419; |
| 79 | private static final int HEARTBEAT_INTERVAL_MS = 100; |
| 80 | private static final int PHI_FAILURE_THRESHOLD = 10; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 81 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 82 | private static final String CONFIG_DIR = "../config"; |
| 83 | private static final String CLUSTER_DEFINITION_FILE = "cluster.json"; |
| 84 | private static final String HEARTBEAT_MESSAGE = "onos-cluster-heartbeat"; |
Yuta HIGUCHI | d1a63e9 | 2014-12-02 13:14:28 -0800 | [diff] [blame] | 85 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame^] | 86 | public static final int DEFAULT_PORT = 9876; |
| 87 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 88 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 89 | @Override |
| 90 | protected void setupKryoPool() { |
| 91 | serializerPool = KryoNamespace.newBuilder() |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame^] | 92 | .register(KryoNamespaces.API) |
| 93 | .register(HeartbeatMessage.class) |
| 94 | .build() |
| 95 | .populate(1); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 96 | } |
| 97 | }; |
| 98 | |
| 99 | private static final String INSTANCE_ID_NULL = "Instance ID cannot be null"; |
| 100 | |
| 101 | private ClusterDefinition clusterDefinition; |
| 102 | |
| 103 | private Set<ControllerNode> seedNodes; |
| 104 | private final Map<NodeId, ControllerNode> allNodes = Maps.newConcurrentMap(); |
| 105 | private final Map<NodeId, State> nodeStates = Maps.newConcurrentMap(); |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 106 | private final Map<NodeId, DateTime> nodeStateLastUpdatedTimes = Maps.newConcurrentMap(); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 107 | private NettyMessagingService messagingService = new NettyMessagingService(); |
| 108 | private ScheduledExecutorService heartBeatSender = Executors.newSingleThreadScheduledExecutor( |
| 109 | groupedThreads("onos/cluster/membership", "heartbeat-sender")); |
| 110 | private ExecutorService heartBeatMessageHandler = Executors.newSingleThreadExecutor( |
| 111 | groupedThreads("onos/cluster/membership", "heartbeat-receiver")); |
| 112 | |
| 113 | private PhiAccrualFailureDetector failureDetector; |
| 114 | |
| 115 | private ControllerNode localNode; |
| 116 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 117 | @Activate |
| 118 | public void activate() { |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame^] | 119 | File clusterDefinitionFile = new File(CONFIG_DIR, CLUSTER_DEFINITION_FILE); |
| 120 | ClusterDefinitionStore clusterDefinitionStore = |
| 121 | new ClusterDefinitionStore(clusterDefinitionFile.getPath()); |
| 122 | |
| 123 | if (!clusterDefinitionFile.exists()) { |
| 124 | createDefaultClusterDefinition(clusterDefinitionStore); |
| 125 | } |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 126 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 127 | try { |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame^] | 128 | clusterDefinition = clusterDefinitionStore.read(); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 129 | seedNodes = ImmutableSet |
| 130 | .copyOf(clusterDefinition.getNodes()) |
| 131 | .stream() |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame^] | 132 | .map(nodeInfo -> new DefaultControllerNode(new NodeId(nodeInfo.getId()), |
| 133 | IpAddress.valueOf(nodeInfo.getIp()), |
| 134 | nodeInfo.getTcpPort())) |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 135 | .collect(Collectors.toSet()); |
| 136 | } catch (IOException e) { |
| 137 | throw new IllegalStateException( |
| 138 | "Failed to read cluster definition.", e); |
| 139 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 140 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 141 | seedNodes.forEach(node -> { |
| 142 | allNodes.put(node.id(), node); |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 143 | updateState(node.id(), State.INACTIVE); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 144 | }); |
| 145 | |
| 146 | establishSelfIdentity(); |
| 147 | |
| 148 | messagingService = new NettyMessagingService(HEARTBEAT_FD_PORT); |
| 149 | |
| 150 | try { |
| 151 | messagingService.activate(); |
| 152 | } catch (InterruptedException e) { |
| 153 | Thread.currentThread().interrupt(); |
| 154 | throw new IllegalStateException( |
| 155 | "Failed to cleanly initialize membership and" |
| 156 | + " failure detector communication channel.", e); |
| 157 | } |
| 158 | messagingService.registerHandler(HEARTBEAT_MESSAGE, |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame^] | 159 | new HeartbeatMessageHandler(), heartBeatMessageHandler); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 160 | |
| 161 | failureDetector = new PhiAccrualFailureDetector(); |
| 162 | |
| 163 | heartBeatSender.scheduleWithFixedDelay(this::heartbeat, 0, |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame^] | 164 | HEARTBEAT_INTERVAL_MS, TimeUnit.MILLISECONDS); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 165 | |
| 166 | log.info("Started"); |
| 167 | } |
| 168 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame^] | 169 | private void createDefaultClusterDefinition(ClusterDefinitionStore store) { |
| 170 | // Assumes IPv4 is returned. |
| 171 | String ip = DistributedClusterStore.getSiteLocalAddress(); |
| 172 | String ipPrefix = ip.replaceFirst("\\.[0-9]*$", ".*"); |
| 173 | NodeInfo node = NodeInfo.from(ip, ip, DEFAULT_PORT); |
| 174 | try { |
| 175 | store.write(ClusterDefinition.from(ImmutableSet.of(node), ipPrefix)); |
| 176 | } catch (IOException e) { |
| 177 | log.warn("Unable to write default cluster definition", e); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Returns the site local address if one can be found, loopback otherwise. |
| 183 | * |
| 184 | * @return site-local address in string form |
| 185 | */ |
| 186 | public static String getSiteLocalAddress() { |
| 187 | try { |
| 188 | for (NetworkInterface nif : list(getNetworkInterfaces())) { |
| 189 | for (InetAddress address : list(nif.getInetAddresses())) { |
| 190 | if (address.getAddress()[0] == (byte) 0xC0) { |
| 191 | return address.toString().substring(1); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | return InetAddress.getLoopbackAddress().toString().substring(1); |
| 196 | |
| 197 | } catch (SocketException e) { |
| 198 | log.error("Unable to get network interfaces", e); |
| 199 | } |
| 200 | |
| 201 | return null; |
| 202 | } |
| 203 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 204 | @Deactivate |
| 205 | public void deactivate() { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 206 | try { |
| 207 | messagingService.deactivate(); |
| 208 | } catch (Exception e) { |
| 209 | log.trace("Failed to cleanly shutdown cluster membership messaging", e); |
| 210 | } |
| 211 | |
| 212 | heartBeatSender.shutdownNow(); |
| 213 | heartBeatMessageHandler.shutdownNow(); |
| 214 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 215 | log.info("Stopped"); |
| 216 | } |
| 217 | |
| 218 | @Override |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 219 | public void setDelegate(ClusterStoreDelegate delegate) { |
| 220 | checkNotNull(delegate, "Delegate cannot be null"); |
| 221 | this.delegate = delegate; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void unsetDelegate(ClusterStoreDelegate delegate) { |
| 226 | this.delegate = null; |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public boolean hasDelegate() { |
| 231 | return this.delegate != null; |
| 232 | } |
| 233 | |
| 234 | @Override |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 235 | public ControllerNode getLocalNode() { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 236 | return localNode; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public Set<ControllerNode> getNodes() { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 241 | return ImmutableSet.copyOf(allNodes.values()); |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public ControllerNode getNode(NodeId nodeId) { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 246 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 247 | return allNodes.get(nodeId); |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 251 | public State getState(NodeId nodeId) { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 252 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 253 | return nodeStates.get(nodeId); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 257 | public ControllerNode addNode(NodeId nodeId, IpAddress ip, int tcpPort) { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 258 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 259 | checkNotNull(ip, "IP address must not be null"); |
| 260 | checkArgument(tcpPort > 5000, "Tcp port must be greater than 5000"); |
| 261 | ControllerNode node = new DefaultControllerNode(nodeId, ip, tcpPort); |
| 262 | allNodes.put(node.id(), node); |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 263 | updateState(nodeId, State.INACTIVE); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 264 | delegate.notify(new ClusterEvent(ClusterEvent.Type.INSTANCE_ADDED, node)); |
| 265 | return node; |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 269 | public void removeNode(NodeId nodeId) { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 270 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 271 | ControllerNode node = allNodes.remove(nodeId); |
| 272 | if (node != null) { |
| 273 | nodeStates.remove(nodeId); |
| 274 | delegate.notify(new ClusterEvent(ClusterEvent.Type.INSTANCE_REMOVED, node)); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 278 | private void updateState(NodeId nodeId, State newState) { |
| 279 | nodeStates.put(nodeId, newState); |
| 280 | nodeStateLastUpdatedTimes.put(nodeId, DateTime.now()); |
| 281 | } |
| 282 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 283 | private void establishSelfIdentity() { |
| 284 | try { |
| 285 | IpAddress ip = findLocalIp(); |
| 286 | localNode = new DefaultControllerNode(new NodeId(ip.toString()), ip); |
| 287 | allNodes.put(localNode.id(), localNode); |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 288 | updateState(localNode.id(), State.ACTIVE); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 289 | log.info("Local Node: {}", localNode); |
| 290 | } catch (SocketException e) { |
| 291 | throw new IllegalStateException("Cannot determine local IP", e); |
| 292 | } |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 295 | private void heartbeat() { |
| 296 | try { |
| 297 | Set<ControllerNode> peers = allNodes.values() |
| 298 | .stream() |
| 299 | .filter(node -> !(node.id().equals(localNode.id()))) |
| 300 | .collect(Collectors.toSet()); |
| 301 | byte[] hbMessagePayload = SERIALIZER.encode(new HeartbeatMessage(localNode, peers)); |
| 302 | peers.forEach((node) -> { |
| 303 | heartbeatToPeer(hbMessagePayload, node); |
| 304 | State currentState = nodeStates.get(node.id()); |
| 305 | double phi = failureDetector.phi(node.id()); |
| 306 | if (phi >= PHI_FAILURE_THRESHOLD) { |
| 307 | if (currentState == State.ACTIVE) { |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 308 | updateState(node.id(), State.INACTIVE); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 309 | notifyStateChange(node.id(), State.ACTIVE, State.INACTIVE); |
| 310 | } |
| 311 | } else { |
| 312 | if (currentState == State.INACTIVE) { |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 313 | updateState(node.id(), State.ACTIVE); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 314 | notifyStateChange(node.id(), State.INACTIVE, State.ACTIVE); |
| 315 | } |
| 316 | } |
| 317 | }); |
| 318 | } catch (Exception e) { |
| 319 | log.debug("Failed to send heartbeat", e); |
| 320 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 323 | private void notifyStateChange(NodeId nodeId, State oldState, State newState) { |
| 324 | ControllerNode node = allNodes.get(nodeId); |
| 325 | if (newState == State.ACTIVE) { |
| 326 | delegate.notify(new ClusterEvent(ClusterEvent.Type.INSTANCE_ACTIVATED, node)); |
| 327 | } else { |
| 328 | delegate.notify(new ClusterEvent(ClusterEvent.Type.INSTANCE_DEACTIVATED, node)); |
| 329 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 332 | private void heartbeatToPeer(byte[] messagePayload, ControllerNode peer) { |
| 333 | Endpoint remoteEp = new Endpoint(peer.ip(), HEARTBEAT_FD_PORT); |
| 334 | try { |
| 335 | messagingService.sendAsync(remoteEp, HEARTBEAT_MESSAGE, messagePayload); |
| 336 | } catch (IOException e) { |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 337 | log.trace("Sending heartbeat to {} failed", remoteEp, e); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | private IpAddress findLocalIp() throws SocketException { |
| 342 | Enumeration<NetworkInterface> interfaces = |
| 343 | NetworkInterface.getNetworkInterfaces(); |
| 344 | while (interfaces.hasMoreElements()) { |
| 345 | NetworkInterface iface = interfaces.nextElement(); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame^] | 346 | Enumeration<InetAddress> inetAddresses = iface.getInetAddresses(); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 347 | while (inetAddresses.hasMoreElements()) { |
| 348 | IpAddress ip = IpAddress.valueOf(inetAddresses.nextElement()); |
| 349 | if (AddressUtil.matchInterface(ip.toString(), clusterDefinition.getIpPrefix())) { |
| 350 | return ip; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | throw new IllegalStateException("Unable to determine local ip"); |
| 355 | } |
| 356 | |
| 357 | private class HeartbeatMessageHandler implements MessageHandler { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 358 | @Override |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 359 | public void handle(Message message) throws IOException { |
| 360 | HeartbeatMessage hb = SERIALIZER.decode(message.payload()); |
| 361 | failureDetector.report(hb.source().id()); |
| 362 | hb.knownPeers().forEach(node -> { |
| 363 | allNodes.put(node.id(), node); |
| 364 | }); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 365 | } |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 366 | } |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 367 | |
| 368 | private static class HeartbeatMessage { |
| 369 | private ControllerNode source; |
| 370 | private Set<ControllerNode> knownPeers; |
| 371 | |
| 372 | public HeartbeatMessage(ControllerNode source, Set<ControllerNode> members) { |
| 373 | this.source = source; |
| 374 | this.knownPeers = ImmutableSet.copyOf(members); |
| 375 | } |
| 376 | |
| 377 | public ControllerNode source() { |
| 378 | return source; |
| 379 | } |
| 380 | |
| 381 | public Set<ControllerNode> knownPeers() { |
| 382 | return knownPeers; |
| 383 | } |
| 384 | } |
| 385 | |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 386 | @Override |
| 387 | public DateTime getLastUpdated(NodeId nodeId) { |
| 388 | return nodeStateLastUpdatedTimes.get(nodeId); |
| 389 | } |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 390 | } |