diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /container-search/src/main/java/com/yahoo/search/searchchain/FutureResult.java |
Publish
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/searchchain/FutureResult.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/search/searchchain/FutureResult.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/searchchain/FutureResult.java b/container-search/src/main/java/com/yahoo/search/searchchain/FutureResult.java new file mode 100644 index 00000000000..877252f07e6 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/searchchain/FutureResult.java @@ -0,0 +1,86 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.searchchain; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.yahoo.yolean.Exceptions; +import com.yahoo.search.Query; +import com.yahoo.search.Result; +import com.yahoo.search.result.ErrorMessage; + +/** + * Extends a {@code FutureTask<Result>}, with some added error handling + */ +public class FutureResult extends FutureTask<Result> { + + private final Query query; + + /** Only used for generating messages */ + private final Execution execution; + + private final static Logger log = Logger.getLogger(FutureResult.class.getName()); + + FutureResult(Callable<Result> callable, Execution execution, final Query query) { + super(callable); + this.query = query; + this.execution = execution; + } + + @Override + public Result get() { + Result result; + try { + result = super.get(); + } + catch (InterruptedException e) { + result = new Result(getQuery(), ErrorMessage.createUnspecifiedError( + "'" + execution + "' was interrupted while executing: " + Exceptions.toMessageString(e))); + } + catch (ExecutionException e) { + log.log(Level.WARNING,"Exception on executing " + execution + " for " + query,e); + result = new Result(getQuery(), ErrorMessage.createErrorInPluginSearcher( + "Error in '" + execution + "': " + Exceptions.toMessageString(e), + e.getCause())); + } + return result; + } + + @Override + public Result get(long timeout, TimeUnit timeunit) { + Result result; + try { + result = super.get(timeout, timeunit); + } + catch (InterruptedException e) { + result = new Result(getQuery(), ErrorMessage.createUnspecifiedError( + "'" + execution + "' was interrupted while executing: " + Exceptions.toMessageString(e))); + } + catch (ExecutionException e) { + log.log(Level.WARNING,"Exception on executing " + execution + " for " + query, e); + result = new Result(getQuery(), ErrorMessage.createErrorInPluginSearcher( + "Error in '" + execution + "': " + Exceptions.toMessageString(e), + e.getCause())); + } + catch (TimeoutException e) { + result = new Result(getQuery(), createTimeoutError()); + } + return result; + } + + /** Returns the query used in this execution, never null */ + public Query getQuery() { + return query; + } + + ErrorMessage createTimeoutError() { + return ErrorMessage.createTimeout( + "Error executing '" + execution + "': " + " Chain timed out."); + + } +} |