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.ipc;
019
020import java.net.InetSocketAddress;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
029
030/**
031 * A class to manage a list of servers that failed recently.
032 */
033@InterfaceAudience.Private
034public class FailedServers {
035  private final Map<String, Long> failedServers = new HashMap<String, Long>();
036  private long latestExpiry = 0;
037  private final int recheckServersTimeout;
038  private static final Logger LOG = LoggerFactory.getLogger(FailedServers.class);
039
040  public FailedServers(Configuration conf) {
041    this.recheckServersTimeout = conf.getInt(
042        RpcClient.FAILED_SERVER_EXPIRY_KEY, RpcClient.FAILED_SERVER_EXPIRY_DEFAULT);
043  }
044
045  /**
046   * Add an address to the list of the failed servers list.
047   */
048  public synchronized void addToFailedServers(InetSocketAddress address, Throwable throwable) {
049    final long expiry = EnvironmentEdgeManager.currentTime() + recheckServersTimeout;
050    this.failedServers.put(address.toString(), expiry);
051    this.latestExpiry = expiry;
052    if (LOG.isDebugEnabled()) {
053      LOG.debug(
054        "Added failed server with address " + address.toString() + " to list caused by "
055            + throwable.toString());
056    }
057  }
058
059  /**
060   * Check if the server should be considered as bad. Clean the old entries of the list.
061   *
062   * @return true if the server is in the failed servers list
063   */
064  public synchronized boolean isFailedServer(final InetSocketAddress address) {
065    if (failedServers.isEmpty()) {
066      return false;
067    }
068    final long now = EnvironmentEdgeManager.currentTime();
069    if (now > this.latestExpiry) {
070      failedServers.clear();
071      return false;
072    }
073    String key = address.toString();
074    Long expiry = this.failedServers.get(key);
075    if (expiry == null) {
076      return false;
077    }
078    if (expiry >= now) {
079      return true;
080    } else {
081      this.failedServers.remove(key);
082    }
083    return false;
084  }
085}