Skip to content

Commit 8f400c3

Browse files
boldtrnkarussell
authored andcommitted
Use CompletionService to fail early when a thread crashes (graphhopper#980)
* Changed Runnable to Callable * Use isInterrupted
1 parent 951b443 commit 8f400c3

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

core/src/main/java/com/graphhopper/routing/ch/CHAlgoFactoryDecorator.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
import org.slf4j.LoggerFactory;
3333

3434
import java.util.*;
35-
import java.util.concurrent.ExecutorService;
36-
import java.util.concurrent.TimeUnit;
35+
import java.util.concurrent.*;
3736

3837
import static com.graphhopper.util.Parameters.CH.DISABLE;
3938

@@ -279,38 +278,41 @@ public void setPreparationThreads(int preparationThreads) {
279278
}
280279

281280
public void prepare(final StorableProperties properties) {
281+
ExecutorCompletionService completionService = new ExecutorCompletionService<>(threadPool);
282282
int counter = 0;
283283
for (final PrepareContractionHierarchies prepare : getPreparations()) {
284284
LOGGER.info((++counter) + "/" + getPreparations().size() + " calling CH prepare.doWork for " + prepare.getWeighting() + " ... (" + Helper.getMemInfo() + ")");
285285
final String name = AbstractWeighting.weightingToFileName(prepare.getWeighting());
286-
threadPool.execute(new Runnable() {
286+
completionService.submit(new Runnable() {
287287
@Override
288288
public void run() {
289289
String errorKey = CH.PREPARE + "error." + name;
290290
try {
291291
// toString is not taken into account so we need to cheat, see http://stackoverflow.com/q/6113746/194609 for other options
292292
Thread.currentThread().setName(name);
293-
294293
properties.put(errorKey, "CH preparation incomplete");
295294
prepare.doWork();
296295
properties.remove(errorKey);
297296
properties.put(CH.PREPARE + "date." + name, Helper.createFormatter().format(new Date()));
298297
} catch (Exception ex) {
299298
LOGGER.error("Problem while CH preparation " + name, ex);
300299
properties.put(errorKey, ex.getMessage());
300+
throw ex;
301301
}
302302
}
303-
});
303+
}, name);
304+
304305
}
305306

306307
threadPool.shutdown();
307-
try {
308-
if (!threadPool.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS))
309-
threadPool.shutdownNow();
310308

311-
} catch (InterruptedException ie) {
309+
try {
310+
for (int i = 0; i < getPreparations().size(); i++) {
311+
completionService.take().get();
312+
}
313+
} catch (Exception e) {
312314
threadPool.shutdownNow();
313-
throw new RuntimeException(ie);
315+
throw new RuntimeException(e);
314316
}
315317
}
316318

core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ void contractNodes() {
304304

305305
CHEdgeIterator iter = vehicleAllExplorer.setBaseNode(polledNode);
306306
while (iter.next()) {
307+
308+
if(Thread.currentThread().isInterrupted()){
309+
throw new RuntimeException("Thread was interrupted");
310+
}
311+
307312
int nn = iter.getAdjNode();
308313
if (prepareGraph.getLevel(nn) != maxLevel)
309314
continue;

0 commit comments

Comments
 (0)