Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Maps; |
Madan Jampani | e337506 | 2016-04-19 16:32:10 -0700 | [diff] [blame] | 21 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Modified; |
| 26 | import org.apache.felix.scr.annotations.Property; |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Deepa Vaddireddy | 6690f59 | 2017-03-12 03:31:48 +0530 | [diff] [blame^] | 29 | import org.apache.felix.scr.annotations.ReferencePolicy; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 31 | import org.joda.time.DateTime; |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 32 | import org.onlab.packet.IpAddress; |
| 33 | import org.onlab.util.KryoNamespace; |
Deepa Vaddireddy | 6690f59 | 2017-03-12 03:31:48 +0530 | [diff] [blame^] | 34 | import org.onosproject.cfg.ConfigProperty; |
sangyun-han | 9f0af2d | 2016-08-04 13:04:59 +0900 | [diff] [blame] | 35 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.cluster.ClusterEvent; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 37 | import org.onosproject.cluster.ClusterMetadataService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.cluster.ClusterStore; |
| 39 | import org.onosproject.cluster.ClusterStoreDelegate; |
| 40 | import org.onosproject.cluster.ControllerNode; |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 41 | import org.onosproject.cluster.ControllerNode.State; |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 42 | import org.onosproject.cluster.DefaultControllerNode; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.cluster.NodeId; |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 44 | import org.onosproject.store.AbstractStore; |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 45 | import org.onosproject.store.cluster.messaging.Endpoint; |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 46 | import org.onosproject.store.cluster.messaging.MessagingService; |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 47 | import org.onosproject.store.serializers.KryoNamespaces; |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 48 | import org.onosproject.store.serializers.StoreSerializer; |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 49 | import org.osgi.service.component.ComponentContext; |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 50 | import org.slf4j.Logger; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 51 | |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 52 | import java.util.Map; |
Thomas Vachuska | fba7f3d | 2016-03-23 15:46:25 -0700 | [diff] [blame] | 53 | import java.util.Objects; |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 54 | import java.util.Set; |
| 55 | import java.util.concurrent.ExecutorService; |
| 56 | import java.util.concurrent.Executors; |
| 57 | import java.util.concurrent.ScheduledExecutorService; |
| 58 | import java.util.concurrent.TimeUnit; |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 59 | import java.util.function.BiConsumer; |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 60 | import java.util.stream.Collectors; |
| 61 | |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 62 | import static com.google.common.base.Preconditions.checkArgument; |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 63 | import static com.google.common.base.Preconditions.checkNotNull; |
Deepa Vaddireddy | 6690f59 | 2017-03-12 03:31:48 +0530 | [diff] [blame^] | 64 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 65 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | fba7f3d | 2016-03-23 15:46:25 -0700 | [diff] [blame] | 66 | import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ACTIVATED; |
| 67 | import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_DEACTIVATED; |
| 68 | import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_READY; |
Jonathan Hart | 4a4d18f | 2015-03-26 12:16:16 -0700 | [diff] [blame] | 69 | import static org.slf4j.LoggerFactory.getLogger; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 70 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 71 | @Component(immediate = true) |
| 72 | @Service |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 73 | /** |
| 74 | * Distributed cluster nodes store that employs an accrual failure |
| 75 | * detector to identify cluster member up/down status. |
| 76 | */ |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 77 | public class DistributedClusterStore |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 78 | extends AbstractStore<ClusterEvent, ClusterStoreDelegate> |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 79 | implements ClusterStore { |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 80 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 81 | private static final Logger log = getLogger(DistributedClusterStore.class); |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 82 | |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 83 | public static final String HEARTBEAT_MESSAGE = "onos-cluster-heartbeat"; |
| 84 | |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 85 | private static final int DEFAULT_HEARTBEAT_INTERVAL = 100; |
| 86 | @Property(name = "heartbeatInterval", intValue = DEFAULT_HEARTBEAT_INTERVAL, |
| 87 | label = "Interval time to send heartbeat to other controller nodes (millisecond)") |
| 88 | private int heartbeatInterval = DEFAULT_HEARTBEAT_INTERVAL; |
| 89 | |
| 90 | private static final int DEFAULT_PHI_FAILURE_THRESHOLD = 10; |
| 91 | @Property(name = "phiFailureThreshold", intValue = DEFAULT_PHI_FAILURE_THRESHOLD, |
| 92 | label = "the value of Phi threshold to detect accrual failure") |
| 93 | private int phiFailureThreshold = DEFAULT_PHI_FAILURE_THRESHOLD; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 94 | |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 95 | private static final StoreSerializer SERIALIZER = StoreSerializer.using( |
| 96 | KryoNamespace.newBuilder() |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 97 | .register(KryoNamespaces.API) |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 98 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 99 | .register(HeartbeatMessage.class) |
HIGUCHI Yuta | e729065 | 2016-05-18 11:29:01 -0700 | [diff] [blame] | 100 | .build("ClusterStore")); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 101 | |
| 102 | private static final String INSTANCE_ID_NULL = "Instance ID cannot be null"; |
| 103 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 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(); |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 107 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 108 | private ScheduledExecutorService heartBeatSender = Executors.newSingleThreadScheduledExecutor( |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 109 | groupedThreads("onos/cluster/membership", "heartbeat-sender", log)); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 110 | private ExecutorService heartBeatMessageHandler = Executors.newSingleThreadExecutor( |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 111 | groupedThreads("onos/cluster/membership", "heartbeat-receiver", log)); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 112 | |
| 113 | private PhiAccrualFailureDetector failureDetector; |
| 114 | |
| 115 | private ControllerNode localNode; |
| 116 | |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 118 | protected ClusterMetadataService clusterMetadataService; |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 121 | protected MessagingService messagingService; |
| 122 | |
Thomas Vachuska | 1ca7e9f | 2016-08-05 10:21:41 -0700 | [diff] [blame] | 123 | // This must be optional to avoid a cyclic dependency |
Deepa Vaddireddy | 6690f59 | 2017-03-12 03:31:48 +0530 | [diff] [blame^] | 124 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, |
| 125 | bind = "bindComponentConfigService", |
| 126 | unbind = "unbindComponentConfigService", |
| 127 | policy = ReferencePolicy.DYNAMIC) |
sangyun-han | 9f0af2d | 2016-08-04 13:04:59 +0900 | [diff] [blame] | 128 | protected ComponentConfigService cfgService; |
| 129 | |
Thomas Vachuska | 1ca7e9f | 2016-08-05 10:21:41 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Hook for wiring up optional reference to a service. |
| 132 | * |
| 133 | * @param service service being announced |
| 134 | */ |
| 135 | protected void bindComponentConfigService(ComponentConfigService service) { |
| 136 | if (cfgService == null) { |
| 137 | cfgService = service; |
| 138 | cfgService.registerProperties(getClass()); |
Deepa Vaddireddy | 6690f59 | 2017-03-12 03:31:48 +0530 | [diff] [blame^] | 139 | readComponentConfiguration(); |
Thomas Vachuska | 1ca7e9f | 2016-08-05 10:21:41 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Hook for unwiring optional reference to a service. |
| 145 | * |
| 146 | * @param service service being withdrawn |
| 147 | */ |
| 148 | protected void unbindComponentConfigService(ComponentConfigService service) { |
| 149 | if (cfgService == service) { |
| 150 | cfgService.unregisterProperties(getClass(), false); |
| 151 | cfgService = null; |
| 152 | } |
| 153 | } |
| 154 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 155 | @Activate |
Thomas Vachuska | 1ca7e9f | 2016-08-05 10:21:41 -0700 | [diff] [blame] | 156 | public void activate() { |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 157 | localNode = clusterMetadataService.getLocalNode(); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 158 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 159 | messagingService.registerHandler(HEARTBEAT_MESSAGE, |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 160 | new HeartbeatMessageHandler(), heartBeatMessageHandler); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 161 | |
| 162 | failureDetector = new PhiAccrualFailureDetector(); |
| 163 | |
| 164 | heartBeatSender.scheduleWithFixedDelay(this::heartbeat, 0, |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 165 | heartbeatInterval, TimeUnit.MILLISECONDS); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 166 | |
| 167 | log.info("Started"); |
| 168 | } |
| 169 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 170 | @Deactivate |
| 171 | public void deactivate() { |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 172 | messagingService.unregisterHandler(HEARTBEAT_MESSAGE); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 173 | heartBeatSender.shutdownNow(); |
| 174 | heartBeatMessageHandler.shutdownNow(); |
| 175 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 176 | log.info("Stopped"); |
| 177 | } |
| 178 | |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 179 | @Modified |
| 180 | public void modified(ComponentContext context) { |
Deepa Vaddireddy | 6690f59 | 2017-03-12 03:31:48 +0530 | [diff] [blame^] | 181 | readComponentConfiguration(); |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 182 | } |
| 183 | |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 184 | @Override |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 185 | public void setDelegate(ClusterStoreDelegate delegate) { |
| 186 | checkNotNull(delegate, "Delegate cannot be null"); |
| 187 | this.delegate = delegate; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void unsetDelegate(ClusterStoreDelegate delegate) { |
| 192 | this.delegate = null; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public boolean hasDelegate() { |
| 197 | return this.delegate != null; |
| 198 | } |
| 199 | |
| 200 | @Override |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 201 | public ControllerNode getLocalNode() { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 202 | return localNode; |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Set<ControllerNode> getNodes() { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 207 | return ImmutableSet.copyOf(allNodes.values()); |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public ControllerNode getNode(NodeId nodeId) { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 212 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 213 | return allNodes.get(nodeId); |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 217 | public State getState(NodeId nodeId) { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 218 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 219 | return MoreObjects.firstNonNull(nodeStates.get(nodeId), State.INACTIVE); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | @Override |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 223 | public void markFullyStarted(boolean started) { |
| 224 | updateState(localNode.id(), started ? State.READY : State.ACTIVE); |
| 225 | } |
| 226 | |
| 227 | @Override |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 228 | public ControllerNode addNode(NodeId nodeId, IpAddress ip, int tcpPort) { |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 229 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 230 | ControllerNode node = new DefaultControllerNode(nodeId, ip, tcpPort); |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 231 | addNode(node); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 232 | return node; |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 236 | public void removeNode(NodeId nodeId) { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 237 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 238 | ControllerNode node = allNodes.remove(nodeId); |
| 239 | if (node != null) { |
| 240 | nodeStates.remove(nodeId); |
Jonathan Hart | f114126 | 2015-04-23 11:27:07 -0700 | [diff] [blame] | 241 | notifyDelegate(new ClusterEvent(ClusterEvent.Type.INSTANCE_REMOVED, node)); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 245 | private void addNode(ControllerNode node) { |
| 246 | allNodes.put(node.id(), node); |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 247 | updateState(node.id(), node.equals(localNode) ? State.ACTIVE : State.INACTIVE); |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 248 | notifyDelegate(new ClusterEvent(ClusterEvent.Type.INSTANCE_ADDED, node)); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 251 | private void updateState(NodeId nodeId, State newState) { |
Thomas Vachuska | fba7f3d | 2016-03-23 15:46:25 -0700 | [diff] [blame] | 252 | State currentState = nodeStates.get(nodeId); |
| 253 | if (!Objects.equals(currentState, newState)) { |
| 254 | nodeStates.put(nodeId, newState); |
| 255 | nodeStateLastUpdatedTimes.put(nodeId, DateTime.now()); |
| 256 | notifyStateChange(nodeId, currentState, newState); |
| 257 | } |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 260 | private void heartbeat() { |
| 261 | try { |
| 262 | Set<ControllerNode> peers = allNodes.values() |
| 263 | .stream() |
| 264 | .filter(node -> !(node.id().equals(localNode.id()))) |
| 265 | .collect(Collectors.toSet()); |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 266 | State state = nodeStates.get(localNode.id()); |
Madan Jampani | e337506 | 2016-04-19 16:32:10 -0700 | [diff] [blame] | 267 | byte[] hbMessagePayload = SERIALIZER.encode(new HeartbeatMessage(localNode, state)); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 268 | peers.forEach((node) -> { |
| 269 | heartbeatToPeer(hbMessagePayload, node); |
| 270 | State currentState = nodeStates.get(node.id()); |
| 271 | double phi = failureDetector.phi(node.id()); |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 272 | if (phi >= phiFailureThreshold) { |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 273 | if (currentState.isActive()) { |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 274 | updateState(node.id(), State.INACTIVE); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 275 | } |
| 276 | } else { |
| 277 | if (currentState == State.INACTIVE) { |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 278 | updateState(node.id(), State.ACTIVE); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | }); |
| 282 | } catch (Exception e) { |
| 283 | log.debug("Failed to send heartbeat", e); |
| 284 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 287 | private void notifyStateChange(NodeId nodeId, State oldState, State newState) { |
Thomas Vachuska | fba7f3d | 2016-03-23 15:46:25 -0700 | [diff] [blame] | 288 | if (oldState != newState) { |
| 289 | ControllerNode node = allNodes.get(nodeId); |
Jon Hall | 1f13d64 | 2016-05-13 17:53:01 -0700 | [diff] [blame] | 290 | // Either this node or that node is no longer part of the same cluster |
| 291 | if (node == null) { |
Jon Hall | 66870baa | 2016-06-20 12:12:10 -0700 | [diff] [blame] | 292 | log.debug("Could not find node {} in the cluster, ignoring state change", nodeId); |
Jon Hall | 1f13d64 | 2016-05-13 17:53:01 -0700 | [diff] [blame] | 293 | return; |
| 294 | } |
Thomas Vachuska | fba7f3d | 2016-03-23 15:46:25 -0700 | [diff] [blame] | 295 | ClusterEvent.Type type = newState == State.READY ? INSTANCE_READY : |
| 296 | newState == State.ACTIVE ? INSTANCE_ACTIVATED : |
| 297 | INSTANCE_DEACTIVATED; |
| 298 | notifyDelegate(new ClusterEvent(type, node)); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 299 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 302 | private void heartbeatToPeer(byte[] messagePayload, ControllerNode peer) { |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 303 | Endpoint remoteEp = new Endpoint(peer.ip(), peer.tcpPort()); |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 304 | messagingService.sendAsync(remoteEp, HEARTBEAT_MESSAGE, messagePayload).whenComplete((result, error) -> { |
| 305 | if (error != null) { |
| 306 | log.trace("Sending heartbeat to {} failed", remoteEp, error); |
| 307 | } |
| 308 | }); |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 311 | private class HeartbeatMessageHandler implements BiConsumer<Endpoint, byte[]> { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 312 | @Override |
Madan Jampani | d36def0 | 2016-01-13 11:21:56 -0800 | [diff] [blame] | 313 | public void accept(Endpoint sender, byte[] message) { |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 314 | HeartbeatMessage hb = SERIALIZER.decode(message); |
Madan Jampani | e337506 | 2016-04-19 16:32:10 -0700 | [diff] [blame] | 315 | if (clusterMetadataService.getClusterMetadata().getNodes().contains(hb.source())) { |
| 316 | failureDetector.report(hb.source().id()); |
| 317 | updateState(hb.source().id(), hb.state); |
| 318 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 319 | } |
tom | 2d7c65f | 2014-09-23 01:09:35 -0700 | [diff] [blame] | 320 | } |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 321 | |
| 322 | private static class HeartbeatMessage { |
| 323 | private ControllerNode source; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 324 | private State state; |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 325 | |
Madan Jampani | e337506 | 2016-04-19 16:32:10 -0700 | [diff] [blame] | 326 | public HeartbeatMessage(ControllerNode source, State state) { |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 327 | this.source = source; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 328 | this.state = state != null ? state : State.ACTIVE; |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | public ControllerNode source() { |
| 332 | return source; |
| 333 | } |
Ayaka Koshibe | dd91b84 | 2015-03-02 14:48:47 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 336 | @Override |
| 337 | public DateTime getLastUpdated(NodeId nodeId) { |
| 338 | return nodeStateLastUpdatedTimes.get(nodeId); |
| 339 | } |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 340 | |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 341 | /** |
Deepa Vaddireddy | 6690f59 | 2017-03-12 03:31:48 +0530 | [diff] [blame^] | 342 | * Extracts properties from the component configuration. |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 343 | * |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 344 | */ |
Deepa Vaddireddy | 6690f59 | 2017-03-12 03:31:48 +0530 | [diff] [blame^] | 345 | private void readComponentConfiguration() { |
| 346 | Set<ConfigProperty> configProperties = cfgService.getProperties(getClass().getName()); |
| 347 | for (ConfigProperty property : configProperties) { |
| 348 | if (property.name().equals("heartbeatInterval")) { |
| 349 | String s = property.value(); |
| 350 | if (s == null) { |
| 351 | setHeartbeatInterval(DEFAULT_HEARTBEAT_INTERVAL); |
| 352 | log.info("Heartbeat interval time is not configured, default value is {}", |
| 353 | DEFAULT_HEARTBEAT_INTERVAL); |
| 354 | } else { |
| 355 | int newHeartbeatInterval = isNullOrEmpty(s) ? DEFAULT_HEARTBEAT_INTERVAL |
| 356 | : Integer.parseInt(s.trim()); |
| 357 | if (newHeartbeatInterval > 0 && heartbeatInterval != newHeartbeatInterval) { |
| 358 | heartbeatInterval = newHeartbeatInterval; |
| 359 | restartHeartbeatSender(); |
| 360 | } |
| 361 | log.info("Configured. Heartbeat interval time is configured to {}", |
| 362 | heartbeatInterval); |
| 363 | } |
| 364 | } |
| 365 | if (property.name().equals("phiFailureThreshold")) { |
| 366 | String s = property.value(); |
| 367 | if (s == null) { |
| 368 | setPhiFailureThreshold(DEFAULT_PHI_FAILURE_THRESHOLD); |
| 369 | log.info("Phi failure threshold is not configured, default value is {}", |
| 370 | DEFAULT_PHI_FAILURE_THRESHOLD); |
| 371 | } else { |
| 372 | int newPhiFailureThreshold = isNullOrEmpty(s) ? DEFAULT_HEARTBEAT_INTERVAL |
| 373 | : Integer.parseInt(s.trim()); |
| 374 | setPhiFailureThreshold(newPhiFailureThreshold); |
| 375 | log.info("Configured. Phi failure threshold is configured to {}", |
| 376 | phiFailureThreshold); |
| 377 | } |
| 378 | } |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Sets heartbeat interval between the termination of one execution of heartbeat |
| 384 | * and the commencement of the next. |
| 385 | * |
| 386 | * @param interval term between each heartbeat |
| 387 | */ |
| 388 | private void setHeartbeatInterval(int interval) { |
| 389 | try { |
| 390 | checkArgument(interval > 0, "Interval must be greater than zero"); |
| 391 | heartbeatInterval = interval; |
| 392 | } catch (IllegalArgumentException e) { |
| 393 | log.warn(e.getMessage()); |
| 394 | heartbeatInterval = DEFAULT_HEARTBEAT_INTERVAL; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Sets Phi failure threshold. |
| 400 | * Phi is based on a paper titled: "The φ Accrual Failure Detector" by Hayashibara, et al. |
| 401 | * |
| 402 | * @param threshold |
| 403 | */ |
| 404 | private void setPhiFailureThreshold(int threshold) { |
| 405 | phiFailureThreshold = threshold; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Restarts heartbeatSender executor. |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 410 | */ |
| 411 | private void restartHeartbeatSender() { |
| 412 | try { |
| 413 | ScheduledExecutorService prevSender = heartBeatSender; |
| 414 | heartBeatSender = Executors.newSingleThreadScheduledExecutor( |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 415 | groupedThreads("onos/cluster/membership", "heartbeat-sender-%d", log)); |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 416 | heartBeatSender.scheduleWithFixedDelay(this::heartbeat, 0, |
| 417 | heartbeatInterval, TimeUnit.MILLISECONDS); |
| 418 | prevSender.shutdown(); |
| 419 | } catch (Exception e) { |
| 420 | log.warn(e.getMessage()); |
| 421 | } |
| 422 | } |
Jon Hall | 1f13d64 | 2016-05-13 17:53:01 -0700 | [diff] [blame] | 423 | } |