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