|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.remotestore; |
| 10 | + |
| 11 | +import org.junit.Before; |
| 12 | +import org.opensearch.action.admin.cluster.remotestore.restore.RestoreRemoteStoreRequest; |
| 13 | +import org.opensearch.action.index.IndexResponse; |
| 14 | +import org.opensearch.action.support.PlainActionFuture; |
| 15 | +import org.opensearch.common.settings.Settings; |
| 16 | +import org.opensearch.plugins.Plugin; |
| 17 | +import org.opensearch.test.InternalTestCluster; |
| 18 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 19 | +import org.opensearch.test.transport.MockTransportService; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
| 30 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount; |
| 31 | + |
| 32 | +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.SUITE, numDataNodes = 3) |
| 33 | +public class RemoteStoreForceMergeIT extends RemoteStoreBaseIntegTestCase { |
| 34 | + |
| 35 | + private static final String INDEX_NAME = "remote-store-test-idx-1"; |
| 36 | + private static final String TOTAL_OPERATIONS = "total-operations"; |
| 37 | + private static final String MAX_SEQ_NO_TOTAL = "max-seq-no-total"; |
| 38 | + |
| 39 | + @Override |
| 40 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 41 | + return Arrays.asList(MockTransportService.TestPlugin.class); |
| 42 | + } |
| 43 | + |
| 44 | + @Before |
| 45 | + public void setup() { |
| 46 | + setupRepo(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Settings indexSettings() { |
| 51 | + return remoteStoreIndexSettings(0); |
| 52 | + } |
| 53 | + |
| 54 | + private Map<String, Long> indexData(int numberOfIterations, boolean invokeFlush, boolean flushAfterMerge, long deletedDocs) { |
| 55 | + long totalOperations = 0; |
| 56 | + long maxSeqNo = -1; |
| 57 | + List<IndexResponse> indexResponseList = new ArrayList<>(); |
| 58 | + for (int i = 0; i < numberOfIterations; i++) { |
| 59 | + int numberOfOperations = randomIntBetween(20, 50); |
| 60 | + for (int j = 0; j < numberOfOperations; j++) { |
| 61 | + IndexResponse response = indexSingleDoc(INDEX_NAME); |
| 62 | + maxSeqNo = response.getSeqNo(); |
| 63 | + indexResponseList.add(response); |
| 64 | + } |
| 65 | + totalOperations += numberOfOperations; |
| 66 | + if (invokeFlush) { |
| 67 | + flush(INDEX_NAME); |
| 68 | + } else { |
| 69 | + refresh(INDEX_NAME); |
| 70 | + } |
| 71 | + } |
| 72 | + if (deletedDocs == -1) { |
| 73 | + deletedDocs = totalOperations; |
| 74 | + } |
| 75 | + int length = indexResponseList.size(); |
| 76 | + for (int j = 0; j < deletedDocs; j++) { |
| 77 | + maxSeqNo = client().prepareDelete().setIndex(INDEX_NAME).setId(indexResponseList.get(length - j - 1).getId()).get().getSeqNo(); |
| 78 | + } |
| 79 | + client().admin().indices().prepareForceMerge(INDEX_NAME).setMaxNumSegments(1).setFlush(flushAfterMerge).get(); |
| 80 | + refresh(INDEX_NAME); |
| 81 | + assertHitCount(client().prepareSearch(INDEX_NAME).setSize(0).get(), totalOperations - deletedDocs); |
| 82 | + Map<String, Long> indexingStats = new HashMap<>(); |
| 83 | + indexingStats.put(TOTAL_OPERATIONS, totalOperations); |
| 84 | + indexingStats.put(MAX_SEQ_NO_TOTAL, maxSeqNo); |
| 85 | + return indexingStats; |
| 86 | + } |
| 87 | + |
| 88 | + private void verifyRestoredData(Map<String, Long> indexStats, long deletedDocs) { |
| 89 | + ensureYellowAndNoInitializingShards(INDEX_NAME); |
| 90 | + ensureGreen(INDEX_NAME); |
| 91 | + assertHitCount(client().prepareSearch(INDEX_NAME).setSize(0).get(), indexStats.get(TOTAL_OPERATIONS) - deletedDocs); |
| 92 | + IndexResponse response = indexSingleDoc(INDEX_NAME); |
| 93 | + assertEquals(indexStats.get(MAX_SEQ_NO_TOTAL) + 1, response.getSeqNo()); |
| 94 | + refresh(INDEX_NAME); |
| 95 | + assertHitCount(client().prepareSearch(INDEX_NAME).setSize(0).get(), indexStats.get(TOTAL_OPERATIONS) + 1 - deletedDocs); |
| 96 | + } |
| 97 | + |
| 98 | + private void testRestoreWithMergeFlow(int numberOfIterations, boolean invokeFlush, boolean flushAfterMerge, long deletedDocs) |
| 99 | + throws IOException { |
| 100 | + createIndex(INDEX_NAME, remoteTranslogIndexSettings(0)); |
| 101 | + ensureYellowAndNoInitializingShards(INDEX_NAME); |
| 102 | + ensureGreen(INDEX_NAME); |
| 103 | + |
| 104 | + Map<String, Long> indexStats = indexData(numberOfIterations, invokeFlush, flushAfterMerge, deletedDocs); |
| 105 | + |
| 106 | + internalCluster().stopRandomNode(InternalTestCluster.nameFilter(primaryNodeName(INDEX_NAME))); |
| 107 | + assertAcked(client().admin().indices().prepareClose(INDEX_NAME)); |
| 108 | + |
| 109 | + client().admin().cluster().restoreRemoteStore(new RestoreRemoteStoreRequest().indices(INDEX_NAME), PlainActionFuture.newFuture()); |
| 110 | + ensureGreen(INDEX_NAME); |
| 111 | + |
| 112 | + if (deletedDocs == -1) { |
| 113 | + verifyRestoredData(indexStats, indexStats.get(TOTAL_OPERATIONS)); |
| 114 | + } else { |
| 115 | + verifyRestoredData(indexStats, deletedDocs); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Following integ tests use randomBoolean to control the number of integ tests. If we use the separate |
| 120 | + // values for each of the flags, number of integ tests become 16 in comparison to current 2. |
| 121 | + // We have run all the 16 tests on local and they run fine. |
| 122 | + public void testRestoreForceMergeSingleIteration() throws IOException { |
| 123 | + boolean invokeFLush = randomBoolean(); |
| 124 | + boolean flushAfterMerge = randomBoolean(); |
| 125 | + testRestoreWithMergeFlow(1, invokeFLush, flushAfterMerge, randomIntBetween(0, 10)); |
| 126 | + } |
| 127 | + |
| 128 | + public void testRestoreForceMergeMultipleIterations() throws IOException { |
| 129 | + boolean invokeFLush = randomBoolean(); |
| 130 | + boolean flushAfterMerge = randomBoolean(); |
| 131 | + testRestoreWithMergeFlow(randomIntBetween(2, 5), invokeFLush, flushAfterMerge, randomIntBetween(0, 10)); |
| 132 | + } |
| 133 | + |
| 134 | + public void testRestoreForceMergeMultipleIterationsDeleteAll() throws IOException { |
| 135 | + boolean invokeFLush = randomBoolean(); |
| 136 | + boolean flushAfterMerge = randomBoolean(); |
| 137 | + testRestoreWithMergeFlow(randomIntBetween(2, 3), invokeFLush, flushAfterMerge, -1); |
| 138 | + } |
| 139 | +} |
0 commit comments