1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40 @InterfaceAudience.Private
41 public class DeadServer {
42
43
44
45
46
47
48
49 private final Map<ServerName, Long> deadServers = new HashMap<ServerName, Long>();
50
51
52
53
54 private int numProcessing = 0;
55
56
57
58
59
60
61
62
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
79
80
81 public synchronized boolean isDeadServer(final ServerName serverName) {
82 return deadServers.containsKey(serverName);
83 }
84
85
86
87
88
89
90
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
104
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
149
150
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
167
168
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 }