View Javadoc
1   /*
2    * Copyright 2021 TiKV Project Authors.
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   */
17  package org.tikv.common.replica;
18  
19  import static com.google.common.base.MoreObjects.toStringHelper;
20  
21  import java.util.List;
22  import org.tikv.kvproto.Metapb;
23  
24  public class Store {
25    public static class Label {
26      private final org.tikv.kvproto.Metapb.StoreLabel label;
27  
28      Label(org.tikv.kvproto.Metapb.StoreLabel label) {
29        this.label = label;
30      }
31  
32      public String getKey() {
33        return label.getKey();
34      }
35  
36      public String getValue() {
37        return label.getValue();
38      }
39    }
40  
41    public enum State {
42      Unknown,
43      Up,
44      Offline,
45      Tombstone
46    }
47  
48    private static final Label[] EMPTY_LABELS = new Label[0];
49    private Label[] labels;
50    private final Metapb.Peer peer;
51    private final Metapb.Store store;
52    private final boolean isLeader;
53  
54    Store(
55        final org.tikv.kvproto.Metapb.Peer peer,
56        final org.tikv.kvproto.Metapb.Store store,
57        boolean isLeader) {
58      this.peer = peer;
59      this.store = store;
60      this.isLeader = isLeader;
61    }
62  
63    public Metapb.Peer getPeer() {
64      return peer;
65    }
66  
67    public Label[] getLabels() {
68      if (labels == null) {
69        List<Metapb.StoreLabel> labelList = store.getLabelsList();
70        if (labelList.isEmpty()) {
71          labels = EMPTY_LABELS;
72        } else {
73          labels = labelList.stream().map(Label::new).toArray(Label[]::new);
74        }
75      }
76      return labels;
77    }
78  
79    public boolean isLearner() {
80      return peer.getRole() == Metapb.PeerRole.Learner;
81    }
82  
83    public boolean isLeader() {
84      return isLeader;
85    }
86  
87    public boolean isFollower() {
88      return peer.getRole() == Metapb.PeerRole.Voter && !isLeader;
89    }
90  
91    public long getId() {
92      return store.getId();
93    }
94  
95    public String getAddress() {
96      return store.getAddress();
97    }
98  
99    public String getVersion() {
100     return store.getVersion();
101   }
102 
103   public State getState() {
104     switch (store.getState()) {
105       case Up:
106         return State.Up;
107       case Offline:
108         return State.Offline;
109       case Tombstone:
110         return State.Tombstone;
111       default:
112         return State.Unknown;
113     }
114   }
115 
116   public boolean equals(Object o) {
117     if (!(o instanceof Store)) {
118       return false;
119     }
120     Store other = (Store) o;
121     return this.peer.equals(other.peer);
122   }
123 
124   public String toString() {
125     return toStringHelper(this).add("peer", peer).add("store", store).toString();
126   }
127 }