From dfd9128f331bafb58a264e3c4f5bfc6c8fa2f0c5 Mon Sep 17 00:00:00 2001 From: Peter Alfonsi Date: Wed, 18 Oct 2023 17:48:44 -0700 Subject: [PATCH] Addressed Ankit's comments Signed-off-by: Peter Alfonsi --- .../org/opensearch/search/query/QueryPhase.java | 2 +- .../opensearch/search/query/QuerySearchResult.java | 14 +++++++++++--- .../opensearch/search/query/QueryPhaseTests.java | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/org/opensearch/search/query/QueryPhase.java b/server/src/main/java/org/opensearch/search/query/QueryPhase.java index 14b239d3270d4..e6c281fdf74f4 100644 --- a/server/src/main/java/org/opensearch/search/query/QueryPhase.java +++ b/server/src/main/java/org/opensearch/search/query/QueryPhase.java @@ -131,7 +131,7 @@ public void preProcess(SearchContext context) { } public void execute(SearchContext searchContext) throws QueryPhaseExecutionException { - long startTime = System.nanoTime(); + final long startTime = System.nanoTime(); if (searchContext.hasOnlySuggest()) { suggestProcessor.process(searchContext); searchContext.queryResult() diff --git a/server/src/main/java/org/opensearch/search/query/QuerySearchResult.java b/server/src/main/java/org/opensearch/search/query/QuerySearchResult.java index 5a8cf06bd8b0a..bb484214325dd 100644 --- a/server/src/main/java/org/opensearch/search/query/QuerySearchResult.java +++ b/server/src/main/java/org/opensearch/search/query/QuerySearchResult.java @@ -34,6 +34,7 @@ import org.apache.lucene.search.FieldDoc; import org.apache.lucene.search.TotalHits; +import org.opensearch.Version; import org.opensearch.common.io.stream.DelayableWriteable; import org.opensearch.common.lucene.search.TopDocsAndMaxScore; import org.opensearch.core.common.io.stream.StreamInput; @@ -88,6 +89,7 @@ public final class QuerySearchResult extends SearchPhaseResult { private final boolean isNull; private long tookTimeNanos; + private final static Version minimumTookTimeVersion = Version.V_3_0_0; public QuerySearchResult() { this(false); @@ -365,7 +367,11 @@ public void readFromWithId(ShardSearchContextId id, StreamInput in) throws IOExc nodeQueueSize = in.readInt(); setShardSearchRequest(in.readOptionalWriteable(ShardSearchRequest::new)); setRescoreDocIds(new RescoreDocIds(in)); - tookTimeNanos = in.readVLong(); + if (in.getVersion().onOrAfter(minimumTookTimeVersion)) { + tookTimeNanos = in.readVLong(); + } else { + tookTimeNanos = -1L; + } } @Override @@ -408,7 +414,9 @@ public void writeToNoId(StreamOutput out) throws IOException { out.writeInt(nodeQueueSize); out.writeOptionalWriteable(getShardSearchRequest()); getRescoreDocIds().writeTo(out); - out.writeVLong(tookTimeNanos); // VLong as took time should always be positive + if (out.getVersion().onOrAfter(minimumTookTimeVersion)) { + out.writeVLong(tookTimeNanos); // VLong as took time should always be positive + } } public TotalHits getTotalHits() { @@ -423,7 +431,7 @@ public long getTookTimeNanos() { return tookTimeNanos; } - public void setTookTimeNanos(long tookTime) { + void setTookTimeNanos(long tookTime) { tookTimeNanos = tookTime; } } diff --git a/server/src/test/java/org/opensearch/search/query/QueryPhaseTests.java b/server/src/test/java/org/opensearch/search/query/QueryPhaseTests.java index 93c54f93801ab..7aa37a4cbe342 100644 --- a/server/src/test/java/org/opensearch/search/query/QueryPhaseTests.java +++ b/server/src/test/java/org/opensearch/search/query/QueryPhaseTests.java @@ -1154,7 +1154,7 @@ public void testQueryTimeoutChecker() throws Exception { } public void testQuerySearchResultTookTime() throws IOException { - int sleepMillis = randomIntBetween(500, 4000); // between 0.5 and 4 sec + int sleepMillis = randomIntBetween(10, 100); // between 0.01 and 0.1 sec DelayedQueryPhaseSearcher delayedQueryPhaseSearcher = new DelayedQueryPhaseSearcher(sleepMillis); // we need to test queryPhase.execute(), not executeInternal(), since that's what the timer wraps around