diff options
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/query/profile/compiled/CompiledQueryProfileRegistry.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/search/query/profile/compiled/CompiledQueryProfileRegistry.java | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/query/profile/compiled/CompiledQueryProfileRegistry.java b/container-search/src/main/java/com/yahoo/search/query/profile/compiled/CompiledQueryProfileRegistry.java index fb1552e549b..39ec3e1c647 100644 --- a/container-search/src/main/java/com/yahoo/search/query/profile/compiled/CompiledQueryProfileRegistry.java +++ b/container-search/src/main/java/com/yahoo/search/query/profile/compiled/CompiledQueryProfileRegistry.java @@ -10,6 +10,11 @@ import com.yahoo.search.query.profile.QueryProfileRegistry; import com.yahoo.search.query.profile.config.QueryProfileConfigurer; import com.yahoo.search.query.profile.config.QueryProfilesConfig; import com.yahoo.search.query.profile.types.QueryProfileTypeRegistry; +import com.yahoo.yolean.UncheckedInterruptedException; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Executor; +import java.util.concurrent.LinkedBlockingQueue; /** * A set of compiled query profiles. @@ -24,14 +29,47 @@ public class CompiledQueryProfileRegistry extends ComponentRegistry<CompiledQuer private final QueryProfileTypeRegistry typeRegistry; @Inject - public CompiledQueryProfileRegistry(QueryProfilesConfig config) { + public CompiledQueryProfileRegistry(QueryProfilesConfig config, Executor executor) { QueryProfileRegistry registry = QueryProfileConfigurer.createFromConfig(config); typeRegistry = registry.getTypeRegistry(); - for (QueryProfile inputProfile : registry.allComponents()) { - register(QueryProfileCompiler.compile(inputProfile, this)); + int maxConcurrent = 1; // TODO hold this one after Concurrency issue has been found: Math.max(1, (int)(Runtime.getRuntime().availableProcessors() * 0.20)); + BlockingQueue<CompiledQueryProfile> doneQ = new LinkedBlockingQueue<>(); + int started = 0; + int completed = 0; + try { + for (QueryProfile inputProfile : registry.allComponents()) { + abortIfInterrupted(); + if (started++ >= maxConcurrent) { + register(doneQ.take()); + completed++; + } + executor.execute(() -> { + Thread self = Thread.currentThread(); + int prevPriority = self.getPriority(); + try { + self.setPriority(Thread.MIN_PRIORITY); + doneQ.add(QueryProfileCompiler.compile(inputProfile, this)); + } finally { + self.setPriority(prevPriority); + } + }); + } + while (completed < started) { + register(doneQ.take()); + completed++; + } + } catch (InterruptedException e) { + throw new UncheckedInterruptedException("Interrupted while waiting for compiled query profiles", true); } } + // Query profile construction is very expensive and triggers no operations that automatically throws on interrupt + // We need to manually check the interrupt flag in case the container reconfigurer should shut down + private void abortIfInterrupted() { + if (Thread.interrupted()) + throw new UncheckedInterruptedException("Interrupted while building query profile registry", true); + } + /** Creates a compiled query profile registry with no types */ public CompiledQueryProfileRegistry() { this(QueryProfileTypeRegistry.emptyFrozen()); |