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.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.client.Connection;
28  import org.apache.hadoop.hbase.net.Address;
29  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
30  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
31  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.AddRSGroupRequest;
32  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.BalanceRSGroupRequest;
33  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfServerRequest;
34  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfServerResponse;
35  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfTableRequest;
36  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfTableResponse;
37  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoRequest;
38  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoResponse;
39  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.ListRSGroupInfosRequest;
40  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.MoveServersAndTablesRequest;
41  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.MoveServersRequest;
42  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.MoveTablesRequest;
43  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.RSGroupAdminService;
44  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.RemoveRSGroupRequest;
45  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.RemoveServersRequest;
46  import org.apache.hadoop.hbase.protobuf.generated.RSGroupProtos;
47  
48  import com.google.common.collect.Sets;
49  import com.google.protobuf.ServiceException;
50  
51  /**
52   * Client used for managing region server group information.
53   */
54  @InterfaceAudience.Private
55  class RSGroupAdminClient implements RSGroupAdmin {
56    private RSGroupAdminService.BlockingInterface stub;
57  
58    public RSGroupAdminClient(Connection conn) throws IOException {
59      stub = RSGroupAdminService.newBlockingStub(conn.getAdmin().coprocessorService());
60    }
61  
62    @Override
63    public RSGroupInfo getRSGroupInfo(String groupName) throws IOException {
64      try {
65        GetRSGroupInfoResponse resp = stub.getRSGroupInfo(null,
66            GetRSGroupInfoRequest.newBuilder().setRSGroupName(groupName).build());
67        if(resp.hasRSGroupInfo()) {
68          return RSGroupProtobufUtil.toGroupInfo(resp.getRSGroupInfo());
69        }
70        return null;
71      } catch (ServiceException e) {
72        throw ProtobufUtil.handleRemoteException(e);
73      }
74    }
75  
76    @Override
77    public RSGroupInfo getRSGroupInfoOfTable(TableName tableName) throws IOException {
78      GetRSGroupInfoOfTableRequest request = GetRSGroupInfoOfTableRequest.newBuilder().setTableName(
79          ProtobufUtil.toProtoTableName(tableName)).build();
80      try {
81        GetRSGroupInfoOfTableResponse resp = stub.getRSGroupInfoOfTable(null, request);
82        if (resp.hasRSGroupInfo()) {
83          return RSGroupProtobufUtil.toGroupInfo(resp.getRSGroupInfo());
84        }
85        return null;
86      } catch (ServiceException e) {
87        throw ProtobufUtil.handleRemoteException(e);
88      }
89    }
90  
91    @Override
92    public void moveServers(Set<Address> servers, String targetGroup) throws IOException {
93      Set<HBaseProtos.ServerName> hostPorts = Sets.newHashSet();
94      for(Address el: servers) {
95        hostPorts.add(HBaseProtos.ServerName.newBuilder()
96          .setHostName(el.getHostname())
97          .setPort(el.getPort())
98          .build());
99      }
100     MoveServersRequest request = MoveServersRequest.newBuilder()
101             .setTargetGroup(targetGroup)
102             .addAllServers(hostPorts)
103             .build();
104     try {
105       stub.moveServers(null, request);
106     } catch (ServiceException e) {
107       throw ProtobufUtil.handleRemoteException(e);
108     }
109   }
110 
111   @Override
112   public void moveTables(Set<TableName> tables, String targetGroup) throws IOException {
113     MoveTablesRequest.Builder builder = MoveTablesRequest.newBuilder().setTargetGroup(targetGroup);
114     for(TableName tableName: tables) {
115       builder.addTableName(ProtobufUtil.toProtoTableName(tableName));
116     }
117     try {
118       stub.moveTables(null, builder.build());
119     } catch (ServiceException e) {
120       throw ProtobufUtil.handleRemoteException(e);
121     }
122   }
123 
124   @Override
125   public void addRSGroup(String groupName) throws IOException {
126     AddRSGroupRequest request = AddRSGroupRequest.newBuilder().setRSGroupName(groupName).build();
127     try {
128       stub.addRSGroup(null, request);
129     } catch (ServiceException e) {
130       throw ProtobufUtil.handleRemoteException(e);
131     }
132   }
133 
134   @Override
135   public void removeRSGroup(String name) throws IOException {
136     RemoveRSGroupRequest request = RemoveRSGroupRequest.newBuilder().setRSGroupName(name).build();
137     try {
138       stub.removeRSGroup(null, request);
139     } catch (ServiceException e) {
140       throw ProtobufUtil.handleRemoteException(e);
141     }
142   }
143 
144   @Override
145   public boolean balanceRSGroup(String groupName) throws IOException {
146     BalanceRSGroupRequest request = BalanceRSGroupRequest.newBuilder()
147         .setRSGroupName(groupName).build();
148     try {
149       return stub.balanceRSGroup(null, request).getBalanceRan();
150     } catch (ServiceException e) {
151       throw ProtobufUtil.handleRemoteException(e);
152     }
153   }
154 
155   @Override
156   public List<RSGroupInfo> listRSGroups() throws IOException {
157     try {
158       List<RSGroupProtos.RSGroupInfo> resp = stub.listRSGroupInfos(null,
159           ListRSGroupInfosRequest.getDefaultInstance()).getRSGroupInfoList();
160       List<RSGroupInfo> result = new ArrayList<>(resp.size());
161       for(RSGroupProtos.RSGroupInfo entry : resp) {
162         result.add(RSGroupProtobufUtil.toGroupInfo(entry));
163       }
164       return result;
165     } catch (ServiceException e) {
166       throw ProtobufUtil.handleRemoteException(e);
167     }
168   }
169 
170   @Override
171   public RSGroupInfo getRSGroupOfServer(Address hostPort) throws IOException {
172     GetRSGroupInfoOfServerRequest request = GetRSGroupInfoOfServerRequest.newBuilder()
173             .setServer(HBaseProtos.ServerName.newBuilder()
174                 .setHostName(hostPort.getHostname())
175                 .setPort(hostPort.getPort())
176                 .build())
177             .build();
178     try {
179       GetRSGroupInfoOfServerResponse resp = stub.getRSGroupInfoOfServer(null, request);
180       if (resp.hasRSGroupInfo()) {
181         return RSGroupProtobufUtil.toGroupInfo(resp.getRSGroupInfo());
182       }
183       return null;
184     } catch (ServiceException e) {
185       throw ProtobufUtil.handleRemoteException(e);
186     }
187   }
188 
189   @Override
190   public void moveServersAndTables(Set<Address> servers, Set<TableName> tables, String targetGroup)
191       throws IOException {
192     MoveServersAndTablesRequest.Builder builder =
193             MoveServersAndTablesRequest.newBuilder().setTargetGroup(targetGroup);
194     for(Address el: servers) {
195       builder.addServers(HBaseProtos.ServerName.newBuilder()
196               .setHostName(el.getHostname())
197               .setPort(el.getPort())
198               .build());
199     }
200     for(TableName tableName: tables) {
201       builder.addTableName(ProtobufUtil.toProtoTableName(tableName));
202     }
203     try {
204       stub.moveServersAndTables(null, builder.build());
205     } catch (ServiceException e) {
206       throw ProtobufUtil.handleRemoteException(e);
207     }
208   }
209 
210   @Override
211   public void removeServers(Set<Address> servers) throws IOException {
212     Set<HBaseProtos.ServerName> hostPorts = Sets.newHashSet();
213     for(Address el: servers) {
214       hostPorts.add(HBaseProtos.ServerName.newBuilder()
215           .setHostName(el.getHostname())
216           .setPort(el.getPort())
217           .build());
218     }
219     RemoveServersRequest request = RemoveServersRequest.newBuilder()
220         .addAllServers(hostPorts)
221         .build();
222     try {
223       stub.removeServers(null, request);
224     } catch (ServiceException e) {
225       throw ProtobufUtil.handleRemoteException(e);
226     }
227   }
228 
229   @Override
230   public void close() throws IOException {
231   }
232 }