View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master;
20  
21  import org.apache.hadoop.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.ServerName;
23  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
24  import org.apache.hadoop.hbase.util.Pair;
25  
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.Comparator;
29  import java.util.HashMap;
30  import java.util.HashSet;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Set;
35  
36  /**
37   * Class to hold dead servers list and utility querying dead server list.
38   */
39  @InterfaceAudience.Private
40  public class DeadServer {
41    /**
42     * Set of known dead servers.  On znode expiration, servers are added here.
43     * This is needed in case of a network partitioning where the server's lease
44     * expires, but the server is still running. After the network is healed,
45     * and it's server logs are recovered, it will be told to call server startup
46     * because by then, its regions have probably been reassigned.
47     */
48    private final Map<ServerName, Long> deadServers = new HashMap<ServerName, Long>();
49  
50    /**
51     * Number of dead servers currently being processed
52     */
53    private int numProcessing = 0;
54  
55    /**
56     * A dead server that comes back alive has a different start code. The new start code should be
57     *  greater than the old one, but we don't take this into account in this method.
58     *
59     * @param newServerName Servername as either <code>host:port</code> or
60     *                      <code>host,port,startcode</code>.
61     * @return true if this server was dead before and coming back alive again
62     */
63    public synchronized boolean cleanPreviousInstance(final ServerName newServerName) {
64      Iterator<ServerName> it = deadServers.keySet().iterator();
65      while (it.hasNext()) {
66        ServerName sn = it.next();
67        if (ServerName.isSameHostnameAndPort(sn, newServerName)) {
68          it.remove();
69          return true;
70        }
71      }
72  
73      return false;
74    }
75  
76    /**
77     * @param serverName server name.
78     * @return true if this server is on the dead servers list false otherwise
79     */
80    public synchronized boolean isDeadServer(final ServerName serverName) {
81      return deadServers.containsKey(serverName);
82    }
83  
84    /**
85     * Checks if there are currently any dead servers being processed by the
86     * master.  Returns true if at least one region server is currently being
87     * processed as dead.
88     *
89     * @return true if any RS are being processed as dead
90     */
91    public synchronized boolean areDeadServersInProgress() {
92      return numProcessing != 0;
93    }
94  
95    public synchronized Set<ServerName> copyServerNames() {
96      Set<ServerName> clone = new HashSet<ServerName>(deadServers.size());
97      clone.addAll(deadServers.keySet());
98      return clone;
99    }
100 
101   /**
102    * Adds the server to the dead server list if it's not there already.
103    * @param sn the server name
104    */
105   public synchronized void add(ServerName sn) {
106     this.numProcessing++;
107     if (!deadServers.containsKey(sn)){
108       deadServers.put(sn, EnvironmentEdgeManager.currentTimeMillis());
109     }
110   }
111 
112   @SuppressWarnings("UnusedParameters")
113   public synchronized void finish(ServerName ignored) {
114     this.numProcessing--;
115   }
116 
117   public synchronized int size() {
118     return deadServers.size();
119   }
120 
121   public synchronized boolean isEmpty() {
122     return deadServers.isEmpty();
123   }
124 
125   public synchronized void cleanAllPreviousInstances(final ServerName newServerName) {
126     Iterator<ServerName> it = deadServers.keySet().iterator();
127     while (it.hasNext()) {
128       ServerName sn = it.next();
129       if (ServerName.isSameHostnameAndPort(sn, newServerName)) {
130         it.remove();
131       }
132     }
133   }
134 
135   public synchronized String toString() {
136     StringBuilder sb = new StringBuilder();
137     for (ServerName sn : deadServers.keySet()) {
138       if (sb.length() > 0) {
139         sb.append(", ");
140       }
141       sb.append(sn.toString());
142     }
143     return sb.toString();
144   }
145 
146   /**
147    * Extract all the servers dead since a given time, and sort them.
148    * @param ts the time, 0 for all
149    * @return a sorted array list, by death time, lowest values first.
150    */
151   public synchronized List<Pair<ServerName, Long>> copyDeadServersSince(long ts){
152     List<Pair<ServerName, Long>> res =  new ArrayList<Pair<ServerName, Long>>(size());
153 
154     for (Map.Entry<ServerName, Long> entry:deadServers.entrySet()){
155       if (entry.getValue() >= ts){
156         res.add(new Pair<ServerName, Long>(entry.getKey(), entry.getValue()));
157       }
158     }
159 
160     Collections.sort(res, ServerNameDeathDateComparator);
161     return res;
162   }
163 
164   private static Comparator<Pair<ServerName, Long>> ServerNameDeathDateComparator =
165       new Comparator<Pair<ServerName, Long>>(){
166 
167     @Override
168     public int compare(Pair<ServerName, Long> o1, Pair<ServerName, Long> o2) {
169       return o1.getSecond().compareTo(o2.getSecond());
170     }
171   };
172 }