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  package org.apache.hadoop.hbase.ipc;
19
20  import com.google.protobuf.Descriptors;
21  import com.google.protobuf.Message;
22  import com.google.protobuf.RpcController;
23
24  import java.io.IOException;
25
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.client.ClusterConnection;
31  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
32  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
33  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.CoprocessorServiceResponse;
34
35  /**
36   * Provides clients with an RPC connection to call coprocessor endpoint {@link com.google.protobuf.Service}s
37   * against the active master.  An instance of this class may be obtained
38   * by calling {@link org.apache.hadoop.hbase.client.HBaseAdmin#coprocessorService()},
39   * but should normally only be used in creating a new {@link com.google.protobuf.Service} stub to call the endpoint
40   * methods.
41   * @see org.apache.hadoop.hbase.client.HBaseAdmin#coprocessorService()
42   */
43  @InterfaceAudience.Private
44  public class MasterCoprocessorRpcChannel extends SyncCoprocessorRpcChannel {
45    private static final Log LOG = LogFactory.getLog(MasterCoprocessorRpcChannel.class);
46
47    private final ClusterConnection connection;
48
49    public MasterCoprocessorRpcChannel(ClusterConnection conn) {
50      this.connection = conn;
51    }
52
53    @Override
54    protected Message callExecService(RpcController controller, Descriptors.MethodDescriptor method,
55                                    Message request, Message responsePrototype)
56        throws IOException {
57      if (LOG.isTraceEnabled()) {
58        LOG.trace("Call: "+method.getName()+", "+request.toString());
59      }
60
61      final ClientProtos.CoprocessorServiceCall call =
62          CoprocessorRpcUtils.buildServiceCall(HConstants.EMPTY_BYTE_ARRAY, method, request);
63
64      // TODO: Are we retrying here? Does not seem so. We should use RetryingRpcCaller
65      CoprocessorServiceResponse result = ProtobufUtil.execService(controller,
66        connection.getMaster(), call);
67      Message response = null;
68      if (result.getValue().hasValue()) {
69        Message.Builder builder = responsePrototype.newBuilderForType();
70        ProtobufUtil.mergeFrom(builder, result.getValue().getValue());
71        response = builder.build();
72      } else {
73        response = responsePrototype.getDefaultInstanceForType();
74      }
75      if (LOG.isTraceEnabled()) {
76        LOG.trace("Master Result is value=" + response);
77      }
78      return response;
79    }
80  }