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.RSGroupProtos;
050import org.apache.yetus.audience.InterfaceAudience;
051
052import org.apache.hbase.thirdparty.com.google.common.collect.Sets;
053
054/**
055 * Client used for managing region server group information.
056 */
057@InterfaceAudience.Private
058public class RSGroupAdminClient implements RSGroupAdmin {
059  private RSGroupAdminService.BlockingInterface stub;
060  private Admin admin;
061
062  public RSGroupAdminClient(Connection conn) throws IOException {
063    admin = conn.getAdmin();
064    stub = RSGroupAdminService.newBlockingStub(admin.coprocessorService());
065  }
066
067  @Override
068  public RSGroupInfo getRSGroupInfo(String groupName) throws IOException {
069    try {
070      GetRSGroupInfoResponse resp = stub.getRSGroupInfo(null,
071          GetRSGroupInfoRequest.newBuilder().setRSGroupName(groupName).build());
072      if(resp.hasRSGroupInfo()) {
073        return RSGroupProtobufUtil.toGroupInfo(resp.getRSGroupInfo());
074      }
075      return null;
076    } catch (ServiceException e) {
077      throw ProtobufUtil.handleRemoteException(e);
078    }
079  }
080
081  @Override
082  public RSGroupInfo getRSGroupInfoOfTable(TableName tableName) throws IOException {
083    GetRSGroupInfoOfTableRequest request = GetRSGroupInfoOfTableRequest.newBuilder().setTableName(
084        ProtobufUtil.toProtoTableName(tableName)).build();
085    try {
086      GetRSGroupInfoOfTableResponse resp = stub.getRSGroupInfoOfTable(null, request);
087      if (resp.hasRSGroupInfo()) {
088        return RSGroupProtobufUtil.toGroupInfo(resp.getRSGroupInfo());
089      }
090      return null;
091    } catch (ServiceException e) {
092      throw ProtobufUtil.handleRemoteException(e);
093    }
094  }
095
096  @Override
097  public void moveServers(Set<Address> servers, String targetGroup) throws IOException {
098    Set<HBaseProtos.ServerName> hostPorts = Sets.newHashSet();
099    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}