|
| 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.indices.replication; |
| 10 | + |
| 11 | +import org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest; |
| 12 | +import org.opensearch.action.admin.indices.segments.IndicesSegmentResponse; |
| 13 | +import org.opensearch.action.support.WriteRequest; |
| 14 | +import org.opensearch.action.support.replication.TransportReplicationAction; |
| 15 | +import org.opensearch.common.settings.Settings; |
| 16 | +import org.opensearch.common.util.FeatureFlags; |
| 17 | +import org.opensearch.index.IndexSettings; |
| 18 | +import org.opensearch.index.TieredMergePolicyProvider; |
| 19 | +import org.opensearch.indices.replication.checkpoint.RemoteStorePublishMergedSegmentRequest; |
| 20 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 21 | +import org.opensearch.test.transport.MockTransportService; |
| 22 | +import org.opensearch.test.transport.StubbableTransport; |
| 23 | +import org.opensearch.transport.TransportService; |
| 24 | +import org.junit.Before; |
| 25 | + |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.concurrent.ConcurrentHashMap; |
| 29 | +import java.util.concurrent.CountDownLatch; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import java.util.concurrent.atomic.AtomicLong; |
| 32 | + |
| 33 | +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 34 | +public class RemoteStoreMergedSegmentWarmerIT extends SegmentReplicationBaseIT { |
| 35 | + private Path absolutePath; |
| 36 | + |
| 37 | + @Override |
| 38 | + protected Settings nodeSettings(int nodeOrdinal) { |
| 39 | + if (absolutePath == null) { |
| 40 | + absolutePath = randomRepoPath().toAbsolutePath(); |
| 41 | + } |
| 42 | + return Settings.builder() |
| 43 | + .put(super.nodeSettings(nodeOrdinal)) |
| 44 | + .put(remoteStoreClusterSettings("test-remote-store-repo", absolutePath)) |
| 45 | + .build(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected Settings featureFlagSettings() { |
| 50 | + Settings.Builder featureSettings = Settings.builder(); |
| 51 | + featureSettings.put(FeatureFlags.MERGED_SEGMENT_WARMER_EXPERIMENTAL_FLAG, true); |
| 52 | + return featureSettings.build(); |
| 53 | + } |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setup() { |
| 57 | + internalCluster().startClusterManagerOnlyNode(); |
| 58 | + } |
| 59 | + |
| 60 | + public void testMergeSegmentWarmerRemote() throws Exception { |
| 61 | + final String node1 = internalCluster().startDataOnlyNode(); |
| 62 | + final String node2 = internalCluster().startDataOnlyNode(); |
| 63 | + createIndex(INDEX_NAME); |
| 64 | + ensureGreen(INDEX_NAME); |
| 65 | + MockTransportService mockTransportServiceNode1 = (MockTransportService) internalCluster().getInstance( |
| 66 | + TransportService.class, |
| 67 | + node1 |
| 68 | + ); |
| 69 | + MockTransportService mockTransportServiceNode2 = (MockTransportService) internalCluster().getInstance( |
| 70 | + TransportService.class, |
| 71 | + node2 |
| 72 | + ); |
| 73 | + final CountDownLatch latch = new CountDownLatch(1); |
| 74 | + StubbableTransport.SendRequestBehavior behavior = (connection, requestId, action, request, options) -> { |
| 75 | + if (action.equals("indices:admin/remote_publish_merged_segment[r]")) { |
| 76 | + assertTrue( |
| 77 | + ((TransportReplicationAction.ConcreteReplicaRequest) request) |
| 78 | + .getRequest() instanceof RemoteStorePublishMergedSegmentRequest |
| 79 | + ); |
| 80 | + latch.countDown(); |
| 81 | + } |
| 82 | + connection.sendRequest(requestId, action, request, options); |
| 83 | + }; |
| 84 | + |
| 85 | + for (int i = 0; i < 30; i++) { |
| 86 | + client().prepareIndex(INDEX_NAME) |
| 87 | + .setId(String.valueOf(i)) |
| 88 | + .setSource("foo" + i, "bar" + i) |
| 89 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 90 | + .get(); |
| 91 | + } |
| 92 | + |
| 93 | + waitForSearchableDocs(30, node1, node2); |
| 94 | + |
| 95 | + mockTransportServiceNode1.addSendBehavior(behavior); |
| 96 | + mockTransportServiceNode2.addSendBehavior(behavior); |
| 97 | + |
| 98 | + client().admin().indices().forceMerge(new ForceMergeRequest(INDEX_NAME).maxNumSegments(2)); |
| 99 | + waitForSegmentCount(INDEX_NAME, 2, logger); |
| 100 | + assertTrue(latch.await(10, TimeUnit.SECONDS)); |
| 101 | + mockTransportServiceNode1.clearAllRules(); |
| 102 | + mockTransportServiceNode2.clearAllRules(); |
| 103 | + } |
| 104 | + |
| 105 | + public void testConcurrentMergeSegmentWarmerRemote() throws Exception { |
| 106 | + String node1 = internalCluster().startDataOnlyNode(); |
| 107 | + String node2 = internalCluster().startDataOnlyNode(); |
| 108 | + createIndex( |
| 109 | + INDEX_NAME, |
| 110 | + Settings.builder() |
| 111 | + .put(indexSettings()) |
| 112 | + .put(TieredMergePolicyProvider.INDEX_MERGE_POLICY_SEGMENTS_PER_TIER_SETTING.getKey(), 5) |
| 113 | + .put(TieredMergePolicyProvider.INDEX_MERGE_POLICY_MAX_MERGE_AT_ONCE_SETTING.getKey(), 5) |
| 114 | + .put(IndexSettings.INDEX_MERGE_ON_FLUSH_ENABLED.getKey(), false) |
| 115 | + .build() |
| 116 | + ); |
| 117 | + ensureGreen(INDEX_NAME); |
| 118 | + MockTransportService mockTransportServiceNode1 = (MockTransportService) internalCluster().getInstance( |
| 119 | + TransportService.class, |
| 120 | + node1 |
| 121 | + ); |
| 122 | + MockTransportService mockTransportServiceNode2 = (MockTransportService) internalCluster().getInstance( |
| 123 | + TransportService.class, |
| 124 | + node2 |
| 125 | + ); |
| 126 | + CountDownLatch latch = new CountDownLatch(2); |
| 127 | + AtomicLong numInvocations = new AtomicLong(0); |
| 128 | + Set<String> executingThreads = ConcurrentHashMap.newKeySet(); |
| 129 | + StubbableTransport.SendRequestBehavior behavior = (connection, requestId, action, request, options) -> { |
| 130 | + if (action.equals("indices:admin/remote_publish_merged_segment[r]")) { |
| 131 | + assertTrue( |
| 132 | + ((TransportReplicationAction.ConcreteReplicaRequest) request) |
| 133 | + .getRequest() instanceof RemoteStorePublishMergedSegmentRequest |
| 134 | + ); |
| 135 | + latch.countDown(); |
| 136 | + numInvocations.incrementAndGet(); |
| 137 | + executingThreads.add(Thread.currentThread().getName()); |
| 138 | + } |
| 139 | + connection.sendRequest(requestId, action, request, options); |
| 140 | + }; |
| 141 | + |
| 142 | + mockTransportServiceNode1.addSendBehavior(behavior); |
| 143 | + mockTransportServiceNode2.addSendBehavior(behavior); |
| 144 | + |
| 145 | + for (int i = 0; i < 30; i++) { |
| 146 | + client().prepareIndex(INDEX_NAME) |
| 147 | + .setId(String.valueOf(i)) |
| 148 | + .setSource("foo" + i, "bar" + i) |
| 149 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 150 | + .get(); |
| 151 | + } |
| 152 | + |
| 153 | + client().admin().indices().forceMerge(new ForceMergeRequest(INDEX_NAME).maxNumSegments(2)); |
| 154 | + |
| 155 | + waitForSegmentCount(INDEX_NAME, 2, logger); |
| 156 | + logger.info("Number of merge invocations: {}", numInvocations.get()); |
| 157 | + assertTrue(latch.await(10, TimeUnit.SECONDS)); |
| 158 | + assertTrue(executingThreads.size() > 1); |
| 159 | + // Verify concurrent execution by checking that multiple unique threads handled merge operations |
| 160 | + assertTrue(numInvocations.get() > 1); |
| 161 | + mockTransportServiceNode1.clearAllRules(); |
| 162 | + mockTransportServiceNode2.clearAllRules(); |
| 163 | + } |
| 164 | + |
| 165 | + public void testMergeSegmentWarmerWithInactiveReplicaRemote() throws Exception { |
| 166 | + internalCluster().startDataOnlyNode(); |
| 167 | + createIndex(INDEX_NAME); |
| 168 | + ensureYellowAndNoInitializingShards(INDEX_NAME); |
| 169 | + |
| 170 | + for (int i = 0; i < 30; i++) { |
| 171 | + client().prepareIndex(INDEX_NAME) |
| 172 | + .setId(String.valueOf(i)) |
| 173 | + .setSource("foo" + i, "bar" + i) |
| 174 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 175 | + .get(); |
| 176 | + } |
| 177 | + |
| 178 | + client().admin().indices().forceMerge(new ForceMergeRequest(INDEX_NAME).maxNumSegments(1)).get(); |
| 179 | + final IndicesSegmentResponse response = client().admin().indices().prepareSegments(INDEX_NAME).get(); |
| 180 | + assertEquals(1, response.getIndices().get(INDEX_NAME).getShards().values().size()); |
| 181 | + } |
| 182 | +} |
0 commit comments