Skip to content

Commit

Permalink
Moved took time logic from QSR to IndicesService
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi committed Jan 11, 2024
1 parent 564351c commit 3466ae0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1697,8 +1697,9 @@ public void loadIntoContext(ShardSearchRequest request, SearchContext context, Q

boolean[] loadedFromCache = new boolean[] { true };
BytesReference bytesReference = cacheShardLevelResult(context.indexShard(), directoryReader, request.cacheKey(), out -> {
long beforeQueryPhase = System.nanoTime();
queryPhase.execute(context);
CachePolicyInfoWrapper policyInfo = new CachePolicyInfoWrapper(context.queryResult().getTookTimeNanos());
CachePolicyInfoWrapper policyInfo = new CachePolicyInfoWrapper(System.nanoTime() - beforeQueryPhase);
policyInfo.writeTo(out);
// Write relevant info for cache tier policies before the whole QuerySearchResult, so we don't have to read
// the whole QSR into memory when we decide whether to allow it into a particular cache tier based on took time/other info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ private DiskTierTookTimePolicy getTookTimePolicy() {
return new DiskTierTookTimePolicy(dummySettings, dummyClusterSettings, transformationFunction);
}

public void testQSRSetupFunction() throws IOException {
Long ttn = 100000000000L;
QuerySearchResult qsr = getQSR(ttn);
assertEquals(ttn, qsr.getTookTimeNanos());
}

public void testTookTimePolicy() throws Exception {
DiskTierTookTimePolicy tookTimePolicy = getTookTimePolicy();

Expand All @@ -73,8 +67,8 @@ public void testTookTimePolicy() throws Exception {
long shortMillis = (long) (0.9 * threshMillis);
long longMillis = (long) (1.5 * threshMillis);
tookTimePolicy.setThreshold(new TimeValue((long) threshMillis));
BytesReference shortTime = getValidPolicyInput(getQSR(shortMillis * 1000000));
BytesReference longTime = getValidPolicyInput(getQSR(longMillis * 1000000));
BytesReference shortTime = getValidPolicyInput(getQSR(), shortMillis * 1000000);
BytesReference longTime = getValidPolicyInput(getQSR(), longMillis * 1000000);

boolean shortResult = tookTimePolicy.checkData(shortTime);
assertFalse(shortResult);
Expand All @@ -89,7 +83,7 @@ public void testTookTimePolicy() throws Exception {
assertTrue(longResult);
}

public static QuerySearchResult getQSR(long tookTimeNanos) {
public static QuerySearchResult getQSR() {
// package-private, also used by IndicesRequestCacheTests.java
// setup from QuerySearchResultTests.java
ShardId shardId = new ShardId("index", "uuid", randomInt());
Expand All @@ -114,14 +108,13 @@ public static QuerySearchResult getQSR(long tookTimeNanos) {
TopDocs topDocs = new TopDocs(new TotalHits(randomLongBetween(0, Long.MAX_VALUE), TotalHits.Relation.EQUAL_TO), new ScoreDoc[0]);
result.topDocs(new TopDocsAndMaxScore(topDocs, randomBoolean() ? Float.NaN : randomFloat()), new DocValueFormat[0]);

result.setTookTimeNanos(tookTimeNanos);
return result;
}

private BytesReference getValidPolicyInput(QuerySearchResult qsr) throws IOException {
private BytesReference getValidPolicyInput(QuerySearchResult qsr, long tookTimeNanos) throws IOException {
// When it's used in the cache, the policy will receive BytesReferences which have a CachePolicyInfoWrapper
// at the beginning of them, followed by the actual QSR.
CachePolicyInfoWrapper policyInfo = new CachePolicyInfoWrapper(qsr.getTookTimeNanos());
CachePolicyInfoWrapper policyInfo = new CachePolicyInfoWrapper(tookTimeNanos);
BytesStreamOutput out = new BytesStreamOutput();
policyInfo.writeTo(out);
qsr.writeTo(out);
Expand Down

0 comments on commit 3466ae0

Please sign in to comment.