blob: 5f3a9efd52441d4ca90d62e5c12653c2508df275 [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;
19
Madan Jampani30a57f82015-03-02 12:19:41 -080020import org.joda.time.DateTime;
21
Madan Jampani1d3494e2014-11-20 11:24:22 -080022import com.google.common.base.MoreObjects;
23
24/**
25 * Abstract leadership concept.
26 */
27public class Leadership {
28
29 private final String topic;
Madan Jampani8d21c792014-12-01 16:31:07 -080030 private final NodeId leader;
Madan Jampani1ee91782014-11-20 20:24:24 -080031 private final long epoch;
Madan Jampani30a57f82015-03-02 12:19:41 -080032 private final long electedTime;
Madan Jampani1d3494e2014-11-20 11:24:22 -080033
Madan Jampani30a57f82015-03-02 12:19:41 -080034 public Leadership(String topic, NodeId leader, long epoch, long electedTime) {
Madan Jampani1d3494e2014-11-20 11:24:22 -080035 this.topic = topic;
36 this.leader = leader;
Madan Jampani1ee91782014-11-20 20:24:24 -080037 this.epoch = epoch;
Madan Jampani30a57f82015-03-02 12:19:41 -080038 this.electedTime = electedTime;
Madan Jampani1d3494e2014-11-20 11:24:22 -080039 }
40
41 /**
42 * The topic for which this leadership applies.
43 * @return leadership topic.
44 */
45 public String topic() {
46 return topic;
47 }
48
49 /**
Madan Jampani8d21c792014-12-01 16:31:07 -080050 * The nodeId of leader for this topic.
Madan Jampani1d3494e2014-11-20 11:24:22 -080051 * @return leader node.
52 */
Madan Jampani8d21c792014-12-01 16:31:07 -080053 public NodeId leader() {
Madan Jampani1d3494e2014-11-20 11:24:22 -080054 return leader;
55 }
56
57 /**
Madan Jampani1ee91782014-11-20 20:24:24 -080058 * The epoch when the leadership was assumed.
Madan Jampani30a57f82015-03-02 12:19:41 -080059 * <p>
60 * Comparing epochs is only appropriate for leadership
61 * events for the same topic. The system guarantees that
62 * for any given topic the epoch for a new term is higher
63 * (not necessarily by 1) than the epoch for any previous term.
Madan Jampani1ee91782014-11-20 20:24:24 -080064 * @return leadership epoch
Madan Jampani1d3494e2014-11-20 11:24:22 -080065 */
Madan Jampani1ee91782014-11-20 20:24:24 -080066 public long epoch() {
67 return epoch;
Madan Jampani1d3494e2014-11-20 11:24:22 -080068 }
69
Madan Jampani30a57f82015-03-02 12:19:41 -080070 /**
71 * The system time when the term started.
72 * <p>
73 * The elected time is initially set on the node coordinating
74 * the leader election using its local system time. Due to possible
75 * clock skew, relying on this value for determining event ordering
76 * is discouraged. Epoch is more appropriate for determining
77 * event ordering.
78 * @return elected time.
79 */
80 public long electedTime() {
81 return electedTime;
82 }
83
Madan Jampani1d3494e2014-11-20 11:24:22 -080084 @Override
85 public int hashCode() {
Madan Jampani1ee91782014-11-20 20:24:24 -080086 return Objects.hash(topic, leader, epoch);
Madan Jampani1d3494e2014-11-20 11:24:22 -080087 }
88
89 @Override
Ray Milkey30edb162014-11-24 15:02:14 -080090 public boolean equals(Object obj) {
91 if (this == obj) {
92 return true;
93 }
94 if (obj instanceof Leadership) {
95 final Leadership other = (Leadership) obj;
96 return Objects.equals(this.topic, other.topic) &&
97 Objects.equals(this.leader, other.leader) &&
Madan Jampani30a57f82015-03-02 12:19:41 -080098 Objects.equals(this.epoch, other.epoch) &&
99 Objects.equals(this.electedTime, other.electedTime);
Ray Milkey30edb162014-11-24 15:02:14 -0800100 }
101 return false;
102 }
103
104 @Override
Madan Jampani1d3494e2014-11-20 11:24:22 -0800105 public String toString() {
106 return MoreObjects.toStringHelper(this.getClass())
107 .add("topic", topic)
108 .add("leader", leader)
Madan Jampani1ee91782014-11-20 20:24:24 -0800109 .add("epoch", epoch)
Madan Jampani30a57f82015-03-02 12:19:41 -0800110 .add("electedTime", new DateTime(electedTime))
Madan Jampani1d3494e2014-11-20 11:24:22 -0800111 .toString();
112 }
Ray Milkey30edb162014-11-24 15:02:14 -0800113}