View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.ipc;
19  
20  import java.net.InetSocketAddress;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.LinkedList;
24  import java.util.Map;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
31  import org.apache.hadoop.hbase.util.Pair;
32  
33  /**
34   * A class to manage a list of servers that failed recently.
35   */
36  @InterfaceAudience.Private
37  public class FailedServers {
38    private final LinkedList<Pair<Long, String>> failedServers = new
39        LinkedList<Pair<Long, String>>();
40    private final int recheckServersTimeout;
41    private static final Log LOG = LogFactory.getLog(FailedServers.class);
42  
43    public FailedServers(Configuration conf) {
44      this.recheckServersTimeout = conf.getInt(
45          RpcClient.FAILED_SERVER_EXPIRY_KEY, RpcClient.FAILED_SERVER_EXPIRY_DEFAULT);
46    }
47  
48    /**
49     * Add an address to the list of the failed servers list.
50     */
51    public synchronized void addToFailedServers(InetSocketAddress address, Throwable throwable) {
52      final long expiry = EnvironmentEdgeManager.currentTime() + recheckServersTimeout;
53      failedServers.addFirst(new Pair<Long, String>(expiry, address.toString()));
54      if (LOG.isDebugEnabled()) {
55        LOG.debug(
56          "Added failed server with address " + address.toString() + " to list caused by "
57              + throwable.toString());
58      }
59    }
60  
61    /**
62     * Check if the server should be considered as bad. Clean the old entries of the list.
63     *
64     * @return true if the server is in the failed servers list
65     */
66    public synchronized boolean isFailedServer(final InetSocketAddress address) {
67      if (failedServers.isEmpty()) {
68        return false;
69      }
70  
71      final String lookup = address.toString();
72      final long now = EnvironmentEdgeManager.currentTime();
73  
74      // iterate, looking for the search entry and cleaning expired entries
75      Iterator<Pair<Long, String>> it = failedServers.iterator();
76      while (it.hasNext()) {
77        Pair<Long, String> cur = it.next();
78        if (cur.getFirst() < now) {
79          it.remove();
80        } else {
81          if (lookup.equals(cur.getSecond())) {
82            return true;
83          }
84        }
85      }
86  
87      return false;
88    }
89  }