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.region;
18  
19  import java.io.Serializable;
20  import java.util.concurrent.atomic.AtomicBoolean;
21  import org.tikv.kvproto.Metapb;
22  
23  public class TiStore implements Serializable {
24    private final Metapb.Store store;
25    private final Metapb.Store proxyStore;
26    private final AtomicBoolean reachable;
27    private final AtomicBoolean valid;
28  
29    public TiStore(Metapb.Store store) {
30      this.store = store;
31      this.reachable = new AtomicBoolean(true);
32      this.valid = new AtomicBoolean(true);
33      this.proxyStore = null;
34    }
35  
36    private TiStore(Metapb.Store store, Metapb.Store proxyStore) {
37      this.store = store;
38      if (proxyStore != null) {
39        this.reachable = new AtomicBoolean(false);
40      } else {
41        this.reachable = new AtomicBoolean(true);
42      }
43      this.valid = new AtomicBoolean(true);
44      this.proxyStore = proxyStore;
45    }
46  
47    @java.lang.Override
48    public boolean equals(final java.lang.Object obj) {
49      if (obj == this) {
50        return true;
51      }
52      if (!(obj instanceof TiStore)) {
53        return super.equals(obj);
54      }
55      TiStore other = (TiStore) obj;
56      if (!this.store.equals(other.store)) {
57        return false;
58      }
59  
60      if (proxyStore == null && other.proxyStore == null) {
61        return true;
62      }
63      if (proxyStore != null && other.proxyStore != null) {
64        return proxyStore.equals(other.proxyStore);
65      }
66      return false;
67    }
68  
69    public TiStore withProxy(Metapb.Store proxyStore) {
70      return new TiStore(this.store, proxyStore);
71    }
72  
73    public void markUnreachable() {
74      this.reachable.set(false);
75    }
76  
77    public void markReachable() {
78      this.reachable.set(true);
79    }
80  
81    public boolean isReachable() {
82      return this.reachable.get();
83    }
84  
85    public boolean isValid() {
86      return this.valid.get();
87    }
88  
89    public void markInvalid() {
90      this.valid.set(false);
91    }
92  
93    public Metapb.Store getStore() {
94      return this.store;
95    }
96  
97    public String getAddress() {
98      return this.store.getAddress();
99    }
100 
101   public Metapb.Store getProxyStore() {
102     return this.proxyStore;
103   }
104 
105   public long getId() {
106     return this.store.getId();
107   }
108 
109   public boolean isTiFlash() {
110     for (Metapb.StoreLabel label : store.getLabelsList()) {
111       if (label.getKey().equals(TiStoreType.TiFlash.getLabelKey())
112           && label.getValue().equals(TiStoreType.TiFlash.getLabelValue())) {
113         return true;
114       }
115     }
116     return false;
117   }
118 }