From d49f3306d358284cc734db7d6bae7f9392aa692a Mon Sep 17 00:00:00 2001 From: Poojita Raj Date: Thu, 13 Apr 2023 16:56:57 -0700 Subject: [PATCH] fix failing test suites (#114) Signed-off-by: Poojita Raj --- .../action/termvectors/GetTermVectorsIT.java | 114 +++++++++------- .../org/opensearch/blocks/SimpleBlocksIT.java | 6 +- .../search/suggest/SuggestSearchIT.java | 127 ++++++++++-------- .../snapshots/BlobStoreIncrementalityIT.java | 17 ++- .../snapshots/RestoreSnapshotIT.java | 9 +- .../java/org/opensearch/update/UpdateIT.java | 116 +++++++++------- .../versioning/SimpleVersioningIT.java | 14 +- .../AbstractSnapshotIntegTestCase.java | 7 +- 8 files changed, 239 insertions(+), 171 deletions(-) diff --git a/server/src/internalClusterTest/java/org/opensearch/action/termvectors/GetTermVectorsIT.java b/server/src/internalClusterTest/java/org/opensearch/action/termvectors/GetTermVectorsIT.java index 20fcfad883d58..f7256fb6a828b 100644 --- a/server/src/internalClusterTest/java/org/opensearch/action/termvectors/GetTermVectorsIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/action/termvectors/GetTermVectorsIT.java @@ -152,11 +152,13 @@ public void testExistingFieldButNotInDocNPE() throws Exception { ); // lets see if the null term vectors are caught... - TermVectorsResponse actionGet = termVectors.actionGet(); - assertThat(actionGet, notNullValue()); - assertThat(actionGet.isExists(), equalTo(true)); - assertThat(actionGet.getIndex(), equalTo("test")); - assertThat(actionGet.getFields().terms("existingfield"), nullValue()); + assertBusy(() -> { + TermVectorsResponse actionGet = termVectors.actionGet(); + assertThat(actionGet, notNullValue()); + assertThat(actionGet.isExists(), equalTo(true)); + assertThat(actionGet.getIndex(), equalTo("test")); + assertThat(actionGet.getFields().terms("existingfield"), nullValue()); + }); } public void testNotIndexedField() throws Exception { @@ -546,7 +548,7 @@ private void checkBrownFoxTermVector(Fields fields, String fieldName, boolean wi assertThat(iterator.next(), nullValue()); } - public void testDuelWithAndWithoutTermVectors() throws IOException, ExecutionException, InterruptedException { + public void testDuelWithAndWithoutTermVectors() throws Exception { // setup indices String[] indexNames = new String[] { "with_tv", "without_tv" }; assertAcked(prepareCreate(indexNames[0]).setMapping("field1", "type=text,term_vector=with_positions_offsets,analyzer=keyword")); @@ -572,19 +574,21 @@ public void testDuelWithAndWithoutTermVectors() throws IOException, ExecutionExc indexRandom(true, indexBuilders); // request tvs and compare from each index - for (int id = 0; id < content.length; id++) { - Fields[] fields = new Fields[2]; - for (int j = 0; j < indexNames.length; j++) { - TermVectorsResponse resp = client().prepareTermVectors(indexNames[j], String.valueOf(id)) - .setOffsets(true) - .setPositions(true) - .setSelectedFields("field1") - .get(); - assertThat("doc with index: " + indexNames[j] + ", type1 and id: " + id, resp.isExists(), equalTo(true)); - fields[j] = resp.getFields(); + assertBusy(() -> { + for (int id = 0; id < content.length; id++) { + Fields[] fields = new Fields[2]; + for (int j = 0; j < indexNames.length; j++) { + TermVectorsResponse resp = client().prepareTermVectors(indexNames[j], String.valueOf(id)) + .setOffsets(true) + .setPositions(true) + .setSelectedFields("field1") + .get(); + assertThat("doc with index: " + indexNames[j] + ", type1 and id: " + id, resp.isExists(), equalTo(true)); + fields[j] = resp.getFields(); + } + compareTermVectors("field1", fields[0], fields[1]); } - compareTermVectors("field1", fields[0], fields[1]); - } + }); } private void compareTermVectors(String fieldName, Fields fields0, Fields fields1) throws IOException { @@ -628,7 +632,7 @@ private void compareTermVectors(String fieldName, Fields fields0, Fields fields1 assertThat(iter1.next(), nullValue()); } - public void testSimpleWildCards() throws IOException { + public void testSimpleWildCards() throws Exception { int numFields = 25; XContentBuilder mapping = jsonBuilder().startObject().startObject("properties"); @@ -646,13 +650,15 @@ public void testSimpleWildCards() throws IOException { client().prepareIndex("test").setId("0").setSource(source).get(); refresh(); - TermVectorsResponse response = client().prepareTermVectors(indexOrAlias(), "0").setSelectedFields("field*").get(); - assertThat("Doc doesn't exists but should", response.isExists(), equalTo(true)); - assertThat(response.getIndex(), equalTo("test")); - assertThat("All term vectors should have been generated", response.getFields().size(), equalTo(numFields)); + assertBusy(() -> { + TermVectorsResponse response = client().prepareTermVectors(indexOrAlias(), "0").setSelectedFields("field*").get(); + assertThat("Doc doesn't exists but should", response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); + assertThat("All term vectors should have been generated", response.getFields().size(), equalTo(numFields)); + }); } - public void testArtificialVsExisting() throws ExecutionException, InterruptedException, IOException { + public void testArtificialVsExisting() throws Exception { // setup indices Settings.Builder settings = Settings.builder().put(indexSettings()).put("index.analysis.analyzer", "standard"); assertAcked(prepareCreate("test").setSettings(settings).setMapping("field1", "type=text,term_vector=with_positions_offsets")); @@ -671,31 +677,33 @@ public void testArtificialVsExisting() throws ExecutionException, InterruptedExc } indexRandom(true, indexBuilders); - for (int i = 0; i < content.length; i++) { - // request tvs from existing document - TermVectorsResponse respExisting = client().prepareTermVectors("test", String.valueOf(i)) - .setOffsets(true) - .setPositions(true) - .setFieldStatistics(true) - .setTermStatistics(true) - .get(); - assertThat("doc with index: test, type1 and id: existing", respExisting.isExists(), equalTo(true)); + assertBusy(() -> { + for (int i = 0; i < content.length; i++) { + // request tvs from existing document + TermVectorsResponse respExisting = client().prepareTermVectors("test", String.valueOf(i)) + .setOffsets(true) + .setPositions(true) + .setFieldStatistics(true) + .setTermStatistics(true) + .get(); - // request tvs from artificial document - TermVectorsResponse respArtificial = client().prepareTermVectors() - .setIndex("test") - .setRouting(String.valueOf(i)) // ensure we get the stats from the same shard as existing doc - .setDoc(jsonBuilder().startObject().field("field1", content[i]).endObject()) - .setOffsets(true) - .setPositions(true) - .setFieldStatistics(true) - .setTermStatistics(true) - .get(); - assertThat("doc with index: test, type1 and id: " + String.valueOf(i), respArtificial.isExists(), equalTo(true)); + assertThat("doc with index: test, type1 and id: existing", respExisting.isExists(), equalTo(true)); + // request tvs from artificial document + TermVectorsResponse respArtificial = client().prepareTermVectors() + .setIndex("test") + .setRouting(String.valueOf(i)) // ensure we get the stats from the same shard as existing doc + .setDoc(jsonBuilder().startObject().field("field1", content[i]).endObject()) + .setOffsets(true) + .setPositions(true) + .setFieldStatistics(true) + .setTermStatistics(true) + .get(); + assertThat("doc with index: test, type1 and id: " + String.valueOf(i), respArtificial.isExists(), equalTo(true)); - // compare existing tvs with artificial - compareTermVectors("field1", respExisting.getFields(), respArtificial.getFields()); - } + // compare existing tvs with artificial + compareTermVectors("field1", respExisting.getFields(), respArtificial.getFields()); + } + }); } public void testArtificialNoDoc() throws IOException { @@ -821,7 +829,7 @@ private static String indexOrAlias() { return randomBoolean() ? "test" : "alias"; } - public void testTermVectorsWithVersion() { + public void testTermVectorsWithVersion() throws Exception { assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(Settings.builder().put("index.refresh_interval", -1))); ensureGreen(); @@ -834,10 +842,12 @@ public void testTermVectorsWithVersion() { // From translog: // version 0 means ignore version, which is the default - response = client().prepareTermVectors(indexOrAlias(), "1").setVersion(Versions.MATCH_ANY).get(); - assertThat(response.isExists(), equalTo(true)); - assertThat(response.getId(), equalTo("1")); - assertThat(response.getVersion(), equalTo(1L)); + assertBusy(() -> { + TermVectorsResponse finalResponse = client().prepareTermVectors(indexOrAlias(), "1").setVersion(Versions.MATCH_ANY).get(); + assertThat(finalResponse.isExists(), equalTo(true)); + assertThat(finalResponse.getId(), equalTo("1")); + assertThat(finalResponse.getVersion(), equalTo(1L)); + }); response = client().prepareTermVectors(indexOrAlias(), "1").setVersion(1).get(); assertThat(response.isExists(), equalTo(true)); diff --git a/server/src/internalClusterTest/java/org/opensearch/blocks/SimpleBlocksIT.java b/server/src/internalClusterTest/java/org/opensearch/blocks/SimpleBlocksIT.java index 1a32915dc0e55..6e1da62d94e3c 100644 --- a/server/src/internalClusterTest/java/org/opensearch/blocks/SimpleBlocksIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/blocks/SimpleBlocksIT.java @@ -318,8 +318,10 @@ public void testAddIndexBlock() throws Exception { disableIndexBlock(indexName, block); } - client().admin().indices().prepareRefresh(indexName).get(); - assertHitCount(client().prepareSearch(indexName).setSize(0).get(), nbDocs); + assertBusy(() -> { + client().admin().indices().prepareRefresh(indexName).get(); + assertHitCount(client().prepareSearch(indexName).setSize(0).get(), nbDocs); + }); } public void testSameBlockTwice() throws Exception { diff --git a/server/src/internalClusterTest/java/org/opensearch/search/suggest/SuggestSearchIT.java b/server/src/internalClusterTest/java/org/opensearch/search/suggest/SuggestSearchIT.java index 31813cd7a8358..8b1cba6a27099 100644 --- a/server/src/internalClusterTest/java/org/opensearch/search/suggest/SuggestSearchIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/search/suggest/SuggestSearchIT.java @@ -59,7 +59,6 @@ import org.opensearch.test.hamcrest.OpenSearchAssertions; import java.io.IOException; -import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -358,13 +357,14 @@ public void testSimple() throws Exception { TermSuggestionBuilder termSuggest = termSuggestion("text").suggestMode(SuggestMode.ALWAYS) // Always, otherwise the results can vary // between requests. .text("abcd"); + assertBusy(() -> { + Suggest suggest = searchSuggest("test", termSuggest); + assertSuggestion(suggest, 0, "test", "aacd", "abbd", "abcc"); + assertThat(suggest.getSuggestion("test").getEntries().get(0).getText().string(), equalTo("abcd")); + }); Suggest suggest = searchSuggest("test", termSuggest); assertSuggestion(suggest, 0, "test", "aacd", "abbd", "abcc"); assertThat(suggest.getSuggestion("test").getEntries().get(0).getText().string(), equalTo("abcd")); - - suggest = searchSuggest("test", termSuggest); - assertSuggestion(suggest, 0, "test", "aacd", "abbd", "abcc"); - assertThat(suggest.getSuggestion("test").getEntries().get(0).getText().string(), equalTo("abcd")); } public void testEmpty() throws Exception { @@ -474,7 +474,7 @@ public void testSizeAndSort() throws Exception { } refresh(); - Map> suggestions = new HashMap<>(); + final Map> suggestions = new HashMap<>(); suggestions.put("size3SortScoreFirst", termSuggestion("field1").size(3).minDocFreq(0).suggestMode(SuggestMode.ALWAYS)); suggestions.put( "size10SortScoreFirst", @@ -488,27 +488,30 @@ public void testSizeAndSort() throws Exception { "size10SortFrequencyFirst", termSuggestion("field1").size(10).sort(SortBy.FREQUENCY).shardSize(1000).minDocFreq(0).suggestMode(SuggestMode.ALWAYS) ); - Suggest suggest = searchSuggest("prefix_abcd", 0, suggestions); - - // The commented out assertions fail sometimes because suggestions are based off of shard frequencies instead of index frequencies. - assertSuggestion(suggest, 0, "size3SortScoreFirst", "prefix_aacd", "prefix_abcc", "prefix_accd"); - assertSuggestion(suggest, 0, "size10SortScoreFirst", 10, "prefix_aacd", "prefix_abcc", "prefix_accd" /*, "prefix_aaad" */); - assertSuggestion(suggest, 0, "size3SortScoreFirstMaxEdits1", "prefix_aacd", "prefix_abcc", "prefix_accd"); - assertSuggestion( - suggest, - 0, - "size10SortFrequencyFirst", - "prefix_aaad", - "prefix_abbb", - "prefix_aaca", - "prefix_abba", - "prefix_accc", - "prefix_addd", - "prefix_abaa", - "prefix_dbca", - "prefix_cbad", - "prefix_aacd" - ); + assertBusy(() -> { + Suggest suggest = searchSuggest("prefix_abcd", 0, suggestions); + + // The commented out assertions fail sometimes because suggestions are based off of shard frequencies instead of index + // frequencies. + assertSuggestion(suggest, 0, "size3SortScoreFirst", "prefix_aacd", "prefix_abcc", "prefix_accd"); + assertSuggestion(suggest, 0, "size10SortScoreFirst", 10, "prefix_aacd", "prefix_abcc", "prefix_accd" /*, "prefix_aaad" */); + assertSuggestion(suggest, 0, "size3SortScoreFirstMaxEdits1", "prefix_aacd", "prefix_abcc", "prefix_accd"); + assertSuggestion( + suggest, + 0, + "size10SortFrequencyFirst", + "prefix_aaad", + "prefix_abbb", + "prefix_aaca", + "prefix_abba", + "prefix_accc", + "prefix_addd", + "prefix_abaa", + "prefix_dbca", + "prefix_cbad", + "prefix_aacd" + ); + }); // assertThat(suggest.get(3).getSuggestedWords().get("prefix_abcd").get(4).getTerm(), equalTo("prefix_abcc")); // assertThat(suggest.get(3).getSuggestedWords().get("prefix_abcd").get(4).getTerm(), equalTo("prefix_accd")); @@ -591,7 +594,7 @@ public void testPrefixLength() throws IOException { assertSuggestion(searchSuggest, 0, "simple_phrase", "hello world"); } - public void testBasicPhraseSuggest() throws IOException, URISyntaxException { + public void testBasicPhraseSuggest() throws Exception { CreateIndexRequestBuilder builder = prepareCreate("test").setSettings( Settings.builder() .put(indexSettings()) @@ -644,26 +647,31 @@ public void testBasicPhraseSuggest() throws IOException, URISyntaxException { } refresh(); - PhraseSuggestionBuilder phraseSuggest = phraseSuggestion("bigram").gramSize(2) + final PhraseSuggestionBuilder phraseSuggest = phraseSuggestion("bigram").gramSize(2) .analyzer("body") .addCandidateGenerator(candidateGenerator("body").minWordLength(1).suggestMode("always")) .size(1); - Suggest searchSuggest = searchSuggest("Frank's Wise", "simple_phrase", phraseSuggest); - assertSuggestion(searchSuggest, 0, "simple_phrase", "frank's wife"); - assertThat(searchSuggest.getSuggestion("simple_phrase").getEntries().get(0).getText().string(), equalTo("Frank's Wise")); + assertBusy(() -> { + Suggest searchSuggest = searchSuggest("Frank's Wise", "simple_phrase", phraseSuggest); + assertSuggestion(searchSuggest, 0, "simple_phrase", "frank's wife"); + assertThat(searchSuggest.getSuggestion("simple_phrase").getEntries().get(0).getText().string(), equalTo("Frank's Wise")); + }); phraseSuggest.realWordErrorLikelihood(0.95f); - searchSuggest = searchSuggest("Artur, Kinh of the Britons", "simple_phrase", phraseSuggest); - assertSuggestion(searchSuggest, 0, "simple_phrase", "arthur king of the britons"); - // Check the "text" field this one time. - assertThat( - searchSuggest.getSuggestion("simple_phrase").getEntries().get(0).getText().string(), - equalTo("Artur, Kinh of the Britons") - ); + final PhraseSuggestionBuilder finalPhraseSuggest = phraseSuggest; + assertBusy(() -> { + Suggest finalSearchSuggest = searchSuggest("Artur, Kinh of the Britons", "simple_phrase", finalPhraseSuggest); + assertSuggestion(finalSearchSuggest, 0, "simple_phrase", "arthur king of the britons"); + // Check the "text" field this one time. + assertThat( + finalSearchSuggest.getSuggestion("simple_phrase").getEntries().get(0).getText().string(), + equalTo("Artur, Kinh of the Britons") + ); + }); // Ask for highlighting phraseSuggest.highlight("", ""); - searchSuggest = searchSuggest("Artur, King of the Britns", "simple_phrase", phraseSuggest); + Suggest searchSuggest = searchSuggest("Artur, King of the Britns", "simple_phrase", phraseSuggest); assertSuggestion(searchSuggest, 0, "simple_phrase", "arthur king of the britons"); assertThat( searchSuggest.getSuggestion("simple_phrase").getEntries().get(0).getOptions().get(0).getHighlighted().string(), @@ -733,7 +741,7 @@ public void testBasicPhraseSuggest() throws IOException, URISyntaxException { ); } - public void testSizeParam() throws IOException { + public void testSizeParam() throws Exception { CreateIndexRequestBuilder builder = prepareCreate("test").setSettings( Settings.builder() .put(SETTING_NUMBER_OF_SHARDS, 1) @@ -784,8 +792,11 @@ public void testSizeParam() throws IOException { .addCandidateGenerator( candidateGenerator("body").minWordLength(1).prefixLength(1).suggestMode("always").size(2).accuracy(0.1f) ); - searchSuggest = searchSuggest("Xorr the Gut-Jewel", "simple_phrase", phraseSuggestion); - assertSuggestion(searchSuggest, 0, "simple_phrase", "xorr the god jewel"); + final PhraseSuggestionBuilder finalPhraseSuggestion = phraseSuggestion; + assertBusy(() -> { + Suggest finalSearchSuggest = searchSuggest("Xorr the Gut-Jewel", "simple_phrase", finalPhraseSuggestion); + assertSuggestion(finalSearchSuggest, 0, "simple_phrase", "xorr the god jewel"); + }); } public void testDifferentShardSize() throws Exception { @@ -798,12 +809,14 @@ public void testDifferentShardSize() throws Exception { client().prepareIndex("test").setId("3").setSource("field1", "foobar3").setRouting("3") ); - Suggest suggest = searchSuggest( - "foobar", - "simple", - termSuggestion("field1").size(10).minDocFreq(0).suggestMode(SuggestMode.ALWAYS) - ); - OpenSearchAssertions.assertSuggestionSize(suggest, 0, 3, "simple"); + assertBusy(() -> { + Suggest suggest = searchSuggest( + "foobar", + "simple", + termSuggestion("field1").size(10).minDocFreq(0).suggestMode(SuggestMode.ALWAYS) + ); + OpenSearchAssertions.assertSuggestionSize(suggest, 0, 3, "simple"); + }); } // see #3469 @@ -1258,7 +1271,7 @@ public Set> getSupportedContexts() { } } - public void testPhraseSuggesterCollate() throws InterruptedException, ExecutionException, IOException { + public void testPhraseSuggesterCollate() throws Exception { CreateIndexRequestBuilder builder = prepareCreate("test").setSettings( Settings.builder() .put(indexSettings()) @@ -1299,11 +1312,13 @@ public void testPhraseSuggesterCollate() throws InterruptedException, ExecutionE indexRandom(true, builders); // suggest without collate - PhraseSuggestionBuilder suggest = phraseSuggestion("title").addCandidateGenerator( + final PhraseSuggestionBuilder suggest = phraseSuggestion("title").addCandidateGenerator( new DirectCandidateGeneratorBuilder("title").suggestMode("always").maxTermFreq(.99f).size(10).maxInspections(200) ).confidence(0f).maxErrors(2f).shardSize(30000).size(10); - Suggest searchSuggest = searchSuggest("united states house of representatives elections in washington 2006", "title", suggest); - assertSuggestionSize(searchSuggest, 0, 10, "title"); + assertBusy(() -> { + Suggest searchSuggest = searchSuggest("united states house of representatives elections in washington 2006", "title", suggest); + assertSuggestionSize(searchSuggest, 0, 10, "title"); + }); // suggest with collate String filterString = Strings.toString( @@ -1316,7 +1331,11 @@ public void testPhraseSuggesterCollate() throws InterruptedException, ExecutionE ); PhraseSuggestionBuilder filteredQuerySuggest = suggest.collateQuery(filterString); filteredQuerySuggest.collateParams(Collections.singletonMap("field", "title")); - searchSuggest = searchSuggest("united states house of representatives elections in washington 2006", "title", filteredQuerySuggest); + Suggest searchSuggest = searchSuggest( + "united states house of representatives elections in washington 2006", + "title", + filteredQuerySuggest + ); assertSuggestionSize(searchSuggest, 0, 2, "title"); // collate suggest with no result (boundary case) diff --git a/server/src/internalClusterTest/java/org/opensearch/snapshots/BlobStoreIncrementalityIT.java b/server/src/internalClusterTest/java/org/opensearch/snapshots/BlobStoreIncrementalityIT.java index 9a40ea2c95b28..adaf3246783b0 100644 --- a/server/src/internalClusterTest/java/org/opensearch/snapshots/BlobStoreIncrementalityIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/snapshots/BlobStoreIncrementalityIT.java @@ -32,6 +32,7 @@ package org.opensearch.snapshots; +import org.opensearch.OpenSearchException; import org.opensearch.action.DocWriteResponse; import org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; import org.opensearch.action.admin.cluster.snapshots.status.SnapshotStats; @@ -203,9 +204,19 @@ private void assertTwoIdenticalShardSnapshots(String repo, String indexName, Str final SnapshotStats firstSnapshotShardStatus = getStats(repo, snapshot1).getIndices().get(indexName).getShards().get(0).getStats(); final int totalFilesInShard = firstSnapshotShardStatus.getTotalFileCount(); assertThat(totalFilesInShard, greaterThan(0)); - final SnapshotStats secondSnapshotShardStatus = getStats(repo, snapshot2).getIndices().get(indexName).getShards().get(0).getStats(); - assertThat(secondSnapshotShardStatus.getTotalFileCount(), is(totalFilesInShard)); - assertThat(secondSnapshotShardStatus.getIncrementalFileCount(), is(0)); + try { + assertBusy(() -> { + final SnapshotStats secondSnapshotShardStatus = getStats(repo, snapshot2).getIndices() + .get(indexName) + .getShards() + .get(0) + .getStats(); + assertThat(secondSnapshotShardStatus.getTotalFileCount(), is(totalFilesInShard)); + assertThat(secondSnapshotShardStatus.getIncrementalFileCount(), is(0)); + }); + } catch (Exception e) { + throw new OpenSearchException("assertTwoIdenticalShardSnapshots failed", e); + } } private SnapshotStatus getStats(String repository, String snapshot) { diff --git a/server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotIT.java b/server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotIT.java index 8be14d1188db8..e036cbb6761c5 100644 --- a/server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotIT.java @@ -743,10 +743,11 @@ public void testChangeSettingsOnRestore() throws Exception { indexRandom(true, builders); flushAndRefresh(); - assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "foo")).get(), numdocs); - assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "Foo")).get(), 0); - assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "bar")).get(), numdocs); - + assertBusy(() -> { + assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "foo")).get(), numdocs); + assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "Foo")).get(), 0); + assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "bar")).get(), numdocs); + }); logger.info("--> snapshot it"); CreateSnapshotResponse createSnapshotResponse = client.admin() .cluster() diff --git a/server/src/internalClusterTest/java/org/opensearch/update/UpdateIT.java b/server/src/internalClusterTest/java/org/opensearch/update/UpdateIT.java index 872a502154690..bbf34cea81bf5 100644 --- a/server/src/internalClusterTest/java/org/opensearch/update/UpdateIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/update/UpdateIT.java @@ -177,8 +177,10 @@ public void testUpsert() throws Exception { assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { - GetResponse getResponse = client().prepareGet("test", "1").execute().actionGet(); - assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1")); + assertBusy(() -> { + GetResponse getResponse = client().prepareGet("test", "1").execute().actionGet(); + assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1")); + }); } updateResponse = client().prepareUpdate(indexOrAlias(), "1") @@ -220,8 +222,10 @@ public void testScriptedUpsert() throws Exception { assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { - GetResponse getResponse = client().prepareGet("test", "1").execute().actionGet(); - assertThat(getResponse.getSourceAsMap().get("balance").toString(), equalTo("9")); + assertBusy(() -> { + GetResponse getResponse = client().prepareGet("test", "1").execute().actionGet(); + assertThat(getResponse.getSourceAsMap().get("balance").toString(), equalTo("9")); + }); } // Now pay money for an existing account where balance is stored in es @@ -337,8 +341,10 @@ public void testUpdate() throws Exception { assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { - GetResponse getResponse = client().prepareGet("test", "1").execute().actionGet(); - assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2")); + assertBusy(() -> { + GetResponse getResponse = client().prepareGet("test", "1").execute().actionGet(); + assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2")); + }, 30, TimeUnit.SECONDS); } Map params = new HashMap<>(); @@ -353,8 +359,10 @@ public void testUpdate() throws Exception { assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { - GetResponse getResponse = client().prepareGet("test", "1").execute().actionGet(); - assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5")); + assertBusy(() -> { + GetResponse getResponse = client().prepareGet("test", "1").execute().actionGet(); + assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5")); + }, 30, TimeUnit.SECONDS); } // check noop @@ -374,8 +382,10 @@ public void testUpdate() throws Exception { assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { - GetResponse getResponse = client().prepareGet("test", "1").execute().actionGet(); - assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5")); + assertBusy(() -> { + GetResponse getResponse = client().prepareGet("test", "1").execute().actionGet(); + assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("5")); + }, 60, TimeUnit.SECONDS); } // check delete @@ -566,12 +576,14 @@ public void testContextVariables() throws Exception { assertEquals(2, updateResponse.getVersion()); - GetResponse getResponse = client().prepareGet("test", "id1").setRouting("routing1").execute().actionGet(); - Map updateContext = (Map) getResponse.getSourceAsMap().get("update_context"); - assertEquals("test", updateContext.get("_index")); - assertEquals("id1", updateContext.get("_id")); - assertEquals(1, updateContext.get("_version")); - assertEquals("routing1", updateContext.get("_routing")); + assertBusy(() -> { + GetResponse getResponse = client().prepareGet("test", "id1").setRouting("routing1").execute().actionGet(); + Map updateContext = (Map) getResponse.getSourceAsMap().get("update_context"); + assertEquals("test", updateContext.get("_index")); + assertEquals("id1", updateContext.get("_id")); + assertEquals(1, updateContext.get("_version")); + assertEquals("routing1", updateContext.get("_routing")); + }, 30, TimeUnit.SECONDS); // Idem with the second object updateResponse = client().prepareUpdate("test", "id2") @@ -581,13 +593,15 @@ public void testContextVariables() throws Exception { assertEquals(2, updateResponse.getVersion()); - getResponse = client().prepareGet("test", "id2").execute().actionGet(); - updateContext = (Map) getResponse.getSourceAsMap().get("update_context"); - assertEquals("test", updateContext.get("_index")); - assertEquals("id2", updateContext.get("_id")); - assertEquals(1, updateContext.get("_version")); - assertNull(updateContext.get("_routing")); - assertNull(updateContext.get("_ttl")); + assertBusy(() -> { + GetResponse getResponse = client().prepareGet("test", "id2").execute().actionGet(); + Map updateContext = (Map) getResponse.getSourceAsMap().get("update_context"); + assertEquals("test", updateContext.get("_index")); + assertEquals("id2", updateContext.get("_id")); + assertEquals(1, updateContext.get("_version")); + assertNull(updateContext.get("_routing")); + assertNull(updateContext.get("_ttl")); + }); } public void testConcurrentUpdateWithRetryOnConflict() throws Exception { @@ -659,13 +673,15 @@ public void run() { logger.info("Captured failure on concurrent update:", throwable); } assertThat(failures.size(), equalTo(0)); - for (int i = 0; i < numberOfUpdatesPerThread; i++) { - GetResponse response = client().prepareGet("test", Integer.toString(i)).execute().actionGet(); - assertThat(response.getId(), equalTo(Integer.toString(i))); - assertThat(response.isExists(), equalTo(true)); - assertThat(response.getVersion(), equalTo((long) numberOfThreads)); - assertThat(response.getSource().get("field"), equalTo(numberOfThreads)); - } + assertBusy(() -> { + for (int i = 0; i < numberOfUpdatesPerThread; i++) { + GetResponse response = client().prepareGet("test", Integer.toString(i)).execute().actionGet(); + assertThat(response.getId(), equalTo(Integer.toString(i))); + assertThat(response.isExists(), equalTo(true)); + assertThat(response.getVersion(), equalTo((long) numberOfThreads)); + assertThat(response.getSource().get("field"), equalTo(numberOfThreads)); + } + }); } public void testStressUpdateDeleteConcurrency() throws Exception { @@ -875,28 +891,30 @@ private void waitForOutstandingRequests(TimeValue timeOut, Semaphore requestsOut refresh(); - for (int i = 0; i < numberOfIdsPerThread; ++i) { - int totalFailures = 0; - GetResponse response = client().prepareGet("test", Integer.toString(i)).execute().actionGet(); - if (response.isExists()) { - assertThat(response.getId(), equalTo(Integer.toString(i))); - int expectedVersion = (numberOfThreads * numberOfUpdatesPerId * 2) + 1; - for (UpdateThread ut : threads) { - if (ut.failedMap.containsKey(i)) { - totalFailures += ut.failedMap.get(i); + assertBusy(() -> { + for (int i = 0; i < numberOfIdsPerThread; ++i) { + int totalFailures = 0; + GetResponse response = client().prepareGet("test", Integer.toString(i)).execute().actionGet(); + if (response.isExists()) { + assertThat(response.getId(), equalTo(Integer.toString(i))); + int expectedVersion = (numberOfThreads * numberOfUpdatesPerId * 2) + 1; + for (UpdateThread ut : threads) { + if (ut.failedMap.containsKey(i)) { + totalFailures += ut.failedMap.get(i); + } } + expectedVersion -= totalFailures; + logger.error( + "Actual version [{}] Expected version [{}] Total failures [{}]", + response.getVersion(), + expectedVersion, + totalFailures + ); + assertThat(response.getVersion(), equalTo((long) expectedVersion)); + assertThat(response.getVersion() + totalFailures, equalTo((long) ((numberOfUpdatesPerId * numberOfThreads * 2) + 1))); } - expectedVersion -= totalFailures; - logger.error( - "Actual version [{}] Expected version [{}] Total failures [{}]", - response.getVersion(), - expectedVersion, - totalFailures - ); - assertThat(response.getVersion(), equalTo((long) expectedVersion)); - assertThat(response.getVersion() + totalFailures, equalTo((long) ((numberOfUpdatesPerId * numberOfThreads * 2) + 1))); } - } + }); } private static String indexOrAlias() { diff --git a/server/src/internalClusterTest/java/org/opensearch/versioning/SimpleVersioningIT.java b/server/src/internalClusterTest/java/org/opensearch/versioning/SimpleVersioningIT.java index 5898bba9762ad..f14db92bae511 100644 --- a/server/src/internalClusterTest/java/org/opensearch/versioning/SimpleVersioningIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/versioning/SimpleVersioningIT.java @@ -141,7 +141,7 @@ public void testExternalGTE() throws Exception { refresh(); } for (int i = 0; i < 10; i++) { - assertThat(client().prepareGet("test", "1").get().getVersion(), equalTo(14L)); + assertBusy(() -> { assertThat(client().prepareGet("test", "1").get().getVersion(), equalTo(14L)); }); } // deleting with a lower version fails. @@ -208,7 +208,7 @@ public void testExternalVersioning() throws Exception { refresh(); } for (int i = 0; i < 10; i++) { - assertThat(client().prepareGet("test", "1").execute().actionGet().getVersion(), equalTo(14L)); + assertBusy(() -> { assertThat(client().prepareGet("test", "1").execute().actionGet().getVersion(), equalTo(14L)); }); } // deleting with a lower version fails. @@ -307,7 +307,7 @@ public void testCompareAndSetInitialDelete() throws Exception { assertThat(indexResponse.getVersion(), equalTo(1L)); } - public void testCompareAndSet() { + public void testCompareAndSet() throws Exception { createIndex("test"); ensureGreen(); @@ -349,9 +349,11 @@ public void testCompareAndSet() { client().admin().indices().prepareRefresh().execute().actionGet(); for (int i = 0; i < 10; i++) { - final GetResponse response = client().prepareGet("test", "1").get(); - assertThat(response.getSeqNo(), equalTo(1L)); - assertThat(response.getPrimaryTerm(), equalTo(1L)); + assertBusy(() -> { + final GetResponse response = client().prepareGet("test", "1").get(); + assertThat(response.getSeqNo(), equalTo(1L)); + assertThat(response.getPrimaryTerm(), equalTo(1L)); + }); } // search with versioning diff --git a/test/framework/src/main/java/org/opensearch/snapshots/AbstractSnapshotIntegTestCase.java b/test/framework/src/main/java/org/opensearch/snapshots/AbstractSnapshotIntegTestCase.java index ff987371433c8..c20fd2bb9e091 100644 --- a/test/framework/src/main/java/org/opensearch/snapshots/AbstractSnapshotIntegTestCase.java +++ b/test/framework/src/main/java/org/opensearch/snapshots/AbstractSnapshotIntegTestCase.java @@ -31,6 +31,7 @@ package org.opensearch.snapshots; +import org.opensearch.OpenSearchException; import org.opensearch.Version; import org.opensearch.action.ActionFuture; import org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; @@ -482,7 +483,11 @@ protected long getCountForIndex(String indexName) { } protected void assertDocCount(String index, long count) { - assertEquals(getCountForIndex(index), count); + try { + assertBusy(() -> { assertEquals(getCountForIndex(index), count); }); + } catch (Exception e) { + throw new OpenSearchException("assert doc count failed", e); + } } /**