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 com.google.protobuf.ServiceException; 021 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.List; 025import java.util.Set; 026 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.TableNotFoundException; 029import org.apache.hadoop.hbase.client.Admin; 030import org.apache.hadoop.hbase.client.Connection; 031import org.apache.hadoop.hbase.net.Address; 032import org.apache.hadoop.hbase.protobuf.ProtobufUtil; 033import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos; 034import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.AddRSGroupRequest; 035import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.BalanceRSGroupRequest; 036import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfServerRequest; 037import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfServerResponse; 038import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfTableRequest; 039import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfTableResponse; 040import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoRequest; 041import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoResponse; 042import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.ListRSGroupInfosRequest; 043import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.MoveServersAndTablesRequest; 044import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.MoveServersRequest; 045import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.MoveTablesRequest; 046import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.RSGroupAdminService; 047import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.RemoveRSGroupRequest; 048import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.RemoveServersRequest; 049import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.RenameRSGroupRequest; 050import org.apache.hadoop.hbase.protobuf.generated.RSGroupProtos; 051import org.apache.yetus.audience.InterfaceAudience; 052 053import org.apache.hbase.thirdparty.com.google.common.collect.Sets; 054 055/** 056 * Client used for managing region server group information. 057 */ 058@InterfaceAudience.Private 059public class RSGroupAdminClient implements RSGroupAdmin { 060 private RSGroupAdminService.BlockingInterface stub; 061 private Admin admin; 062 063 public RSGroupAdminClient(Connection conn) throws IOException { 064 admin = conn.getAdmin(); 065 stub = RSGroupAdminService.newBlockingStub(admin.coprocessorService()); 066 } 067 068 @Override 069 public RSGroupInfo getRSGroupInfo(String groupName) throws IOException { 070 try { 071 GetRSGroupInfoResponse resp = stub.getRSGroupInfo(null, 072 GetRSGroupInfoRequest.newBuilder().setRSGroupName(groupName).build()); 073 if(resp.hasRSGroupInfo()) { 074 return RSGroupProtobufUtil.toGroupInfo(resp.getRSGroupInfo()); 075 } 076 return null; 077 } catch (ServiceException e) { 078 throw ProtobufUtil.handleRemoteException(e); 079 } 080 } 081 082 @Override 083 public RSGroupInfo getRSGroupInfoOfTable(TableName tableName) throws IOException { 084 GetRSGroupInfoOfTableRequest request = GetRSGroupInfoOfTableRequest.newBuilder().setTableName( 085 ProtobufUtil.toProtoTableName(tableName)).build(); 086 try { 087 GetRSGroupInfoOfTableResponse resp = stub.getRSGroupInfoOfTable(null, request); 088 if (resp.hasRSGroupInfo()) { 089 return RSGroupProtobufUtil.toGroupInfo(resp.getRSGroupInfo()); 090 } 091 return null; 092 } catch (ServiceException e) { 093 throw ProtobufUtil.handleRemoteException(e); 094 } 095 } 096 097 @Override 098 public void moveServers(Set<Address> servers, String targetGroup) throws IOException { 099 Set<HBaseProtos.ServerName> hostPorts = Sets.newHashSet(); 100 for(Address el: servers) { 101 hostPorts.add(HBaseProtos.ServerName.newBuilder() 102 .setHostName(el.getHostname()) 103 .setPort(el.getPort()) 104 .build()); 105 } 106 MoveServersRequest request = MoveServersRequest.newBuilder() 107 .setTargetGroup(targetGroup) 108 .addAllServers(hostPorts) 109 .build(); 110 try { 111 stub.moveServers(null, request); 112 } catch (ServiceException e) { 113 throw ProtobufUtil.handleRemoteException(e); 114 } 115 } 116 117 @Override 118 public void moveTables(Set<TableName> tables, String targetGroup) throws IOException { 119 MoveTablesRequest.Builder builder = MoveTablesRequest.newBuilder().setTargetGroup(targetGroup); 120 for(TableName tableName: tables) { 121 builder.addTableName(ProtobufUtil.toProtoTableName(tableName)); 122 if (!admin.tableExists(tableName)) { 123 throw new TableNotFoundException(tableName); 124 } 125 } 126 try { 127 stub.moveTables(null, builder.build()); 128 } catch (ServiceException e) { 129 throw ProtobufUtil.handleRemoteException(e); 130 } 131 } 132 133 @Override 134 public void addRSGroup(String groupName) throws IOException { 135 AddRSGroupRequest request = AddRSGroupRequest.newBuilder().setRSGroupName(groupName).build(); 136 try { 137 stub.addRSGroup(null, request); 138 } catch (ServiceException e) { 139 throw ProtobufUtil.handleRemoteException(e); 140 } 141 } 142 143 @Override 144 public void removeRSGroup(String name) throws IOException { 145 RemoveRSGroupRequest request = RemoveRSGroupRequest.newBuilder().setRSGroupName(name).build(); 146 try { 147 stub.removeRSGroup(null, request); 148 } catch (ServiceException e) { 149 throw ProtobufUtil.handleRemoteException(e); 150 } 151 } 152 153 @Override 154 public boolean balanceRSGroup(String groupName) throws IOException { 155 BalanceRSGroupRequest request = BalanceRSGroupRequest.newBuilder() 156 .setRSGroupName(groupName).build(); 157 try { 158 return stub.balanceRSGroup(null, request).getBalanceRan(); 159 } catch (ServiceException e) { 160 throw ProtobufUtil.handleRemoteException(e); 161 } 162 } 163 164 @Override 165 public List<RSGroupInfo> listRSGroups() throws IOException { 166 try { 167 List<RSGroupProtos.RSGroupInfo> resp = stub.listRSGroupInfos(null, 168 ListRSGroupInfosRequest.getDefaultInstance()).getRSGroupInfoList(); 169 List<RSGroupInfo> result = new ArrayList<>(resp.size()); 170 for(RSGroupProtos.RSGroupInfo entry : resp) { 171 result.add(RSGroupProtobufUtil.toGroupInfo(entry)); 172 } 173 return result; 174 } catch (ServiceException e) { 175 throw ProtobufUtil.handleRemoteException(e); 176 } 177 } 178 179 @Override 180 public RSGroupInfo getRSGroupOfServer(Address hostPort) throws IOException { 181 GetRSGroupInfoOfServerRequest request = GetRSGroupInfoOfServerRequest.newBuilder() 182 .setServer(HBaseProtos.ServerName.newBuilder() 183 .setHostName(hostPort.getHostname()) 184 .setPort(hostPort.getPort()) 185 .build()) 186 .build(); 187 try { 188 GetRSGroupInfoOfServerResponse resp = stub.getRSGroupInfoOfServer(null, request); 189 if (resp.hasRSGroupInfo()) { 190 return RSGroupProtobufUtil.toGroupInfo(resp.getRSGroupInfo()); 191 } 192 return null; 193 } catch (ServiceException e) { 194 throw ProtobufUtil.handleRemoteException(e); 195 } 196 } 197 198 @Override 199 public void moveServersAndTables(Set<Address> servers, Set<TableName> tables, String targetGroup) 200 throws IOException { 201 MoveServersAndTablesRequest.Builder builder = 202 MoveServersAndTablesRequest.newBuilder().setTargetGroup(targetGroup); 203 for(Address el: servers) { 204 builder.addServers(HBaseProtos.ServerName.newBuilder() 205 .setHostName(el.getHostname()) 206 .setPort(el.getPort()) 207 .build()); 208 } 209 for(TableName tableName: tables) { 210 builder.addTableName(ProtobufUtil.toProtoTableName(tableName)); 211 if (!admin.tableExists(tableName)) { 212 throw new TableNotFoundException(tableName); 213 } 214 } 215 try { 216 stub.moveServersAndTables(null, builder.build()); 217 } catch (ServiceException e) { 218 throw ProtobufUtil.handleRemoteException(e); 219 } 220 } 221 222 @Override 223 public void removeServers(Set<Address> servers) throws IOException { 224 Set<HBaseProtos.ServerName> hostPorts = Sets.newHashSet(); 225 for(Address el: servers) { 226 hostPorts.add(HBaseProtos.ServerName.newBuilder() 227 .setHostName(el.getHostname()) 228 .setPort(el.getPort()) 229 .build()); 230 } 231 RemoveServersRequest request = RemoveServersRequest.newBuilder() 232 .addAllServers(hostPorts) 233 .build(); 234 try { 235 stub.removeServers(null, request); 236 } catch (ServiceException e) { 237 throw ProtobufUtil.handleRemoteException(e); 238 } 239 } 240 241 @Override 242 public void renameRSGroup(String oldName, String newName) throws IOException { 243 RenameRSGroupRequest request = RenameRSGroupRequest.newBuilder() 244 .setOldRsgroupName(oldName) 245 .setNewRsgroupName(newName).build(); 246 try { 247 stub.renameRSGroup(null, request); 248 } catch (ServiceException e) { 249 throw ProtobufUtil.handleRemoteException(e); 250 } 251 } 252}