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(controller, csr, 056 new org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback< 057 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 addListener( 079 callerBuilder.action((c, s) -> rpcCall(method, request, responsePrototype, c, s)).call(), 080 (r, e) -> { 081 if (e != null) { 082 ((ClientCoprocessorRpcController) controller).setFailed(e); 083 } 084 done.run(r); 085 }); 086 } 087}