1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 package org.apache.hadoop.hbase.client;
20
21 import java.lang.reflect.Constructor;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HConstants;
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28
29 /**
30 * Factory implementation to provide the {@link HConnectionImplementation} with
31 * the implementation of the {@link RetryingCallerInterceptor} that we would use
32 * to intercept the {@link RpcRetryingCaller} during the course of their calls.
33 *
34 */
35
36 @InterfaceAudience.Private
37 class RetryingCallerInterceptorFactory {
38 private static final Log LOG = LogFactory
39 .getLog(RetryingCallerInterceptorFactory.class);
40 private Configuration conf;
41 private final boolean failFast;
42 public static final RetryingCallerInterceptor NO_OP_INTERCEPTOR =
43 new NoOpRetryableCallerInterceptor(null);
44
45 public RetryingCallerInterceptorFactory(Configuration conf) {
46 this.conf = conf;
47 failFast = conf.getBoolean(HConstants.HBASE_CLIENT_FAST_FAIL_MODE_ENABLED,
48 HConstants.HBASE_CLIENT_ENABLE_FAST_FAIL_MODE_DEFAULT);
49 }
50
51 /**
52 * This builds the implementation of {@link RetryingCallerInterceptor} that we
53 * specify in the conf and returns the same.
54 *
55 * To use {@link PreemptiveFastFailInterceptor}, set HBASE_CLIENT_ENABLE_FAST_FAIL_MODE to true.
56 * HBASE_CLIENT_FAST_FAIL_INTERCEPTOR_IMPL is defaulted to {@link PreemptiveFastFailInterceptor}
57 *
58 * @return The factory build method which creates the
59 * {@link RetryingCallerInterceptor} object according to the
60 * configuration.
61 */
62 public RetryingCallerInterceptor build() {
63 RetryingCallerInterceptor ret = NO_OP_INTERCEPTOR;
64 if (failFast) {
65 try {
66 Class<?> c = conf.getClass(
67 HConstants.HBASE_CLIENT_FAST_FAIL_INTERCEPTOR_IMPL,
68 PreemptiveFastFailInterceptor.class);
69 Constructor<?> constructor = c
70 .getDeclaredConstructor(Configuration.class);
71 constructor.setAccessible(true);
72 ret = (RetryingCallerInterceptor) constructor.newInstance(conf);
73 } catch (Exception e) {
74 ret = new PreemptiveFastFailInterceptor(conf);
75 }
76 }
77 LOG.trace("Using " + ret.toString() + " for intercepting the RpcRetryingCaller");
78 return ret;
79 }
80 }