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.Set;
023import java.util.SortedSet;
024import java.util.TreeSet;
025
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.net.Address;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * Stores the group information of region server groups.
032 */
033@InterfaceAudience.Public
034public class RSGroupInfo {
035  public static final String DEFAULT_GROUP = "default";
036  public static final String NAMESPACE_DESC_PROP_GROUP = "hbase.rsgroup.name";
037
038  private final String name;
039  // Keep servers in a sorted set so has an expected ordering when displayed.
040  private final SortedSet<Address> servers;
041  // Keep tables sorted too.
042  private final SortedSet<TableName> tables;
043
044  public RSGroupInfo(String name) {
045    this(name, new TreeSet<Address>(), new TreeSet<TableName>());
046  }
047
048  RSGroupInfo(String name, SortedSet<Address> servers, SortedSet<TableName> tables) {
049    this.name = name;
050    this.servers = (servers == null) ? new TreeSet<>() : new TreeSet<>(servers);
051    this.tables  = (tables  == null) ? new TreeSet<>() : new TreeSet<>(tables);
052  }
053
054  public RSGroupInfo(RSGroupInfo src) {
055    this(src.name, src.servers, src.tables);
056  }
057
058  /**
059   * Get group name.
060   */
061  public String getName() {
062    return name;
063  }
064
065  /**
066   * Adds the given server to the group.
067   */
068  public void addServer(Address hostPort){
069    servers.add(hostPort);
070  }
071
072  /**
073   * Adds the given servers to the group.
074   */
075  public void addAllServers(Collection<Address> hostPort){
076    servers.addAll(hostPort);
077  }
078
079  /**
080   * @param hostPort hostPort of the server
081   * @return true, if a server with hostPort is found
082   */
083  public boolean containsServer(Address hostPort) {
084    return servers.contains(hostPort);
085  }
086
087  /**
088   * Get list of servers.
089   */
090  public Set<Address> getServers() {
091    return servers;
092  }
093
094  /**
095   * Remove given server from the group.
096   */
097  public boolean removeServer(Address hostPort) {
098    return servers.remove(hostPort);
099  }
100
101  /**
102   * Get set of tables that are members of the group.
103   */
104  public SortedSet<TableName> getTables() {
105    return tables;
106  }
107
108  public void addTable(TableName table) {
109    tables.add(table);
110  }
111
112  public void addAllTables(Collection<TableName> arg) {
113    tables.addAll(arg);
114  }
115
116  public boolean containsTable(TableName table) {
117    return tables.contains(table);
118  }
119
120  public boolean removeTable(TableName table) {
121    return tables.remove(table);
122  }
123
124  @Override
125  public String toString() {
126    StringBuilder sb = new StringBuilder();
127    sb.append("Name:");
128    sb.append(this.name);
129    sb.append(", ");
130    sb.append(" Servers:");
131    sb.append(this.servers);
132    sb.append(", ");
133    sb.append(" Tables:");
134    sb.append(this.tables);
135    return sb.toString();
136
137  }
138
139  @Override
140  public boolean equals(Object o) {
141    if (this == o) {
142      return true;
143    }
144    if (o == null || getClass() != o.getClass()) {
145      return false;
146    }
147
148    RSGroupInfo RSGroupInfo = (RSGroupInfo) o;
149
150    if (!name.equals(RSGroupInfo.name)) {
151      return false;
152    }
153    if (!servers.equals(RSGroupInfo.servers)) {
154      return false;
155    }
156    if (!tables.equals(RSGroupInfo.tables)) {
157      return false;
158    }
159
160    return true;
161  }
162
163  @Override
164  public int hashCode() {
165    int result = servers.hashCode();
166    result = 31 * result + tables.hashCode();
167    result = 31 * result + name.hashCode();
168    return result;
169  }
170}