|
| 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.composite; |
| 10 | + |
| 11 | +import org.opensearch.action.admin.indices.refresh.RefreshResponse; |
| 12 | +import org.opensearch.action.admin.indices.stats.IndicesStatsResponse; |
| 13 | +import org.opensearch.action.admin.indices.stats.ShardStats; |
| 14 | +import org.opensearch.action.index.IndexResponse; |
| 15 | +import org.opensearch.be.lucene.LucenePlugin; |
| 16 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 17 | +import org.opensearch.common.SuppressForbidden; |
| 18 | +import org.opensearch.common.settings.Settings; |
| 19 | +import org.opensearch.common.util.FeatureFlags; |
| 20 | +import org.opensearch.core.rest.RestStatus; |
| 21 | +import org.opensearch.index.IndexSettings; |
| 22 | +import org.opensearch.index.engine.CommitStats; |
| 23 | +import org.opensearch.index.engine.dataformat.DataFormatDescriptor; |
| 24 | +import org.opensearch.index.engine.dataformat.DataFormatRegistry; |
| 25 | +import org.opensearch.index.engine.dataformat.ReaderManagerConfig; |
| 26 | +import org.opensearch.index.engine.dataformat.stub.MockDataFormat; |
| 27 | +import org.opensearch.index.engine.dataformat.stub.MockDataFormatPlugin; |
| 28 | +import org.opensearch.index.engine.dataformat.stub.MockReaderManager; |
| 29 | +import org.opensearch.index.engine.exec.EngineReaderManager; |
| 30 | +import org.opensearch.index.engine.exec.coord.DataformatAwareCatalogSnapshot; |
| 31 | +import org.opensearch.index.merge.MergeStats; |
| 32 | +import org.opensearch.index.store.PrecomputedChecksumStrategy; |
| 33 | +import org.opensearch.plugins.Plugin; |
| 34 | +import org.opensearch.plugins.SearchBackEndPlugin; |
| 35 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.Collection; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Map; |
| 42 | +import java.util.Set; |
| 43 | +import java.util.function.Function; |
| 44 | + |
| 45 | +/** |
| 46 | + * Integration tests for composite merge operations across single and multiple data format engines. |
| 47 | + * |
| 48 | + * Requires JDK 25 and sandbox enabled. Run with: |
| 49 | + * ./gradlew :sandbox:plugins:composite-engine:internalClusterTest \ |
| 50 | + * --tests "*.CompositeMergeIT" \ |
| 51 | + * -Dsandbox.enabled=true |
| 52 | + */ |
| 53 | +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 1) |
| 54 | +public class CompositeMergeIT extends OpenSearchIntegTestCase { |
| 55 | + |
| 56 | + private static final String INDEX_NAME = "test-composite-merge"; |
| 57 | + private static final String MERGE_ENABLED_PROPERTY = "opensearch.pluggable.dataformat.merge.enabled"; |
| 58 | + |
| 59 | + // ── Mock DataFormatPlugin using test framework stubs ── |
| 60 | + |
| 61 | + public static class MockParquetDataFormatPlugin extends MockDataFormatPlugin implements SearchBackEndPlugin<Object> { |
| 62 | + private static final MockDataFormat PARQUET_FORMAT = new MockDataFormat("parquet", 0L, Set.of()); |
| 63 | + |
| 64 | + public MockParquetDataFormatPlugin() { |
| 65 | + super(PARQUET_FORMAT); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Map<String, DataFormatDescriptor> getFormatDescriptors(IndexSettings indexSettings, DataFormatRegistry registry) { |
| 70 | + return Map.of("parquet", new DataFormatDescriptor("parquet", new PrecomputedChecksumStrategy())); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String name() { |
| 75 | + return "mock-parquet-backend"; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public List<String> getSupportedFormats() { |
| 80 | + return List.of("parquet"); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public EngineReaderManager<?> createReaderManager(ReaderManagerConfig settings) { |
| 85 | + return new MockReaderManager("parquet"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // ── Test setup ── |
| 90 | + |
| 91 | + @Override |
| 92 | + public void setUp() throws Exception { |
| 93 | + enableMerge(); |
| 94 | + super.setUp(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void tearDown() throws Exception { |
| 99 | + try { |
| 100 | + client().admin().indices().prepareDelete(INDEX_NAME).get(); |
| 101 | + } catch (Exception e) { |
| 102 | + // index may not exist if test failed before creation |
| 103 | + } |
| 104 | + super.tearDown(); |
| 105 | + disableMerge(); |
| 106 | + } |
| 107 | + |
| 108 | + @SuppressForbidden(reason = "enable pluggable dataformat merge for integration testing") |
| 109 | + private static void enableMerge() { |
| 110 | + System.setProperty(MERGE_ENABLED_PROPERTY, "true"); |
| 111 | + } |
| 112 | + |
| 113 | + @SuppressForbidden(reason = "restore pluggable dataformat merge property after test") |
| 114 | + private static void disableMerge() { |
| 115 | + System.clearProperty(MERGE_ENABLED_PROPERTY); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 120 | + return Arrays.asList(MockParquetDataFormatPlugin.class, CompositeDataFormatPlugin.class, LucenePlugin.class); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected Settings nodeSettings(int nodeOrdinal) { |
| 125 | + return Settings.builder() |
| 126 | + .put(super.nodeSettings(nodeOrdinal)) |
| 127 | + .put(FeatureFlags.PLUGGABLE_DATAFORMAT_EXPERIMENTAL_FLAG, true) |
| 128 | + .build(); |
| 129 | + } |
| 130 | + |
| 131 | + // ── Tests ── |
| 132 | + |
| 133 | + /** |
| 134 | + * Verifies that background merges are triggered automatically after refresh |
| 135 | + * when enough segments accumulate to exceed the TieredMergePolicy threshold. |
| 136 | + * <p> |
| 137 | + * Flow: index docs across many refresh cycles → each refresh calls |
| 138 | + * triggerPossibleMerges() → MergeScheduler picks up merge candidates |
| 139 | + * asynchronously → segment count decreases. |
| 140 | + */ |
| 141 | + public void testBackgroundMergeSingleEngine() throws Exception { |
| 142 | + createIndex(INDEX_NAME, singleEngineSettings()); |
| 143 | + ensureGreen(INDEX_NAME); |
| 144 | + |
| 145 | + // Create enough segments to exceed TieredMergePolicy's default threshold (~10) |
| 146 | + int totalSegmentsCreated = indexDocsAcrossMultipleRefreshes(15, 5); |
| 147 | + |
| 148 | + // Wait for async background merges to complete |
| 149 | + assertBusy(() -> { |
| 150 | + flush(INDEX_NAME); |
| 151 | + DataformatAwareCatalogSnapshot snapshot = getCatalogSnapshot(); |
| 152 | + assertTrue( |
| 153 | + "Expected merges to reduce segment count below " + totalSegmentsCreated + ", but got: " + snapshot.getSegments().size(), |
| 154 | + snapshot.getSegments().size() < totalSegmentsCreated |
| 155 | + ); |
| 156 | + }); |
| 157 | + |
| 158 | + MergeStats mergeStats = getMergeStats(); |
| 159 | + assertTrue("Expected at least one merge to have occurred", mergeStats.getTotal() > 0); |
| 160 | + |
| 161 | + DataformatAwareCatalogSnapshot snapshot = getCatalogSnapshot(); |
| 162 | + assertEquals(Set.of("parquet"), snapshot.getDataFormats()); |
| 163 | + } |
| 164 | + |
| 165 | + // ── Helpers ── |
| 166 | + |
| 167 | + private Settings singleEngineSettings() { |
| 168 | + return Settings.builder() |
| 169 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 170 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) |
| 171 | + .put("index.pluggable.dataformat.enabled", true) |
| 172 | + .put("index.pluggable.dataformat", "composite") |
| 173 | + .put("index.composite.primary_data_format", "parquet") |
| 174 | + .putList("index.composite.secondary_data_formats") |
| 175 | + .build(); |
| 176 | + } |
| 177 | + |
| 178 | + private int indexDocsAcrossMultipleRefreshes(int refreshCycles, int docsPerCycle) { |
| 179 | + for (int cycle = 0; cycle < refreshCycles; cycle++) { |
| 180 | + for (int i = 0; i < docsPerCycle; i++) { |
| 181 | + IndexResponse response = client().prepareIndex() |
| 182 | + .setIndex(INDEX_NAME) |
| 183 | + .setSource("field_text", randomAlphaOfLength(10), "field_number", randomIntBetween(1, 1000)) |
| 184 | + .get(); |
| 185 | + assertEquals(RestStatus.CREATED, response.status()); |
| 186 | + } |
| 187 | + RefreshResponse refreshResponse = client().admin().indices().prepareRefresh(INDEX_NAME).get(); |
| 188 | + assertEquals(RestStatus.OK, refreshResponse.getStatus()); |
| 189 | + } |
| 190 | + return refreshCycles; |
| 191 | + } |
| 192 | + |
| 193 | + private DataformatAwareCatalogSnapshot getCatalogSnapshot() throws IOException { |
| 194 | + IndicesStatsResponse statsResponse = client().admin().indices().prepareStats(INDEX_NAME).clear().setStore(true).get(); |
| 195 | + ShardStats shardStats = statsResponse.getIndex(INDEX_NAME).getShards()[0]; |
| 196 | + CommitStats commitStats = shardStats.getCommitStats(); |
| 197 | + assertNotNull(commitStats); |
| 198 | + assertTrue(commitStats.getUserData().containsKey(DataformatAwareCatalogSnapshot.CATALOG_SNAPSHOT_KEY)); |
| 199 | + return DataformatAwareCatalogSnapshot.deserializeFromString( |
| 200 | + commitStats.getUserData().get(DataformatAwareCatalogSnapshot.CATALOG_SNAPSHOT_KEY), |
| 201 | + Function.identity() |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + private MergeStats getMergeStats() { |
| 206 | + IndicesStatsResponse statsResponse = client().admin().indices().prepareStats(INDEX_NAME).clear().setMerge(true).get(); |
| 207 | + return statsResponse.getIndex(INDEX_NAME).getShards()[0].getStats().getMerge(); |
| 208 | + } |
| 209 | +} |
0 commit comments