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 java.util.List;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.CellScannable;
26  import org.apache.hadoop.hbase.CellScanner;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.util.ReflectionUtils;
29  
30  /**
31   * Factory to create a {@link PayloadCarryingRpcController}
32   */
33  @InterfaceAudience.Private
34  public class RpcControllerFactory {
35    private static final Log LOG = LogFactory.getLog(RpcControllerFactory.class);
36  
37    /**
38     * Custom RPC Controller factory allows frameworks to change the RPC controller. If the configured
39     * controller cannot be found in the classpath or loaded, we fall back to the default RPC
40     * controller factory.
41     */
42    public static final String CUSTOM_CONTROLLER_CONF_KEY = "hbase.rpc.controllerfactory.class";
43    protected final Configuration conf;
44  
45    public RpcControllerFactory(Configuration conf) {
46      this.conf = conf;
47    }
48  
49    public PayloadCarryingRpcController newController() {
50      // TODO: Set HConstants default rpc timeout here rather than nothing?
51      return new PayloadCarryingRpcController();
52    }
53
54    public PayloadCarryingRpcController newController(final CellScanner cellScanner) {
55      return new PayloadCarryingRpcController(cellScanner);
56    }
57
58    public PayloadCarryingRpcController newController(final List<CellScannable> cellIterables) {
59      return new PayloadCarryingRpcController(cellIterables);
60    }
61  
62
63    public static RpcControllerFactory instantiate(Configuration configuration) {
64      String rpcControllerFactoryClazz =
65          configuration.get(CUSTOM_CONTROLLER_CONF_KEY,
66            RpcControllerFactory.class.getName());
67      try {
68        return ReflectionUtils.instantiateWithCustomCtor(rpcControllerFactoryClazz,
69          new Class[] { Configuration.class }, new Object[] { configuration });
70      } catch (UnsupportedOperationException | NoClassDefFoundError ex) {
71        // HBASE-14960: In case the RPCController is in a non-HBase jar (Phoenix), but the application
72        // is a pure HBase application, we want to fallback to the default one.
73        String msg = "Cannot load configured \"" + CUSTOM_CONTROLLER_CONF_KEY + "\" ("
74            + rpcControllerFactoryClazz + ") from hbase-site.xml, falling back to use "
75            + "default RpcControllerFactory";
76        if (LOG.isDebugEnabled()) {
77          LOG.warn(msg, ex); // if DEBUG enabled, we want the exception, but still log in WARN level
78        } else {
79          LOG.warn(msg);
80        }
81        return new RpcControllerFactory(configuration);
82      }
83    }
84  }