1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.examples.utils; |
20 | |
|
21 | |
import java.io.DataInput; |
22 | |
import java.io.DataOutput; |
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import org.apache.hadoop.io.LongWritable; |
29 | |
import org.apache.hadoop.io.Writable; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public class BrachaTouegDeadlockVertexValue implements Writable { |
35 | |
|
36 | 0 | public static final Long INVALID_ID = Long.valueOf(-1); |
37 | |
|
38 | |
|
39 | |
private boolean isFree; |
40 | |
|
41 | |
private boolean isNotified; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private HashMap<Long, ArrayList<Long>> requests; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
private HashMap<Long, Long> waitingList; |
56 | |
|
57 | |
private ArrayList<Long> parents; |
58 | |
|
59 | |
|
60 | |
private Long idWithInHoldAck; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private Long idWithInHoldDone; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public BrachaTouegDeadlockVertexValue() { |
71 | 0 | this(new HashMap<Long, ArrayList<Long>>()); |
72 | 0 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public BrachaTouegDeadlockVertexValue( |
80 | 0 | HashMap<Long, ArrayList<Long>> requests) { |
81 | |
|
82 | 0 | this.isFree = false; |
83 | 0 | this.isNotified = false; |
84 | 0 | this.requests = requests; |
85 | 0 | this.waitingList = new HashMap<Long, Long>(); |
86 | 0 | this.parents = new ArrayList<Long>(); |
87 | 0 | this.idWithInHoldAck = INVALID_ID; |
88 | 0 | this.idWithInHoldDone = INVALID_ID; |
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
@Override |
94 | |
public void readFields(DataInput input) throws IOException { |
95 | |
int sz; |
96 | |
|
97 | 0 | this.isFree = input.readBoolean(); |
98 | 0 | this.isNotified = input.readBoolean(); |
99 | |
|
100 | 0 | sz = input.readInt(); |
101 | 0 | for (int i = 0; i < sz; ++i) { |
102 | 0 | ArrayList<Long> targets = new ArrayList<Long>(); |
103 | 0 | Long tag = input.readLong(); |
104 | 0 | int sw = input.readInt(); |
105 | |
|
106 | 0 | for (int j = 0; j < sw; ++j) { |
107 | 0 | Long target = input.readLong(); |
108 | |
|
109 | 0 | targets.add(target); |
110 | |
} |
111 | |
|
112 | 0 | this.requests.put(tag, targets); |
113 | |
} |
114 | |
|
115 | 0 | sz = input.readInt(); |
116 | 0 | for (int i = 0; i < sz; ++i) { |
117 | 0 | Long key = input.readLong(); |
118 | 0 | Long value = input.readLong(); |
119 | |
|
120 | 0 | this.waitingList.put(key, value); |
121 | |
} |
122 | |
|
123 | 0 | sz = input.readInt(); |
124 | 0 | for (int i = 0; i < sz; ++i) { |
125 | 0 | this.parents.add(Long.valueOf(input.readLong())); |
126 | |
} |
127 | |
|
128 | 0 | this.idWithInHoldAck = input.readLong(); |
129 | 0 | this.idWithInHoldDone = input.readLong(); |
130 | 0 | } |
131 | |
|
132 | |
@Override |
133 | |
public void write(DataOutput output) throws IOException { |
134 | |
int sz; |
135 | |
|
136 | 0 | output.writeBoolean(this.isFree); |
137 | 0 | output.writeBoolean(this.isNotified); |
138 | |
|
139 | 0 | sz = this.requests.size(); |
140 | 0 | output.writeInt(sz); |
141 | 0 | for (Map.Entry<Long, ArrayList<Long>> entry : this.requests.entrySet()) { |
142 | |
ArrayList<Long> targets; |
143 | |
|
144 | 0 | output.writeLong(entry.getKey()); |
145 | 0 | targets = entry.getValue(); |
146 | 0 | sz = targets.size(); |
147 | 0 | output.writeInt(sz); |
148 | 0 | for (Long target : targets) { |
149 | 0 | output.writeLong(target); |
150 | 0 | } |
151 | 0 | } |
152 | |
|
153 | 0 | sz = this.waitingList.size(); |
154 | 0 | output.writeInt(sz); |
155 | 0 | for (Map.Entry<Long, Long> entry : this.waitingList.entrySet()) { |
156 | 0 | output.writeLong(entry.getKey()); |
157 | 0 | output.writeLong(entry.getValue()); |
158 | 0 | } |
159 | |
|
160 | 0 | sz = this.parents.size(); |
161 | 0 | output.writeInt(sz); |
162 | 0 | for (int i = 0; i < sz; ++i) { |
163 | 0 | output.writeLong(this.parents.get(i)); |
164 | |
} |
165 | |
|
166 | 0 | output.writeLong(this.idWithInHoldAck); |
167 | 0 | output.writeLong(this.idWithInHoldDone); |
168 | 0 | } |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public boolean isFree() { |
176 | 0 | return this.isFree; |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
public void setFree() { |
183 | 0 | this.isFree = true; |
184 | 0 | } |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public boolean isNotified() { |
190 | 0 | return this.isNotified; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
public void setNotified() { |
197 | 0 | this.isNotified = true; |
198 | 0 | } |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
public boolean hasPendingRequests() { |
205 | 0 | boolean withPendingRequests = true; |
206 | |
|
207 | 0 | if (this.requests.isEmpty()) { |
208 | 0 | withPendingRequests = false; |
209 | |
} |
210 | |
|
211 | 0 | for (Map.Entry<Long, ArrayList<Long>> request : this.requests.entrySet()) { |
212 | 0 | ArrayList<Long> targets = request.getValue(); |
213 | |
|
214 | 0 | if (targets.size() == 0) { |
215 | 0 | withPendingRequests = false; |
216 | |
} |
217 | 0 | } |
218 | 0 | return withPendingRequests; |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public void removeRequest(LongWritable tag, LongWritable targetId) { |
228 | 0 | Long l = Long.valueOf(tag.get()); |
229 | 0 | ArrayList<Long> targets = this.requests.get(l); |
230 | |
|
231 | 0 | if (targets.contains(targetId.get())) { |
232 | 0 | targets.remove(Long.valueOf(targetId.get())); |
233 | |
} |
234 | 0 | } |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
public int getNumOfRequests(LongWritable tag) { |
246 | 0 | Long l = Long.valueOf(tag.get()); |
247 | 0 | ArrayList<Long> targets = this.requests.get(l); |
248 | |
|
249 | 0 | return targets.size(); |
250 | |
} |
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
public void waitForMessage(Long id, Long type) { |
259 | |
|
260 | 0 | assert waitingList.get(id) == null; |
261 | 0 | waitingList.put(id, type); |
262 | 0 | } |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
public void receivedMessage(Long id, Long type) { |
272 | |
long typel; |
273 | |
|
274 | 0 | assert waitingList.get(id) != null; |
275 | 0 | typel = waitingList.get(id).longValue(); |
276 | 0 | assert typel > 0; |
277 | 0 | waitingList.remove(id); |
278 | 0 | } |
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
public boolean isWaitingForMessage(Long type) { |
285 | 0 | for (Map.Entry<Long, Long> entry : waitingList.entrySet()) { |
286 | 0 | long typel = entry.getValue().longValue(); |
287 | 0 | if ((typel & type) > 0) { |
288 | 0 | return true; |
289 | |
} |
290 | 0 | } |
291 | |
|
292 | 0 | return false; |
293 | |
} |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
public void addParent(Long parentId) { |
300 | 0 | this.parents.add(parentId); |
301 | 0 | } |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
public ArrayList<Long> getParents() { |
307 | 0 | return this.parents; |
308 | |
} |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
public Long getIdWithInHoldAck() { |
314 | 0 | return this.idWithInHoldAck; |
315 | |
} |
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
public void setIdWithInHoldAck(Long id) { |
321 | 0 | this.idWithInHoldAck = id; |
322 | 0 | } |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
public Long getIdWithInHoldDone() { |
328 | 0 | return idWithInHoldDone; |
329 | |
} |
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
public void setIdWithInHoldDone(Long doneId) { |
335 | 0 | this.idWithInHoldDone = doneId; |
336 | 0 | } |
337 | |
|
338 | |
@Override |
339 | |
public String toString() { |
340 | 0 | return "isFree=" + Boolean.toString(isFree); |
341 | |
} |
342 | |
} |