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