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.client;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.hbase.HConstants;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.util.ReflectionUtils;
26  
27  /**
28   * Factory to create an {@link RpcRetryingCaller}
29   */
30  @InterfaceAudience.Private
31  public class RpcRetryingCallerFactory {
32  
33    /** Configuration key for a custom {@link RpcRetryingCaller} */
34    public static final String CUSTOM_CALLER_CONF_KEY = "hbase.rpc.callerfactory.class";
35    private static final Log LOG = LogFactory.getLog(RpcRetryingCallerFactory.class);
36    protected final Configuration conf;
37    private final long pause;
38    private final long pauseForCQTBE;// pause for CallQueueTooBigException, if specified
39    private final int retries;
40    private final int rpcTimeout;
41    private final RetryingCallerInterceptor interceptor;
42    private final int startLogErrorsCnt;
43    private final boolean enableBackPressure;
44    private ServerStatisticTracker stats;
45  
46    public RpcRetryingCallerFactory(Configuration conf) {
47      this(conf, RetryingCallerInterceptorFactory.NO_OP_INTERCEPTOR);
48    }
49    
50    public RpcRetryingCallerFactory(Configuration conf, RetryingCallerInterceptor interceptor) {
51      this.conf = conf;
52      pause = conf.getLong(HConstants.HBASE_CLIENT_PAUSE,
53          HConstants.DEFAULT_HBASE_CLIENT_PAUSE);
54      long configuredPauseForCQTBE = conf.getLong(HConstants.HBASE_CLIENT_PAUSE_FOR_CQTBE, pause);
55      if (configuredPauseForCQTBE < pause) {
56        LOG.warn("The " + HConstants.HBASE_CLIENT_PAUSE_FOR_CQTBE + " setting: "
57            + configuredPauseForCQTBE + " is smaller than " + HConstants.HBASE_CLIENT_PAUSE
58            + ", will use " + pause + " instead.");
59        this.pauseForCQTBE = pause;
60      } else {
61        this.pauseForCQTBE = configuredPauseForCQTBE;
62      }
63      retries = conf.getInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER,
64          HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER);
65      startLogErrorsCnt = conf.getInt(AsyncProcess.START_LOG_ERRORS_AFTER_COUNT_KEY,
66          AsyncProcess.DEFAULT_START_LOG_ERRORS_AFTER_COUNT);
67      this.interceptor = interceptor;
68      enableBackPressure = conf.getBoolean(HConstants.ENABLE_CLIENT_BACKPRESSURE,
69          HConstants.DEFAULT_ENABLE_CLIENT_BACKPRESSURE);
70      rpcTimeout = conf.getInt(HConstants.HBASE_RPC_TIMEOUT_KEY,HConstants.DEFAULT_HBASE_RPC_TIMEOUT);
71    }
72  
73    /**
74     * Set the tracker that should be used for tracking statistics about the server
75     */
76    public void setStatisticTracker(ServerStatisticTracker statisticTracker) {
77      this.stats = statisticTracker;
78    }
79  
80    /**
81     * Create a new RetryingCaller with specific rpc timeout.
82     */
83    public <T> RpcRetryingCaller<T> newCaller(int rpcTimeout) {
84      // We store the values in the factory instance. This way, constructing new objects
85      //  is cheap as it does not require parsing a complex structure.
86      RpcRetryingCaller<T> caller = new RpcRetryingCaller<T>(pause, pauseForCQTBE, retries,
87          interceptor, startLogErrorsCnt, rpcTimeout);
88      return caller;
89    }
90  
91    /**
92     * Create a new RetryingCaller with configured rpc timeout.
93     */
94    public <T> RpcRetryingCaller<T> newCaller() {
95      // We store the values in the factory instance. This way, constructing new objects
96      //  is cheap as it does not require parsing a complex structure.
97      RpcRetryingCaller<T> caller = new RpcRetryingCaller<T>(pause, pauseForCQTBE, retries,
98          interceptor, startLogErrorsCnt, rpcTimeout);
99      return caller;
100   }
101 
102   public static RpcRetryingCallerFactory instantiate(Configuration configuration) {
103     return instantiate(configuration, RetryingCallerInterceptorFactory.NO_OP_INTERCEPTOR, null);
104   }
105 
106   public static RpcRetryingCallerFactory instantiate(Configuration configuration,
107       ServerStatisticTracker stats) {
108     return instantiate(configuration, RetryingCallerInterceptorFactory.NO_OP_INTERCEPTOR, stats);
109   }
110 
111   public static RpcRetryingCallerFactory instantiate(Configuration configuration,
112       RetryingCallerInterceptor interceptor, ServerStatisticTracker stats) {
113     String clazzName = RpcRetryingCallerFactory.class.getName();
114     String rpcCallerFactoryClazz =
115         configuration.get(RpcRetryingCallerFactory.CUSTOM_CALLER_CONF_KEY, clazzName);
116     RpcRetryingCallerFactory factory;
117     if (rpcCallerFactoryClazz.equals(clazzName)) {
118       factory = new RpcRetryingCallerFactory(configuration, interceptor);
119     } else {
120       factory = ReflectionUtils.instantiateWithCustomCtor(
121           rpcCallerFactoryClazz, new Class[] { Configuration.class },
122           new Object[] { configuration });
123     }
124 
125     // setting for backwards compat with existing caller factories, rather than in the ctor
126     factory.setStatisticTracker(stats);
127     return factory;
128   }
129 }