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 */
018package org.apache.hadoop.hbase.rsgroup;
019
020import java.util.Collection;
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.Map;
024import java.util.Objects;
025import java.util.Set;
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, Set<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   * Constructor
068   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information for a table will be
069   *             stored in the configuration of a table so this will be removed.
070   */
071  @Deprecated
072  RSGroupInfo(String name, Set<Address> servers, Set<TableName> tables) {
073    this.name = name;
074    this.servers = (servers == null) ? new TreeSet<>() : new TreeSet<>(servers);
075    this.tables = (tables == null) ? new TreeSet<>() : new TreeSet<>(tables);
076    configuration = new HashMap<>();
077  }
078
079  public RSGroupInfo(RSGroupInfo src) {
080    this(src.name, src.servers, src.tables);
081  }
082
083  /** Get group name. */
084  public String getName() {
085    return name;
086  }
087
088  /** Adds the given server to the group. */
089  public void addServer(Address hostPort) {
090    servers.add(hostPort);
091  }
092
093  /** Adds the given servers to the group. */
094  public void addAllServers(Collection<Address> hostPort) {
095    servers.addAll(hostPort);
096  }
097
098  /** Returns true if a server with hostPort is found */
099  public boolean containsServer(Address hostPort) {
100    return servers.contains(hostPort);
101  }
102
103  /** Get list of servers. */
104  public Set<Address> getServers() {
105    return servers;
106  }
107
108  /** Remove given server from the group. */
109  public boolean removeServer(Address hostPort) {
110    return servers.remove(hostPort);
111  }
112
113  /** Getter for fetching an unmodifiable {@link #configuration} map. */
114  public Map<String, String> getConfiguration() {
115    // shallow pointer copy
116    return Collections.unmodifiableMap(configuration);
117  }
118
119  /**
120   * Setter for storing a configuration setting in {@link #configuration} map.
121   * @param key   Config key.
122   * @param value String value.
123   */
124  public void setConfiguration(String key, String value) {
125    configuration.put(key, Objects.requireNonNull(value));
126  }
127
128  /** Remove a config setting represented by the key from the {@link #configuration} map */
129  public void removeConfiguration(final String key) {
130    configuration.remove(key);
131  }
132
133  /**
134   * Get set of tables that are members of the group.
135   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information will be stored in
136   *             the configuration of a table so this will be removed.
137   */
138  @Deprecated
139  public SortedSet<TableName> getTables() {
140    return tables;
141  }
142
143  /**
144   * Add a table
145   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information will be stored in
146   *             the configuration of a table so this will be removed.
147   */
148  @Deprecated
149  public void addTable(TableName table) {
150    tables.add(table);
151  }
152
153  /**
154   * Add a collection of tables
155   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information will be stored in
156   *             the configuration of a table so this will be removed.
157   */
158  @Deprecated
159  public void addAllTables(Collection<TableName> arg) {
160    tables.addAll(arg);
161  }
162
163  /**
164   * Check if the group contains a table
165   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information will be stored in
166   *             the configuration of a table so this will be removed.
167   */
168  @Deprecated
169  public boolean containsTable(TableName table) {
170    return tables.contains(table);
171  }
172
173  /**
174   * Remove a table
175   * @deprecated Since 3.0.0, will be removed in 4.0.0. The rsgroup information will be stored in
176   *             the configuration of a table so this will be removed.
177   */
178  @Deprecated
179  public boolean removeTable(TableName table) {
180    return tables.remove(table);
181  }
182
183  @Override
184  public String toString() {
185    StringBuilder sb = new StringBuilder();
186    sb.append("Name:");
187    sb.append(this.name);
188    sb.append(", ");
189    sb.append(" Servers:");
190    sb.append(this.servers);
191    sb.append(", ");
192    sb.append(" Tables:");
193    sb.append(this.tables);
194    sb.append(", ");
195    sb.append(" Configurations:");
196    sb.append(this.configuration);
197    return sb.toString();
198
199  }
200
201  @Override
202  public boolean equals(Object o) {
203    if (this == o) {
204      return true;
205    }
206    if (!(o instanceof RSGroupInfo)) {
207      return false;
208    }
209    RSGroupInfo rsGroupInfo = (RSGroupInfo) o;
210    if (!name.equals(rsGroupInfo.name)) {
211      return false;
212    }
213    if (!servers.equals(rsGroupInfo.servers)) {
214      return false;
215    }
216    if (!tables.equals(rsGroupInfo.tables)) {
217      return false;
218    }
219    if (!configuration.equals(rsGroupInfo.configuration)) {
220      return false;
221    }
222    return true;
223  }
224
225  @Override
226  public int hashCode() {
227    int result = servers.hashCode();
228    result = 31 * result + tables.hashCode();
229    result = 31 * result + name.hashCode();
230    result = 31 * result + configuration.hashCode();
231    return result;
232  }
233}