1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.examples; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.HashMap; |
24 | |
|
25 | |
import org.apache.giraph.examples.utils.BrachaTouegDeadlockVertexValue; |
26 | |
import org.apache.giraph.examples.utils.BrachaTouegDeadlockMessage; |
27 | |
import org.apache.giraph.conf.LongConfOption; |
28 | |
import org.apache.giraph.edge.Edge; |
29 | |
import org.apache.giraph.graph.BasicComputation; |
30 | |
import org.apache.giraph.graph.Vertex; |
31 | |
import org.apache.hadoop.io.LongWritable; |
32 | |
import org.apache.log4j.Logger; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
@Algorithm( |
46 | |
name = "Bracha Toueg deadlock detection" |
47 | |
) |
48 | 0 | public class BrachaTouegDeadlockComputation |
49 | |
extends BasicComputation<LongWritable, BrachaTouegDeadlockVertexValue, |
50 | |
LongWritable, BrachaTouegDeadlockMessage> { |
51 | |
|
52 | |
|
53 | 0 | public static final LongConfOption BRACHA_TOUEG_DL_INITIATOR_ID = |
54 | |
new LongConfOption("BrachaTouegDeadlockVertex.initiatorId", 1, |
55 | |
"The deadlock detection initiator id"); |
56 | |
|
57 | |
|
58 | 0 | private static final Logger LOG = |
59 | 0 | Logger.getLogger(BrachaTouegDeadlockComputation.class); |
60 | |
|
61 | |
@Override |
62 | |
public void compute( |
63 | |
Vertex<LongWritable, BrachaTouegDeadlockVertexValue, LongWritable> vertex, |
64 | |
Iterable<BrachaTouegDeadlockMessage> messages) |
65 | |
throws IOException { |
66 | |
|
67 | |
BrachaTouegDeadlockVertexValue value; |
68 | 0 | long superstep = getSuperstep(); |
69 | |
|
70 | 0 | if (superstep == 0) { |
71 | |
|
72 | |
|
73 | 0 | initAlgorithm(vertex); |
74 | |
|
75 | |
|
76 | |
|
77 | 0 | } else if (superstep == 1) { |
78 | |
|
79 | 0 | value = vertex.getValue(); |
80 | |
|
81 | 0 | if (LOG.isDebugEnabled()) { |
82 | 0 | LOG.debug("Vertex ID " + vertex.getId() + " status is:"); |
83 | 0 | LOG.debug("\tpending requests? " + value.hasPendingRequests()); |
84 | 0 | LOG.debug("\tis free? " + value.isFree()); |
85 | 0 | LOG.debug("\tis notified? " + value.isNotified()); |
86 | |
} |
87 | |
|
88 | |
|
89 | 0 | for (BrachaTouegDeadlockMessage message : messages) { |
90 | 0 | value.addParent(Long.valueOf(message.getSenderId())); |
91 | 0 | } |
92 | |
|
93 | |
|
94 | 0 | if (LOG.isDebugEnabled()) { |
95 | 0 | logParents(vertex); |
96 | 0 | if (isInitiator(vertex)) { |
97 | 0 | LOG.debug("Vertex ID " + vertex.getId() + " start the algorithm."); |
98 | |
} |
99 | |
} |
100 | |
|
101 | 0 | if (isInitiator(vertex)) { |
102 | |
|
103 | 0 | notifyVertices(vertex); |
104 | |
} else { |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | 0 | vertex.voteToHalt(); |
113 | 0 | return; |
114 | |
} |
115 | |
|
116 | |
|
117 | |
} else { |
118 | |
Long ackSenderId; |
119 | |
|
120 | 0 | value = vertex.getValue(); |
121 | |
|
122 | |
|
123 | |
|
124 | 0 | for (BrachaTouegDeadlockMessage message : messages) { |
125 | 0 | long type = message.getType(); |
126 | |
|
127 | 0 | if (LOG.isDebugEnabled()) { |
128 | 0 | LOG.debug("Vertex ID " + vertex.getId() + " received: " + message); |
129 | |
} |
130 | |
|
131 | 0 | if (type == BrachaTouegDeadlockMessage.NOTIFY) { |
132 | 0 | handleNotifyMessage(vertex, message); |
133 | 0 | } else if (type == BrachaTouegDeadlockMessage.GRANT) { |
134 | 0 | handleGrantMessage(vertex, message); |
135 | 0 | } else if (type == BrachaTouegDeadlockMessage.DONE || |
136 | |
type == BrachaTouegDeadlockMessage.ACK) { |
137 | |
|
138 | |
|
139 | |
|
140 | 0 | value.receivedMessage(message.getSenderId(), message.getType()); |
141 | |
} |
142 | 0 | } |
143 | |
|
144 | 0 | ackSenderId = value.getIdWithInHoldAck(); |
145 | 0 | if (value.isFree() && |
146 | 0 | !value.isWaitingForMessage(BrachaTouegDeadlockMessage.ACK) && |
147 | 0 | !ackSenderId.equals(BrachaTouegDeadlockVertexValue.INVALID_ID)) { |
148 | |
|
149 | 0 | sendAckMessage(ackSenderId, vertex); |
150 | 0 | value.setIdWithInHoldAck(BrachaTouegDeadlockVertexValue.INVALID_ID); |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | 0 | if (value.isNotified() && |
156 | 0 | !value.isWaitingForMessage(BrachaTouegDeadlockMessage.ACK) && |
157 | 0 | !value.isWaitingForMessage(BrachaTouegDeadlockMessage.DONE)) { |
158 | |
|
159 | 0 | Long senderId = value.getIdWithInHoldDone(); |
160 | |
|
161 | 0 | if (LOG.isDebugEnabled()) { |
162 | 0 | LOG.debug("Vertex ID " + vertex.getId() + |
163 | |
" sent the last DONE message."); |
164 | 0 | LOG.debug("Vertex ID " + vertex.getId() + " voted to halt."); |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | 0 | if (!isInitiator(vertex) && |
170 | 0 | !senderId.equals(BrachaTouegDeadlockVertexValue.INVALID_ID)) { |
171 | 0 | sendMessage(vertex.getId().get(), senderId, |
172 | |
BrachaTouegDeadlockMessage.DONE); |
173 | 0 | value.setIdWithInHoldDone(BrachaTouegDeadlockVertexValue.INVALID_ID); |
174 | |
} |
175 | |
|
176 | 0 | vertex.voteToHalt(); |
177 | |
} |
178 | |
} |
179 | 0 | } |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
private boolean isInitiator(Vertex<LongWritable, ?, ?> vertex) { |
188 | 0 | return vertex.getId().get() == BRACHA_TOUEG_DL_INITIATOR_ID.get(getConf()); |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
private void initAlgorithm(Vertex<LongWritable, |
198 | |
BrachaTouegDeadlockVertexValue, LongWritable> vertex) { |
199 | |
|
200 | |
BrachaTouegDeadlockVertexValue value; |
201 | 0 | HashMap<Long, ArrayList<Long>> requests = |
202 | |
new HashMap<Long, ArrayList<Long>>(); |
203 | 0 | long vertexId = vertex.getId().get(); |
204 | |
|
205 | |
|
206 | 0 | for (Edge<LongWritable, LongWritable> edge : vertex.getEdges()) { |
207 | |
ArrayList<Long> targets; |
208 | 0 | Long tag = Long.valueOf(edge.getValue().get()); |
209 | 0 | Long target = Long.valueOf(edge.getTargetVertexId().get()); |
210 | |
|
211 | 0 | if (requests.containsKey(tag)) { |
212 | 0 | targets = requests.get(tag); |
213 | |
} else { |
214 | 0 | targets = new ArrayList<Long>(); |
215 | |
} |
216 | |
|
217 | 0 | targets.add(target); |
218 | 0 | requests.put(tag, targets); |
219 | 0 | } |
220 | |
|
221 | |
|
222 | |
|
223 | 0 | value = new BrachaTouegDeadlockVertexValue(requests); |
224 | 0 | vertex.setValue(value); |
225 | |
|
226 | |
|
227 | 0 | for (Edge<LongWritable, LongWritable> edge : vertex.getEdges()) { |
228 | 0 | sendMessage(vertexId, edge.getTargetVertexId().get(), |
229 | |
BrachaTouegDeadlockMessage.CTRL_IN_EDGE); |
230 | 0 | } |
231 | 0 | } |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
private void sendAckMessage(long receiver, Vertex<LongWritable, |
241 | |
BrachaTouegDeadlockVertexValue, LongWritable> vertex) { |
242 | |
|
243 | 0 | this.sendMessage(Long.valueOf(vertex.getId().get()), |
244 | |
receiver, BrachaTouegDeadlockMessage.ACK); |
245 | |
|
246 | 0 | if (!vertex.getValue().isNotified()) { |
247 | 0 | vertex.voteToHalt(); |
248 | |
} |
249 | 0 | } |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
private void sendMessage(long sender, long receiver, long messageType) { |
259 | |
BrachaTouegDeadlockMessage message; |
260 | |
|
261 | 0 | message = new BrachaTouegDeadlockMessage(sender, messageType); |
262 | 0 | sendMessage(new LongWritable(receiver), message); |
263 | 0 | if (LOG.isDebugEnabled()) { |
264 | 0 | LOG.debug("sent message " + message + " from " + sender + |
265 | |
" to " + receiver); |
266 | |
} |
267 | 0 | } |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
private void logParents(Vertex<LongWritable, |
276 | |
BrachaTouegDeadlockVertexValue, |
277 | |
LongWritable> vertex) { |
278 | 0 | ArrayList<Long> parents = vertex.getValue().getParents(); |
279 | 0 | int sz = parents.size(); |
280 | 0 | StringBuffer buffer = new StringBuffer(); |
281 | |
|
282 | 0 | buffer.append("Vertex " + vertex.getId() + " parents:"); |
283 | 0 | for (int i = 0; i < sz; ++i) { |
284 | 0 | buffer.append(" - " + parents.get(i)); |
285 | |
} |
286 | 0 | LOG.debug(buffer.toString()); |
287 | 0 | } |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
private void notifyVertices( |
300 | |
Vertex<LongWritable, BrachaTouegDeadlockVertexValue, LongWritable> vertex) { |
301 | |
|
302 | 0 | BrachaTouegDeadlockVertexValue value = vertex.getValue(); |
303 | 0 | long vertexId = vertex.getId().get(); |
304 | 0 | boolean hasOutEdges = false; |
305 | |
|
306 | 0 | value.setNotified(); |
307 | |
|
308 | 0 | for (Edge<LongWritable, LongWritable> edge : vertex.getEdges()) { |
309 | 0 | hasOutEdges = true; |
310 | 0 | sendMessage(vertexId, |
311 | 0 | edge.getTargetVertexId().get(), |
312 | |
BrachaTouegDeadlockMessage.NOTIFY); |
313 | |
|
314 | |
|
315 | 0 | value.waitForMessage(Long.valueOf(edge.getTargetVertexId().get()), |
316 | 0 | Long.valueOf(BrachaTouegDeadlockMessage.DONE)); |
317 | 0 | } |
318 | |
|
319 | |
|
320 | |
|
321 | 0 | if (!hasOutEdges && isInitiator(vertex)) { |
322 | 0 | value.setFree(); |
323 | 0 | } else if (!value.hasPendingRequests() && !value.isFree()) { |
324 | 0 | grantVertices(vertex); |
325 | |
} |
326 | 0 | } |
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
private void grantVertices( |
332 | |
Vertex<LongWritable, BrachaTouegDeadlockVertexValue, LongWritable> vertex) { |
333 | |
|
334 | 0 | BrachaTouegDeadlockVertexValue value = vertex.getValue(); |
335 | 0 | ArrayList<Long> parents = value.getParents(); |
336 | 0 | long vertexId = vertex.getId().get(); |
337 | |
|
338 | 0 | value.setFree(); |
339 | |
|
340 | |
|
341 | 0 | for (Long parent : parents) { |
342 | 0 | sendMessage(vertexId, parent, |
343 | |
BrachaTouegDeadlockMessage.GRANT); |
344 | |
|
345 | |
|
346 | 0 | value.waitForMessage(parent, |
347 | 0 | Long.valueOf(BrachaTouegDeadlockMessage.ACK)); |
348 | 0 | } |
349 | 0 | } |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
private void handleNotifyMessage( |
367 | |
Vertex<LongWritable, BrachaTouegDeadlockVertexValue, LongWritable> vertex, |
368 | |
BrachaTouegDeadlockMessage message) { |
369 | |
|
370 | 0 | BrachaTouegDeadlockVertexValue value = vertex.getValue(); |
371 | |
|
372 | 0 | if (!value.isNotified()) { |
373 | 0 | notifyVertices(vertex); |
374 | 0 | value.setIdWithInHoldDone(message.getSenderId()); |
375 | |
} else { |
376 | 0 | sendMessage(vertex.getId().get(), message.getSenderId(), |
377 | |
BrachaTouegDeadlockMessage.DONE); |
378 | |
} |
379 | 0 | } |
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
private void handleGrantMessage( |
399 | |
Vertex<LongWritable, BrachaTouegDeadlockVertexValue, LongWritable> vertex, |
400 | |
BrachaTouegDeadlockMessage message) { |
401 | |
|
402 | 0 | BrachaTouegDeadlockVertexValue value = vertex.getValue(); |
403 | 0 | Long senderId = Long.valueOf(message.getSenderId()); |
404 | 0 | LongWritable wId = new LongWritable(senderId); |
405 | 0 | LongWritable tag = vertex.getEdgeValue(wId); |
406 | |
|
407 | 0 | value.removeRequest(tag, wId); |
408 | |
|
409 | 0 | if (value.isFree() || value.getNumOfRequests(tag) > 0) { |
410 | 0 | sendAckMessage(senderId, vertex); |
411 | 0 | return; |
412 | |
} else { |
413 | 0 | grantVertices(vertex); |
414 | 0 | value.setIdWithInHoldAck(senderId); |
415 | |
} |
416 | 0 | } |
417 | |
} |