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