Skip to content

Commit 7ac1b80

Browse files
liuguoqingfzlawofcycles
authored andcommitted
Fix a flaky test that force caching (#19761)
Signed-off-by: Joe Liu <[email protected]>
1 parent 8caa8fa commit 7ac1b80

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

plugins/cache-ehcache/src/internalClusterTest/java/org/opensearch/cache/EhcacheDiskCacheIT.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest;
1818
import org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse;
1919
import org.opensearch.action.admin.indices.forcemerge.ForceMergeResponse;
20+
import org.opensearch.action.search.SearchRequestBuilder;
2021
import org.opensearch.action.search.SearchResponse;
2122
import org.opensearch.action.search.SearchType;
2223
import org.opensearch.cache.store.disk.EhcacheDiskCache;
@@ -126,7 +127,7 @@ public void testPluginsAreInstalled() {
126127
);
127128
}
128129

129-
public void testSanityChecksWithIndicesRequestCache() throws InterruptedException {
130+
public void testSanityChecksWithIndicesRequestCache() throws Exception {
130131
internalCluster().startNode(Settings.builder().put(defaultSettings(DEFAULT_CACHE_SIZE_IN_BYTES, null)).build());
131132
Client client = client();
132133
assertAcked(
@@ -150,26 +151,34 @@ public void testSanityChecksWithIndicesRequestCache() throws InterruptedExceptio
150151
);
151152
ensureSearchable("index");
152153

153-
// This is not a random example: serialization with time zones writes shared strings
154-
// which used to not work well with the query cache because of the handles stream output
155-
// see #9500
156-
final SearchResponse r1 = client.prepareSearch("index")
154+
// build a cacheable search (size=0) and force request-level caching
155+
final SearchRequestBuilder srb = client.prepareSearch("index")
157156
.setSize(0)
158157
.setSearchType(SearchType.QUERY_THEN_FETCH)
158+
.setRequestCache(true) // ensure it goes through the indices request cache
159159
.addAggregation(
160160
dateHistogram("histo").field("f")
161161
.timeZone(ZoneId.of("+01:00"))
162162
.minDocCount(0)
163163
.dateHistogramInterval(DateHistogramInterval.MONTH)
164-
)
165-
.get();
166-
assertSearchResponse(r1);
164+
);
167165

168-
// The cached is actually used
169-
assertThat(
170-
client.admin().indices().prepareStats("index").setRequestCache(true).get().getTotal().getRequestCache().getMemorySizeInBytes(),
171-
greaterThan(0L)
172-
);
166+
// warm cache (run the exact same request twice)
167+
assertSearchResponse(srb.get());
168+
assertSearchResponse(srb.get());
169+
170+
// stats may lag slightly; wait until cache shows non-zero memory usage
171+
assertBusy(() -> {
172+
long bytes = client.admin()
173+
.indices()
174+
.prepareStats("index")
175+
.setRequestCache(true)
176+
.get()
177+
.getTotal()
178+
.getRequestCache()
179+
.getMemorySizeInBytes();
180+
assertThat(bytes, greaterThan(0L));
181+
});
173182
}
174183

175184
public void testInvalidationWithIndicesRequestCache() throws Exception {

0 commit comments

Comments
 (0)