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.ExtendedCellScanner;
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,
099    ExtendedCellScanner cellScanner) {
100    CompletableFuture<RESP> future = new CompletableFuture<>();
101    HBaseRpcController controller = conn.rpcControllerFactory.newController(null, cellScanner);
102    try {
103      rpcCall.call(conn.getAdminStub(server), controller, new RpcCallback<RESP>() {
104
105        @Override
106        public void run(RESP resp) {
107          if (controller.failed()) {
108            future.completeExceptionally(controller.getFailed());
109          } else {
110            future.complete(resp);
111          }
112        }
113      });
114    } catch (IOException e) {
115      future.completeExceptionally(e);
116    }
117    return future;
118  }
119
120  private <RESP> CompletableFuture<RESP> call(RpcCall<RESP> rpcCall) {
121    return call(rpcCall, null);
122  }
123
124  public CompletableFuture<GetRegionInfoResponse> getRegionInfo(GetRegionInfoRequest request) {
125    return call((stub, controller, done) -> stub.getRegionInfo(controller, request, done));
126  }
127
128  public CompletableFuture<GetStoreFileResponse> getStoreFile(GetStoreFileRequest request) {
129    return call((stub, controller, done) -> stub.getStoreFile(controller, request, done));
130  }
131
132  public CompletableFuture<GetOnlineRegionResponse>
133    getOnlineRegion(GetOnlineRegionRequest request) {
134    return call((stub, controller, done) -> stub.getOnlineRegion(controller, request, done));
135  }
136
137  public CompletableFuture<OpenRegionResponse> openRegion(OpenRegionRequest request) {
138    return call((stub, controller, done) -> stub.openRegion(controller, request, done));
139  }
140
141  public CompletableFuture<WarmupRegionResponse> warmupRegion(WarmupRegionRequest request) {
142    return call((stub, controller, done) -> stub.warmupRegion(controller, request, done));
143  }
144
145  public CompletableFuture<CloseRegionResponse> closeRegion(CloseRegionRequest request) {
146    return call((stub, controller, done) -> stub.closeRegion(controller, request, done));
147  }
148
149  public CompletableFuture<FlushRegionResponse> flushRegion(FlushRegionRequest request) {
150    return call((stub, controller, done) -> stub.flushRegion(controller, request, done));
151  }
152
153  public CompletableFuture<CompactionSwitchResponse>
154    compactionSwitch(CompactionSwitchRequest request) {
155    return call((stub, controller, done) -> stub.compactionSwitch(controller, request, done));
156  }
157
158  public CompletableFuture<CompactRegionResponse> compactRegion(CompactRegionRequest request) {
159    return call((stub, controller, done) -> stub.compactRegion(controller, request, done));
160  }
161
162  public CompletableFuture<ReplicateWALEntryResponse> replicateWALEntry(
163    ReplicateWALEntryRequest request, ExtendedCellScanner cellScanner, int timeout) {
164    return call((stub, controller, done) -> {
165      controller.setCallTimeout(timeout);
166      stub.replicateWALEntry(controller, request, done);
167    }, cellScanner);
168  }
169
170  public CompletableFuture<ReplicateWALEntryResponse> replay(ReplicateWALEntryRequest request,
171    ExtendedCellScanner cellScanner) {
172    return call((stub, controller, done) -> stub.replay(controller, request, done), cellScanner);
173  }
174
175  public CompletableFuture<RollWALWriterResponse> rollWALWriter(RollWALWriterRequest request) {
176    return call((stub, controller, done) -> stub.rollWALWriter(controller, request, done));
177  }
178
179  public CompletableFuture<GetServerInfoResponse> getServerInfo(GetServerInfoRequest request) {
180    return call((stub, controller, done) -> stub.getServerInfo(controller, request, done));
181  }
182
183  public CompletableFuture<StopServerResponse> stopServer(StopServerRequest request) {
184    return call((stub, controller, done) -> stub.stopServer(controller, request, done));
185  }
186
187  public CompletableFuture<UpdateFavoredNodesResponse>
188    updateFavoredNodes(UpdateFavoredNodesRequest request) {
189    return call((stub, controller, done) -> stub.updateFavoredNodes(controller, request, done));
190  }
191
192  public CompletableFuture<UpdateConfigurationResponse>
193    updateConfiguration(UpdateConfigurationRequest request) {
194    return call((stub, controller, done) -> stub.updateConfiguration(controller, request, done));
195  }
196
197  public CompletableFuture<GetRegionLoadResponse> getRegionLoad(GetRegionLoadRequest request) {
198    return call((stub, controller, done) -> stub.getRegionLoad(controller, request, done));
199  }
200
201  public CompletableFuture<ClearCompactionQueuesResponse>
202    clearCompactionQueues(ClearCompactionQueuesRequest request) {
203    return call((stub, controller, done) -> stub.clearCompactionQueues(controller, request, done));
204  }
205
206  public CompletableFuture<ClearRegionBlockCacheResponse>
207    clearRegionBlockCache(ClearRegionBlockCacheRequest request) {
208    return call((stub, controller, done) -> stub.clearRegionBlockCache(controller, request, done));
209  }
210
211  public CompletableFuture<GetSpaceQuotaSnapshotsResponse>
212    getSpaceQuotaSnapshots(GetSpaceQuotaSnapshotsRequest request) {
213    return call((stub, controller, done) -> stub.getSpaceQuotaSnapshots(controller, request, done));
214  }
215
216  public CompletableFuture<ExecuteProceduresResponse>
217    executeProcedures(ExecuteProceduresRequest request) {
218    return call((stub, controller, done) -> stub.executeProcedures(controller, request, done));
219  }
220}