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