Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -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 | */ |
| 16 | package org.onosproject.cluster; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.base.Objects; |
| 20 | import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | |
| 23 | /** |
| 24 | * Topic leader. |
| 25 | * <p> |
| 26 | * Identified by the {@link NodeId node identifier} and a monotonically increasing term number. |
| 27 | * The term number is incremented by one every time a new node is elected as leader. |
| 28 | * Also available is the system clock time at the instant when this node was elected as leader. |
| 29 | * Keep in mind though that as with any system clock based time stamps this particular information |
| 30 | * susceptible to clock skew and should only be relied on for simple diagnostic purposes. |
| 31 | */ |
| 32 | public class Leader { |
| 33 | private final NodeId nodeId; |
| 34 | private final long term; |
| 35 | private final long termStartTime; |
| 36 | |
| 37 | public Leader(NodeId nodeId, long term, long termStartTime) { |
| 38 | this.nodeId = checkNotNull(nodeId); |
| 39 | checkArgument(term >= 0, "term must be non-negative"); |
| 40 | this.term = term; |
| 41 | checkArgument(termStartTime >= 0, "termStartTime must be non-negative"); |
| 42 | this.termStartTime = termStartTime; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns the identifier for of leader. |
| 47 | * @return node identifier |
| 48 | */ |
| 49 | public NodeId nodeId() { |
| 50 | return nodeId; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the leader's term. |
| 55 | * @return leader term |
| 56 | */ |
| 57 | public long term() { |
| 58 | return term; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the system time when the current leadership term started. |
| 63 | * @return current leader term start time |
| 64 | */ |
| 65 | public long termStartTime() { |
| 66 | return termStartTime; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public boolean equals(Object other) { |
| 71 | if (this == other) { |
| 72 | return true; |
| 73 | } |
| 74 | if (other != null && other instanceof Leader) { |
| 75 | Leader that = (Leader) other; |
| 76 | return Objects.equal(this.nodeId, that.nodeId) && |
| 77 | this.term == that.term && |
| 78 | this.termStartTime == that.termStartTime; |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public int hashCode() { |
| 85 | return Objects.hashCode(nodeId, term, termStartTime); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public String toString() { |
| 90 | return MoreObjects.toStringHelper(getClass()) |
| 91 | .add("nodeId", nodeId) |
| 92 | .add("term", term) |
| 93 | .add("termStartTime", termStartTime) |
| 94 | .toString(); |
| 95 | } |
| 96 | } |