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