diff options
author | Jon Bratseth <bratseth@gmail.com> | 2021-12-09 15:31:17 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2021-12-09 15:31:17 +0100 |
commit | 9822c3c3e54cab0befb7dfb7f34dc4eced6a796d (patch) | |
tree | 34bad004b0c26f6d290215c5272b64469c8dc13a /container-search/src/main | |
parent | 800c53c580717f7f1d8bcc02d31235ac6d3673d2 (diff) |
Add zone info to query profile context
Diffstat (limited to 'container-search/src/main')
3 files changed, 91 insertions, 24 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/Query.java b/container-search/src/main/java/com/yahoo/search/Query.java index ddb8a392730..786a0d0e04f 100644 --- a/container-search/src/main/java/com/yahoo/search/Query.java +++ b/container-search/src/main/java/com/yahoo/search/Query.java @@ -1,6 +1,9 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.search; +import ai.vespa.cloud.Environment; +import ai.vespa.cloud.Zone; +import ai.vespa.cloud.ZoneInfo; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.yahoo.collections.Tuple2; @@ -334,22 +337,30 @@ public class Query extends com.yahoo.processing.Request implements Cloneable { public Query(HttpRequest request, Map<String, String> requestMap, CompiledQueryProfile queryProfile) { super(new QueryPropertyAliases(propertyAliases)); this.httpRequest = request; - init(requestMap, queryProfile, Embedder.throwsOnUse); + init(requestMap, queryProfile, Embedder.throwsOnUse, ZoneInfo.defaultInfo()); } // TODO: Deprecate most constructors above here private Query(Builder builder) { - this(builder.getRequest(), builder.getRequestMap(), builder.getQueryProfile(), builder.getEmbedder()); + this(builder.getRequest(), + builder.getRequestMap(), + builder.getQueryProfile(), + builder.getEmbedder(), + builder.getZoneInfo()); } - private Query(HttpRequest request, Map<String, String> requestMap, CompiledQueryProfile queryProfile, Embedder embedder) { + private Query(HttpRequest request, Map<String, String> requestMap, CompiledQueryProfile queryProfile, Embedder embedder, + ZoneInfo zoneInfo) { super(new QueryPropertyAliases(propertyAliases)); this.httpRequest = request; - init(requestMap, queryProfile, embedder); + init(requestMap, queryProfile, embedder, zoneInfo); } - private void init(Map<String, String> requestMap, CompiledQueryProfile queryProfile, Embedder embedder) { + private void init(Map<String, String> requestMap, + CompiledQueryProfile queryProfile, + Embedder embedder, + ZoneInfo zoneInfo) { startTime = httpRequest.getJDiscRequest().creationTime(TimeUnit.MILLISECONDS); if (queryProfile != null) { // Move all request parameters to the query profile just to validate that the parameter settings are legal @@ -361,7 +372,7 @@ public class Query extends com.yahoo.processing.Request implements Cloneable { // Create the full chain properties().chain(new QueryProperties(this, queryProfile.getRegistry(), embedder)). chain(new ModelObjectMap()). - chain(new RequestContextProperties(requestMap)). + chain(new RequestContextProperties(requestMap, zoneInfo)). chain(queryProfileProperties). chain(new DefaultProperties()); @@ -1131,6 +1142,7 @@ public class Query extends com.yahoo.processing.Request implements Cloneable { private Map<String, String> requestMap = null; private CompiledQueryProfile queryProfile = null; private Embedder embedder = Embedder.throwsOnUse; + private ZoneInfo zoneInfo = ZoneInfo.defaultInfo(); public Builder setRequest(String query) { request = HttpRequest.createTestRequest(query, com.yahoo.jdisc.http.HttpRequest.Method.GET); @@ -1175,6 +1187,13 @@ public class Query extends com.yahoo.processing.Request implements Cloneable { public Embedder getEmbedder() { return embedder; } + public Builder setZoneInfo(ZoneInfo zoneInfo) { + this.zoneInfo = zoneInfo; + return this; + } + + public ZoneInfo getZoneInfo() { return zoneInfo; } + /** Creates a new query from this builder. No properties are required to before calling this. */ public Query build() { return new Query(this); } diff --git a/container-search/src/main/java/com/yahoo/search/handler/SearchHandler.java b/container-search/src/main/java/com/yahoo/search/handler/SearchHandler.java index 739058ade71..8925c647ad2 100644 --- a/container-search/src/main/java/com/yahoo/search/handler/SearchHandler.java +++ b/container-search/src/main/java/com/yahoo/search/handler/SearchHandler.java @@ -1,6 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.search.handler; +import ai.vespa.cloud.Environment; +import ai.vespa.cloud.Zone; import com.google.inject.Inject; import com.yahoo.collections.Tuple2; import com.yahoo.component.ComponentSpecification; @@ -53,6 +55,7 @@ import com.yahoo.statistics.Statistics; import com.yahoo.vespa.configdefinition.SpecialtokensConfig; import com.yahoo.yolean.Exceptions; import com.yahoo.yolean.trace.TraceNode; +import ai.vespa.cloud.ZoneInfo; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -70,6 +73,7 @@ import java.util.logging.Logger; * Handles search request. * * @author Steinar Knutsen + * @author bratseth */ public class SearchHandler extends LoggingRequestHandler { @@ -103,22 +107,46 @@ public class SearchHandler extends LoggingRequestHandler { private final ExecutionFactory executionFactory; private final AtomicLong numRequestsLeftToTrace; + private final ZoneInfo zoneInfo; + private final static RequestHandlerSpec REQUEST_HANDLER_SPEC = RequestHandlerSpec.builder() .withAclMapping(SearchHandler.aclRequestMapper()).build(); + @Inject public SearchHandler(Metric metric, ContainerThreadPool threadpool, CompiledQueryProfileRegistry queryProfileRegistry, ContainerHttpConfig config, Embedder embedder, + ExecutionFactory executionFactory, + ZoneInfo zoneInfo) { + this(metric, threadpool.executor(), queryProfileRegistry, embedder, executionFactory, + config.numQueriesToTraceOnDebugAfterConstruction(), + config.hostResponseHeaderKey().equals("") ? Optional.empty() : Optional.of(config.hostResponseHeaderKey()), + zoneInfo); + } + + /** + * @deprecated Use the @Inject annotated constructor instead. + */ + @Deprecated // Vespa 8 + public SearchHandler(Metric metric, + ContainerThreadPool threadpool, + CompiledQueryProfileRegistry queryProfileRegistry, + ContainerHttpConfig config, + Embedder embedder, ExecutionFactory executionFactory) { this(metric, threadpool.executor(), queryProfileRegistry, embedder, executionFactory, - config.numQueriesToTraceOnDebugAfterConstruction(), - config.hostResponseHeaderKey().equals("") ? Optional.empty() : Optional.of(config.hostResponseHeaderKey())); + config.numQueriesToTraceOnDebugAfterConstruction(), + config.hostResponseHeaderKey().equals("") ? Optional.empty() : Optional.of(config.hostResponseHeaderKey()), + ZoneInfo.defaultInfo()); } - @Deprecated + /** + * @deprecated Use the @Inject annotated constructor instead. + */ + @Deprecated // Vespa 8 public SearchHandler(Statistics statistics, Metric metric, ContainerThreadPool threadpool, @@ -128,7 +156,8 @@ public class SearchHandler extends LoggingRequestHandler { ExecutionFactory executionFactory) { this(metric, threadpool.executor(), queryProfileRegistry, embedder, executionFactory, config.numQueriesToTraceOnDebugAfterConstruction(), - config.hostResponseHeaderKey().equals("") ? Optional.empty() : Optional.of(config.hostResponseHeaderKey())); + config.hostResponseHeaderKey().equals("") ? Optional.empty() : Optional.of(config.hostResponseHeaderKey()), + ZoneInfo.defaultInfo()); } /** @@ -158,8 +187,9 @@ public class SearchHandler extends LoggingRequestHandler { ExecutionFactory executionFactory) { this(metric, executor, queryProfileRegistry, Embedder.throwsOnUse, executionFactory, containerHttpConfig.numQueriesToTraceOnDebugAfterConstruction(), - containerHttpConfig.hostResponseHeaderKey().equals("") ? - Optional.empty() : Optional.of(containerHttpConfig.hostResponseHeaderKey())); + containerHttpConfig.hostResponseHeaderKey().equals("") ? Optional.empty() + : Optional.of(containerHttpConfig.hostResponseHeaderKey()), + ZoneInfo.defaultInfo()); } /** @@ -176,8 +206,9 @@ public class SearchHandler extends LoggingRequestHandler { this(metric, executor, QueryProfileConfigurer.createFromConfig(queryProfileConfig).compile(), Embedder.throwsOnUse, executionFactory, containerHttpConfig.numQueriesToTraceOnDebugAfterConstruction(), - containerHttpConfig.hostResponseHeaderKey().equals("") ? - Optional.empty() : Optional.of( containerHttpConfig.hostResponseHeaderKey())); + containerHttpConfig.hostResponseHeaderKey().equals("") ? Optional.empty() + : Optional.of( containerHttpConfig.hostResponseHeaderKey()), + ZoneInfo.defaultInfo()); } /** @@ -192,7 +223,8 @@ public class SearchHandler extends LoggingRequestHandler { ExecutionFactory executionFactory, Optional<String> hostResponseHeaderKey) { this(metric, executor, queryProfileRegistry, Embedder.throwsOnUse, - executionFactory, 0, hostResponseHeaderKey); + executionFactory, 0, hostResponseHeaderKey, + ZoneInfo.defaultInfo()); } private SearchHandler(Metric metric, @@ -201,7 +233,8 @@ public class SearchHandler extends LoggingRequestHandler { Embedder embedder, ExecutionFactory executionFactory, long numQueriesToTraceOnDebugAfterStartup, - Optional<String> hostResponseHeaderKey) { + Optional<String> hostResponseHeaderKey, + ZoneInfo zoneInfo) { super(executor, metric, true); log.log(Level.FINE, () -> "SearchHandler.init " + System.identityHashCode(this)); this.queryProfileRegistry = queryProfileRegistry; @@ -213,6 +246,7 @@ public class SearchHandler extends LoggingRequestHandler { this.hostResponseHeaderKey = hostResponseHeaderKey; this.numRequestsLeftToTrace = new AtomicLong(numQueriesToTraceOnDebugAfterStartup); metric.set(SEARCH_CONNECTIONS, 0.0d, null); + this.zoneInfo = zoneInfo; } /** @deprecated use the other constructor */ @@ -308,6 +342,7 @@ public class SearchHandler extends LoggingRequestHandler { .setRequestMap(requestMap) .setQueryProfile(queryProfile) .setEmbedder(embedder) + .setZoneInfo(zoneInfo) .build(); boolean benchmarking = VespaHeaders.benchmarkOutput(request); diff --git a/container-search/src/main/java/com/yahoo/search/query/properties/RequestContextProperties.java b/container-search/src/main/java/com/yahoo/search/query/properties/RequestContextProperties.java index 06d827faf68..46aafcb11e3 100644 --- a/container-search/src/main/java/com/yahoo/search/query/properties/RequestContextProperties.java +++ b/container-search/src/main/java/com/yahoo/search/query/properties/RequestContextProperties.java @@ -1,13 +1,17 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.search.query.properties; +import ai.vespa.cloud.ZoneInfo; import com.yahoo.processing.request.CompoundName; import com.yahoo.search.query.Properties; +import java.util.Collections; +import java.util.HashMap; import java.util.Map; /** - * Turns get(name) into get(name, request) using the request given at construction time. + * Turns get(name) into get(name, context) using the request given at construction time + * and the zone info. * This is used to allow the query's request to be supplied to all property requests * without forcing users of the query.properties() to supply this explicitly. * @@ -15,27 +19,36 @@ import java.util.Map; */ public class RequestContextProperties extends Properties { - private final Map<String, String> requestMap; - - public RequestContextProperties(Map<String, String> properties) { - this.requestMap = properties; + private final Map<String, String> context; + + public RequestContextProperties(Map<String, String> properties, ZoneInfo zoneInfo) { + if (zoneInfo == ZoneInfo.defaultInfo()) { + context = properties; + } + else { + Map<String, String> context = new HashMap<>(properties); + context.putIfAbsent("environment", zoneInfo.zone().environment().name()); + context.putIfAbsent("region", zoneInfo.zone().region()); + context.putIfAbsent("instance", zoneInfo.application().instance()); + this.context = Collections.unmodifiableMap(context); + } } @Override public Object get(CompoundName name, Map<String,String> context, com.yahoo.processing.request.Properties substitution) { - return super.get(name, context == null ? requestMap : context, substitution); + return super.get(name, context == null ? this.context : context, substitution); } @Override public void set(CompoundName name, Object value, Map<String,String> context) { - super.set(name, value, context == null ? requestMap : context); + super.set(name, value, context == null ? this.context : context); } @Override public Map<String, Object> listProperties(CompoundName path, Map<String,String> context, com.yahoo.processing.request.Properties substitution) { - return super.listProperties(path, context == null ? requestMap : context, substitution); + return super.listProperties(path, context == null ? this.context : context, substitution); } } |