blob: d063a19f82d99f17bd80d25b2f2dabf6a9ee3be9 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cluster;
Madan Jampani1d3494e2014-11-20 11:24:22 -080017
18import java.util.Objects;
Ayaka Koshibea48f7522015-04-01 17:06:09 -070019import java.util.List;
Madan Jampani1d3494e2014-11-20 11:24:22 -080020
Madan Jampani30a57f82015-03-02 12:19:41 -080021import org.joda.time.DateTime;
22
Madan Jampani1d3494e2014-11-20 11:24:22 -080023import com.google.common.base.MoreObjects;
Ayaka Koshibea48f7522015-04-01 17:06:09 -070024import com.google.common.collect.ImmutableList;
Madan Jampani1d3494e2014-11-20 11:24:22 -080025
26/**
Ayaka Koshibea48f7522015-04-01 17:06:09 -070027 * Abstract leadership concept. The information carried by this construct
28 * include the topic of contention, the {@link NodeId}s of Nodes that could
29 * become leader for the topic, the epoch when the term for a given leader
30 * began, and the system time when the term began. Note:
31 * <ul>
32 * <li>The list of NodeIds may include the current leader at index 0, and the
33 * rest in decreasing preference order.</li>
34 * <li>The epoch is the logical age of a Leadership construct, and should be
35 * used for comparing two Leaderships, but only of the same topic.</li>
36 * </ul>
Madan Jampani1d3494e2014-11-20 11:24:22 -080037 */
38public class Leadership {
39
40 private final String topic;
Madan Jampani8d21c792014-12-01 16:31:07 -080041 private final NodeId leader;
Ayaka Koshibea48f7522015-04-01 17:06:09 -070042 private final List<NodeId> candidates;
Madan Jampani1ee91782014-11-20 20:24:24 -080043 private final long epoch;
Madan Jampani30a57f82015-03-02 12:19:41 -080044 private final long electedTime;
Madan Jampani1d3494e2014-11-20 11:24:22 -080045
Madan Jampani30a57f82015-03-02 12:19:41 -080046 public Leadership(String topic, NodeId leader, long epoch, long electedTime) {
Madan Jampani1d3494e2014-11-20 11:24:22 -080047 this.topic = topic;
48 this.leader = leader;
Ayaka Koshibea48f7522015-04-01 17:06:09 -070049 this.candidates = ImmutableList.of(leader);
50 this.epoch = epoch;
51 this.electedTime = electedTime;
52 }
53
54 public Leadership(String topic, NodeId leader, List<NodeId> candidates,
55 long epoch, long electedTime) {
56 this.topic = topic;
57 this.leader = leader;
58 this.candidates = ImmutableList.copyOf(candidates);
Madan Jampani1ee91782014-11-20 20:24:24 -080059 this.epoch = epoch;
Madan Jampani30a57f82015-03-02 12:19:41 -080060 this.electedTime = electedTime;
Madan Jampani1d3494e2014-11-20 11:24:22 -080061 }
62
63 /**
64 * The topic for which this leadership applies.
65 * @return leadership topic.
66 */
67 public String topic() {
68 return topic;
69 }
70
71 /**
Madan Jampani8d21c792014-12-01 16:31:07 -080072 * The nodeId of leader for this topic.
Madan Jampani1d3494e2014-11-20 11:24:22 -080073 * @return leader node.
74 */
Madan Jampani8d21c792014-12-01 16:31:07 -080075 public NodeId leader() {
Madan Jampani1d3494e2014-11-20 11:24:22 -080076 return leader;
77 }
78
79 /**
Ayaka Koshibea48f7522015-04-01 17:06:09 -070080 * Returns an preference-ordered list of nodes that are in the leadership
81 * race for this topic.
82 *
83 * @return a list of NodeIds in priority-order, or an empty list.
84 */
85 public List<NodeId> candidates() {
86 return candidates;
87 }
88
89 /**
Madan Jampani1ee91782014-11-20 20:24:24 -080090 * The epoch when the leadership was assumed.
Madan Jampani30a57f82015-03-02 12:19:41 -080091 * <p>
Ayaka Koshibea48f7522015-04-01 17:06:09 -070092 * Comparing epochs is only appropriate for leadership events for the same
93 * topic. The system guarantees that for any given topic the epoch for a new
94 * term is higher (not necessarily by 1) than the epoch for any previous
95 * term.
96 *
Madan Jampani1ee91782014-11-20 20:24:24 -080097 * @return leadership epoch
Madan Jampani1d3494e2014-11-20 11:24:22 -080098 */
Madan Jampani1ee91782014-11-20 20:24:24 -080099 public long epoch() {
100 return epoch;
Madan Jampani1d3494e2014-11-20 11:24:22 -0800101 }
102
Madan Jampani30a57f82015-03-02 12:19:41 -0800103 /**
104 * The system time when the term started.
105 * <p>
106 * The elected time is initially set on the node coordinating
107 * the leader election using its local system time. Due to possible
108 * clock skew, relying on this value for determining event ordering
109 * is discouraged. Epoch is more appropriate for determining
110 * event ordering.
111 * @return elected time.
112 */
113 public long electedTime() {
114 return electedTime;
115 }
116
Madan Jampani1d3494e2014-11-20 11:24:22 -0800117 @Override
118 public int hashCode() {
Ayaka Koshibea48f7522015-04-01 17:06:09 -0700119 return Objects.hash(topic, leader, candidates, epoch);
Madan Jampani1d3494e2014-11-20 11:24:22 -0800120 }
121
122 @Override
Ray Milkey30edb162014-11-24 15:02:14 -0800123 public boolean equals(Object obj) {
124 if (this == obj) {
125 return true;
126 }
127 if (obj instanceof Leadership) {
128 final Leadership other = (Leadership) obj;
129 return Objects.equals(this.topic, other.topic) &&
130 Objects.equals(this.leader, other.leader) &&
Ayaka Koshibea48f7522015-04-01 17:06:09 -0700131 Objects.equals(this.candidates, other.candidates) &&
Madan Jampani30a57f82015-03-02 12:19:41 -0800132 Objects.equals(this.epoch, other.epoch) &&
133 Objects.equals(this.electedTime, other.electedTime);
Ray Milkey30edb162014-11-24 15:02:14 -0800134 }
135 return false;
136 }
137
138 @Override
Madan Jampani1d3494e2014-11-20 11:24:22 -0800139 public String toString() {
140 return MoreObjects.toStringHelper(this.getClass())
141 .add("topic", topic)
142 .add("leader", leader)
Ayaka Koshibea48f7522015-04-01 17:06:09 -0700143 .add("candidates", candidates)
Madan Jampani1ee91782014-11-20 20:24:24 -0800144 .add("epoch", epoch)
Madan Jampani30a57f82015-03-02 12:19:41 -0800145 .add("electedTime", new DateTime(electedTime))
Madan Jampani1d3494e2014-11-20 11:24:22 -0800146 .toString();
147 }
Ray Milkey30edb162014-11-24 15:02:14 -0800148}