1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.edge; |
20 | |
|
21 | |
import com.google.common.collect.MapMaker; |
22 | |
import org.apache.giraph.bsp.CentralizedServiceWorker; |
23 | |
import org.apache.giraph.conf.DefaultImmutableClassesGiraphConfigurable; |
24 | |
import org.apache.giraph.conf.GiraphConstants; |
25 | |
import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration; |
26 | |
import org.apache.giraph.graph.Vertex; |
27 | |
import org.apache.giraph.ooc.OutOfCoreEngine; |
28 | |
import org.apache.giraph.partition.Partition; |
29 | |
import org.apache.giraph.utils.CallableFactory; |
30 | |
import org.apache.giraph.utils.ProgressCounter; |
31 | |
import org.apache.giraph.utils.ProgressableUtils; |
32 | |
import org.apache.giraph.utils.ThreadLocalProgressCounter; |
33 | |
import org.apache.giraph.utils.Trimmable; |
34 | |
import org.apache.giraph.utils.VertexIdEdgeIterator; |
35 | |
import org.apache.giraph.utils.VertexIdEdges; |
36 | |
import org.apache.hadoop.io.Writable; |
37 | |
import org.apache.hadoop.io.WritableComparable; |
38 | |
import org.apache.hadoop.util.Progressable; |
39 | |
import org.apache.log4j.Logger; |
40 | |
|
41 | |
import java.io.DataInput; |
42 | |
import java.io.DataOutput; |
43 | |
import java.io.IOException; |
44 | |
import java.util.Iterator; |
45 | |
import java.util.Map; |
46 | |
import java.util.concurrent.Callable; |
47 | |
import java.util.concurrent.ConcurrentMap; |
48 | |
|
49 | |
import static com.google.common.base.Preconditions.checkState; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | public abstract class AbstractEdgeStore<I extends WritableComparable, |
62 | |
V extends Writable, E extends Writable, K, Et> |
63 | |
extends DefaultImmutableClassesGiraphConfigurable<I, V, E> |
64 | |
implements EdgeStore<I, V, E> { |
65 | |
|
66 | 0 | public static final ThreadLocalProgressCounter PROGRESS_COUNTER = |
67 | |
new ThreadLocalProgressCounter(); |
68 | |
|
69 | 0 | private static final Logger LOG = Logger.getLogger(AbstractEdgeStore.class); |
70 | |
|
71 | |
protected CentralizedServiceWorker<I, V, E> service; |
72 | |
|
73 | |
protected ImmutableClassesGiraphConfiguration<I, V, E> configuration; |
74 | |
|
75 | |
protected Progressable progressable; |
76 | |
|
77 | |
protected ConcurrentMap<Integer, Map<K, OutEdges<I, E>>> transientEdges; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
protected boolean reuseEdgeObjects; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
protected boolean useInputOutEdges; |
88 | |
|
89 | 0 | private volatile boolean hasEdgesOnDisk = false; |
90 | |
|
91 | |
private CreateSourceVertexCallback<I> createSourceVertexCallback; |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public AbstractEdgeStore( |
102 | |
CentralizedServiceWorker<I, V, E> service, |
103 | |
ImmutableClassesGiraphConfiguration<I, V, E> configuration, |
104 | 0 | Progressable progressable) { |
105 | 0 | this.service = service; |
106 | 0 | this.configuration = configuration; |
107 | 0 | this.progressable = progressable; |
108 | 0 | transientEdges = new MapMaker().concurrencyLevel( |
109 | 0 | configuration.getNettyServerExecutionConcurrency()).makeMap(); |
110 | 0 | reuseEdgeObjects = configuration.reuseEdgeObjects(); |
111 | 0 | useInputOutEdges = configuration.useInputOutEdges(); |
112 | 0 | createSourceVertexCallback = |
113 | |
GiraphConstants.CREATE_EDGE_SOURCE_VERTICES_CALLBACK |
114 | 0 | .newInstance(configuration); |
115 | 0 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
protected abstract I getVertexId(Et entry, I representativeVertexId); |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
protected abstract I createVertexId(Et entry); |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
protected abstract Map<K, OutEdges<I, E>> getPartitionEdges(int partitionId); |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
protected abstract OutEdges<I, E> getPartitionEdges(Et entry); |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
protected abstract void writeVertexKey(K key, DataOutput output) |
157 | |
throws IOException; |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
protected abstract K readVertexKey(DataInput input) throws IOException; |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
protected abstract Iterator<Et> |
174 | |
getPartitionEdgesIterator(Map<K, OutEdges<I, E>> partitionEdges); |
175 | |
|
176 | |
@Override |
177 | |
public boolean hasEdgesForPartition(int partitionId) { |
178 | 0 | return transientEdges.containsKey(partitionId); |
179 | |
} |
180 | |
|
181 | |
@Override |
182 | |
public void writePartitionEdgeStore(int partitionId, DataOutput output) |
183 | |
throws IOException { |
184 | 0 | Map<K, OutEdges<I, E>> edges = transientEdges.remove(partitionId); |
185 | 0 | if (edges != null) { |
186 | 0 | output.writeInt(edges.size()); |
187 | 0 | if (edges.size() > 0) { |
188 | 0 | hasEdgesOnDisk = true; |
189 | |
} |
190 | 0 | for (Map.Entry<K, OutEdges<I, E>> edge : edges.entrySet()) { |
191 | 0 | writeVertexKey(edge.getKey(), output); |
192 | 0 | edge.getValue().write(output); |
193 | 0 | } |
194 | |
} |
195 | 0 | } |
196 | |
|
197 | |
@Override |
198 | |
public void readPartitionEdgeStore(int partitionId, DataInput input) |
199 | |
throws IOException { |
200 | 0 | checkState(!transientEdges.containsKey(partitionId), |
201 | |
"readPartitionEdgeStore: reading a partition that is already there in" + |
202 | |
" the partition store (impossible)"); |
203 | 0 | Map<K, OutEdges<I, E>> partitionEdges = getPartitionEdges(partitionId); |
204 | 0 | int numEntries = input.readInt(); |
205 | 0 | for (int i = 0; i < numEntries; ++i) { |
206 | 0 | K vertexKey = readVertexKey(input); |
207 | 0 | OutEdges<I, E> edges = configuration.createAndInitializeInputOutEdges(); |
208 | 0 | edges.readFields(input); |
209 | 0 | partitionEdges.put(vertexKey, edges); |
210 | |
} |
211 | 0 | } |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
protected abstract OutEdges<I, E> getVertexOutEdges( |
221 | |
VertexIdEdgeIterator<I, E> vertexIdEdgeIterator, |
222 | |
Map<K, OutEdges<I, E>> partitionEdgesIn); |
223 | |
|
224 | |
@Override |
225 | |
public void addPartitionEdges( |
226 | |
int partitionId, VertexIdEdges<I, E> edges) { |
227 | 0 | Map<K, OutEdges<I, E>> partitionEdges = getPartitionEdges(partitionId); |
228 | |
|
229 | 0 | VertexIdEdgeIterator<I, E> vertexIdEdgeIterator = |
230 | 0 | edges.getVertexIdEdgeIterator(); |
231 | 0 | while (vertexIdEdgeIterator.hasNext()) { |
232 | 0 | vertexIdEdgeIterator.next(); |
233 | 0 | Edge<I, E> edge = reuseEdgeObjects ? |
234 | 0 | vertexIdEdgeIterator.getCurrentEdge() : |
235 | 0 | vertexIdEdgeIterator.releaseCurrentEdge(); |
236 | 0 | OutEdges<I, E> outEdges = getVertexOutEdges(vertexIdEdgeIterator, |
237 | |
partitionEdges); |
238 | 0 | synchronized (outEdges) { |
239 | 0 | outEdges.add(edge); |
240 | 0 | } |
241 | 0 | } |
242 | 0 | } |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
private OutEdges<I, E> convertInputToComputeEdges( |
252 | |
OutEdges<I, E> inputEdges) { |
253 | 0 | if (!useInputOutEdges) { |
254 | 0 | return inputEdges; |
255 | |
} else { |
256 | 0 | return configuration.createAndInitializeOutEdges(inputEdges); |
257 | |
} |
258 | |
} |
259 | |
|
260 | |
@Override |
261 | |
public void moveEdgesToVertices() { |
262 | 0 | if (transientEdges.isEmpty() && !hasEdgesOnDisk) { |
263 | 0 | if (LOG.isInfoEnabled()) { |
264 | 0 | LOG.info("moveEdgesToVertices: No edges to move"); |
265 | |
} |
266 | 0 | return; |
267 | |
} |
268 | |
|
269 | 0 | if (LOG.isInfoEnabled()) { |
270 | 0 | LOG.info("moveEdgesToVertices: Moving incoming edges to " + |
271 | |
"vertices. Using " + createSourceVertexCallback); |
272 | |
} |
273 | |
|
274 | 0 | service.getPartitionStore().startIteration(); |
275 | 0 | int numThreads = configuration.getNumInputSplitsThreads(); |
276 | |
|
277 | 0 | CallableFactory<Void> callableFactory = new CallableFactory<Void>() { |
278 | |
@Override |
279 | |
public Callable<Void> newCallable(int callableId) { |
280 | 0 | return new Callable<Void>() { |
281 | |
@Override |
282 | |
public Void call() throws Exception { |
283 | 0 | I representativeVertexId = configuration.createVertexId(); |
284 | 0 | OutOfCoreEngine oocEngine = service.getServerData().getOocEngine(); |
285 | 0 | if (oocEngine != null) { |
286 | 0 | oocEngine.processingThreadStart(); |
287 | |
} |
288 | 0 | ProgressCounter numVerticesProcessed = PROGRESS_COUNTER.get(); |
289 | |
while (true) { |
290 | 0 | Partition<I, V, E> partition = |
291 | 0 | service.getPartitionStore().getNextPartition(); |
292 | 0 | if (partition == null) { |
293 | 0 | break; |
294 | |
} |
295 | 0 | Map<K, OutEdges<I, E>> partitionEdges = |
296 | 0 | transientEdges.remove(partition.getId()); |
297 | 0 | if (partitionEdges == null) { |
298 | 0 | service.getPartitionStore().putPartition(partition); |
299 | 0 | continue; |
300 | |
} |
301 | |
|
302 | 0 | Iterator<Et> iterator = |
303 | 0 | getPartitionEdgesIterator(partitionEdges); |
304 | |
|
305 | 0 | int count = 0; |
306 | 0 | while (iterator.hasNext()) { |
307 | |
|
308 | |
|
309 | |
|
310 | 0 | if (oocEngine != null && |
311 | |
(++count & OutOfCoreEngine.CHECK_IN_INTERVAL) == 0) { |
312 | 0 | oocEngine.activeThreadCheckIn(); |
313 | |
} |
314 | 0 | Et entry = iterator.next(); |
315 | 0 | I vertexId = getVertexId(entry, representativeVertexId); |
316 | 0 | OutEdges<I, E> outEdges = convertInputToComputeEdges( |
317 | 0 | getPartitionEdges(entry)); |
318 | 0 | Vertex<I, V, E> vertex = partition.getVertex(vertexId); |
319 | |
|
320 | |
|
321 | 0 | if (vertex == null) { |
322 | 0 | if (createSourceVertexCallback |
323 | 0 | .shouldCreateSourceVertex(vertexId)) { |
324 | |
|
325 | 0 | vertex = configuration.createVertex(); |
326 | 0 | vertex.initialize(createVertexId(entry), |
327 | 0 | configuration.createVertexValue(), outEdges); |
328 | 0 | partition.putVertex(vertex); |
329 | |
} |
330 | |
} else { |
331 | |
|
332 | |
|
333 | 0 | if (vertex.getNumEdges() == 0) { |
334 | 0 | vertex.setEdges(outEdges); |
335 | |
} else { |
336 | 0 | for (Edge<I, E> edge : outEdges) { |
337 | 0 | vertex.addEdge(edge); |
338 | 0 | } |
339 | |
} |
340 | 0 | if (vertex instanceof Trimmable) { |
341 | 0 | ((Trimmable) vertex).trim(); |
342 | |
} |
343 | |
|
344 | |
|
345 | 0 | partition.saveVertex(vertex); |
346 | |
} |
347 | 0 | numVerticesProcessed.inc(); |
348 | 0 | iterator.remove(); |
349 | 0 | } |
350 | |
|
351 | |
|
352 | |
|
353 | 0 | service.getPartitionStore().putPartition(partition); |
354 | 0 | } |
355 | 0 | if (oocEngine != null) { |
356 | 0 | oocEngine.processingThreadFinish(); |
357 | |
} |
358 | 0 | return null; |
359 | |
} |
360 | |
}; |
361 | |
} |
362 | |
}; |
363 | 0 | ProgressableUtils.getResultsWithNCallables(callableFactory, numThreads, |
364 | |
"move-edges-%d", progressable); |
365 | |
|
366 | |
|
367 | 0 | transientEdges.clear(); |
368 | |
|
369 | 0 | if (LOG.isInfoEnabled()) { |
370 | 0 | LOG.info("moveEdgesToVertices: Finished moving incoming edges to " + |
371 | |
"vertices."); |
372 | |
} |
373 | 0 | } |
374 | |
} |