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.Date;
30  import java.util.HashMap;
31  import java.util.HashSet;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Set;
36  
37  /**
38   * Class to hold dead servers list and utility querying dead server list.
39   */
40  @InterfaceAudience.Private
41  public class DeadServer {
42    /**
43     * Set of known dead servers.  On znode expiration, servers are added here.
44     * This is needed in case of a network partitioning where the server's lease
45     * expires, but the server is still running. After the network is healed,
46     * and it's server logs are recovered, it will be told to call server startup
47     * because by then, its regions have probably been reassigned.
48     */
49    private final Map<ServerName, Long> deadServers = new HashMap<ServerName, Long>();
50  
51    /**
52     * Number of dead servers currently being processed
53     */
54    private int numProcessing = 0;
55  
56    /**
57     * A dead server that comes back alive has a different start code. The new start code should be
58     *  greater than the old one, but we don't take this into account in this method.
59     *
60     * @param newServerName Servername as either <code>host:port</code> or
61     *                      <code>host,port,startcode</code>.
62     * @return true if this server was dead before and coming back alive again
63     */
64    public synchronized boolean cleanPreviousInstance(final ServerName newServerName) {
65      Iterator<ServerName> it = deadServers.keySet().iterator();
66      while (it.hasNext()) {
67        ServerName sn = it.next();
68        if (ServerName.isSameHostnameAndPort(sn, newServerName)) {
69          it.remove();
70          return true;
71        }
72      }
73  
74      return false;
75    }
76  
77    /**
78     * @param serverName server name.
79     * @return true if this server is on the dead servers list false otherwise
80     */
81    public synchronized boolean isDeadServer(final ServerName serverName) {
82      return deadServers.containsKey(serverName);
83    }
84  
85    /**
86     * Checks if there are currently any dead servers being processed by the
87     * master.  Returns true if at least one region server is currently being
88     * processed as dead.
89     *
90     * @return true if any RS are being processed as dead
91     */
92    public synchronized boolean areDeadServersInProgress() {
93      return numProcessing != 0;
94    }
95  
96    public synchronized Set<ServerName> copyServerNames() {
97      Set<ServerName> clone = new HashSet<ServerName>(deadServers.size());
98      clone.addAll(deadServers.keySet());
99      return clone;
100   }
101 
102   /**
103    * Adds the server to the dead server list if it's not there already.
104    * @param sn the server name
105    */
106   public synchronized void add(ServerName sn) {
107     this.numProcessing++;
108     if (!deadServers.containsKey(sn)){
109       deadServers.put(sn, EnvironmentEdgeManager.currentTimeMillis());
110     }
111   }
112 
113   @SuppressWarnings("UnusedParameters")
114   public synchronized void finish(ServerName ignored) {
115     this.numProcessing--;
116   }
117 
118   public synchronized int size() {
119     return deadServers.size();
120   }
121 
122   public synchronized boolean isEmpty() {
123     return deadServers.isEmpty();
124   }
125 
126   public synchronized void cleanAllPreviousInstances(final ServerName newServerName) {
127     Iterator<ServerName> it = deadServers.keySet().iterator();
128     while (it.hasNext()) {
129       ServerName sn = it.next();
130       if (ServerName.isSameHostnameAndPort(sn, newServerName)) {
131         it.remove();
132       }
133     }
134   }
135 
136   public synchronized String toString() {
137     StringBuilder sb = new StringBuilder();
138     for (ServerName sn : deadServers.keySet()) {
139       if (sb.length() > 0) {
140         sb.append(", ");
141       }
142       sb.append(sn.toString());
143     }
144     return sb.toString();
145   }
146 
147   /**
148    * Extract all the servers dead since a given time, and sort them.
149    * @param ts the time, 0 for all
150    * @return a sorted array list, by death time, lowest values first.
151    */
152   public synchronized List<Pair<ServerName, Long>> copyDeadServersSince(long ts){
153     List<Pair<ServerName, Long>> res =  new ArrayList<Pair<ServerName, Long>>(size());
154 
155     for (Map.Entry<ServerName, Long> entry:deadServers.entrySet()){
156       if (entry.getValue() >= ts){
157         res.add(new Pair<ServerName, Long>(entry.getKey(), entry.getValue()));
158       }
159     }
160 
161     Collections.sort(res, ServerNameDeathDateComparator);
162     return res;
163   }
164   
165   /**
166    * Get the time when a server died
167    * @param deadServerName the dead server name
168    * @return the date when the server died 
169    */
170   public synchronized Date getTimeOfDeath(final ServerName deadServerName){
171     Long time = deadServers.get(deadServerName);
172     return time == null ? null : new Date(time);
173   }
174 
175   private static Comparator<Pair<ServerName, Long>> ServerNameDeathDateComparator =
176       new Comparator<Pair<ServerName, Long>>(){
177 
178     @Override
179     public int compare(Pair<ServerName, Long> o1, Pair<ServerName, Long> o2) {
180       return o1.getSecond().compareTo(o2.getSecond());
181     }
182   };
183 }