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.Set;
027import java.util.SortedSet;
028import java.util.TreeSet;
029
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.net.Address;
032import org.apache.yetus.audience.InterfaceAudience;
033
034/**
035 * Stores the group information of region server groups.
036 */
037@InterfaceAudience.Public
038public class RSGroupInfo {
039  public static final String DEFAULT_GROUP = "default";
040  public static final String NAMESPACE_DESC_PROP_GROUP = "hbase.rsgroup.name";
041  public static final String TABLE_DESC_PROP_GROUP = "hbase.rsgroup.name";
042
043  private final String name;
044  // Keep servers in a sorted set so has an expected ordering when displayed.
045  private final SortedSet<Address> servers;
046  // Keep tables sorted too.
047  private final SortedSet<TableName> tables;
048
049  private final Map<String, String> configuration;
050
051  public RSGroupInfo(String name) {
052    this(name, new TreeSet<>(), new TreeSet<>());
053  }
054
055  RSGroupInfo(String name, SortedSet<Address> servers, SortedSet<TableName> tables) {
056    this.name = name;
057    this.servers = (servers == null) ? new TreeSet<>() : new TreeSet<>(servers);
058    this.tables  = (tables  == null) ? new TreeSet<>() : new TreeSet<>(tables);
059    configuration = new HashMap<>();
060  }
061
062  public RSGroupInfo(RSGroupInfo src) {
063    this(src.name, src.servers, src.tables);
064    src.configuration.forEach(this::setConfiguration);
065  }
066
067  /**
068   * Get group name.
069   */
070  public String getName() {
071    return name;
072  }
073
074  /**
075   * Adds the given server to the group.
076   */
077  public void addServer(Address hostPort){
078    servers.add(hostPort);
079  }
080
081  /**
082   * Adds the given servers to the group.
083   */
084  public void addAllServers(Collection<Address> hostPort){
085    servers.addAll(hostPort);
086  }
087
088  /**
089   * @param hostPort hostPort of the server
090   * @return true, if a server with hostPort is found
091   */
092  public boolean containsServer(Address hostPort) {
093    return servers.contains(hostPort);
094  }
095
096  /**
097   * Get list of servers.
098   */
099  public Set<Address> getServers() {
100    return servers;
101  }
102
103  /**
104   * Remove given server from the group.
105   */
106  public boolean removeServer(Address hostPort) {
107    return servers.remove(hostPort);
108  }
109
110  /**
111   * Get set of tables that are members of the group.
112   */
113  public SortedSet<TableName> getTables() {
114    return tables;
115  }
116
117  public void addTable(TableName table) {
118    tables.add(table);
119  }
120
121  public void addAllTables(Collection<TableName> arg) {
122    tables.addAll(arg);
123  }
124
125  public boolean containsTable(TableName table) {
126    return tables.contains(table);
127  }
128
129  public boolean removeTable(TableName table) {
130    return tables.remove(table);
131  }
132
133  /**
134   * Getter for fetching an unmodifiable {@link #configuration} map.
135   */
136  public Map<String, String> getConfiguration() {
137    // shallow pointer copy
138    return Collections.unmodifiableMap(configuration);
139  }
140
141  /**
142   * Setter for storing a configuration setting in {@link #configuration} map.
143   * @param key Config key.
144   * @param value String value.
145   */
146  public void setConfiguration(String key, String value) {
147    configuration.put(key, Objects.requireNonNull(value));
148  }
149
150  /**
151   * Remove a config setting represented by the key from the {@link #configuration} map
152   */
153  public void removeConfiguration(final String key) {
154    configuration.remove(key);
155  }
156
157  @Override
158  public String toString() {
159    StringBuilder sb = new StringBuilder();
160    sb.append("Name:");
161    sb.append(this.name);
162    sb.append(", ");
163    sb.append(" Servers:");
164    sb.append(this.servers);
165    sb.append(", ");
166    sb.append(" Tables:");
167    sb.append(this.tables);
168    sb.append(", ");
169    sb.append(" Configurations:");
170    sb.append(this.configuration);
171    return sb.toString();
172
173  }
174
175  @Override
176  public boolean equals(Object o) {
177    if (this == o) {
178      return true;
179    }
180    if (o == null || getClass() != o.getClass()) {
181      return false;
182    }
183
184    RSGroupInfo rsGroupInfo = (RSGroupInfo) o;
185
186    if (!name.equals(rsGroupInfo.name)) {
187      return false;
188    }
189    if (!servers.equals(rsGroupInfo.servers)) {
190      return false;
191    }
192    if (!tables.equals(rsGroupInfo.tables)) {
193      return false;
194    }
195    if (!configuration.equals(rsGroupInfo.configuration)) {
196      return false;
197    }
198
199    return true;
200  }
201
202  @Override
203  public int hashCode() {
204    int result = servers.hashCode();
205    result = 31 * result + tables.hashCode();
206    result = 31 * result + name.hashCode();
207    result = 31 * result + configuration.hashCode();
208    return result;
209  }
210}