001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.rsgroup;
020
021import java.util.Collection;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.Map;
025import java.util.Objects;
026import java.util.SortedSet;
027import java.util.TreeSet;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.net.Address;
030import org.apache.yetus.audience.InterfaceAudience;
031
032/**
033 * Stores the group information of region server groups.
034 */
035@InterfaceAudience.Public
036public class RSGroupInfo {
037  public static final String DEFAULT_GROUP = "default";
038  public static final String NAMESPACE_DESC_PROP_GROUP = "hbase.rsgroup.name";
039  public static final String TABLE_DESC_PROP_GROUP = "hbase.rsgroup.name";
040
041  private final String name;
042  // Keep servers in a sorted set so has an expected ordering when displayed.
043  private final SortedSet<Address> servers;
044  // Keep tables sorted too.
045
046  /**
047   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information will be stored in
048   *             the configuration of a table so this will be removed.
049   */
050  @Deprecated
051  private final SortedSet<TableName> tables;
052
053  private final Map<String, String> configuration;
054
055  public RSGroupInfo(String name) {
056    this(name, new TreeSet<Address>(), new TreeSet<TableName>());
057  }
058
059  RSGroupInfo(String name, SortedSet<Address> servers) {
060    this.name = name;
061    this.servers = servers == null ? new TreeSet<>() : new TreeSet<>(servers);
062    this.tables = new TreeSet<>();
063    configuration = new HashMap<>();
064  }
065
066  /**
067   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information for a table will be
068   *             stored in the configuration of a table so this will be removed.
069   */
070  @Deprecated
071  RSGroupInfo(String name, SortedSet<Address> servers, SortedSet<TableName> tables) {
072    this.name = name;
073    this.servers = (servers == null) ? new TreeSet<>() : new TreeSet<>(servers);
074    this.tables = (tables == null) ? new TreeSet<>() : new TreeSet<>(tables);
075    configuration = new HashMap<>();
076  }
077
078  public RSGroupInfo(RSGroupInfo src) {
079    this(src.name, src.servers, src.tables);
080  }
081
082  /**
083   * Get group name.
084   */
085  public String getName() {
086    return name;
087  }
088
089  /**
090   * Adds the given server to the group.
091   */
092  public void addServer(Address hostPort){
093    servers.add(hostPort);
094  }
095
096  /**
097   * Adds the given servers to the group.
098   */
099  public void addAllServers(Collection<Address> hostPort){
100    servers.addAll(hostPort);
101  }
102
103  /**
104   * @param hostPort hostPort of the server
105   * @return true, if a server with hostPort is found
106   */
107  public boolean containsServer(Address hostPort) {
108    return servers.contains(hostPort);
109  }
110
111  /**
112   * Get list of servers.
113   */
114  public SortedSet<Address> getServers() {
115    return servers;
116  }
117
118  /**
119   * Remove given server from the group.
120   */
121  public boolean removeServer(Address hostPort) {
122    return servers.remove(hostPort);
123  }
124
125  /**
126   * Getter for fetching an unmodifiable {@link #configuration} map.
127   */
128  public Map<String, String> getConfiguration() {
129    // shallow pointer copy
130    return Collections.unmodifiableMap(configuration);
131  }
132
133  /**
134   * Setter for storing a configuration setting in {@link #configuration} map.
135   * @param key Config key.
136   * @param value String value.
137   */
138  public void setConfiguration(String key, String value) {
139    configuration.put(key, Objects.requireNonNull(value));
140  }
141
142  /**
143   * Remove a config setting represented by the key from the {@link #configuration} map
144   */
145  public void removeConfiguration(final String key) {
146    configuration.remove(key);
147  }
148
149  /**
150   * Get set of tables that are members of the group.
151   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information will be stored in
152   *             the configuration of a table so this will be removed.
153   */
154  @Deprecated
155  public SortedSet<TableName> getTables() {
156    return tables;
157  }
158
159  /**
160   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information will be stored in
161   *             the configuration of a table so this will be removed.
162   */
163  @Deprecated
164  public void addTable(TableName table) {
165    tables.add(table);
166  }
167
168  /**
169   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information will be stored in
170   *             the configuration of a table so this will be removed.
171   */
172  @Deprecated
173  public void addAllTables(Collection<TableName> arg) {
174    tables.addAll(arg);
175  }
176
177  /**
178   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information will be stored in
179   *             the configuration of a table so this will be removed.
180   */
181  @Deprecated
182  public boolean containsTable(TableName table) {
183    return tables.contains(table);
184  }
185
186  /**
187   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information will be stored in
188   *             the configuration of a table so this will be removed.
189   */
190  @Deprecated
191  public boolean removeTable(TableName table) {
192    return tables.remove(table);
193  }
194
195  @Override
196  public String toString() {
197    StringBuilder sb = new StringBuilder();
198    sb.append("Name:");
199    sb.append(this.name);
200    sb.append(", ");
201    sb.append(" Servers:");
202    sb.append(this.servers);
203    sb.append(", ");
204    sb.append(" Tables:");
205    sb.append(this.tables);
206    sb.append(", ");
207    sb.append(" Configurations:");
208    sb.append(this.configuration);
209    return sb.toString();
210
211  }
212
213  @Override
214  public boolean equals(Object o) {
215    if (this == o) {
216      return true;
217    }
218    if (o == null || getClass() != o.getClass()) {
219      return false;
220    }
221
222    RSGroupInfo rsGroupInfo = (RSGroupInfo) o;
223
224    if (!name.equals(rsGroupInfo.name)) {
225      return false;
226    }
227    if (!servers.equals(rsGroupInfo.servers)) {
228      return false;
229    }
230    if (!tables.equals(rsGroupInfo.tables)) {
231      return false;
232    }
233    if (!configuration.equals(rsGroupInfo.configuration)) {
234      return false;
235    }
236
237    return true;
238  }
239
240  @Override
241  public int hashCode() {
242    int result = servers.hashCode();
243    result = 31 * result + tables.hashCode();
244    result = 31 * result + name.hashCode();
245    result = 31 * result + configuration.hashCode();
246    return result;
247  }
248}