View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.master;
21  
22  import java.util.Collection;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import org.apache.commons.lang.NotImplementedException;
28  import org.apache.hadoop.hbase.ServerName;
29  
30  /**
31   * Class to hold dead servers list and utility querying dead server list.
32   */
33  public class DeadServer implements Set<ServerName> {
34    /**
35     * Set of known dead servers.  On znode expiration, servers are added here.
36     * This is needed in case of a network partitioning where the server's lease
37     * expires, but the server is still running. After the network is healed,
38     * and it's server logs are recovered, it will be told to call server startup
39     * because by then, its regions have probably been reassigned.
40     */
41    private final Set<ServerName> deadServers = new HashSet<ServerName>();
42  
43    /** Number of dead servers currently being processed */
44    private int numProcessing;
45  
46    public DeadServer() {
47      super();
48      this.numProcessing = 0;
49    }
50  
51    /**
52     * @param serverName Server name
53     * @return true if server is dead
54     */
55    public boolean isDeadServer(final String serverName) {
56      return isDeadServer(new ServerName(serverName));
57    }
58  
59    /**
60     * A dead server that comes back alive has a different start code.
61     * @param newServerName Servername as either <code>host:port</code> or
62     * <code>host,port,startcode</code>.
63     * @return true if this server was dead before and coming back alive again
64     */
65    public boolean cleanPreviousInstance(final ServerName newServerName) {
66      ServerName sn =
67        ServerName.findServerWithSameHostnamePort(this.deadServers, newServerName);
68      if (sn == null) return false;
69      return this.deadServers.remove(sn);
70    }
71  
72    /**
73     * @param serverName
74     * @return true if this server is on the dead servers list.
75     */
76    boolean isDeadServer(final ServerName serverName) {
77      return this.deadServers.contains(serverName);
78    }
79  
80    /**
81     * @return True if we have a server with matching hostname and port.
82     */
83    boolean isDeadServerWithSameHostnamePort(final ServerName serverName) {
84      return ServerName.findServerWithSameHostnamePort(this.deadServers,
85        serverName) != null;
86    }
87  
88    /**
89     * Checks if there are currently any dead servers being processed by the
90     * master.  Returns true if at least one region server is currently being
91     * processed as dead.
92     * @return true if any RS are being processed as dead
93     */
94    public boolean areDeadServersInProgress() {
95      return numProcessing != 0;
96    }
97  
98    public synchronized Set<ServerName> clone() {
99      Set<ServerName> clone = new HashSet<ServerName>(this.deadServers.size());
100     clone.addAll(this.deadServers);
101     return clone;
102   }
103 
104   public synchronized boolean add(ServerName e) {
105     this.numProcessing++;
106     return deadServers.add(e);
107   }
108 
109   public synchronized void finish(ServerName e) {
110     this.numProcessing--;
111   }
112 
113   public synchronized int size() {
114     return deadServers.size();
115   }
116 
117   public synchronized boolean isEmpty() {
118     return deadServers.isEmpty();
119   }
120 
121   public synchronized boolean contains(Object o) {
122     return deadServers.contains(o);
123   }
124 
125   public Iterator<ServerName> iterator() {
126     return this.deadServers.iterator();
127   }
128 
129   public synchronized Object[] toArray() {
130     return deadServers.toArray();
131   }
132 
133   public synchronized <T> T[] toArray(T[] a) {
134     return deadServers.toArray(a);
135   }
136 
137   public synchronized boolean remove(Object o) {
138     return this.deadServers.remove(o);
139   }
140 
141   public synchronized boolean containsAll(Collection<?> c) {
142     return deadServers.containsAll(c);
143   }
144 
145   public synchronized boolean addAll(Collection<? extends ServerName> c) {
146     return deadServers.addAll(c);
147   }
148 
149   public synchronized boolean retainAll(Collection<?> c) {
150     return deadServers.retainAll(c);
151   }
152 
153   public synchronized boolean removeAll(Collection<?> c) {
154     return deadServers.removeAll(c);
155   }
156 
157   public synchronized void clear() {
158     throw new NotImplementedException();
159   }
160 
161   public synchronized boolean equals(Object o) {
162     return deadServers.equals(o);
163   }
164 
165   public synchronized int hashCode() {
166     return deadServers.hashCode();
167   }
168 
169   public synchronized String toString() {
170     return this.deadServers.toString();
171   }
172 }