View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rsgroup;
22  
23  import com.google.common.collect.Sets;
24  
25  import java.util.Collection;
26  import java.util.NavigableSet;
27  import java.util.Set;
28  
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.classification.InterfaceStability;
32  import org.apache.hadoop.hbase.net.Address;
33  
34  /**
35   * Stores the group information of region server groups.
36   */
37  @InterfaceAudience.Public
38  @InterfaceStability.Evolving
39  public class RSGroupInfo {
40  
41    public static final String DEFAULT_GROUP = "default";
42    public static final String NAMESPACE_DESC_PROP_GROUP = "hbase.rsgroup.name";
43  
44    private String name;
45    private Set<Address> servers;
46    private NavigableSet<TableName> tables;
47  
48    public RSGroupInfo(String name) {
49      this(name, Sets.<Address>newHashSet(), Sets.<TableName>newTreeSet());
50    }
51  
52    RSGroupInfo(String name,
53                Set<Address> servers,
54                NavigableSet<TableName> tables) {
55      this.name = name;
56      this.servers = servers;
57      this.tables = tables;
58    }
59  
60    public RSGroupInfo(RSGroupInfo src) {
61      name = src.getName();
62      servers = Sets.newHashSet(src.getServers());
63      tables = Sets.newTreeSet(src.getTables());
64    }
65  
66    /**
67     * Get group name.
68     *
69     * @return group name
70     */
71    public String getName() {
72      return name;
73    }
74  
75    /**
76     * Adds the server to the group.
77     *
78     * @param server the server
79     */
80    public void addServer(Address server){
81      servers.add(server);
82    }
83  
84    /**
85     * Adds a group of servers.
86     *
87     * @param servers the servers
88     */
89    public void addAllServers(Collection<Address> addresses){
90      this.servers.addAll(addresses);
91    }
92  
93    /**
94     * @param address Address of the server
95     * @return true, if a server with address is found
96     */
97    public boolean containsServer(Address address) {
98      return servers.contains(address);
99    }
100 
101   /**
102    * Get list of servers.
103    *
104    * @return set of servers
105    */
106   public Set<Address> getServers() {
107     return servers;
108   }
109 
110   /**
111    * Remove a server from this group.
112    *
113    * @param address Address of the server to remove
114    */
115   public boolean removeServer(Address address) {
116     return servers.remove(address);
117   }
118 
119   /**
120    * Set of tables that are members of this group
121    * @return set of tables
122    */
123   public NavigableSet<TableName> getTables() {
124     return tables;
125   }
126 
127   public void addTable(TableName table) {
128     tables.add(table);
129   }
130 
131   public void addAllTables(Collection<TableName> arg) {
132     tables.addAll(arg);
133   }
134 
135   public boolean containsTable(TableName table) {
136     return tables.contains(table);
137   }
138 
139   public boolean removeTable(TableName table) {
140     return tables.remove(table);
141   }
142 
143   @Override
144   public String toString() {
145     StringBuffer sb = new StringBuffer();
146     sb.append("Name:");
147     sb.append(this.name);
148     sb.append(", ");
149     sb.append(" Servers:");
150     sb.append(this.servers);
151     sb.append(", ");
152     sb.append(" Tables:");
153     sb.append(this.tables);
154     return sb.toString();
155 
156   }
157 
158   @Override
159   public boolean equals(Object o) {
160     if (this == o) {
161       return true;
162     }
163     if (o == null || getClass() != o.getClass()) {
164       return false;
165     }
166 
167     RSGroupInfo RSGroupInfo = (RSGroupInfo) o;
168 
169     if (!name.equals(RSGroupInfo.name)) {
170       return false;
171     }
172     if (!servers.equals(RSGroupInfo.servers)) {
173       return false;
174     }
175     if (!tables.equals(RSGroupInfo.tables)) {
176       return false;
177     }
178 
179     return true;
180   }
181 
182   @Override
183   public int hashCode() {
184     int result = servers.hashCode();
185     result = 31 * result + tables.hashCode();
186     result = 31 * result + name.hashCode();
187     return result;
188   }
189 
190 }