View Javadoc

1   /**
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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.rsgroup;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.apache.hadoop.hbase.TableName;
26  import org.apache.hadoop.hbase.TableNotFoundException;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.client.Admin;
29  import org.apache.hadoop.hbase.client.Connection;
30  import org.apache.hadoop.hbase.net.Address;
31  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
32  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
33  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.AddRSGroupRequest;
34  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.BalanceRSGroupRequest;
35  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfServerRequest;
36  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfServerResponse;
37  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfTableRequest;
38  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfTableResponse;
39  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoRequest;
40  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoResponse;
41  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.ListRSGroupInfosRequest;
42  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.MoveServersAndTablesRequest;
43  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.MoveServersRequest;
44  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.MoveTablesRequest;
45  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.RSGroupAdminService;
46  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.RemoveRSGroupRequest;
47  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.RemoveServersRequest;
48  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.RenameRSGroupRequest;
49  import org.apache.hadoop.hbase.protobuf.generated.RSGroupProtos;
50  
51  import com.google.common.collect.Sets;
52  import com.google.protobuf.ServiceException;
53  
54  /**
55   * Client used for managing region server group information.
56   */
57  @InterfaceAudience.Private
58  public class RSGroupAdminClient implements RSGroupAdmin {
59    private RSGroupAdminService.BlockingInterface stub;
60    private Admin admin;
61  
62    public RSGroupAdminClient(Connection conn) throws IOException {
63      admin = conn.getAdmin();
64      stub = RSGroupAdminService.newBlockingStub(admin.coprocessorService());
65    }
66  
67    @Override
68    public RSGroupInfo getRSGroupInfo(String groupName) throws IOException {
69      try {
70        GetRSGroupInfoResponse resp = stub.getRSGroupInfo(null,
71            GetRSGroupInfoRequest.newBuilder().setRSGroupName(groupName).build());
72        if(resp.hasRSGroupInfo()) {
73          return RSGroupProtobufUtil.toGroupInfo(resp.getRSGroupInfo());
74        }
75        return null;
76      } catch (ServiceException e) {
77        throw ProtobufUtil.handleRemoteException(e);
78      }
79    }
80  
81    @Override
82    public RSGroupInfo getRSGroupInfoOfTable(TableName tableName) throws IOException {
83      GetRSGroupInfoOfTableRequest request = GetRSGroupInfoOfTableRequest.newBuilder().setTableName(
84          ProtobufUtil.toProtoTableName(tableName)).build();
85      try {
86        GetRSGroupInfoOfTableResponse resp = stub.getRSGroupInfoOfTable(null, request);
87        if (resp.hasRSGroupInfo()) {
88          return RSGroupProtobufUtil.toGroupInfo(resp.getRSGroupInfo());
89        }
90        return null;
91      } catch (ServiceException e) {
92        throw ProtobufUtil.handleRemoteException(e);
93      }
94    }
95  
96    @Override
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())
103         .build());
104     }
105     MoveServersRequest request = MoveServersRequest.newBuilder()
106             .setTargetGroup(targetGroup)
107             .addAllServers(hostPorts)
108             .build();
109     try {
110       stub.moveServers(null, request);
111     } catch (ServiceException e) {
112       throw ProtobufUtil.handleRemoteException(e);
113     }
114   }
115 
116   @Override
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);
123       }
124     }
125     try {
126       stub.moveTables(null, builder.build());
127     } catch (ServiceException e) {
128       throw ProtobufUtil.handleRemoteException(e);
129     }
130   }
131 
132   @Override
133   public void addRSGroup(String groupName) throws IOException {
134     AddRSGroupRequest request = AddRSGroupRequest.newBuilder().setRSGroupName(groupName).build();
135     try {
136       stub.addRSGroup(null, request);
137     } catch (ServiceException e) {
138       throw ProtobufUtil.handleRemoteException(e);
139     }
140   }
141 
142   @Override
143   public void removeRSGroup(String name) throws IOException {
144     RemoveRSGroupRequest request = RemoveRSGroupRequest.newBuilder().setRSGroupName(name).build();
145     try {
146       stub.removeRSGroup(null, request);
147     } catch (ServiceException e) {
148       throw ProtobufUtil.handleRemoteException(e);
149     }
150   }
151 
152   @Override
153   public boolean balanceRSGroup(String groupName) throws IOException {
154     BalanceRSGroupRequest request = BalanceRSGroupRequest.newBuilder()
155         .setRSGroupName(groupName).build();
156     try {
157       return stub.balanceRSGroup(null, request).getBalanceRan();
158     } catch (ServiceException e) {
159       throw ProtobufUtil.handleRemoteException(e);
160     }
161   }
162 
163   @Override
164   public List<RSGroupInfo> listRSGroups() throws IOException {
165     try {
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));
171       }
172       return result;
173     } catch (ServiceException e) {
174       throw ProtobufUtil.handleRemoteException(e);
175     }
176   }
177 
178   @Override
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())
184                 .build())
185             .build();
186     try {
187       GetRSGroupInfoOfServerResponse resp = stub.getRSGroupInfoOfServer(null, request);
188       if (resp.hasRSGroupInfo()) {
189         return RSGroupProtobufUtil.toGroupInfo(resp.getRSGroupInfo());
190       }
191       return null;
192     } catch (ServiceException e) {
193       throw ProtobufUtil.handleRemoteException(e);
194     }
195   }
196 
197   @Override
198   public void moveServersAndTables(Set<Address> servers, Set<TableName> tables, String targetGroup)
199       throws IOException {
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())
206               .build());
207     }
208     for(TableName tableName: tables) {
209       builder.addTableName(ProtobufUtil.toProtoTableName(tableName));
210       if (!admin.tableExists(tableName)) {
211         throw new TableNotFoundException(tableName);
212       }
213     }
214     try {
215       stub.moveServersAndTables(null, builder.build());
216     } catch (ServiceException e) {
217       throw ProtobufUtil.handleRemoteException(e);
218     }
219   }
220 
221   @Override
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())
228           .build());
229     }
230     RemoveServersRequest request = RemoveServersRequest.newBuilder()
231         .addAllServers(hostPorts)
232         .build();
233     try {
234       stub.removeServers(null, request);
235     } catch (ServiceException e) {
236       throw ProtobufUtil.handleRemoteException(e);
237     }
238   }
239 
240   @Override
241   public void renameRSGroup(String oldName, String newName) throws IOException {
242     RenameRSGroupRequest request = RenameRSGroupRequest.newBuilder()
243         .setOldRsgroupName(oldName)
244         .setNewRsgroupName(newName).build();
245     try {
246       stub.renameRSGroup(null, request);
247     } catch (ServiceException e) {
248       throw ProtobufUtil.handleRemoteException(e);
249     }
250   }
251 
252   @Override
253   public void close() throws IOException {
254   }
255 }