View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.rsgroup;
21  
22  import com.google.common.collect.Sets;
23  import com.google.common.net.HostAndPort;
24  import com.google.protobuf.ServiceException;
25  
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Set;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.hbase.classification.InterfaceStability;
36  import org.apache.hadoop.hbase.client.Connection;
37  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
38  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
39  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos;
40  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfServerResponse;
41  import org.apache.hadoop.hbase.protobuf.generated.RSGroupAdminProtos.GetRSGroupInfoOfTableResponse;
42  import org.apache.hadoop.hbase.protobuf.generated.RSGroupProtos;
43  
44  
45  /**
46   * Client used for managing region server group information.
47   */
48  @InterfaceAudience.Public
49  @InterfaceStability.Evolving
50  class RSGroupAdminClient extends RSGroupAdmin {
51    private RSGroupAdminProtos.RSGroupAdminService.BlockingInterface proxy;
52    private static final Log LOG = LogFactory.getLog(RSGroupAdminClient.class);
53  
54    public RSGroupAdminClient(Connection conn) throws IOException {
55      proxy = RSGroupAdminProtos.RSGroupAdminService.newBlockingStub(
56          conn.getAdmin().coprocessorService());
57    }
58  
59    @Override
60    public RSGroupInfo getRSGroupInfo(String groupName) throws IOException {
61      try {
62        RSGroupAdminProtos.GetRSGroupInfoResponse resp =
63          proxy.getRSGroupInfo(null,
64              RSGroupAdminProtos.GetRSGroupInfoRequest.newBuilder()
65                  .setRSGroupName(groupName).build());
66        if(resp.hasRSGroupInfo()) {
67          return ProtobufUtil.toGroupInfo(resp.getRSGroupInfo());
68        }
69        return null;
70      } catch (ServiceException e) {
71        throw ProtobufUtil.getRemoteException(e);
72      }
73    }
74  
75    @Override
76    public RSGroupInfo getRSGroupInfoOfTable(TableName tableName) throws IOException {
77      RSGroupAdminProtos.GetRSGroupInfoOfTableRequest request =
78          RSGroupAdminProtos.GetRSGroupInfoOfTableRequest.newBuilder()
79              .setTableName(ProtobufUtil.toProtoTableName(tableName)).build();
80  
81      try {
82        GetRSGroupInfoOfTableResponse resp = proxy.getRSGroupInfoOfTable(null, request);
83        if (resp.hasRSGroupInfo()) {
84          return ProtobufUtil.toGroupInfo(resp.getRSGroupInfo());
85        }
86        return null;
87      } catch (ServiceException e) {
88        throw ProtobufUtil.getRemoteException(e);
89      }
90    }
91  
92    @Override
93    public void moveServers(Set<HostAndPort> servers, String targetGroup) throws IOException {
94      Set<HBaseProtos.ServerName> hostPorts = Sets.newHashSet();
95      for(HostAndPort el: servers) {
96        hostPorts.add(HBaseProtos.ServerName.newBuilder()
97          .setHostName(el.getHostText())
98          .setPort(el.getPort())
99          .build());
100     }
101     RSGroupAdminProtos.MoveServersRequest request =
102         RSGroupAdminProtos.MoveServersRequest.newBuilder()
103             .setTargetGroup(targetGroup)
104             .addAllServers(hostPorts).build();
105 
106     try {
107       proxy.moveServers(null, request);
108     } catch (ServiceException e) {
109       throw ProtobufUtil.getRemoteException(e);
110     }
111   }
112 
113   @Override
114   public void moveTables(Set<TableName> tables, String targetGroup) throws IOException {
115     RSGroupAdminProtos.MoveTablesRequest.Builder builder =
116         RSGroupAdminProtos.MoveTablesRequest.newBuilder()
117             .setTargetGroup(targetGroup);
118     for(TableName tableName: tables) {
119       builder.addTableName(ProtobufUtil.toProtoTableName(tableName));
120     }
121     try {
122       proxy.moveTables(null, builder.build());
123     } catch (ServiceException e) {
124       throw ProtobufUtil.getRemoteException(e);
125     }
126   }
127 
128   @Override
129   public void addRSGroup(String groupName) throws IOException {
130     RSGroupAdminProtos.AddRSGroupRequest request =
131         RSGroupAdminProtos.AddRSGroupRequest.newBuilder()
132             .setRSGroupName(groupName).build();
133     try {
134       proxy.addRSGroup(null, request);
135     } catch (ServiceException e) {
136       throw ProtobufUtil.getRemoteException(e);
137     }
138   }
139 
140   @Override
141   public void removeRSGroup(String name) throws IOException {
142     RSGroupAdminProtos.RemoveRSGroupRequest request =
143         RSGroupAdminProtos.RemoveRSGroupRequest.newBuilder()
144             .setRSGroupName(name).build();
145     try {
146       proxy.removeRSGroup(null, request);
147     } catch (ServiceException e) {
148       throw ProtobufUtil.getRemoteException(e);
149     }
150   }
151 
152   @Override
153   public boolean balanceRSGroup(String name) throws IOException {
154     RSGroupAdminProtos.BalanceRSGroupRequest request =
155         RSGroupAdminProtos.BalanceRSGroupRequest.newBuilder()
156             .setRSGroupName(name).build();
157 
158     try {
159       return proxy.balanceRSGroup(null, request).getBalanceRan();
160     } catch (ServiceException e) {
161       throw ProtobufUtil.getRemoteException(e);
162     }
163   }
164 
165   @Override
166   public List<RSGroupInfo> listRSGroups() throws IOException {
167     try {
168       List<RSGroupProtos.RSGroupInfo> resp =
169           proxy.listRSGroupInfos(null,
170               RSGroupAdminProtos.ListRSGroupInfosRequest.newBuilder().build()).getRSGroupInfoList();
171       List<RSGroupInfo> result = new ArrayList<RSGroupInfo>(resp.size());
172       for(RSGroupProtos.RSGroupInfo entry: resp) {
173         result.add(ProtobufUtil.toGroupInfo(entry));
174       }
175       return result;
176     } catch (ServiceException e) {
177       throw ProtobufUtil.getRemoteException(e);
178     }
179   }
180 
181   @Override
182   public RSGroupInfo getRSGroupOfServer(HostAndPort hostPort) throws IOException {
183     RSGroupAdminProtos.GetRSGroupInfoOfServerRequest request =
184         RSGroupAdminProtos.GetRSGroupInfoOfServerRequest.newBuilder()
185             .setServer(HBaseProtos.ServerName.newBuilder()
186                 .setHostName(hostPort.getHostText())
187                 .setPort(hostPort.getPort())
188                 .build())
189             .build();
190     try {
191       GetRSGroupInfoOfServerResponse resp = proxy.getRSGroupInfoOfServer(null, request);
192       if (resp.hasRSGroupInfo()) {
193         return ProtobufUtil.toGroupInfo(resp.getRSGroupInfo());
194       }
195       return null;
196     } catch (ServiceException e) {
197       throw ProtobufUtil.getRemoteException(e);
198     }
199   }
200 
201   @Override
202   public void close() throws IOException {
203   }
204 }