summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/federation/FederationSearcher.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2017-01-24 10:44:09 +0100
committerJon Bratseth <bratseth@yahoo-inc.com>2017-01-24 10:44:09 +0100
commitcdc34a66c66703cfb1c62f51b2a86f4873d55167 (patch)
treec13e93d62ffc6a379e0c2b6b8f2c99b1ec1ef58c /container-search/src/main/java/com/yahoo/search/federation/FederationSearcher.java
parentfa47a60ef99b617d7d5d2ae615300e1a2ca6b7f0 (diff)
Refactor
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/federation/FederationSearcher.java')
-rw-r--r--container-search/src/main/java/com/yahoo/search/federation/FederationSearcher.java789
1 files changed, 322 insertions, 467 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/federation/FederationSearcher.java b/container-search/src/main/java/com/yahoo/search/federation/FederationSearcher.java
index 9c94fd04ea5..0339bc3681b 100644
--- a/container-search/src/main/java/com/yahoo/search/federation/FederationSearcher.java
+++ b/container-search/src/main/java/com/yahoo/search/federation/FederationSearcher.java
@@ -22,7 +22,6 @@ import com.yahoo.search.federation.sourceref.SearchChainResolver;
import com.yahoo.search.federation.sourceref.SingleTarget;
import com.yahoo.search.federation.sourceref.SourceRefResolver;
import com.yahoo.search.federation.sourceref.SourcesTarget;
-import com.yahoo.search.federation.sourceref.Target;
import com.yahoo.search.federation.sourceref.UnresolvedSearchChainException;
import com.yahoo.search.query.Properties;
import com.yahoo.search.query.properties.QueryProperties;
@@ -54,7 +53,7 @@ import java.util.logging.Logger;
/**
* This searcher takes a set of sources, looks them up in config and fire off the correct searchchains.
*
- * @author <a href="mailto:arnebef@yahoo-inc.com">Arne Bergene Fossaa</a>
+ * @author Arne Bergene Fossaa
* @author tonytv
*/
@Provides(FederationSearcher.FEDERATION)
@@ -63,188 +62,6 @@ public class FederationSearcher extends ForkingSearcher {
public static final String FEDERATION = "Federation";
- /** A target for federation, containing a chain to which a federation query can be forwarded. */
- private static abstract class TargetHandler {
-
- abstract Chain<Searcher> getChain();
- abstract void modifyTargetQuery(Query query);
- abstract void modifyTargetResult(Result result);
-
- ComponentId getId() {
- return getChain().getId();
- }
-
- public abstract FederationOptions federationOptions();
-
- @Override
- public String toString() { return getChain().getId().stringValue(); }
-
- }
-
- /**
- * A handler representing a target created by the federation logic.
- * This is a value object, to ensure that identical target invocations are not invoked multiple times.
- */
- private static class StandardTargetHandler extends TargetHandler {
-
- private final SearchChainInvocationSpec target;
- private final Chain<Searcher> chain;
-
- public StandardTargetHandler(SearchChainInvocationSpec target, Chain<Searcher> chain) {
- this.target = target;
- this.chain = chain;
- }
-
- @Override
- Chain<Searcher> getChain() { return chain; }
-
- @Override
- void modifyTargetQuery(Query query) {}
- @Override
- void modifyTargetResult(Result result) {}
-
- @Override
- public FederationOptions federationOptions() { return target.federationOptions; }
-
- @Override
- public boolean equals(Object o) {
- if (o == this) return true;
- if ( ! ( o instanceof StandardTargetHandler)) return false;
-
- StandardTargetHandler other = (StandardTargetHandler)o;
- if ( ! Objects.equals(other.chain.getId(), this.chain.getId())) return false;
- if ( ! Objects.equals(other.target, this.target)) return false;
- return true;
- }
-
- @Override
- public int hashCode() { return Objects.hash(chain.getId(), target); }
-
- }
-
- /** A target handler where the target generation logic is delegated to the application provided target selector */
- private static class CustomTargetHandler<T> extends TargetHandler {
-
- private final TargetSelector<T> selector;
- private final FederationTarget<T> target;
-
- CustomTargetHandler(TargetSelector<T> selector, FederationTarget<T> target) {
- this.selector = selector;
- this.target = target;
- }
-
- @Override
- Chain<Searcher> getChain() {
- return target.getChain();
- }
-
- @Override
- public void modifyTargetQuery(Query query) {
- selector.modifyTargetQuery(target, query);
- }
-
- @Override
- public void modifyTargetResult(Result result) {
- selector.modifyTargetResult(target, result);
- }
-
- @Override
- public FederationOptions federationOptions() {
- return target.getFederationOptions();
- }
-
- }
-
- private static class ExecutionInfo {
-
- private final TargetHandler targetHandler;
- private final FederationOptions federationOptions;
- private final FutureResult futureResult;
-
- public ExecutionInfo(TargetHandler targetHandler, FederationOptions federationOptions, FutureResult futureResult) {
- this.targetHandler = targetHandler;
- this.federationOptions = federationOptions;
- this.futureResult = futureResult;
- }
-
- }
-
- private static class CompoundKey {
-
- private final String sourceName;
- private final String propertyName;
-
- CompoundKey(String sourceName, String propertyName) {
- this.sourceName = sourceName;
- this.propertyName = propertyName;
- }
-
- @Override
- public int hashCode() {
- return sourceName.hashCode() ^ propertyName.hashCode();
- }
-
- @Override
- public boolean equals(Object o) {
- CompoundKey rhs = (CompoundKey) o;
- return sourceName.equals(rhs.sourceName) && propertyName.equals(rhs.propertyName);
- }
-
- @Override
- public String toString() {
- return sourceName + '.' + propertyName;
- }
- }
-
- private static class SourceKey extends CompoundKey {
-
- public static final String SOURCE = "source.";
-
- SourceKey(String sourceName, String propertyName) {
- super(sourceName, propertyName);
- }
-
- @Override
- public int hashCode() {
- return super.hashCode() ^ 7;
- }
-
- @Override
- public boolean equals(Object o) {
- return (o instanceof SourceKey) && super.equals(o);
- }
-
- @Override
- public String toString() {
- return SOURCE + super.toString();
- }
- }
-
- private static class ProviderKey extends CompoundKey {
-
- public static final String PROVIDER = "provider.";
-
- ProviderKey(String sourceName, String propertyName) {
- super(sourceName, propertyName);
- }
-
- @Override
- public int hashCode() {
- return super.hashCode() ^ 17;
- }
-
- @Override
- public boolean equals(Object o) {
- return (o instanceof ProviderKey) && super.equals(o);
- }
-
- @Override
- public String toString() {
- return PROVIDER + super.toString();
- }
-
- }
-
private static final Logger log = Logger.getLogger(FederationSearcher.class.getName());
/** The name of the query property containing the source name added to the query to each source by this */
@@ -274,7 +91,6 @@ public class FederationSearcher extends ForkingSearcher {
private static TargetSelector resolveSelector(String selectorId,
ComponentRegistry<TargetSelector> targetSelectors) {
if (selectorId.isEmpty()) return null;
-
return checkNotNull(targetSelectors.getComponent(selectorId),
"Missing target selector with id" + quote(selectorId));
}
@@ -294,7 +110,6 @@ public class FederationSearcher extends ForkingSearcher {
this.targetSelector = targetSelector;
}
-
private static SearchChainResolver createResolver(FederationConfig config) {
SearchChainResolver.Builder builder = new SearchChainResolver.Builder();
@@ -345,139 +160,83 @@ public class FederationSearcher extends ForkingSearcher {
setRequestTimeoutInMilliseconds(searchChain.requestTimeoutMillis());
}
- private static long calculateTimeout(Query query, Collection<TargetHandler> targets) {
-
- class PartitionByOptional {
- final List<TargetHandler> mandatoryTargets;
- final List<TargetHandler> optionalTargets;
+ @Override
+ public Result search(Query query, Execution execution) {
+ Result mergedResults = execution.search(query);
- PartitionByOptional(Collection<TargetHandler> targets) {
- List<TargetHandler> mandatoryTargets = new ArrayList<>();
- List<TargetHandler> optionalTargets = new ArrayList<>();
+ Results<SearchChainInvocationSpec, UnresolvedSearchChainException> targets =
+ getTargets(query.getModel().getSources(), query.properties(), execution.context().getIndexFacts());
+ warnIfUnresolvedSearchChains(targets.errors(), mergedResults.hits());
- for (TargetHandler target : targets) {
- if (target.federationOptions().getOptional()) {
- optionalTargets.add(target);
- } else {
- mandatoryTargets.add(target);
- }
- }
+ Collection<SearchChainInvocationSpec> prunedTargets =
+ pruneTargetsWithoutDocumentTypes(query.getModel().getRestrict(), targets.data());
- this.mandatoryTargets = Collections.unmodifiableList(mandatoryTargets);
- this.optionalTargets = Collections.unmodifiableList(optionalTargets);
- }
- }
+ Results<Target, ErrorMessage> regularTargetHandlers = resolveSearchChains(prunedTargets, execution.searchChainRegistry());
+ query.errors().addAll(regularTargetHandlers.errors());
- if (query.requestHasProperty("timeout") || targets.isEmpty()) {
- return query.getTimeLeft();
- } else {
- PartitionByOptional partition = new PartitionByOptional(targets);
- long queryTimeout = query.getTimeout();
+ Set<Target> targetHandlers = new LinkedHashSet<>(regularTargetHandlers.data());
+ targetHandlers.addAll(getAdditionalTargets(query, execution, targetSelector));
- return partition.mandatoryTargets.isEmpty() ?
- maximumTimeout(partition.optionalTargets, queryTimeout) :
- maximumTimeout(partition.mandatoryTargets, queryTimeout);
- }
- }
+ traceTargets(query, targetHandlers);
- private static long maximumTimeout(List<TargetHandler> invocationSpecs, long queryTimeout) {
- long timeout = 0;
- for (TargetHandler target : invocationSpecs) {
- timeout = Math.max(timeout,
- target.federationOptions().getSearchChainExecutionTimeoutInMilliseconds(queryTimeout));
- }
- return timeout;
+ if (targetHandlers.isEmpty())
+ return mergedResults;
+ else if (targetHandlers.size() > 1)
+ search(query, execution, targetHandlers, mergedResults);
+ else if (shouldExecuteTargetLongerThanThread(query, targetHandlers.iterator().next()))
+ search(query, execution, targetHandlers, mergedResults); // one target, but search in separate thread
+ else
+ search(query, execution, first(targetHandlers), mergedResults); // search in this thread
+ return mergedResults;
}
- private void addSearchChainTimedOutError(Query query,
- ComponentId searchChainId) {
- ErrorMessage timeoutMessage=
- ErrorMessage.createTimeout("The search chain '" + searchChainId + "' timed out.");
- timeoutMessage.setSource(searchChainId.stringValue());
- query.errors().add(timeoutMessage);
+ private void search(Query query, Execution execution, Target target, Result mergedResults) {
+ Result result = search(query, execution, target);
+ mergeResult(query, target, mergedResults, result);
}
- private void mergeResult(Query query, TargetHandler targetHandler,
- Result mergedResults, Result result) {
-
+ private void search(Query query, Execution execution, Collection<Target> targets, Result mergedResults) {
+ FederationResult results = search(query, execution, targets);
+ results.waitForAll(query.getTimeLeft());
- targetHandler.modifyTargetResult(result);
- final ComponentId searchChainId = targetHandler.getId();
- Chain<Searcher> searchChain = targetHandler.getChain();
-
- mergedResults.mergeWith(result);
- HitGroup group = result.hits();
- group.setId("source:" + searchChainId.getName());
-
- group.setSearcherSpecificMetaData(this, searchChain);
- group.setMeta(false); // Set hit groups as non-meta as a default
- group.setAuxiliary(true); // Set hit group as auxiliary so that it doesn't contribute to count
- group.setSource(searchChainId.getName());
- group.setQuery(result.getQuery());
-
- for (Iterator<Hit> it = group.unorderedDeepIterator(); it.hasNext();) {
- Hit hit = it.next();
- hit.setSearcherSpecificMetaData(this, searchChain);
- hit.setSource(searchChainId.stringValue());
+ HitOrderer s = null;
+ for (FederationResult.TargetResult targetResult : results.all()) {
+ if ( ! targetResult.successfullyCompleted()) {
+ addSearchChainTimedOutError(query, targetResult.target.getId());
+ } else {
+ if (s == null) {
+ s = dirtyCopyIfModifiedOrderer(mergedResults.hits(), targetResult.futureResult.get().hits().getOrderer());
+ }
+ mergeResult(query, targetResult.target, mergedResults, targetResult.futureResult.get());
- // This is the backend request meta hit, that is holding logging information
- // See HTTPBackendSearcher, where this hit is created
- if (hit.isMeta() && hit.types().contains("logging")) {
- // Augment this hit with count fields
- hit.setField(LOG_COUNT_PREFIX + "deep", result.getDeepHitCount());
- hit.setField(LOG_COUNT_PREFIX + "total", result.getTotalHitCount());
- int offset = result.getQuery().getOffset();
- hit.setField(LOG_COUNT_PREFIX + "first", offset + 1);
- hit.setField(LOG_COUNT_PREFIX + "last", result.getConcreteHitCount() + offset);
}
-
}
- if (query.getTraceLevel()>=4)
- query.trace("Got " + group.getConcreteSize() + " hits from " + group.getId(),false, 4);
- mergedResults.hits().add(group);
}
- private boolean successfullyCompleted(FutureResult result) {
- return result.isDone() && !result.isCancelled();
- }
-
- private Query setupSingleQuery(Query query, long timeout, TargetHandler targetHandler) {
+ private Result search(Query query, Execution execution, Target target) {
+ long timeout = target.federationOptions().getSearchChainExecutionTimeoutInMilliseconds(query.getTimeLeft());
+ Execution newExecution = new Execution(target.getChain(), execution.context());
if (strictSearchchain) {
query.resetTimeout();
- return setupFederationQuery(query, query,
- windowParameters(query.getHits(), query.getOffset()), timeout, targetHandler);
+ return newExecution.search(createFederationQuery(query, query,
+ windowParameters(query.getHits(), query.getOffset()), timeout, target));
} else {
- return cloneFederationQuery(query,
- windowParameters(query.getHits(), query.getOffset()), timeout, targetHandler);
+ return newExecution.search(cloneFederationQuery(query,
+ windowParameters(query.getHits(), query.getOffset()), timeout, target));
}
}
- private Result startExecuteSingleQuery(Query query, TargetHandler chain, long timeout, Execution execution) {
- Query outgoing = setupSingleQuery(query, timeout, chain);
- Execution exec = new Execution(chain.getChain(), execution.context());
- return exec.search(outgoing);
- }
-
- private List<ExecutionInfo> startExecuteQueryForEachTarget(
- Query query, Collection<TargetHandler> targets, long timeout, Execution execution) {
-
- List<ExecutionInfo> results = new ArrayList<>();
-
+ private FederationResult search(Query query, Execution execution, Collection<Target> targets) {
+ FederationResult.Builder result = new FederationResult.Builder();
Map<String, Object> windowParameters;
- if (targets.size()==1) // preserve requested top-level offset by default as an optimization
+ if (targets.size() == 1) // preserve requested top-level offset by default as an optimization
windowParameters = Collections.unmodifiableMap(windowParameters(query.getHits(), query.getOffset()));
else // request from offset 0 to enable correct upstream blending into a single top-level hit list
windowParameters = Collections.unmodifiableMap(windowParameters(query.getHits() + query.getOffset(), 0));
- for (TargetHandler targetHandler : targets) {
- long executeTimeout = timeout;
- if (targetHandler.federationOptions().getRequestTimeoutInMilliseconds() != -1)
- executeTimeout = targetHandler.federationOptions().getRequestTimeoutInMilliseconds();
- results.add(new ExecutionInfo(targetHandler, targetHandler.federationOptions(),
- createFutureSearch(query, windowParameters, targetHandler, executeTimeout, execution)));
- }
-
- return results;
+ for (Target target : targets)
+ result.add(target, searchAsynchronously(query, execution, windowParameters, target));
+ return result.build();
}
private Map<String, Object> windowParameters(int hits, int offset) {
@@ -487,23 +246,21 @@ public class FederationSearcher extends ForkingSearcher {
return params;
}
- private FutureResult createFutureSearch(Query query, Map<String, Object> windowParameters, TargetHandler targetHandler,
- long timeout, Execution execution) {
- Query clonedQuery = cloneFederationQuery(query, windowParameters, timeout, targetHandler);
- return new AsyncExecution(targetHandler.getChain(), execution).search(clonedQuery);
+ private FutureResult searchAsynchronously(Query query, Execution execution, Map<String, Object> windowParameters, Target target) {
+ long timeout = target.federationOptions().getSearchChainExecutionTimeoutInMilliseconds(query.getTimeLeft());
+ Query clonedQuery = cloneFederationQuery(query, windowParameters, timeout, target);
+ return new AsyncExecution(target.getChain(), execution).search(clonedQuery);
}
-
private Query cloneFederationQuery(Query query,
- Map<String, Object> windowParameters, long timeout, TargetHandler targetHandler) {
+ Map<String, Object> windowParameters, long timeout, Target target) {
Query clonedQuery = Query.createNewQuery(query);
- return setupFederationQuery(query, clonedQuery, windowParameters, timeout, targetHandler);
+ return createFederationQuery(query, clonedQuery, windowParameters, timeout, target);
}
- private Query setupFederationQuery(Query query, Query outgoing,
- Map<String, Object> windowParameters, long timeout, TargetHandler targetHandler) {
-
- ComponentId chainId = targetHandler.getChain().getId();
+ private Query createFederationQuery(Query query, Query outgoing,
+ Map<String, Object> windowParameters, long timeout, Target target) {
+ ComponentId chainId = target.getChain().getId();
String sourceName = chainId.getName();
outgoing.properties().set(SOURCENAME, sourceName);
@@ -527,7 +284,7 @@ public class FederationSearcher extends ForkingSearcher {
//TODO: FederationTarget
//TODO: only for target produced by this, not others
- targetHandler.modifyTargetQuery(outgoing);
+ target.modifyTargetQuery(outgoing);
return outgoing;
}
@@ -535,12 +292,10 @@ public class FederationSearcher extends ForkingSearcher {
Map<String, Object> windowParameters,
String sourceName, String providerName,
CompoundName[] queryProperties) {
-
for (CompoundName key : queryProperties) {
Object value = getSourceOrProviderProperty(original, key, sourceName, providerName, windowParameters.get(key.toString()));
- if (value != null) {
+ if (value != null)
outgoing.properties().set(key, value);
- }
}
}
@@ -552,12 +307,10 @@ public class FederationSearcher extends ForkingSearcher {
result = getProperty(query, new ProviderKey(providerName, propertyName.toString()));
if (result == null)
result = defaultValue;
-
return result;
}
private Object getProperty(Query query, CompoundKey key) {
-
CompoundName name = map.get(key);
if (name == null) {
name = new CompoundName(key.toString());
@@ -567,22 +320,15 @@ public class FederationSearcher extends ForkingSearcher {
}
private ErrorMessage missingSearchChainsErrorMessage(List<UnresolvedSearchChainException> unresolvedSearchChainExceptions) {
- StringBuilder sb = new StringBuilder();
- sb.append(StringUtils.join(getMessagesSet(unresolvedSearchChainExceptions), ' '));
-
-
- sb.append(" Valid source refs are ");
- sb.append(
- StringUtils.join(allSourceRefDescriptions().iterator(),
- ", ")).append('.');
-
- return ErrorMessage.createInvalidQueryParameter(sb.toString());
+ String message = StringUtils.join(getMessagesSet(unresolvedSearchChainExceptions), ' ') +
+ " Valid source refs are " + StringUtils.join(allSourceRefDescriptions().iterator(), ", ") +'.';
+ return ErrorMessage.createInvalidQueryParameter(message);
}
private List<String> allSourceRefDescriptions() {
List<String> descriptions = new ArrayList<>();
- for (Target target : searchChainResolver.allTopLevelTargets()) {
+ for (com.yahoo.search.federation.sourceref.Target target : searchChainResolver.allTopLevelTargets()) {
descriptions.add(target.searchRefDescription());
}
return descriptions;
@@ -598,7 +344,6 @@ public class FederationSearcher extends ForkingSearcher {
private void warnIfUnresolvedSearchChains(List<UnresolvedSearchChainException> missingTargets,
HitGroup errorHitGroup) {
-
if (!missingTargets.isEmpty()) {
errorHitGroup.addError(missingSearchChainsErrorMessage(missingTargets));
}
@@ -608,7 +353,7 @@ public class FederationSearcher extends ForkingSearcher {
public Collection<CommentedSearchChain> getSearchChainsForwarded(SearchChainRegistry registry) {
List<CommentedSearchChain> searchChains = new ArrayList<>();
- for (Target target : searchChainResolver.allTopLevelTargets()) {
+ for (com.yahoo.search.federation.sourceref.Target target : searchChainResolver.allTopLevelTargets()) {
if (target instanceof SourcesTarget) {
searchChains.addAll(commentedSourceProviderSearchChains((SourcesTarget)target, registry));
} else if (target instanceof SingleTarget) {
@@ -623,12 +368,11 @@ public class FederationSearcher extends ForkingSearcher {
private CommentedSearchChain commentedSearchChain(SingleTarget singleTarget, SearchChainRegistry registry) {
return new CommentedSearchChain("If source refs contains '" + singleTarget.getId() + "'.",
- registry.getChain(singleTarget.getId()));
+ registry.getChain(singleTarget.getId()));
}
private List<CommentedSearchChain> commentedSourceProviderSearchChains(SourcesTarget sourcesTarget,
SearchChainRegistry registry) {
-
List<CommentedSearchChain> commentedSearchChains = new ArrayList<>();
String ifMatchingSourceRefPrefix = "If source refs contains '" + sourcesTarget.getId() + "' and provider is '";
@@ -646,9 +390,11 @@ public class FederationSearcher extends ForkingSearcher {
return commentedSearchChains;
}
- /** Returns the set of properties set for the source or provider given in the query (if any).
+ /**
+ * Returns the set of properties set for the source or provider given in the query (if any).
*
- * If the query has not set sourceName or providerName, null will be returned */
+ * If the query has not set sourceName or providerName, null will be returned
+ */
public static Properties getSourceProperties(Query query) {
String sourceName = query.properties().getString(SOURCENAME);
String providerName = query.properties().getString(PROVIDERNAME);
@@ -661,12 +407,12 @@ public class FederationSearcher extends ForkingSearcher {
}
@Override
- public void fill(final Result result, final String summaryClass, Execution execution) {
+ public void fill(Result result, String summaryClass, Execution execution) {
List<FutureResult> filledResults = new ArrayList<>();
UniqueExecutionsToResults uniqueExecutionsToResults = new UniqueExecutionsToResults();
addResultsToFill(result.hits(), result, summaryClass, uniqueExecutionsToResults);
- final Set<Entry<Chain<Searcher>, Map<Query, Result>>> resultsForAllChains = uniqueExecutionsToResults.resultsToFill
- .entrySet();
+ Set<Entry<Chain<Searcher>, Map<Query, Result>>> resultsForAllChains =
+ uniqueExecutionsToResults.resultsToFill.entrySet();
int numberOfCallsToFillNeeded = 0;
for (Entry<Chain<Searcher>, Map<Query, Result>> resultsToFillForAChain : resultsForAllChains) {
@@ -699,31 +445,6 @@ public class FederationSearcher extends ForkingSearcher {
destination.hits().addError(error);
}
- /** A map from a unique search chain and query instance to a result */
- private static class UniqueExecutionsToResults {
-
- /** Implemented as a nested identity hashmap */
- final Map<Chain<Searcher>,Map<Query,Result>> resultsToFill = new IdentityHashMap<>();
-
- /** Returns a result to fill for a query and chain, by creating it if necessary */
- public Result get(Chain<Searcher> chain, Query query) {
- Map<Query,Result> resultsToFillForAChain = resultsToFill.get(chain);
- if (resultsToFillForAChain == null) {
- resultsToFillForAChain = new IdentityHashMap<>();
- resultsToFill.put(chain,resultsToFillForAChain);
- }
-
- Result resultsToFillForAChainAndQuery = resultsToFillForAChain.get(query);
- if (resultsToFillForAChainAndQuery == null) {
- resultsToFillForAChainAndQuery = new Result(query);
- resultsToFillForAChain.put(query,resultsToFillForAChainAndQuery);
- }
-
- return resultsToFillForAChainAndQuery;
- }
-
- }
-
private void addResultsToFill(HitGroup hitGroup, Result result, String summaryClass,
UniqueExecutionsToResults uniqueExecutionsToResults) {
for (Hit hit : hitGroup) {
@@ -744,28 +465,6 @@ public class FederationSearcher extends ForkingSearcher {
return uniqueExecutionsToResults.get(chain,query);
}
- private void searchMultipleTargets(Query query, Result mergedResults,
- Collection<TargetHandler> targets,
- long timeout,
- Execution execution) {
-
- List<ExecutionInfo> executionInfos = startExecuteQueryForEachTarget(query, targets, timeout, execution);
- waitForMandatoryTargets(executionInfos, query.getTimeout());
-
- HitOrderer s=null;
- for (ExecutionInfo executionInfo : executionInfos) {
- if ( ! successfullyCompleted(executionInfo.futureResult)) {
- addSearchChainTimedOutError(query, executionInfo.targetHandler.getId());
- } else {
- if (s == null) {
- s = dirtyCopyIfModifiedOrderer(mergedResults.hits(), executionInfo.futureResult.get().hits().getOrderer());
- }
- mergeResult(query, executionInfo.targetHandler, mergedResults, executionInfo.futureResult.get());
-
- }
- }
- }
-
/**
* TODO This is probably a dirty hack for bug 4711376. There are probably better ways.
* But I will leave that to trd-processing@
@@ -785,46 +484,6 @@ public class FederationSearcher extends ForkingSearcher {
return orderer;
}
- private void waitForMandatoryTargets(List<ExecutionInfo> executionInfos, long queryTimeout) {
- FutureWaiter futureWaiter = new FutureWaiter();
-
- boolean hasMandatoryTargets = false;
- for (ExecutionInfo executionInfo : executionInfos) {
- if (isMandatory(executionInfo)) {
- futureWaiter.add(executionInfo.futureResult,
- getSearchChainExecutionTimeoutInMilliseconds(executionInfo, queryTimeout));
- hasMandatoryTargets = true;
- }
- }
-
- if (!hasMandatoryTargets) {
- for (ExecutionInfo executionInfo : executionInfos) {
- futureWaiter.add(executionInfo.futureResult,
- getSearchChainExecutionTimeoutInMilliseconds(executionInfo, queryTimeout));
- }
- }
-
- futureWaiter.waitForFutures();
- }
-
- private long getSearchChainExecutionTimeoutInMilliseconds(ExecutionInfo executionInfo, long queryTimeout) {
- return executionInfo.federationOptions.
- getSearchChainExecutionTimeoutInMilliseconds(queryTimeout);
- }
-
- private boolean isMandatory(ExecutionInfo executionInfo) {
- return !executionInfo.federationOptions.getOptional();
- }
-
- private void searchSingleTarget(Query query, Result mergedResults,
- TargetHandler targetHandler,
- long timeout,
- Execution execution) {
- Result result = startExecuteSingleQuery(query, targetHandler, timeout, execution);
- mergeResult(query, targetHandler, mergedResults, result);
- }
-
-
private Results<SearchChainInvocationSpec, UnresolvedSearchChainException> getTargets(Set<String> sources, Properties properties, IndexFacts indexFacts) {
return sources.isEmpty() ?
defaultSearchChains(properties):
@@ -845,11 +504,10 @@ public class FederationSearcher extends ForkingSearcher {
return result.build();
}
-
public Results<SearchChainInvocationSpec, UnresolvedSearchChainException> defaultSearchChains(Properties sourceToProviderMap) {
Results.Builder<SearchChainInvocationSpec, UnresolvedSearchChainException> result = new Builder<>();
- for (Target target : searchChainResolver.defaultTargets()) {
+ for (com.yahoo.search.federation.sourceref.Target target : searchChainResolver.defaultTargets()) {
try {
result.addData(target.responsibleSearchChain(sourceToProviderMap));
} catch (UnresolvedSearchChainException e) {
@@ -865,66 +523,70 @@ public class FederationSearcher extends ForkingSearcher {
try {
return new ComponentSpecification(source);
} catch(Exception e) {
- throw new IllegalArgumentException("The source ref '" + source
- + "' used for federation is not valid.", e);
- }
- }
-
- @Override
- public Result search(Query query, Execution execution) {
- Result mergedResults = execution.search(query);
-
- Results<SearchChainInvocationSpec, UnresolvedSearchChainException> targets =
- getTargets(query.getModel().getSources(), query.properties(), execution.context().getIndexFacts());
- warnIfUnresolvedSearchChains(targets.errors(), mergedResults.hits());
-
- Collection<SearchChainInvocationSpec> prunedTargets =
- pruneTargetsWithoutDocumentTypes(query.getModel().getRestrict(), targets.data());
-
- Results<TargetHandler, ErrorMessage> regularTargetHandlers = resolveSearchChains(prunedTargets, execution.searchChainRegistry());
- query.errors().addAll(regularTargetHandlers.errors());
-
- Set<TargetHandler> targetHandlers = new LinkedHashSet<>(regularTargetHandlers.data());
- targetHandlers.addAll(getAdditionalTargets(query, execution, targetSelector));
-
- long targetsTimeout = calculateTimeout(query, targetHandlers);
- if (targetsTimeout < 0)
- return new Result(query, ErrorMessage.createTimeout("Timed out when about to federate"));
-
- traceTargets(query, targetHandlers);
-
- if (targetHandlers.size() == 0) {
- return mergedResults;
- } else if (targetHandlers.size() == 1 &&
- ! shouldExecuteTargetLongerThanThread(query, targetHandlers.iterator().next())) {
- TargetHandler chain = first(targetHandlers);
- searchSingleTarget(query, mergedResults, chain, targetsTimeout, execution);
- } else {
- searchMultipleTargets(query, mergedResults, targetHandlers, targetsTimeout, execution);
+ throw new IllegalArgumentException("The source ref '" + source + "' used for federation is not valid.", e);
}
-
- return mergedResults;
}
- private void traceTargets(Query query, Collection<TargetHandler> targetHandlers) {
+ private void traceTargets(Query query, Collection<Target> targets) {
int traceFederationLevel = 2;
if ( ! query.isTraceable(traceFederationLevel)) return;
- query.trace("Federating to " + targetHandlers, traceFederationLevel);
+ query.trace("Federating to " + targets, traceFederationLevel);
}
/**
* Returns true if we are requested to keep executing a target longer than we're waiting for it.
* This is useful to populate caches inside targets.
*/
- private boolean shouldExecuteTargetLongerThanThread(Query query, TargetHandler target) {
+ private boolean shouldExecuteTargetLongerThanThread(Query query, Target target) {
return target.federationOptions().getRequestTimeoutInMilliseconds() > query.getTimeout();
}
- private static Results<TargetHandler, ErrorMessage> resolveSearchChains(
- Collection<SearchChainInvocationSpec> prunedTargets,
- SearchChainRegistry registry) {
+ private void addSearchChainTimedOutError(Query query, ComponentId searchChainId) {
+ ErrorMessage timeoutMessage = ErrorMessage.createTimeout("The search chain '" + searchChainId + "' timed out.");
+ timeoutMessage.setSource(searchChainId.stringValue());
+ query.errors().add(timeoutMessage);
+ }
+
+ private void mergeResult(Query query, Target target, Result mergedResults, Result result) {
+ target.modifyTargetResult(result);
+ ComponentId searchChainId = target.getId();
+ Chain<Searcher> searchChain = target.getChain();
+
+ mergedResults.mergeWith(result);
+ HitGroup group = result.hits();
+ group.setId("source:" + searchChainId.getName());
+
+ group.setSearcherSpecificMetaData(this, searchChain);
+ group.setMeta(false); // Set hit groups as non-meta as a default
+ group.setAuxiliary(true); // Set hit group as auxiliary so that it doesn't contribute to count
+ group.setSource(searchChainId.getName());
+ group.setQuery(result.getQuery());
+
+ for (Iterator<Hit> it = group.unorderedDeepIterator(); it.hasNext();) {
+ Hit hit = it.next();
+ hit.setSearcherSpecificMetaData(this, searchChain);
+ hit.setSource(searchChainId.stringValue());
+
+ // This is the backend request meta hit, that is holding logging information
+ // See HTTPBackendSearcher, where this hit is created
+ if (hit.isMeta() && hit.types().contains("logging")) {
+ // Augment this hit with count fields
+ hit.setField(LOG_COUNT_PREFIX + "deep", result.getDeepHitCount());
+ hit.setField(LOG_COUNT_PREFIX + "total", result.getTotalHitCount());
+ int offset = result.getQuery().getOffset();
+ hit.setField(LOG_COUNT_PREFIX + "first", offset + 1);
+ hit.setField(LOG_COUNT_PREFIX + "last", result.getConcreteHitCount() + offset);
+ }
+
+ }
+ if (query.getTraceLevel()>=4)
+ query.trace("Got " + group.getConcreteSize() + " hits from " + group.getId(),false, 4);
+ mergedResults.hits().add(group);
+ }
- Results.Builder<TargetHandler, ErrorMessage> targetHandlers = new Results.Builder<>();
+ private Results<Target, ErrorMessage> resolveSearchChains(Collection<SearchChainInvocationSpec> prunedTargets,
+ SearchChainRegistry registry) {
+ Results.Builder<Target, ErrorMessage> targetHandlers = new Results.Builder<>();
for (SearchChainInvocationSpec target: prunedTargets) {
Chain<Searcher> chain = registry.getChain(target.searchChainId);
@@ -932,19 +594,19 @@ public class FederationSearcher extends ForkingSearcher {
targetHandlers.addError(ErrorMessage.createIllegalQuery("Could not find search chain '"
+ target.searchChainId + "'"));
} else {
- targetHandlers.addData(new StandardTargetHandler(target, chain));
+ targetHandlers.addData(new StandardTarget(target, chain));
}
}
return targetHandlers.build();
}
- private static <T> List<TargetHandler> getAdditionalTargets(Query query, Execution execution, TargetSelector<T> targetSelector) {
+ private static <T> List<Target> getAdditionalTargets(Query query, Execution execution, TargetSelector<T> targetSelector) {
if (targetSelector == null) return Collections.emptyList();
- ArrayList<TargetHandler> result = new ArrayList<>();
+ ArrayList<Target> result = new ArrayList<>();
for (FederationTarget<T> target: targetSelector.getTargets(query, execution.searchChainRegistry()))
- result.add(new CustomTargetHandler<>(targetSelector, target));
+ result.add(new CustomTarget<>(targetSelector, target));
return result;
}
@@ -971,4 +633,197 @@ public class FederationSearcher extends ForkingSearcher {
return false;
}
+ /** A map from a unique search chain and query instance to a result */
+ private static class UniqueExecutionsToResults {
+
+ /** Implemented as a nested identity hashmap */
+ final Map<Chain<Searcher>,Map<Query,Result>> resultsToFill = new IdentityHashMap<>();
+
+ /** Returns a result to fill for a query and chain, by creating it if necessary */
+ public Result get(Chain<Searcher> chain, Query query) {
+ Map<Query,Result> resultsToFillForAChain = resultsToFill.get(chain);
+ if (resultsToFillForAChain == null) {
+ resultsToFillForAChain = new IdentityHashMap<>();
+ resultsToFill.put(chain,resultsToFillForAChain);
+ }
+
+ Result resultsToFillForAChainAndQuery = resultsToFillForAChain.get(query);
+ if (resultsToFillForAChainAndQuery == null) {
+ resultsToFillForAChainAndQuery = new Result(query);
+ resultsToFillForAChain.put(query,resultsToFillForAChainAndQuery);
+ }
+
+ return resultsToFillForAChainAndQuery;
+ }
+
+ }
+
+ /** A target for federation, containing a chain to which a federation query can be forwarded. */
+ static abstract class Target {
+
+ abstract Chain<Searcher> getChain();
+ abstract void modifyTargetQuery(Query query);
+ abstract void modifyTargetResult(Result result);
+
+ ComponentId getId() {
+ return getChain().getId();
+ }
+
+ public abstract FederationOptions federationOptions();
+
+ @Override
+ public String toString() { return getChain().getId().stringValue(); }
+
+ }
+
+ /**
+ * A handler representing a target created by the federation logic.
+ * This is a value object, to ensure that identical target invocations are not invoked multiple times.
+ */
+ private static class StandardTarget extends Target {
+
+ private final SearchChainInvocationSpec target;
+ private final Chain<Searcher> chain;
+
+ public StandardTarget(SearchChainInvocationSpec target, Chain<Searcher> chain) {
+ this.target = target;
+ this.chain = chain;
+ }
+
+ @Override
+ Chain<Searcher> getChain() { return chain; }
+
+ @Override
+ void modifyTargetQuery(Query query) {}
+ @Override
+ void modifyTargetResult(Result result) {}
+
+ @Override
+ public FederationOptions federationOptions() { return target.federationOptions; }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == this) return true;
+ if ( ! ( o instanceof StandardTarget)) return false;
+
+ StandardTarget other = (StandardTarget)o;
+ if ( ! Objects.equals(other.chain.getId(), this.chain.getId())) return false;
+ if ( ! Objects.equals(other.target, this.target)) return false;
+ return true;
+ }
+
+ @Override
+ public int hashCode() { return Objects.hash(chain.getId(), target); }
+
+ }
+
+ /** A target handler where the target generation logic is delegated to the application provided target selector */
+ private static class CustomTarget<T> extends Target {
+
+ private final TargetSelector<T> selector;
+ private final FederationTarget<T> target;
+
+ CustomTarget(TargetSelector<T> selector, FederationTarget<T> target) {
+ this.selector = selector;
+ this.target = target;
+ }
+
+ @Override
+ Chain<Searcher> getChain() {
+ return target.getChain();
+ }
+
+ @Override
+ public void modifyTargetQuery(Query query) {
+ selector.modifyTargetQuery(target, query);
+ }
+
+ @Override
+ public void modifyTargetResult(Result result) {
+ selector.modifyTargetResult(target, result);
+ }
+
+ @Override
+ public FederationOptions federationOptions() {
+ return target.getFederationOptions();
+ }
+
+ }
+
+ private static class CompoundKey {
+
+ private final String sourceName;
+ private final String propertyName;
+
+ CompoundKey(String sourceName, String propertyName) {
+ this.sourceName = sourceName;
+ this.propertyName = propertyName;
+ }
+
+ @Override
+ public int hashCode() {
+ return sourceName.hashCode() ^ propertyName.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ CompoundKey rhs = (CompoundKey) o;
+ return sourceName.equals(rhs.sourceName) && propertyName.equals(rhs.propertyName);
+ }
+
+ @Override
+ public String toString() {
+ return sourceName + '.' + propertyName;
+ }
+ }
+
+ private static class SourceKey extends CompoundKey {
+
+ public static final String SOURCE = "source.";
+
+ SourceKey(String sourceName, String propertyName) {
+ super(sourceName, propertyName);
+ }
+
+ @Override
+ public int hashCode() {
+ return super.hashCode() ^ 7;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return (o instanceof SourceKey) && super.equals(o);
+ }
+
+ @Override
+ public String toString() {
+ return SOURCE + super.toString();
+ }
+ }
+
+ private static class ProviderKey extends CompoundKey {
+
+ public static final String PROVIDER = "provider.";
+
+ ProviderKey(String sourceName, String propertyName) {
+ super(sourceName, propertyName);
+ }
+
+ @Override
+ public int hashCode() {
+ return super.hashCode() ^ 17;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return (o instanceof ProviderKey) && super.equals(o);
+ }
+
+ @Override
+ public String toString() {
+ return PROVIDER + super.toString();
+ }
+
+ }
+
}