View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.ipc;
20  
21  import java.io.IOException;
22  
23  import org.apache.hadoop.hbase.util.ByteStringer;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.client.HConnection;
29  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
30  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
31  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.CoprocessorServiceResponse;
32  
33  import com.google.protobuf.Descriptors;
34  import com.google.protobuf.Message;
35  
36  /**
37   * Provides clients with an RPC connection to call coprocessor endpoint {@link com.google.protobuf.Service}s
38   * against the active master.  An instance of this class may be obtained
39   * by calling {@link org.apache.hadoop.hbase.client.HBaseAdmin#coprocessorService()},
40   * but should normally only be used in creating a new {@link com.google.protobuf.Service} stub to call the endpoint
41   * methods.
42   * @see org.apache.hadoop.hbase.client.HBaseAdmin#coprocessorService()
43   */
44  @InterfaceAudience.Private
45  public class MasterCoprocessorRpcChannel extends CoprocessorRpcChannel{
46    private static Log LOG = LogFactory.getLog(MasterCoprocessorRpcChannel.class);
47  
48    private final HConnection connection;
49  
50    public MasterCoprocessorRpcChannel(HConnection conn) {
51      this.connection = conn;
52    }
53  
54    @Override
55    protected Message callExecService(Descriptors.MethodDescriptor method,
56                                    Message request, Message responsePrototype)
57        throws IOException {
58      if (LOG.isDebugEnabled()) {
59        LOG.debug("Call: "+method.getName()+", "+request.toString());
60      }
61  
62      final ClientProtos.CoprocessorServiceCall call =
63          ClientProtos.CoprocessorServiceCall.newBuilder()
64              .setRow(ByteStringer.wrap(HConstants.EMPTY_BYTE_ARRAY))
65              .setServiceName(method.getService().getFullName())
66              .setMethodName(method.getName())
67              .setRequest(request.toByteString()).build();
68      CoprocessorServiceResponse result = ProtobufUtil.execService(connection.getMaster(), call);
69      Message response = null;
70      if (result.getValue().hasValue()) {
71        response = responsePrototype.newBuilderForType()
72            .mergeFrom(result.getValue().getValue()).build();
73      } else {
74        response = responsePrototype.getDefaultInstanceForType();
75      }
76      if (LOG.isTraceEnabled()) {
77        LOG.trace("Master Result is value=" + response);
78      }
79      return response;
80    }
81  
82  }