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;
022
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.hadoop.hbase.client.AsyncRpcRetryingCallerFactory.ServerRequestCallerBuilder;
025import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils;
026import org.apache.hadoop.hbase.ipc.HBaseRpcController;
027import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ClientService;
028import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.CoprocessorServiceRequest;
029import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.CoprocessorServiceResponse;
030
031import com.google.protobuf.Descriptors.MethodDescriptor;
032import com.google.protobuf.Message;
033import com.google.protobuf.RpcCallback;
034import com.google.protobuf.RpcChannel;
035import com.google.protobuf.RpcController;
036
037/**
038 * The implementation of a region server based coprocessor rpc channel.
039 */
040@InterfaceAudience.Private
041public class RegionServerCoprocessorRpcChannelImpl implements RpcChannel {
042
043  ServerRequestCallerBuilder<Message> callerBuilder;
044
045  RegionServerCoprocessorRpcChannelImpl(ServerRequestCallerBuilder<Message> callerBuilder) {
046    this.callerBuilder = callerBuilder;
047  }
048
049  private CompletableFuture<Message> rpcCall(MethodDescriptor method, Message request,
050      Message responsePrototype, HBaseRpcController controller, ClientService.Interface stub) {
051    CompletableFuture<Message> future = new CompletableFuture<>();
052    CoprocessorServiceRequest csr =
053        CoprocessorRpcUtils.getCoprocessorServiceRequest(method, request);
054    stub.execRegionServerService(
055      controller,
056      csr,
057      new org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback<CoprocessorServiceResponse>() {
058
059        @Override
060        public void run(CoprocessorServiceResponse resp) {
061          if (controller.failed()) {
062            future.completeExceptionally(controller.getFailed());
063          } else {
064            try {
065              future.complete(CoprocessorRpcUtils.getResponse(resp, responsePrototype));
066            } catch (IOException e) {
067              future.completeExceptionally(e);
068            }
069          }
070        }
071      });
072    return future;
073  }
074
075  @Override
076  public void callMethod(MethodDescriptor method, RpcController controller, Message request,
077      Message responsePrototype, RpcCallback<Message> done) {
078    callerBuilder.action((c, s) -> rpcCall(method, request, responsePrototype, c, s)).call()
079        .whenComplete(((r, e) -> {
080          if (e != null) {
081            ((ClientCoprocessorRpcController) controller).setFailed(e);
082          }
083          done.run(r);
084        }));
085  }
086}