2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.rsgroup
;
20 import com
.google
.protobuf
.ServiceException
;
22 import java
.io
.IOException
;
23 import java
.util
.ArrayList
;
24 import java
.util
.List
;
27 import org
.apache
.hadoop
.hbase
.TableName
;
28 import org
.apache
.hadoop
.hbase
.TableNotFoundException
;
29 import org
.apache
.hadoop
.hbase
.client
.Admin
;
30 import org
.apache
.hadoop
.hbase
.client
.Connection
;
31 import org
.apache
.hadoop
.hbase
.net
.Address
;
32 import org
.apache
.hadoop
.hbase
.protobuf
.ProtobufUtil
;
33 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.HBaseProtos
;
34 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.AddRSGroupRequest
;
35 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.BalanceRSGroupRequest
;
36 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.GetRSGroupInfoOfServerRequest
;
37 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.GetRSGroupInfoOfServerResponse
;
38 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.GetRSGroupInfoOfTableRequest
;
39 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.GetRSGroupInfoOfTableResponse
;
40 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.GetRSGroupInfoRequest
;
41 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.GetRSGroupInfoResponse
;
42 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.ListRSGroupInfosRequest
;
43 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.MoveServersAndTablesRequest
;
44 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.MoveServersRequest
;
45 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.MoveTablesRequest
;
46 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.RSGroupAdminService
;
47 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.RemoveRSGroupRequest
;
48 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupAdminProtos
.RemoveServersRequest
;
49 import org
.apache
.hadoop
.hbase
.protobuf
.generated
.RSGroupProtos
;
50 import org
.apache
.yetus
.audience
.InterfaceAudience
;
52 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.collect
.Sets
;
55 * Client used for managing region server group information.
57 @InterfaceAudience.Private
58 public class RSGroupAdminClient
implements RSGroupAdmin
{
59 private RSGroupAdminService
.BlockingInterface stub
;
62 public RSGroupAdminClient(Connection conn
) throws IOException
{
63 admin
= conn
.getAdmin();
64 stub
= RSGroupAdminService
.newBlockingStub(admin
.coprocessorService());
68 public RSGroupInfo
getRSGroupInfo(String groupName
) throws IOException
{
70 GetRSGroupInfoResponse resp
= stub
.getRSGroupInfo(null,
71 GetRSGroupInfoRequest
.newBuilder().setRSGroupName(groupName
).build());
72 if(resp
.hasRSGroupInfo()) {
73 return RSGroupProtobufUtil
.toGroupInfo(resp
.getRSGroupInfo());
76 } catch (ServiceException e
) {
77 throw ProtobufUtil
.handleRemoteException(e
);
82 public RSGroupInfo
getRSGroupInfoOfTable(TableName tableName
) throws IOException
{
83 GetRSGroupInfoOfTableRequest request
= GetRSGroupInfoOfTableRequest
.newBuilder().setTableName(
84 ProtobufUtil
.toProtoTableName(tableName
)).build();
86 GetRSGroupInfoOfTableResponse resp
= stub
.getRSGroupInfoOfTable(null, request
);
87 if (resp
.hasRSGroupInfo()) {
88 return RSGroupProtobufUtil
.toGroupInfo(resp
.getRSGroupInfo());
91 } catch (ServiceException e
) {
92 throw ProtobufUtil
.handleRemoteException(e
);
97 public void moveServers(Set
<Address
> servers
, String targetGroup
) throws IOException
{
98 Set
<HBaseProtos
.ServerName
> hostPorts
= Sets
.newHashSet();
99 for(Address el
: servers
) {
100 hostPorts
.add(HBaseProtos
.ServerName
.newBuilder()
101 .setHostName(el
.getHostname())
102 .setPort(el
.getPort())
105 MoveServersRequest request
= MoveServersRequest
.newBuilder()
106 .setTargetGroup(targetGroup
)
107 .addAllServers(hostPorts
)
110 stub
.moveServers(null, request
);
111 } catch (ServiceException e
) {
112 throw ProtobufUtil
.handleRemoteException(e
);
117 public void moveTables(Set
<TableName
> tables
, String targetGroup
) throws IOException
{
118 MoveTablesRequest
.Builder builder
= MoveTablesRequest
.newBuilder().setTargetGroup(targetGroup
);
119 for(TableName tableName
: tables
) {
120 builder
.addTableName(ProtobufUtil
.toProtoTableName(tableName
));
121 if (!admin
.tableExists(tableName
)) {
122 throw new TableNotFoundException(tableName
);
126 stub
.moveTables(null, builder
.build());
127 } catch (ServiceException e
) {
128 throw ProtobufUtil
.handleRemoteException(e
);
133 public void addRSGroup(String groupName
) throws IOException
{
134 AddRSGroupRequest request
= AddRSGroupRequest
.newBuilder().setRSGroupName(groupName
).build();
136 stub
.addRSGroup(null, request
);
137 } catch (ServiceException e
) {
138 throw ProtobufUtil
.handleRemoteException(e
);
143 public void removeRSGroup(String name
) throws IOException
{
144 RemoveRSGroupRequest request
= RemoveRSGroupRequest
.newBuilder().setRSGroupName(name
).build();
146 stub
.removeRSGroup(null, request
);
147 } catch (ServiceException e
) {
148 throw ProtobufUtil
.handleRemoteException(e
);
153 public boolean balanceRSGroup(String groupName
) throws IOException
{
154 BalanceRSGroupRequest request
= BalanceRSGroupRequest
.newBuilder()
155 .setRSGroupName(groupName
).build();
157 return stub
.balanceRSGroup(null, request
).getBalanceRan();
158 } catch (ServiceException e
) {
159 throw ProtobufUtil
.handleRemoteException(e
);
164 public List
<RSGroupInfo
> listRSGroups() throws IOException
{
166 List
<RSGroupProtos
.RSGroupInfo
> resp
= stub
.listRSGroupInfos(null,
167 ListRSGroupInfosRequest
.getDefaultInstance()).getRSGroupInfoList();
168 List
<RSGroupInfo
> result
= new ArrayList
<>(resp
.size());
169 for(RSGroupProtos
.RSGroupInfo entry
: resp
) {
170 result
.add(RSGroupProtobufUtil
.toGroupInfo(entry
));
173 } catch (ServiceException e
) {
174 throw ProtobufUtil
.handleRemoteException(e
);
179 public RSGroupInfo
getRSGroupOfServer(Address hostPort
) throws IOException
{
180 GetRSGroupInfoOfServerRequest request
= GetRSGroupInfoOfServerRequest
.newBuilder()
181 .setServer(HBaseProtos
.ServerName
.newBuilder()
182 .setHostName(hostPort
.getHostname())
183 .setPort(hostPort
.getPort())
187 GetRSGroupInfoOfServerResponse resp
= stub
.getRSGroupInfoOfServer(null, request
);
188 if (resp
.hasRSGroupInfo()) {
189 return RSGroupProtobufUtil
.toGroupInfo(resp
.getRSGroupInfo());
192 } catch (ServiceException e
) {
193 throw ProtobufUtil
.handleRemoteException(e
);
198 public void moveServersAndTables(Set
<Address
> servers
, Set
<TableName
> tables
, String targetGroup
)
200 MoveServersAndTablesRequest
.Builder builder
=
201 MoveServersAndTablesRequest
.newBuilder().setTargetGroup(targetGroup
);
202 for(Address el
: servers
) {
203 builder
.addServers(HBaseProtos
.ServerName
.newBuilder()
204 .setHostName(el
.getHostname())
205 .setPort(el
.getPort())
208 for(TableName tableName
: tables
) {
209 builder
.addTableName(ProtobufUtil
.toProtoTableName(tableName
));
210 if (!admin
.tableExists(tableName
)) {
211 throw new TableNotFoundException(tableName
);
215 stub
.moveServersAndTables(null, builder
.build());
216 } catch (ServiceException e
) {
217 throw ProtobufUtil
.handleRemoteException(e
);
222 public void removeServers(Set
<Address
> servers
) throws IOException
{
223 Set
<HBaseProtos
.ServerName
> hostPorts
= Sets
.newHashSet();
224 for(Address el
: servers
) {
225 hostPorts
.add(HBaseProtos
.ServerName
.newBuilder()
226 .setHostName(el
.getHostname())
227 .setPort(el
.getPort())
230 RemoveServersRequest request
= RemoveServersRequest
.newBuilder()
231 .addAllServers(hostPorts
)
234 stub
.removeServers(null, request
);
235 } catch (ServiceException e
) {
236 throw ProtobufUtil
.handleRemoteException(e
);