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.util.Iterator;
21  import java.util.LinkedList;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.net.Address;
28  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
29  import org.apache.hadoop.hbase.util.Pair;
30  
31  /**
32   * A class to manage a list of servers that failed recently.
33   */
34  @InterfaceAudience.Private
35  public class FailedServers {
36    private final LinkedList<Pair<Long, Address>> failedServers = new
37        LinkedList<Pair<Long, Address>>();
38    private final int recheckServersTimeout;
39    private static final Log LOG = LogFactory.getLog(FailedServers.class);
40  
41    public FailedServers(Configuration conf) {
42      this.recheckServersTimeout = conf.getInt(
43          RpcClient.FAILED_SERVER_EXPIRY_KEY, RpcClient.FAILED_SERVER_EXPIRY_DEFAULT);
44    }
45  
46    /**
47     * Add an address to the list of the failed servers list.
48     */
49    public synchronized void addToFailedServers(Address address, Throwable throwable) {
50      final long expiry = EnvironmentEdgeManager.currentTime() + recheckServersTimeout;
51      failedServers.addFirst(new Pair<Long, Address>(expiry, address));
52      if (LOG.isDebugEnabled()) {
53        LOG.debug(
54          "Added failed server with address " + address + " to list caused by "
55              + throwable.toString());
56      }
57    }
58  
59    /**
60     * Check if the server should be considered as bad. Clean the old entries of the list.
61     *
62     * @return true if the server is in the failed servers list
63     */
64    public synchronized boolean isFailedServer(final Address address) {
65      if (failedServers.isEmpty()) {
66        return false;
67      }
68  
69      final long now = EnvironmentEdgeManager.currentTime();
70  
71      // iterate, looking for the search entry and cleaning expired entries
72      Iterator<Pair<Long, Address>> it = failedServers.iterator();
73      while (it.hasNext()) {
74        Pair<Long, Address> cur = it.next();
75        if (cur.getFirst() < now) {
76          it.remove();
77        } else {
78          if (address.equals(cur.getSecond())) {
79            return true;
80          }
81        }
82      }
83  
84      return false;
85    }
86  }