001/**
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *     http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing, software
014  * distributed under the License is distributed on an "AS IS" BASIS,
015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016  * See the License for the specific language governing permissions and
017  * limitations under the License.
018*/
019
020package org.apache.hadoop.hbase.exceptions;
021
022import java.net.ConnectException;
023
024import org.apache.hadoop.hbase.ServerName;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * Thrown when the client believes that we are trying to communicate to has
029 * been repeatedly unresponsive for a while.
030 *
031 * On receiving such an exception. The ConnectionManager will skip all
032 * retries and fast fail the operation.
033 */
034@InterfaceAudience.Public
035public class PreemptiveFastFailException extends ConnectException {
036  private static final long serialVersionUID = 7129103682617007177L;
037  private long failureCount, timeOfFirstFailureMilliSec, timeOfLatestAttemptMilliSec;
038
039  // If set, we guarantee that no modifications went to server
040  private boolean guaranteedClientSideOnly;
041
042  /**
043   * @param count num of consecutive failures
044   * @param timeOfFirstFailureMilliSec when first failure happened
045   * @param timeOfLatestAttemptMilliSec when last attempt happened
046   * @param serverName server we failed to connect to
047   */
048  public PreemptiveFastFailException(long count, long timeOfFirstFailureMilliSec,
049      long timeOfLatestAttemptMilliSec, ServerName serverName) {
050    super("Exception happened " + count + " times. to" + serverName);
051    this.failureCount = count;
052    this.timeOfFirstFailureMilliSec = timeOfFirstFailureMilliSec;
053    this.timeOfLatestAttemptMilliSec = timeOfLatestAttemptMilliSec;
054  }
055
056  /**
057   * @param count num of consecutive failures
058   * @param timeOfFirstFailureMilliSec when first failure happened
059   * @param timeOfLatestAttemptMilliSec when last attempt happened
060   * @param serverName server we failed to connect to
061   * @param guaranteedClientSideOnly if true, guarantees that no mutations
062   *   have been applied on the server
063   */
064  public PreemptiveFastFailException(long count, long timeOfFirstFailureMilliSec,
065                                     long timeOfLatestAttemptMilliSec, ServerName serverName,
066                                     boolean guaranteedClientSideOnly) {
067    super("Exception happened " + count + " times. to" + serverName);
068    this.failureCount = count;
069    this.timeOfFirstFailureMilliSec = timeOfFirstFailureMilliSec;
070    this.timeOfLatestAttemptMilliSec = timeOfLatestAttemptMilliSec;
071    this.guaranteedClientSideOnly = guaranteedClientSideOnly;
072  }
073
074  /**
075   * @return time of the fist failure
076   */
077  public long getFirstFailureAt() {
078    return timeOfFirstFailureMilliSec;
079  }
080
081  /**
082   * @return time of the latest attempt
083   */
084  public long getLastAttemptAt() {
085    return timeOfLatestAttemptMilliSec;
086  }
087
088  /**
089   * @return failure count
090   */
091  public long getFailureCount() {
092    return failureCount;
093  }
094
095  /**
096   * @return true if operation was attempted by server, false otherwise.
097   */
098  public boolean wasOperationAttemptedByServer() {
099    return false;
100  }
101
102  /**
103   * @return true if we know no mutation made it to the server, false otherwise.
104   */
105  public boolean isGuaranteedClientSideOnly() {
106    return guaranteedClientSideOnly;
107  }
108}