-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Allow freezing searchable snapshots #52653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
2ca67a8
2774b7f
206856a
8e0a73a
3f0cca1
9e3d4bf
b10dd74
bc85c25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,7 @@ public abstract class AbstractSearchableSnapshotsRestTestCase extends ESRestTest | |
|
|
||
| protected abstract Settings repositorySettings(); | ||
|
|
||
| public void testSearchableSnapshots() throws Exception { | ||
| private void runSearchableSnapshotsTest(SearchableSnapshotsTestCaseBody testCaseBody) throws Exception { | ||
| final String repositoryType = repositoryType(); | ||
| final Settings repositorySettings = repositorySettings(); | ||
|
|
||
|
|
@@ -117,34 +117,7 @@ public void testSearchableSnapshots() throws Exception { | |
| final Number count = count(restoredIndexName); | ||
| assertThat("Wrong index count for index " + restoredIndexName, count.intValue(), equalTo(numDocs)); | ||
|
|
||
| for (int i = 0; i < 10; i++) { | ||
| final int randomTieBreaker = randomIntBetween(1, numDocs - 1); | ||
| Map<String, Object> searchResults; | ||
| switch (randomInt(3)) { | ||
| case 0: | ||
| searchResults = search(restoredIndexName, QueryBuilders.termQuery("field", String.valueOf(randomTieBreaker))); | ||
| assertThat(extractValue(searchResults, "hits.total.value"), equalTo(1)); | ||
| @SuppressWarnings("unchecked") | ||
| Map<String, Object> searchHit = (Map<String, Object>) ((List<?>) extractValue(searchResults, "hits.hits")).get(0); | ||
| assertThat(extractValue(searchHit, "_index"), equalTo(restoredIndexName)); | ||
| assertThat(extractValue(searchHit, "_source.field"), equalTo(randomTieBreaker)); | ||
| break; | ||
| case 1: | ||
| searchResults = search(restoredIndexName, QueryBuilders.rangeQuery("field").lt(randomTieBreaker)); | ||
| assertThat(extractValue(searchResults, "hits.total.value"), equalTo(randomTieBreaker)); | ||
| break; | ||
| case 2: | ||
| searchResults = search(restoredIndexName, QueryBuilders.rangeQuery("field").gte(randomTieBreaker)); | ||
| assertThat(extractValue(searchResults, "hits.total.value"), equalTo(numDocs - randomTieBreaker)); | ||
| break; | ||
| case 3: | ||
| searchResults = search(restoredIndexName, QueryBuilders.matchQuery("text", "document")); | ||
| assertThat(extractValue(searchResults, "hits.total.value"), equalTo(numDocs)); | ||
| break; | ||
| default: | ||
| fail("Unsupported randomized search query"); | ||
| } | ||
| } | ||
| testCaseBody.runTest(restoredIndexName, numDocs); | ||
|
|
||
| logger.info("deleting snapshot [{}]", snapshot); | ||
| deleteSnapshot(repository, snapshot, false); | ||
|
|
@@ -154,6 +127,71 @@ public void testSearchableSnapshots() throws Exception { | |
| searchableSnapshotStats.size(), equalTo(numberOfShards)); | ||
| } | ||
|
|
||
| public void testSearchResults() throws Exception { | ||
| runSearchableSnapshotsTest((restoredIndexName, numDocs) -> { | ||
| for (int i = 0; i < 10; i++) { | ||
| assertSearchResults(restoredIndexName, numDocs, randomFrom(Boolean.TRUE, Boolean.FALSE, null)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void testSearchResultsWhenFrozen() throws Exception { | ||
| runSearchableSnapshotsTest((restoredIndexName, numDocs) -> { | ||
| final Request freezeRequest = new Request(HttpPost.METHOD_NAME, restoredIndexName + "/_freeze"); | ||
| client().performRequest(freezeRequest); | ||
|
||
| ensureGreen(restoredIndexName); | ||
| for (int i = 0; i < 10; i++) { | ||
| assertSearchResults(restoredIndexName, numDocs, Boolean.FALSE); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void testCloseAndReopen() throws Exception { | ||
| runSearchableSnapshotsTest((restoredIndexName, numDocs) -> { | ||
| final Request closeRequest = new Request(HttpPost.METHOD_NAME, restoredIndexName + "/_close"); | ||
| client().performRequest(closeRequest); | ||
|
||
| ensureGreen(restoredIndexName); | ||
|
|
||
| final Request openRequest = new Request(HttpPost.METHOD_NAME, restoredIndexName + "/_open"); | ||
| client().performRequest(openRequest); | ||
|
||
| ensureGreen(restoredIndexName); | ||
|
|
||
| for (int i = 0; i < 10; i++) { | ||
| assertSearchResults(restoredIndexName, numDocs, randomFrom(Boolean.TRUE, Boolean.FALSE, null)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void assertSearchResults(String indexName, int numDocs, Boolean ignoreThrottled) throws IOException { | ||
| final int randomTieBreaker = randomIntBetween(1, numDocs - 1); | ||
| Map<String, Object> searchResults; | ||
| switch (randomInt(3)) { | ||
| case 0: | ||
| searchResults | ||
| = search(indexName, QueryBuilders.termQuery("field", String.valueOf(randomTieBreaker)), ignoreThrottled); | ||
| assertThat(extractValue(searchResults, "hits.total.value"), equalTo(1)); | ||
| @SuppressWarnings("unchecked") | ||
| Map<String, Object> searchHit = (Map<String, Object>) ((List<?>) extractValue(searchResults, "hits.hits")).get(0); | ||
| assertThat(extractValue(searchHit, "_index"), equalTo(indexName)); | ||
| assertThat(extractValue(searchHit, "_source.field"), equalTo(randomTieBreaker)); | ||
| break; | ||
| case 1: | ||
| searchResults = search(indexName, QueryBuilders.rangeQuery("field").lt(randomTieBreaker), ignoreThrottled); | ||
| assertThat(extractValue(searchResults, "hits.total.value"), equalTo(randomTieBreaker)); | ||
| break; | ||
| case 2: | ||
| searchResults = search(indexName, QueryBuilders.rangeQuery("field").gte(randomTieBreaker), ignoreThrottled); | ||
| assertThat(extractValue(searchResults, "hits.total.value"), equalTo(numDocs - randomTieBreaker)); | ||
| break; | ||
| case 3: | ||
| searchResults = search(indexName, QueryBuilders.matchQuery("text", "document"), ignoreThrottled); | ||
| assertThat(extractValue(searchResults, "hits.total.value"), equalTo(numDocs)); | ||
| break; | ||
| default: | ||
| fail("Unsupported randomized search query"); | ||
| } | ||
| } | ||
|
|
||
| protected static void registerRepository(String repository, String type, boolean verify, Settings settings) throws IOException { | ||
| final Request request = new Request(HttpPut.METHOD_NAME, "_snapshot/" + repository); | ||
| request.setJsonEntity(Strings.toString(new PutRepositoryRequest(repository).type(type).verify(verify).settings(settings))); | ||
|
|
@@ -218,9 +256,12 @@ protected static Number count(String index) throws IOException { | |
| return (Number) extractValue(responseAsMap, "count"); | ||
| } | ||
|
|
||
| protected static Map<String, Object> search(String index, QueryBuilder query) throws IOException { | ||
| protected static Map<String, Object> search(String index, QueryBuilder query, Boolean ignoreThrottled) throws IOException { | ||
| final Request request = new Request(HttpPost.METHOD_NAME, '/' + index + "/_search"); | ||
| request.setJsonEntity(new SearchSourceBuilder().trackTotalHits(true).query(query).toString()); | ||
| if (ignoreThrottled != null) { | ||
| request.addParameter("ignore_throttled", ignoreThrottled.toString()); | ||
| } | ||
|
|
||
| final Response response = client().performRequest(request); | ||
| assertThat("Failed to execute search request on index [" + index + "]: " + response, | ||
|
|
@@ -260,4 +301,12 @@ protected static Map<String, Object> responseAsMap(Response response) throws IOE | |
| protected static <T> T extractValue(Map<String, Object> map, String path) { | ||
| return (T) XContentMapValues.extractValue(path, map); | ||
| } | ||
|
|
||
| /** | ||
| * The body of a test case, which runs after the searchable snapshot has been created and restored. | ||
| */ | ||
| @FunctionalInterface | ||
| interface SearchableSnapshotsTestCaseBody { | ||
| void runTest(String indexName, int numDocs) throws IOException; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about introducing this string literal here, vs adding a dependency on the
frozen-indicesmodule and referring to the setting directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to avoid the dependency and just use the String literal (we have other such cases in our code base).
I also wonder if we need to handle closed replicated snapshot indices (i.e. use
NoOpEnginefor those).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already bypass this mechanism for closed indices:
elasticsearch/server/src/main/java/org/elasticsearch/indices/IndicesService.java
Lines 622 to 627 in 0d1e67d
I added a test case showing that closing and reopening a searchable snapshot index does work.