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