blob: 97db574f55a54d7109448f748ba8e210cb21d0b3 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.cluster.impl;
tom2d7c65f2014-09-23 01:09:35 -070017
HIGUCHI Yuta1979f552015-12-28 21:24:26 -080018import com.google.common.base.MoreObjects;
Jonathan Hart4a4d18f2015-03-26 12:16:16 -070019import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Maps;
Madan Jampanie3375062016-04-19 16:32:10 -070021
tom2d7c65f2014-09-23 01:09:35 -070022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
sangyun-hanf98df542016-03-24 20:28:03 +090025import org.apache.felix.scr.annotations.Modified;
26import org.apache.felix.scr.annotations.Property;
Madan Jampaniafeebbd2015-05-19 15:26:01 -070027import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
tom2d7c65f2014-09-23 01:09:35 -070029import org.apache.felix.scr.annotations.Service;
Madan Jampani7d2fab22015-03-18 17:21:57 -070030import org.joda.time.DateTime;
Ayaka Koshibedd91b842015-03-02 14:48:47 -080031import org.onlab.packet.IpAddress;
32import org.onlab.util.KryoNamespace;
sangyun-hanf98df542016-03-24 20:28:03 +090033import org.onlab.util.Tools;
sangyun-han9f0af2d2016-08-04 13:04:59 +090034import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.cluster.ClusterEvent;
Madan Jampaniec1df022015-10-13 21:23:03 -070036import org.onosproject.cluster.ClusterMetadataService;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.cluster.ClusterStore;
38import org.onosproject.cluster.ClusterStoreDelegate;
39import org.onosproject.cluster.ControllerNode;
Ayaka Koshibedd91b842015-03-02 14:48:47 -080040import org.onosproject.cluster.ControllerNode.State;
Jonathan Hart4a4d18f2015-03-26 12:16:16 -070041import org.onosproject.cluster.DefaultControllerNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.cluster.NodeId;
Ayaka Koshibedd91b842015-03-02 14:48:47 -080043import org.onosproject.store.AbstractStore;
Madan Jampanic26eede2015-04-16 11:42:16 -070044import org.onosproject.store.cluster.messaging.Endpoint;
Madan Jampaniafeebbd2015-05-19 15:26:01 -070045import org.onosproject.store.cluster.messaging.MessagingService;
Ayaka Koshibedd91b842015-03-02 14:48:47 -080046import org.onosproject.store.serializers.KryoNamespaces;
HIGUCHI Yutae7290652016-05-18 11:29:01 -070047import org.onosproject.store.serializers.StoreSerializer;
sangyun-hanf98df542016-03-24 20:28:03 +090048import org.osgi.service.component.ComponentContext;
Ayaka Koshibedd91b842015-03-02 14:48:47 -080049import org.slf4j.Logger;
tom2d7c65f2014-09-23 01:09:35 -070050
sangyun-hanf98df542016-03-24 20:28:03 +090051import java.util.Dictionary;
Jonathan Hart4a4d18f2015-03-26 12:16:16 -070052import java.util.Map;
Thomas Vachuskafba7f3d2016-03-23 15:46:25 -070053import java.util.Objects;
Jonathan Hart4a4d18f2015-03-26 12:16:16 -070054import java.util.Set;
55import java.util.concurrent.ExecutorService;
56import java.util.concurrent.Executors;
57import java.util.concurrent.ScheduledExecutorService;
58import java.util.concurrent.TimeUnit;
Madan Jampanid36def02016-01-13 11:21:56 -080059import java.util.function.BiConsumer;
Jonathan Hart4a4d18f2015-03-26 12:16:16 -070060import java.util.stream.Collectors;
61
sangyun-hanf98df542016-03-24 20:28:03 +090062import static com.google.common.base.Preconditions.checkArgument;
Jonathan Hart4a4d18f2015-03-26 12:16:16 -070063import static com.google.common.base.Preconditions.checkNotNull;
64import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskafba7f3d2016-03-23 15:46:25 -070065import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ACTIVATED;
66import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_DEACTIVATED;
67import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_READY;
Jonathan Hart4a4d18f2015-03-26 12:16:16 -070068import static org.slf4j.LoggerFactory.getLogger;
tom2d7c65f2014-09-23 01:09:35 -070069
tom2d7c65f2014-09-23 01:09:35 -070070@Component(immediate = true)
71@Service
Ayaka Koshibedd91b842015-03-02 14:48:47 -080072/**
73 * Distributed cluster nodes store that employs an accrual failure
74 * detector to identify cluster member up/down status.
75 */
tom0755a362014-09-24 11:54:43 -070076public class DistributedClusterStore
Ayaka Koshibedd91b842015-03-02 14:48:47 -080077 extends AbstractStore<ClusterEvent, ClusterStoreDelegate>
tomb41d1ac2014-09-24 01:51:24 -070078 implements ClusterStore {
tom2d7c65f2014-09-23 01:09:35 -070079
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070080 private static final Logger log = getLogger(DistributedClusterStore.class);
tom2d7c65f2014-09-23 01:09:35 -070081
Thomas Vachuskade563cf2015-04-01 00:28:50 -070082 public static final String HEARTBEAT_MESSAGE = "onos-cluster-heartbeat";
83
sangyun-hanf98df542016-03-24 20:28:03 +090084 private static final int DEFAULT_HEARTBEAT_INTERVAL = 100;
85 @Property(name = "heartbeatInterval", intValue = DEFAULT_HEARTBEAT_INTERVAL,
86 label = "Interval time to send heartbeat to other controller nodes (millisecond)")
87 private int heartbeatInterval = DEFAULT_HEARTBEAT_INTERVAL;
88
89 private static final int DEFAULT_PHI_FAILURE_THRESHOLD = 10;
90 @Property(name = "phiFailureThreshold", intValue = DEFAULT_PHI_FAILURE_THRESHOLD,
91 label = "the value of Phi threshold to detect accrual failure")
92 private int phiFailureThreshold = DEFAULT_PHI_FAILURE_THRESHOLD;
tom2d7c65f2014-09-23 01:09:35 -070093
HIGUCHI Yutae7290652016-05-18 11:29:01 -070094 private static final StoreSerializer SERIALIZER = StoreSerializer.using(
95 KryoNamespace.newBuilder()
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070096 .register(KryoNamespaces.API)
HIGUCHI Yutae7290652016-05-18 11:29:01 -070097 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070098 .register(HeartbeatMessage.class)
HIGUCHI Yutae7290652016-05-18 11:29:01 -070099 .build("ClusterStore"));
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800100
101 private static final String INSTANCE_ID_NULL = "Instance ID cannot be null";
102
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800103 private final Map<NodeId, ControllerNode> allNodes = Maps.newConcurrentMap();
104 private final Map<NodeId, State> nodeStates = Maps.newConcurrentMap();
Madan Jampani7d2fab22015-03-18 17:21:57 -0700105 private final Map<NodeId, DateTime> nodeStateLastUpdatedTimes = Maps.newConcurrentMap();
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800106
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800107 private ScheduledExecutorService heartBeatSender = Executors.newSingleThreadScheduledExecutor(
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700108 groupedThreads("onos/cluster/membership", "heartbeat-sender", log));
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800109 private ExecutorService heartBeatMessageHandler = Executors.newSingleThreadExecutor(
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700110 groupedThreads("onos/cluster/membership", "heartbeat-receiver", log));
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800111
112 private PhiAccrualFailureDetector failureDetector;
113
114 private ControllerNode localNode;
115
Madan Jampaniafeebbd2015-05-19 15:26:01 -0700116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Madan Jampaniec1df022015-10-13 21:23:03 -0700117 protected ClusterMetadataService clusterMetadataService;
Madan Jampaniafeebbd2015-05-19 15:26:01 -0700118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
120 protected MessagingService messagingService;
121
Thomas Vachuska1ca7e9f2016-08-05 10:21:41 -0700122 // This must be optional to avoid a cyclic dependency
123 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
sangyun-han9f0af2d2016-08-04 13:04:59 +0900124 protected ComponentConfigService cfgService;
125
Thomas Vachuska1ca7e9f2016-08-05 10:21:41 -0700126 /**
127 * Hook for wiring up optional reference to a service.
128 *
129 * @param service service being announced
130 */
131 protected void bindComponentConfigService(ComponentConfigService service) {
132 if (cfgService == null) {
133 cfgService = service;
134 cfgService.registerProperties(getClass());
135 }
136 }
137
138 /**
139 * Hook for unwiring optional reference to a service.
140 *
141 * @param service service being withdrawn
142 */
143 protected void unbindComponentConfigService(ComponentConfigService service) {
144 if (cfgService == service) {
145 cfgService.unregisterProperties(getClass(), false);
146 cfgService = null;
147 }
148 }
149
tom2d7c65f2014-09-23 01:09:35 -0700150 @Activate
Thomas Vachuska1ca7e9f2016-08-05 10:21:41 -0700151 public void activate() {
Madan Jampaniec1df022015-10-13 21:23:03 -0700152 localNode = clusterMetadataService.getLocalNode();
Thomas Vachuska8dc1a692015-03-31 01:01:37 -0700153
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800154 messagingService.registerHandler(HEARTBEAT_MESSAGE,
Thomas Vachuska8dc1a692015-03-31 01:01:37 -0700155 new HeartbeatMessageHandler(), heartBeatMessageHandler);
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800156
157 failureDetector = new PhiAccrualFailureDetector();
158
159 heartBeatSender.scheduleWithFixedDelay(this::heartbeat, 0,
sangyun-hanf98df542016-03-24 20:28:03 +0900160 heartbeatInterval, TimeUnit.MILLISECONDS);
tomb41d1ac2014-09-24 01:51:24 -0700161
162 log.info("Started");
163 }
164
tom2d7c65f2014-09-23 01:09:35 -0700165 @Deactivate
166 public void deactivate() {
Madan Jampaniafeebbd2015-05-19 15:26:01 -0700167 messagingService.unregisterHandler(HEARTBEAT_MESSAGE);
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800168 heartBeatSender.shutdownNow();
169 heartBeatMessageHandler.shutdownNow();
170
tom2d7c65f2014-09-23 01:09:35 -0700171 log.info("Stopped");
172 }
173
sangyun-hanf98df542016-03-24 20:28:03 +0900174 @Modified
175 public void modified(ComponentContext context) {
176 readComponentConfiguration(context);
177 restartHeartbeatSender();
178 }
179
tom2d7c65f2014-09-23 01:09:35 -0700180 @Override
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800181 public void setDelegate(ClusterStoreDelegate delegate) {
182 checkNotNull(delegate, "Delegate cannot be null");
183 this.delegate = delegate;
184 }
185
186 @Override
187 public void unsetDelegate(ClusterStoreDelegate delegate) {
188 this.delegate = null;
189 }
190
191 @Override
192 public boolean hasDelegate() {
193 return this.delegate != null;
194 }
195
196 @Override
tom2d7c65f2014-09-23 01:09:35 -0700197 public ControllerNode getLocalNode() {
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800198 return localNode;
tom2d7c65f2014-09-23 01:09:35 -0700199 }
200
201 @Override
202 public Set<ControllerNode> getNodes() {
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800203 return ImmutableSet.copyOf(allNodes.values());
tom2d7c65f2014-09-23 01:09:35 -0700204 }
205
206 @Override
207 public ControllerNode getNode(NodeId nodeId) {
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800208 checkNotNull(nodeId, INSTANCE_ID_NULL);
209 return allNodes.get(nodeId);
tom2d7c65f2014-09-23 01:09:35 -0700210 }
211
212 @Override
tomb41d1ac2014-09-24 01:51:24 -0700213 public State getState(NodeId nodeId) {
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800214 checkNotNull(nodeId, INSTANCE_ID_NULL);
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800215 return MoreObjects.firstNonNull(nodeStates.get(nodeId), State.INACTIVE);
tomb41d1ac2014-09-24 01:51:24 -0700216 }
217
218 @Override
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800219 public void markFullyStarted(boolean started) {
220 updateState(localNode.id(), started ? State.READY : State.ACTIVE);
221 }
222
223 @Override
Pavlin Radoslavov444b5192014-10-28 10:45:19 -0700224 public ControllerNode addNode(NodeId nodeId, IpAddress ip, int tcpPort) {
sangyun-hanf98df542016-03-24 20:28:03 +0900225 checkNotNull(nodeId, INSTANCE_ID_NULL);
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800226 ControllerNode node = new DefaultControllerNode(nodeId, ip, tcpPort);
Madan Jampaniafeebbd2015-05-19 15:26:01 -0700227 addNode(node);
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800228 return node;
tomee49c372014-09-26 15:14:50 -0700229 }
230
231 @Override
tomb41d1ac2014-09-24 01:51:24 -0700232 public void removeNode(NodeId nodeId) {
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800233 checkNotNull(nodeId, INSTANCE_ID_NULL);
234 ControllerNode node = allNodes.remove(nodeId);
235 if (node != null) {
236 nodeStates.remove(nodeId);
Jonathan Hartf1141262015-04-23 11:27:07 -0700237 notifyDelegate(new ClusterEvent(ClusterEvent.Type.INSTANCE_REMOVED, node));
tomb41d1ac2014-09-24 01:51:24 -0700238 }
239 }
240
Madan Jampaniafeebbd2015-05-19 15:26:01 -0700241 private void addNode(ControllerNode node) {
242 allNodes.put(node.id(), node);
Madan Jampaniec1df022015-10-13 21:23:03 -0700243 updateState(node.id(), node.equals(localNode) ? State.ACTIVE : State.INACTIVE);
Madan Jampaniafeebbd2015-05-19 15:26:01 -0700244 notifyDelegate(new ClusterEvent(ClusterEvent.Type.INSTANCE_ADDED, node));
Thomas Vachuskade563cf2015-04-01 00:28:50 -0700245 }
246
Madan Jampani7d2fab22015-03-18 17:21:57 -0700247 private void updateState(NodeId nodeId, State newState) {
Thomas Vachuskafba7f3d2016-03-23 15:46:25 -0700248 State currentState = nodeStates.get(nodeId);
249 if (!Objects.equals(currentState, newState)) {
250 nodeStates.put(nodeId, newState);
251 nodeStateLastUpdatedTimes.put(nodeId, DateTime.now());
252 notifyStateChange(nodeId, currentState, newState);
253 }
Madan Jampani7d2fab22015-03-18 17:21:57 -0700254 }
255
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800256 private void heartbeat() {
257 try {
258 Set<ControllerNode> peers = allNodes.values()
259 .stream()
260 .filter(node -> !(node.id().equals(localNode.id())))
261 .collect(Collectors.toSet());
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800262 State state = nodeStates.get(localNode.id());
Madan Jampanie3375062016-04-19 16:32:10 -0700263 byte[] hbMessagePayload = SERIALIZER.encode(new HeartbeatMessage(localNode, state));
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800264 peers.forEach((node) -> {
265 heartbeatToPeer(hbMessagePayload, node);
266 State currentState = nodeStates.get(node.id());
267 double phi = failureDetector.phi(node.id());
sangyun-hanf98df542016-03-24 20:28:03 +0900268 if (phi >= phiFailureThreshold) {
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800269 if (currentState.isActive()) {
Madan Jampani7d2fab22015-03-18 17:21:57 -0700270 updateState(node.id(), State.INACTIVE);
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800271 }
272 } else {
273 if (currentState == State.INACTIVE) {
Madan Jampani7d2fab22015-03-18 17:21:57 -0700274 updateState(node.id(), State.ACTIVE);
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800275 }
276 }
277 });
278 } catch (Exception e) {
279 log.debug("Failed to send heartbeat", e);
280 }
tomb41d1ac2014-09-24 01:51:24 -0700281 }
282
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800283 private void notifyStateChange(NodeId nodeId, State oldState, State newState) {
Thomas Vachuskafba7f3d2016-03-23 15:46:25 -0700284 if (oldState != newState) {
285 ControllerNode node = allNodes.get(nodeId);
Jon Hall1f13d642016-05-13 17:53:01 -0700286 // Either this node or that node is no longer part of the same cluster
287 if (node == null) {
Jon Hall66870baa2016-06-20 12:12:10 -0700288 log.debug("Could not find node {} in the cluster, ignoring state change", nodeId);
Jon Hall1f13d642016-05-13 17:53:01 -0700289 return;
290 }
Thomas Vachuskafba7f3d2016-03-23 15:46:25 -0700291 ClusterEvent.Type type = newState == State.READY ? INSTANCE_READY :
292 newState == State.ACTIVE ? INSTANCE_ACTIVATED :
293 INSTANCE_DEACTIVATED;
294 notifyDelegate(new ClusterEvent(type, node));
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800295 }
tomb41d1ac2014-09-24 01:51:24 -0700296 }
297
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800298 private void heartbeatToPeer(byte[] messagePayload, ControllerNode peer) {
Madan Jampaniafeebbd2015-05-19 15:26:01 -0700299 Endpoint remoteEp = new Endpoint(peer.ip(), peer.tcpPort());
Madan Jampani175e8fd2015-05-20 14:10:45 -0700300 messagingService.sendAsync(remoteEp, HEARTBEAT_MESSAGE, messagePayload).whenComplete((result, error) -> {
301 if (error != null) {
302 log.trace("Sending heartbeat to {} failed", remoteEp, error);
303 }
304 });
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800305 }
306
Madan Jampanid36def02016-01-13 11:21:56 -0800307 private class HeartbeatMessageHandler implements BiConsumer<Endpoint, byte[]> {
tomb41d1ac2014-09-24 01:51:24 -0700308 @Override
Madan Jampanid36def02016-01-13 11:21:56 -0800309 public void accept(Endpoint sender, byte[] message) {
Madan Jampanic26eede2015-04-16 11:42:16 -0700310 HeartbeatMessage hb = SERIALIZER.decode(message);
Madan Jampanie3375062016-04-19 16:32:10 -0700311 if (clusterMetadataService.getClusterMetadata().getNodes().contains(hb.source())) {
312 failureDetector.report(hb.source().id());
313 updateState(hb.source().id(), hb.state);
314 }
tomb41d1ac2014-09-24 01:51:24 -0700315 }
tom2d7c65f2014-09-23 01:09:35 -0700316 }
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800317
318 private static class HeartbeatMessage {
319 private ControllerNode source;
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800320 private State state;
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800321
Madan Jampanie3375062016-04-19 16:32:10 -0700322 public HeartbeatMessage(ControllerNode source, State state) {
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800323 this.source = source;
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800324 this.state = state != null ? state : State.ACTIVE;
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800325 }
326
327 public ControllerNode source() {
328 return source;
329 }
Ayaka Koshibedd91b842015-03-02 14:48:47 -0800330 }
331
Madan Jampani7d2fab22015-03-18 17:21:57 -0700332 @Override
333 public DateTime getLastUpdated(NodeId nodeId) {
334 return nodeStateLastUpdatedTimes.get(nodeId);
335 }
Thomas Vachuskade563cf2015-04-01 00:28:50 -0700336
sangyun-hanf98df542016-03-24 20:28:03 +0900337 /**
338 * Extracts properties from the component configuration context.
339 *
340 * @param context the component context
341 */
342 private void readComponentConfiguration(ComponentContext context) {
343 Dictionary<?, ?> properties = context.getProperties();
344
345 Integer newHeartbeatInterval = Tools.getIntegerProperty(properties,
346 "heartbeatInterval");
347 if (newHeartbeatInterval == null) {
348 setHeartbeatInterval(DEFAULT_HEARTBEAT_INTERVAL);
349 log.info("Heartbeat interval time is not configured, default value is {}",
350 DEFAULT_HEARTBEAT_INTERVAL);
351 } else {
352 setHeartbeatInterval(newHeartbeatInterval);
353 log.info("Configured. Heartbeat interval time is configured to {}",
354 heartbeatInterval);
355 }
356
357 Integer newPhiFailureThreshold = Tools.getIntegerProperty(properties,
358 "phiFailureThreshold");
359 if (newPhiFailureThreshold == null) {
360 setPhiFailureThreshold(DEFAULT_PHI_FAILURE_THRESHOLD);
361 log.info("Phi failure threshold is not configured, default value is {}",
362 DEFAULT_PHI_FAILURE_THRESHOLD);
363 } else {
364 setPhiFailureThreshold(newPhiFailureThreshold);
365 log.info("Configured. Phi failure threshold is configured to {}",
366 phiFailureThreshold);
367 }
368 }
369
370 /**
371 * Sets heartbeat interval between the termination of one execution of heartbeat
372 * and the commencement of the next.
373 *
374 * @param interval term between each heartbeat
375 */
376 private void setHeartbeatInterval(int interval) {
377 try {
378 checkArgument(interval > 0, "Interval must be greater than zero");
379 heartbeatInterval = interval;
380 } catch (IllegalArgumentException e) {
381 log.warn(e.getMessage());
382 heartbeatInterval = DEFAULT_HEARTBEAT_INTERVAL;
383 }
384 }
385
386 /**
387 * Sets Phi failure threshold.
388 * Phi is based on a paper titled: "The φ Accrual Failure Detector" by Hayashibara, et al.
389 *
390 * @param threshold
391 */
392 private void setPhiFailureThreshold(int threshold) {
393 phiFailureThreshold = threshold;
394 }
395
396 /**
397 * Restarts heartbeatSender executor.
sangyun-hanf98df542016-03-24 20:28:03 +0900398 */
399 private void restartHeartbeatSender() {
400 try {
401 ScheduledExecutorService prevSender = heartBeatSender;
402 heartBeatSender = Executors.newSingleThreadScheduledExecutor(
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700403 groupedThreads("onos/cluster/membership", "heartbeat-sender-%d", log));
sangyun-hanf98df542016-03-24 20:28:03 +0900404 heartBeatSender.scheduleWithFixedDelay(this::heartbeat, 0,
405 heartbeatInterval, TimeUnit.MILLISECONDS);
406 prevSender.shutdown();
407 } catch (Exception e) {
408 log.warn(e.getMessage());
409 }
410 }
Jon Hall1f13d642016-05-13 17:53:01 -0700411}