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