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.client;
019
020import java.io.IOException;
021import java.util.concurrent.CompletableFuture;
022import org.apache.hadoop.hbase.CellScanner;
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.ipc.HBaseRpcController;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback;
028import org.apache.hbase.thirdparty.com.google.protobuf.RpcController;
029
030import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService;
031import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.ClearCompactionQueuesRequest;
032import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.ClearCompactionQueuesResponse;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.ClearRegionBlockCacheRequest;
034import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.ClearRegionBlockCacheResponse;
035import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.CloseRegionRequest;
036import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.CloseRegionResponse;
037import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.CompactRegionRequest;
038import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.CompactRegionResponse;
039import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.CompactionSwitchRequest;
040import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.CompactionSwitchResponse;
041import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.ExecuteProceduresRequest;
042import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.ExecuteProceduresResponse;
043import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.FlushRegionRequest;
044import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.FlushRegionResponse;
045import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetOnlineRegionRequest;
046import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetOnlineRegionResponse;
047import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetRegionInfoRequest;
048import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetRegionInfoResponse;
049import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetRegionLoadRequest;
050import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetRegionLoadResponse;
051import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetServerInfoRequest;
052import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetServerInfoResponse;
053import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetStoreFileRequest;
054import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetStoreFileResponse;
055import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.OpenRegionRequest;
056import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.OpenRegionResponse;
057import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.ReplicateWALEntryRequest;
058import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.ReplicateWALEntryResponse;
059import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.RollWALWriterRequest;
060import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.RollWALWriterResponse;
061import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerRequest;
062import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerResponse;
063import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateConfigurationRequest;
064import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateConfigurationResponse;
065import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateFavoredNodesRequest;
066import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateFavoredNodesResponse;
067import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.WarmupRegionRequest;
068import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.WarmupRegionResponse;
069import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.GetSpaceQuotaSnapshotsRequest;
070import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.GetSpaceQuotaSnapshotsResponse;
071
072/**
073 * A simple wrapper of the {@link AdminService} for a region server, which returns a
074 * {@link CompletableFuture}. This is easier to use, as if you use the raw protobuf interface, you
075 * need to get the result from the {@link RpcCallback}, and if there is an exception, you need to
076 * get it from the {@link RpcController} passed in.
077 * <p/>
078 * Notice that there is no retry, and this is intentional. We have different retry for different
079 * usage for now, if later we want to unify them, we can move the retry logic into this class.
080 */
081@InterfaceAudience.Private
082public class AsyncRegionServerAdmin {
083
084  private final ServerName server;
085
086  private final AsyncConnectionImpl conn;
087
088  AsyncRegionServerAdmin(ServerName server, AsyncConnectionImpl conn) {
089    this.server = server;
090    this.conn = conn;
091  }
092
093  @FunctionalInterface
094  private interface RpcCall<RESP> {
095    void call(AdminService.Interface stub, HBaseRpcController controller, RpcCallback<RESP> done);
096  }
097
098  private <RESP> CompletableFuture<RESP> call(RpcCall<RESP> rpcCall, CellScanner cellScanner) {
099    CompletableFuture<RESP> future = new CompletableFuture<>();
100    HBaseRpcController controller = conn.rpcControllerFactory.newController(cellScanner);
101    try {
102      rpcCall.call(conn.getAdminStub(server), controller, new RpcCallback<RESP>() {
103
104        @Override
105        public void run(RESP resp) {
106          if (controller.failed()) {
107            future.completeExceptionally(controller.getFailed());
108          } else {
109            future.complete(resp);
110          }
111        }
112      });
113    } catch (IOException e) {
114      future.completeExceptionally(e);
115    }
116    return future;
117  }
118
119  private <RESP> CompletableFuture<RESP> call(RpcCall<RESP> rpcCall) {
120    return call(rpcCall, null);
121  }
122
123  public CompletableFuture<GetRegionInfoResponse> getRegionInfo(GetRegionInfoRequest request) {
124    return call((stub, controller, done) -> stub.getRegionInfo(controller, request, done));
125  }
126
127  public CompletableFuture<GetStoreFileResponse> getStoreFile(GetStoreFileRequest request) {
128    return call((stub, controller, done) -> stub.getStoreFile(controller, request, done));
129  }
130
131  public CompletableFuture<GetOnlineRegionResponse> getOnlineRegion(
132      GetOnlineRegionRequest request) {
133    return call((stub, controller, done) -> stub.getOnlineRegion(controller, request, done));
134  }
135
136  public CompletableFuture<OpenRegionResponse> openRegion(OpenRegionRequest request) {
137    return call((stub, controller, done) -> stub.openRegion(controller, request, done));
138  }
139
140  public CompletableFuture<WarmupRegionResponse> warmupRegion(WarmupRegionRequest request) {
141    return call((stub, controller, done) -> stub.warmupRegion(controller, request, done));
142  }
143
144  public CompletableFuture<CloseRegionResponse> closeRegion(CloseRegionRequest request) {
145    return call((stub, controller, done) -> stub.closeRegion(controller, request, done));
146  }
147
148  public CompletableFuture<FlushRegionResponse> flushRegion(FlushRegionRequest request) {
149    return call((stub, controller, done) -> stub.flushRegion(controller, request, done));
150  }
151
152  public CompletableFuture<CompactionSwitchResponse> compactionSwitch(
153      CompactionSwitchRequest request) {
154    return call((stub, controller, done) -> stub.compactionSwitch(controller, request, done));
155  }
156
157  public CompletableFuture<CompactRegionResponse> compactRegion(CompactRegionRequest request) {
158    return call((stub, controller, done) -> stub.compactRegion(controller, request, done));
159  }
160
161  public CompletableFuture<ReplicateWALEntryResponse> replicateWALEntry(
162      ReplicateWALEntryRequest request, CellScanner cellScanner, int timeout) {
163    return call((stub, controller, done) -> {
164      controller.setCallTimeout(timeout);
165      stub.replicateWALEntry(controller, request, done);
166    }, cellScanner);
167  }
168
169  public CompletableFuture<ReplicateWALEntryResponse> replay(ReplicateWALEntryRequest request,
170      CellScanner cellScanner) {
171    return call((stub, controller, done) -> stub.replay(controller, request, done), cellScanner);
172  }
173
174  public CompletableFuture<RollWALWriterResponse> rollWALWriter(RollWALWriterRequest request) {
175    return call((stub, controller, done) -> stub.rollWALWriter(controller, request, done));
176  }
177
178  public CompletableFuture<GetServerInfoResponse> getServerInfo(GetServerInfoRequest request) {
179    return call((stub, controller, done) -> stub.getServerInfo(controller, request, done));
180  }
181
182  public CompletableFuture<StopServerResponse> stopServer(StopServerRequest request) {
183    return call((stub, controller, done) -> stub.stopServer(controller, request, done));
184  }
185
186  public CompletableFuture<UpdateFavoredNodesResponse> updateFavoredNodes(
187      UpdateFavoredNodesRequest request) {
188    return call((stub, controller, done) -> stub.updateFavoredNodes(controller, request, done));
189  }
190
191  public CompletableFuture<UpdateConfigurationResponse> updateConfiguration(
192      UpdateConfigurationRequest request) {
193    return call((stub, controller, done) -> stub.updateConfiguration(controller, request, done));
194  }
195
196  public CompletableFuture<GetRegionLoadResponse> getRegionLoad(GetRegionLoadRequest request) {
197    return call((stub, controller, done) -> stub.getRegionLoad(controller, request, done));
198  }
199
200  public CompletableFuture<ClearCompactionQueuesResponse> clearCompactionQueues(
201      ClearCompactionQueuesRequest request) {
202    return call((stub, controller, done) -> stub.clearCompactionQueues(controller, request, done));
203  }
204
205  public CompletableFuture<ClearRegionBlockCacheResponse> clearRegionBlockCache(
206      ClearRegionBlockCacheRequest request) {
207    return call((stub, controller, done) -> stub.clearRegionBlockCache(controller, request, done));
208  }
209
210  public CompletableFuture<GetSpaceQuotaSnapshotsResponse> getSpaceQuotaSnapshots(
211      GetSpaceQuotaSnapshotsRequest request) {
212    return call((stub, controller, done) -> stub.getSpaceQuotaSnapshots(controller, request, done));
213  }
214
215  public CompletableFuture<ExecuteProceduresResponse> executeProcedures(
216      ExecuteProceduresRequest request) {
217    return call((stub, controller, done) -> stub.executeProcedures(controller, request, done));
218  }
219}