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