Madan Jampani | a14047d | 2015-02-25 12:23:02 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Foundation |
Madan Jampani | a14047d | 2015-02-25 12:23:02 -0800 | [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 | */ |
Madan Jampani | bd6845d | 2015-02-25 11:43:48 -0800 | [diff] [blame] | 16 | package org.onosproject.store.cluster.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; |
| 24 | import org.onosproject.cluster.NodeId; |
| 25 | |
| 26 | import com.google.common.collect.Maps; |
| 27 | |
| 28 | /** |
| 29 | * Phi Accrual failure detector. |
| 30 | * <p> |
| 31 | * Based on a paper titled: "The φ Accrual Failure Detector" by Hayashibara, et al. |
| 32 | */ |
| 33 | public class PhiAccrualFailureDetector { |
| 34 | private final Map<NodeId, History> states = Maps.newConcurrentMap(); |
| 35 | |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 36 | // Default value |
| 37 | private static final int DEFAULT_WINDOW_SIZE = 250; |
| 38 | private static final int DEFAULT_MIN_SAMPLES = 25; |
| 39 | private static final double DEFAULT_PHI_FACTOR = 1.0 / Math.log(10.0); |
Madan Jampani | bd6845d | 2015-02-25 11:43:48 -0800 | [diff] [blame] | 40 | |
| 41 | // If a node does not have any heartbeats, this is the phi |
| 42 | // value to report. Indicates the node is inactive (from the |
| 43 | // detectors perspective. |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 44 | private static final double DEFAULT_BOOTSTRAP_PHI_VALUE = 100.0; |
| 45 | |
| 46 | |
| 47 | private int minSamples = DEFAULT_MIN_SAMPLES; |
| 48 | private double phiFactor = DEFAULT_PHI_FACTOR; |
| 49 | private double bootstrapPhiValue = DEFAULT_BOOTSTRAP_PHI_VALUE; |
Madan Jampani | bd6845d | 2015-02-25 11:43:48 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Report a new heart beat for the specified node id. |
| 53 | * @param nodeId node id |
| 54 | */ |
| 55 | public void report(NodeId nodeId) { |
| 56 | report(nodeId, System.currentTimeMillis()); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Report a new heart beat for the specified node id. |
| 61 | * @param nodeId node id |
| 62 | * @param arrivalTime arrival time |
| 63 | */ |
| 64 | public void report(NodeId nodeId, long arrivalTime) { |
| 65 | checkNotNull(nodeId, "NodeId must not be null"); |
| 66 | checkArgument(arrivalTime >= 0, "arrivalTime must not be negative"); |
| 67 | History nodeState = |
| 68 | states.computeIfAbsent(nodeId, key -> new History()); |
| 69 | synchronized (nodeState) { |
| 70 | long latestHeartbeat = nodeState.latestHeartbeatTime(); |
| 71 | if (latestHeartbeat != -1) { |
| 72 | nodeState.samples().addValue(arrivalTime - latestHeartbeat); |
| 73 | } |
| 74 | nodeState.setLatestHeartbeatTime(arrivalTime); |
| 75 | } |
| 76 | } |
| 77 | |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 78 | |
| 79 | |
Madan Jampani | bd6845d | 2015-02-25 11:43:48 -0800 | [diff] [blame] | 80 | /** |
| 81 | * Compute phi for the specified node id. |
| 82 | * @param nodeId node id |
| 83 | * @return phi value |
| 84 | */ |
Madan Jampani | a14047d | 2015-02-25 12:23:02 -0800 | [diff] [blame] | 85 | public double phi(NodeId nodeId) { |
| 86 | checkNotNull(nodeId, "NodeId must not be null"); |
Madan Jampani | bd6845d | 2015-02-25 11:43:48 -0800 | [diff] [blame] | 87 | if (!states.containsKey(nodeId)) { |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 88 | return bootstrapPhiValue; |
Madan Jampani | bd6845d | 2015-02-25 11:43:48 -0800 | [diff] [blame] | 89 | } |
Madan Jampani | bd6845d | 2015-02-25 11:43:48 -0800 | [diff] [blame] | 90 | History nodeState = states.get(nodeId); |
| 91 | synchronized (nodeState) { |
| 92 | long latestHeartbeat = nodeState.latestHeartbeatTime(); |
| 93 | DescriptiveStatistics samples = nodeState.samples(); |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 94 | if (latestHeartbeat == -1 || samples.getN() < minSamples) { |
Madan Jampani | bd6845d | 2015-02-25 11:43:48 -0800 | [diff] [blame] | 95 | return 0.0; |
| 96 | } |
| 97 | return computePhi(samples, latestHeartbeat, System.currentTimeMillis()); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private double computePhi(DescriptiveStatistics samples, long tLast, long tNow) { |
| 102 | long size = samples.getN(); |
| 103 | long t = tNow - tLast; |
| 104 | return (size > 0) |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 105 | ? phiFactor * t / samples.getMean() |
| 106 | : bootstrapPhiValue; |
Madan Jampani | bd6845d | 2015-02-25 11:43:48 -0800 | [diff] [blame] | 107 | } |
| 108 | |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 109 | |
| 110 | private void setMinSamples(int samples) { |
| 111 | minSamples = samples; |
| 112 | } |
| 113 | |
| 114 | private void setPhiFactor(double factor) { |
| 115 | phiFactor = factor; |
| 116 | } |
| 117 | |
| 118 | private void setBootstrapPhiValue(double phiValue) { |
| 119 | bootstrapPhiValue = phiValue; |
| 120 | } |
| 121 | |
| 122 | |
Madan Jampani | bd6845d | 2015-02-25 11:43:48 -0800 | [diff] [blame] | 123 | private static class History { |
| 124 | DescriptiveStatistics samples = |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 125 | new DescriptiveStatistics(DEFAULT_WINDOW_SIZE); |
Madan Jampani | bd6845d | 2015-02-25 11:43:48 -0800 | [diff] [blame] | 126 | long lastHeartbeatTime = -1; |
| 127 | |
| 128 | public DescriptiveStatistics samples() { |
| 129 | return samples; |
| 130 | } |
| 131 | |
| 132 | public long latestHeartbeatTime() { |
| 133 | return lastHeartbeatTime; |
| 134 | } |
| 135 | |
| 136 | public void setLatestHeartbeatTime(long value) { |
| 137 | lastHeartbeatTime = value; |
| 138 | } |
| 139 | } |
| 140 | } |