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.ServerRequestCallerBuilder; 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.ClientService; 036import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.CoprocessorServiceRequest; 037import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.CoprocessorServiceResponse; 038 039/** 040 * The implementation of a region server based coprocessor rpc channel. 041 */ 042@InterfaceAudience.Private 043public class RegionServerCoprocessorRpcChannelImpl implements RpcChannel { 044 045 ServerRequestCallerBuilder<Message> callerBuilder; 046 047 RegionServerCoprocessorRpcChannelImpl(ServerRequestCallerBuilder<Message> callerBuilder) { 048 this.callerBuilder = callerBuilder; 049 } 050 051 private CompletableFuture<Message> rpcCall(MethodDescriptor method, Message request, 052 Message responsePrototype, HBaseRpcController controller, ClientService.Interface stub) { 053 CompletableFuture<Message> future = new CompletableFuture<>(); 054 CoprocessorServiceRequest csr = 055 CoprocessorRpcUtils.getCoprocessorServiceRequest(method, request); 056 stub.execRegionServerService(controller, csr, 057 new org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback< 058 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}