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