001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import java.lang.reflect.Constructor;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HConstants;
023import org.apache.yetus.audience.InterfaceAudience;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * Factory implementation to provide the {@link ConnectionImplementation} with the implementation of
029 * the {@link RetryingCallerInterceptor} that we would use to intercept the
030 * {@link RpcRetryingCaller} during the course of their calls.
031 */
032
033@InterfaceAudience.Private
034class RetryingCallerInterceptorFactory {
035  private static final Logger LOG = LoggerFactory.getLogger(RetryingCallerInterceptorFactory.class);
036  private Configuration conf;
037  private final boolean failFast;
038  public static final RetryingCallerInterceptor NO_OP_INTERCEPTOR =
039    new NoOpRetryableCallerInterceptor(null);
040
041  public RetryingCallerInterceptorFactory(Configuration conf) {
042    this.conf = conf;
043    failFast = conf.getBoolean(HConstants.HBASE_CLIENT_FAST_FAIL_MODE_ENABLED,
044      HConstants.HBASE_CLIENT_ENABLE_FAST_FAIL_MODE_DEFAULT);
045  }
046
047  /**
048   * This builds the implementation of {@link RetryingCallerInterceptor} that we specify in the conf
049   * and returns the same. To use {@link PreemptiveFastFailInterceptor}, set
050   * HBASE_CLIENT_ENABLE_FAST_FAIL_MODE to true. HBASE_CLIENT_FAST_FAIL_INTERCEPTOR_IMPL is
051   * defaulted to {@link PreemptiveFastFailInterceptor}
052   * @return The factory build method which creates the {@link RetryingCallerInterceptor} object
053   *         according to the configuration.
054   */
055  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "REC_CATCH_EXCEPTION",
056      justification = "Convert thrown exception to unchecked")
057  public RetryingCallerInterceptor build() {
058    RetryingCallerInterceptor ret = NO_OP_INTERCEPTOR;
059    if (failFast) {
060      try {
061        Class<?> c = conf.getClass(HConstants.HBASE_CLIENT_FAST_FAIL_INTERCEPTOR_IMPL,
062          PreemptiveFastFailInterceptor.class);
063        Constructor<?> constructor = c.getDeclaredConstructor(Configuration.class);
064        constructor.setAccessible(true);
065        ret = (RetryingCallerInterceptor) constructor.newInstance(conf);
066      } catch (Exception e) {
067        ret = new PreemptiveFastFailInterceptor(conf);
068      }
069    }
070    LOG.trace("Using " + ret.toString() + " for intercepting the RpcRetryingCaller");
071    return ret;
072  }
073}