Skip to content

Commit

Permalink
AbstractSearchableSnapshotsRestTestCase.testClearCache() should wait …
Browse files Browse the repository at this point in the history
…for cache writes to complete (#70547)

The test AbstractSearchableSnapshotsRestTestCase.testClearCache() 
sometimes fails because it assumes that all cache writes are completed 
when retrieving and comparing the searchable snapshots stat 
cached_bytes_written.

This commit changes the test so that it now waits for searchable 
snapshots thread pools to finish to process cache related tasks 
before retrieving the stats.

Backport of #70541 for 7.12
  • Loading branch information
tlrx committed Mar 18, 2021
1 parent 17bece7 commit dbbd001
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.notNullValue;

public abstract class AbstractSearchableSnapshotsRestTestCase extends ESRestTestCase {

Expand Down Expand Up @@ -212,6 +215,8 @@ public void testClearCache() throws Exception {
Map<String, Object> searchResults = search(restoredIndexName, QueryBuilders.matchAllQuery(), Boolean.TRUE);
assertThat(extractValue(searchResults, "hits.total.value"), equalTo(numDocs));

waitForIdlingSearchableSnapshotsThreadPools();

final long bytesInCacheBeforeClear = sumCachedBytesWritten.apply(searchableSnapshotStats(restoredIndexName));
assertThat(bytesInCacheBeforeClear, greaterThan(0L));

Expand All @@ -223,6 +228,8 @@ public void testClearCache() throws Exception {
searchResults = search(restoredIndexName, QueryBuilders.matchAllQuery(), Boolean.TRUE);
assertThat(extractValue(searchResults, "hits.total.value"), equalTo(numDocs));

waitForIdlingSearchableSnapshotsThreadPools();

assertBusy(() -> {
final long bytesInCacheAfterSearch = sumCachedBytesWritten.apply(searchableSnapshotStats(restoredIndexName));
assertThat(bytesInCacheAfterSearch, greaterThan(bytesInCacheBeforeClear));
Expand Down Expand Up @@ -459,6 +466,36 @@ protected static Map<String, Object> indexSettings(String index) throws IOExcept
return extractValue(responseAsMap(response), index + ".settings");
}

@SuppressWarnings("unchecked")
protected static void waitForIdlingSearchableSnapshotsThreadPools() throws Exception {
final Set<String> searchableSnapshotsThreadPools = org.elasticsearch.common.collect.Set.of(
SearchableSnapshotsConstants.CACHE_FETCH_ASYNC_THREAD_POOL_NAME,
SearchableSnapshotsConstants.CACHE_PREWARMING_THREAD_POOL_NAME
);
assertBusy(() -> {
final Response response = client().performRequest(new Request(HttpGet.METHOD_NAME, "/_nodes/stats/thread_pool"));
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));

final Map<String, Object> nodes = extractValue(responseAsMap(response), "nodes");
assertThat(nodes, notNullValue());

for (String node : nodes.keySet()) {
final Map<String, Object> threadPools = extractValue((Map<String, Object>) nodes.get(node), "thread_pool");
searchableSnapshotsThreadPools.forEach(threadPoolName -> {
final Map<String, Object> threadPoolStats = (Map<String, Object>) threadPools.get(threadPoolName);
assertThat(threadPoolStats, notNullValue());

final Number active = extractValue(threadPoolStats, "active");
assertThat(threadPoolName + " has still active tasks", active.longValue(), equalTo(0L));

final Number queue = extractValue(threadPoolStats, "queue");
assertThat(threadPoolName + " has still enqueued tasks", queue.longValue(), equalTo(0L));

});
}
}, 30L, TimeUnit.SECONDS);
}

@SuppressWarnings("unchecked")
protected static <T> T extractValue(Map<String, Object> map, String path) {
return (T) XContentMapValues.extractValue(path, map);
Expand Down

0 comments on commit dbbd001

Please sign in to comment.