diff --git a/qa/rolling-upgrade/src/test/java/org/opensearch/upgrades/IndexingIT.java b/qa/rolling-upgrade/src/test/java/org/opensearch/upgrades/IndexingIT.java index 8f3024f5176ac..0c49a91704ed2 100644 --- a/qa/rolling-upgrade/src/test/java/org/opensearch/upgrades/IndexingIT.java +++ b/qa/rolling-upgrade/src/test/java/org/opensearch/upgrades/IndexingIT.java @@ -110,14 +110,25 @@ private void waitForSearchableDocs(String index, int shardCount, int replicaCoun Request segrepStatsRequest = new Request("GET", "/_cat/segments/" + index + "?s=shard,segment,primaryOrReplica"); segrepStatsRequest.addParameter("h", "index,shard,primaryOrReplica,segment,docs.count"); Response segrepStatsResponse = client().performRequest(segrepStatsRequest); - logger.info("--> _cat/segments response\n {}", EntityUtils.toString(client().performRequest(segrepStatsRequest).getEntity())); List responseList = Streams.readAllLines(segrepStatsResponse.getEntity().getContent()); - for (int segmentsIndex=0; segmentsIndex < responseList.size();) { - String[] primaryRow = responseList.get(segmentsIndex++).split(" +"); + logger.info("--> _cat/segments response\n {}", responseList.toString().replace(',', '\n')); + // Filter response for rows with zero doc count + List filteredList = new ArrayList<>(); + for(String row: responseList) { + String count = row.split(" +")[4]; + if (count.equals("0") == false) { + filteredList.add(row); + } + } + // Ensure there is result for replica copies before processing the result. This results in retry when there + // are not enough number of rows vs failing with IndexOutOfBoundsException + assertEquals(0, filteredList.size() % (replicaCount + 1)); + for (int segmentsIndex=0; segmentsIndex < filteredList.size();) { + String[] primaryRow = filteredList.get(segmentsIndex++).split(" +"); String shardId = primaryRow[0] + primaryRow[1]; assertTrue(primaryRow[2].equals("p")); for(int replicaIndex = 1; replicaIndex <= replicaCount; replicaIndex++) { - String[] replicaRow = responseList.get(segmentsIndex).split(" +"); + String[] replicaRow = filteredList.get(segmentsIndex).split(" +"); String replicaShardId = replicaRow[0] + replicaRow[1]; // When segment has 0 doc count, not all replica copies posses that segment. Skip to next segment if (replicaRow[2].equals("p")) { @@ -161,7 +172,7 @@ private void verifySegmentStats(String indexName) throws Exception { }, 1, TimeUnit.MINUTES); } - public void testIndexing() throws IOException, ParseException { + public void testIndexing() throws Exception { switch (CLUSTER_TYPE) { case OLD: break; @@ -403,12 +414,14 @@ private void bulk(String index, String valueSuffix, int count) throws IOExceptio client().performRequest(bulk); } - private void assertCount(String index, int count) throws IOException { - Request searchTestIndexRequest = new Request("POST", "/" + index + "/_search"); - searchTestIndexRequest.addParameter(TOTAL_HITS_AS_INT_PARAM, "true"); - searchTestIndexRequest.addParameter("filter_path", "hits.total"); - Response searchTestIndexResponse = client().performRequest(searchTestIndexRequest); - assertEquals("{\"hits\":{\"total\":" + count + "}}", + private void assertCount(String index, int count) throws Exception { + assertBusy(() -> { + Request searchTestIndexRequest = new Request("POST", "/" + index + "/_search"); + searchTestIndexRequest.addParameter(TOTAL_HITS_AS_INT_PARAM, "true"); + searchTestIndexRequest.addParameter("filter_path", "hits.total"); + Response searchTestIndexResponse = client().performRequest(searchTestIndexRequest); + assertEquals("{\"hits\":{\"total\":" + count + "}}", EntityUtils.toString(searchTestIndexResponse.getEntity(), StandardCharsets.UTF_8)); + }, 30, TimeUnit.SECONDS); } }