alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
alshabib | ab98466 | 2014-12-04 18:56:18 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cluster; |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.Objects; |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 19 | import java.util.List; |
Ayaka Koshibe | fd26a30 | 2015-04-13 13:59:54 -0700 | [diff] [blame] | 20 | import java.util.Optional; |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 21 | |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 22 | import org.joda.time.DateTime; |
| 23 | |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 24 | import com.google.common.base.MoreObjects; |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableList; |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 28 | * Abstract leadership concept. The information carried by this construct |
| 29 | * include the topic of contention, the {@link NodeId}s of Nodes that could |
| 30 | * become leader for the topic, the epoch when the term for a given leader |
| 31 | * began, and the system time when the term began. Note: |
| 32 | * <ul> |
| 33 | * <li>The list of NodeIds may include the current leader at index 0, and the |
| 34 | * rest in decreasing preference order.</li> |
| 35 | * <li>The epoch is the logical age of a Leadership construct, and should be |
| 36 | * used for comparing two Leaderships, but only of the same topic.</li> |
Ayaka Koshibe | fd26a30 | 2015-04-13 13:59:54 -0700 | [diff] [blame] | 37 | * <li>The leader may be null if its accuracy can't be guaranteed. This applies |
| 38 | * to CANDIDATES_CHANGED events and candidate board contents.</li> |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 39 | * </ul> |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 40 | */ |
| 41 | public class Leadership { |
| 42 | |
| 43 | private final String topic; |
Ayaka Koshibe | fd26a30 | 2015-04-13 13:59:54 -0700 | [diff] [blame] | 44 | private final Optional<NodeId> leader; |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 45 | private final List<NodeId> candidates; |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 46 | private final long epoch; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 47 | private final long electedTime; |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 48 | |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 49 | public Leadership(String topic, NodeId leader, long epoch, long electedTime) { |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 50 | this.topic = topic; |
Ayaka Koshibe | fd26a30 | 2015-04-13 13:59:54 -0700 | [diff] [blame] | 51 | this.leader = Optional.of(leader); |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 52 | this.candidates = ImmutableList.of(leader); |
| 53 | this.epoch = epoch; |
| 54 | this.electedTime = electedTime; |
| 55 | } |
| 56 | |
| 57 | public Leadership(String topic, NodeId leader, List<NodeId> candidates, |
| 58 | long epoch, long electedTime) { |
| 59 | this.topic = topic; |
Ayaka Koshibe | fd26a30 | 2015-04-13 13:59:54 -0700 | [diff] [blame] | 60 | this.leader = Optional.of(leader); |
| 61 | this.candidates = ImmutableList.copyOf(candidates); |
| 62 | this.epoch = epoch; |
| 63 | this.electedTime = electedTime; |
| 64 | } |
| 65 | |
| 66 | public Leadership(String topic, List<NodeId> candidates, |
| 67 | long epoch, long electedTime) { |
| 68 | this.topic = topic; |
| 69 | this.leader = Optional.empty(); |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 70 | this.candidates = ImmutableList.copyOf(candidates); |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 71 | this.epoch = epoch; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 72 | this.electedTime = electedTime; |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * The topic for which this leadership applies. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 77 | * |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 78 | * @return leadership topic. |
| 79 | */ |
| 80 | public String topic() { |
| 81 | return topic; |
| 82 | } |
| 83 | |
| 84 | /** |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 85 | * The nodeId of leader for this topic. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 86 | * |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 87 | * @return leader node. |
| 88 | */ |
Ayaka Koshibe | fd26a30 | 2015-04-13 13:59:54 -0700 | [diff] [blame] | 89 | // This will return Optional<NodeId> in the future. |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 90 | public NodeId leader() { |
Ayaka Koshibe | fd26a30 | 2015-04-13 13:59:54 -0700 | [diff] [blame] | 91 | return leader.orElse(null); |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 95 | * Returns an preference-ordered list of nodes that are in the leadership |
| 96 | * race for this topic. |
| 97 | * |
| 98 | * @return a list of NodeIds in priority-order, or an empty list. |
| 99 | */ |
| 100 | public List<NodeId> candidates() { |
| 101 | return candidates; |
| 102 | } |
| 103 | |
| 104 | /** |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 105 | * The epoch when the leadership was assumed. |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 106 | * <p> |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 107 | * Comparing epochs is only appropriate for leadership events for the same |
| 108 | * topic. The system guarantees that for any given topic the epoch for a new |
| 109 | * term is higher (not necessarily by 1) than the epoch for any previous |
| 110 | * term. |
| 111 | * |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 112 | * @return leadership epoch |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 113 | */ |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 114 | public long epoch() { |
| 115 | return epoch; |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 118 | /** |
| 119 | * The system time when the term started. |
| 120 | * <p> |
| 121 | * The elected time is initially set on the node coordinating |
| 122 | * the leader election using its local system time. Due to possible |
| 123 | * clock skew, relying on this value for determining event ordering |
| 124 | * is discouraged. Epoch is more appropriate for determining |
| 125 | * event ordering. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 126 | * |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 127 | * @return elected time. |
| 128 | */ |
| 129 | public long electedTime() { |
| 130 | return electedTime; |
| 131 | } |
| 132 | |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 133 | @Override |
| 134 | public int hashCode() { |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 135 | return Objects.hash(topic, leader, candidates, epoch, electedTime); |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
Ray Milkey | 30edb16 | 2014-11-24 15:02:14 -0800 | [diff] [blame] | 139 | public boolean equals(Object obj) { |
| 140 | if (this == obj) { |
| 141 | return true; |
| 142 | } |
| 143 | if (obj instanceof Leadership) { |
| 144 | final Leadership other = (Leadership) obj; |
| 145 | return Objects.equals(this.topic, other.topic) && |
Ray Milkey | 5051040 | 2015-06-11 14:36:01 -0700 | [diff] [blame] | 146 | Objects.equals(this.leader, other.leader) && |
| 147 | Objects.equals(this.candidates, other.candidates) && |
| 148 | Objects.equals(this.epoch, other.epoch) && |
| 149 | Objects.equals(this.electedTime, other.electedTime); |
Ray Milkey | 30edb16 | 2014-11-24 15:02:14 -0800 | [diff] [blame] | 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | @Override |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 155 | public String toString() { |
| 156 | return MoreObjects.toStringHelper(this.getClass()) |
| 157 | .add("topic", topic) |
| 158 | .add("leader", leader) |
Ayaka Koshibe | a48f752 | 2015-04-01 17:06:09 -0700 | [diff] [blame] | 159 | .add("candidates", candidates) |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 160 | .add("epoch", epoch) |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 161 | .add("electedTime", new DateTime(electedTime)) |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 162 | .toString(); |
| 163 | } |
Ray Milkey | 30edb16 | 2014-11-24 15:02:14 -0800 | [diff] [blame] | 164 | } |