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