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      return new PayloadCarryingRpcController();
51    }
52  
53    public PayloadCarryingRpcController newController(final CellScanner cellScanner) {
54      return new PayloadCarryingRpcController(cellScanner);
55    }
56  
57    public PayloadCarryingRpcController newController(final List<CellScannable> cellIterables) {
58      return new PayloadCarryingRpcController(cellIterables);
59    }
60  
61  
62    public static RpcControllerFactory instantiate(Configuration configuration) {
63      String rpcControllerFactoryClazz =
64          configuration.get(CUSTOM_CONTROLLER_CONF_KEY,
65            RpcControllerFactory.class.getName());
66      try {
67        return ReflectionUtils.instantiateWithCustomCtor(rpcControllerFactoryClazz,
68          new Class[] { Configuration.class }, new Object[] { configuration });
69      } catch (UnsupportedOperationException | NoClassDefFoundError ex) {
70        // HBASE-14960: In case the RPCController is in a non-HBase jar (Phoenix), but the application
71        // is a pure HBase application, we want to fallback to the default one.
72        String msg = "Cannot load configured \"" + CUSTOM_CONTROLLER_CONF_KEY + "\" ("
73            + rpcControllerFactoryClazz + ") from hbase-site.xml, falling back to use "
74            + "default RpcControllerFactory";
75        if (LOG.isDebugEnabled()) {
76          LOG.warn(msg, ex); // if DEBUG enabled, we want the exception, but still log in WARN level
77        } else {
78          LOG.warn(msg);
79        }
80        return new RpcControllerFactory(configuration);
81      }
82    }
83  }