Skip to content

Commit

Permalink
Add IT for multiple writer validation
Browse files Browse the repository at this point in the history
Signed-off-by: Ashish Singh <[email protected]>
  • Loading branch information
ashking94 committed Nov 26, 2023
1 parent 5bb6cae commit d0be4aa
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package org.opensearch.remotestore;

import org.opensearch.action.admin.indices.get.GetIndexRequest;
import org.opensearch.action.admin.indices.get.GetIndexResponse;
import org.opensearch.action.bulk.BulkItemResponse;
import org.opensearch.action.bulk.BulkRequest;
import org.opensearch.action.bulk.BulkResponse;
Expand All @@ -23,9 +25,13 @@
import org.opensearch.common.UUIDs;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.unit.ByteSizeUnit;
import org.opensearch.core.index.Index;
import org.opensearch.index.IndexModule;
import org.opensearch.index.IndexService;
import org.opensearch.index.IndexSettings;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.indices.IndicesService;
import org.opensearch.indices.replication.common.ReplicationType;
import org.opensearch.repositories.RepositoriesService;
import org.opensearch.repositories.blobstore.BlobStoreRepository;
Expand All @@ -43,6 +49,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -380,4 +387,13 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {

return filesExisting.get();
}

protected IndexShard getIndexShard(String dataNode, String indexName) throws ExecutionException, InterruptedException {
String clusterManagerName = internalCluster().getClusterManagerName();
IndicesService indicesService = internalCluster().getInstance(IndicesService.class, dataNode);
GetIndexResponse getIndexResponse = client(clusterManagerName).admin().indices().getIndex(new GetIndexRequest()).get();
String uuid = getIndexResponse.getSettings().get(indexName).get(IndexMetadata.SETTING_INDEX_UUID);
IndexService indexService = indicesService.indexService(new Index(indexName, uuid));
return indexService.getShard(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@

package org.opensearch.remotestore;

import org.opensearch.action.admin.cluster.health.ClusterHealthResponse;
import org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
import org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
import org.opensearch.action.admin.indices.delete.DeleteIndexRequest;
import org.opensearch.action.admin.indices.get.GetIndexRequest;
import org.opensearch.action.admin.indices.get.GetIndexResponse;
import org.opensearch.action.admin.indices.recovery.RecoveryResponse;
import org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.opensearch.action.index.IndexResponse;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.routing.RecoverySource;
import org.opensearch.cluster.routing.allocation.command.MoveAllocationCommand;
import org.opensearch.common.Priority;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.concurrent.BufferedAsyncIOProcessor;
import org.opensearch.core.index.Index;
import org.opensearch.index.IndexService;
import org.opensearch.index.IndexSettings;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.index.translog.Translog.Durability;
Expand Down Expand Up @@ -222,7 +221,7 @@ public void testDefaultBufferInterval() throws ExecutionException, InterruptedEx
ensureGreen(INDEX_NAME);
assertClusterRemoteBufferInterval(IndexSettings.DEFAULT_REMOTE_TRANSLOG_BUFFER_INTERVAL, dataNode);

IndexShard indexShard = getIndexShard(dataNode);
IndexShard indexShard = getIndexShard(dataNode, INDEX_NAME);
assertTrue(indexShard.getTranslogSyncProcessor() instanceof BufferedAsyncIOProcessor);
assertBufferInterval(IndexSettings.DEFAULT_REMOTE_TRANSLOG_BUFFER_INTERVAL, indexShard);

Expand Down Expand Up @@ -255,7 +254,7 @@ public void testOverriddenBufferInterval() throws ExecutionException, Interrupte
ensureYellowAndNoInitializingShards(INDEX_NAME);
ensureGreen(INDEX_NAME);

IndexShard indexShard = getIndexShard(dataNode);
IndexShard indexShard = getIndexShard(dataNode, INDEX_NAME);
assertTrue(indexShard.getTranslogSyncProcessor() instanceof BufferedAsyncIOProcessor);
assertBufferInterval(bufferInterval, indexShard);

Expand Down Expand Up @@ -371,7 +370,7 @@ private void testRestrictSettingFalse(boolean setRestrictFalse, Durability durab
.put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), durability)
.build();
createIndex(INDEX_NAME, indexSettings);
IndexShard indexShard = getIndexShard(dataNode);
IndexShard indexShard = getIndexShard(dataNode, INDEX_NAME);
assertEquals(durability, indexShard.indexSettings().getTranslogDurability());

durability = randomFrom(Durability.values());
Expand Down Expand Up @@ -404,7 +403,7 @@ public void testAsyncDurabilityThrowsExceptionWhenRestrictSettingTrue() throws E

// Case 2 - Test update index fails
createIndex(INDEX_NAME);
IndexShard indexShard = getIndexShard(dataNode);
IndexShard indexShard = getIndexShard(dataNode, INDEX_NAME);
assertEquals(Durability.REQUEST, indexShard.indexSettings().getTranslogDurability());
exception = assertThrows(
IllegalArgumentException.class,
Expand All @@ -416,15 +415,6 @@ public void testAsyncDurabilityThrowsExceptionWhenRestrictSettingTrue() throws E
assertEquals(expectedExceptionMsg, exception.getMessage());
}

private IndexShard getIndexShard(String dataNode) throws ExecutionException, InterruptedException {
String clusterManagerName = internalCluster().getClusterManagerName();
IndicesService indicesService = internalCluster().getInstance(IndicesService.class, dataNode);
GetIndexResponse getIndexResponse = client(clusterManagerName).admin().indices().getIndex(new GetIndexRequest()).get();
String uuid = getIndexResponse.getSettings().get(INDEX_NAME).get(IndexMetadata.SETTING_INDEX_UUID);
IndexService indexService = indicesService.indexService(new Index(INDEX_NAME, uuid));
return indexService.getShard(0);
}

private void assertClusterRemoteBufferInterval(TimeValue expectedBufferInterval, String dataNode) {
IndicesService indicesService = internalCluster().getInstance(IndicesService.class, dataNode);
assertEquals(expectedBufferInterval, indicesService.getClusterRemoteTranslogBufferInterval());
Expand Down Expand Up @@ -516,7 +506,7 @@ public void testNoSearchIdleForAnyReplicaCount() throws ExecutionException, Inte

createIndex(INDEX_NAME, remoteStoreIndexSettings(0));
ensureGreen(INDEX_NAME);
IndexShard indexShard = getIndexShard(primaryShardNode);
IndexShard indexShard = getIndexShard(primaryShardNode, INDEX_NAME);
assertFalse(indexShard.isSearchIdleSupported());

String replicaShardNode = internalCluster().startDataOnlyNodes(1).get(0);
Expand All @@ -529,7 +519,35 @@ public void testNoSearchIdleForAnyReplicaCount() throws ExecutionException, Inte
ensureGreen(INDEX_NAME);
assertFalse(indexShard.isSearchIdleSupported());

indexShard = getIndexShard(replicaShardNode);
indexShard = getIndexShard(replicaShardNode, INDEX_NAME);
assertFalse(indexShard.isSearchIdleSupported());
}

public void testPrimaryRelocationWithMultipleWriter() {
internalCluster().startClusterManagerOnlyNode();
String oldPrimary = internalCluster().startDataOnlyNodes(1).get(0);
createIndex(INDEX_NAME, remoteStoreIndexSettings(0));
ensureGreen(INDEX_NAME);
indexBulk(INDEX_NAME, randomIntBetween(5, 10));
String newPrimary = internalCluster().startDataOnlyNodes(1).get(0);
ensureStableCluster(3);
logger.info("--> relocate the shard");

client().admin()
.cluster()
.prepareReroute()
.add(new MoveAllocationCommand(INDEX_NAME, 0, oldPrimary, newPrimary))
.execute()
.actionGet();
TimeValue ACCEPTABLE_RELOCATION_TIME = new TimeValue(5, TimeUnit.MINUTES);
ClusterHealthResponse clusterHealthResponse = client().admin()
.cluster()
.prepareHealth()
.setWaitForEvents(Priority.LANGUID)
.setWaitForNoRelocatingShards(true)
.setTimeout(ACCEPTABLE_RELOCATION_TIME)
.execute()
.actionGet();
assertFalse(clusterHealthResponse.isTimedOut());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.remotestore;

import org.opensearch.action.admin.cluster.health.ClusterHealthResponse;
import org.opensearch.action.admin.indices.flush.FlushRequest;
import org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.opensearch.cluster.health.ClusterHealthStatus;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.routing.allocation.command.MoveAllocationCommand;
import org.opensearch.common.Priority;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.indices.recovery.PeerRecoveryTargetService;
import org.opensearch.plugins.Plugin;
import org.opensearch.remotestore.multipart.mocks.MockFsRepository;
import org.opensearch.remotestore.multipart.mocks.MockFsRepositoryPlugin;
import org.opensearch.repositories.RepositoriesService;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.transport.MockTransportService;
import org.opensearch.transport.TransportService;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0)
public class RemoteStoreMultipleWriterVerificationIT extends RemoteStoreBaseIntegTestCase {

protected final String INDEX_NAME = "remote-store-test-idx-1";

@Override
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put(
remoteStoreClusterSettings(
REPOSITORY_NAME,
segmentRepoPath,
MockFsRepositoryPlugin.TYPE,
REPOSITORY_2_NAME,
translogRepoPath,
MockFsRepositoryPlugin.TYPE
)
)
.build();
}

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Arrays.asList(MockTransportService.TestPlugin.class, MockFsRepositoryPlugin.class);
}

public void testNoMultipleWriterDuringPrimaryRelocation() throws ExecutionException, InterruptedException {
// In this test, we trigger a force flush on existing primary while the primary mode on new primary has been
// activated. There was a bug in primary relocation of remote store enabled indexes where the new primary
// starts uploading translog and segments even before the cluster manager has started this shard. With this test,
// we check that we do not overwrite any file on remote store. Here we will also increase the replica count to
// check that there are no duplicate metadata files for translog or upload.

internalCluster().startClusterManagerOnlyNode();
String oldPrimary = internalCluster().startDataOnlyNodes(1).get(0);
createIndex(INDEX_NAME, remoteStoreIndexSettings(0));
ensureGreen(INDEX_NAME);
indexBulk(INDEX_NAME, randomIntBetween(5, 10));
String newPrimary = internalCluster().startDataOnlyNodes(1).get(0);
ensureStableCluster(3);

IndexShard oldPrimaryIndexShard = getIndexShard(oldPrimary, INDEX_NAME);
RepositoriesService newPrimaryRepositories = internalCluster().getInstance(RepositoriesService.class, newPrimary);
MockFsRepository newPrimaryTranslogRepo = (MockFsRepository) newPrimaryRepositories.repository(REPOSITORY_2_NAME);
CountDownLatch flushLatch = new CountDownLatch(1);

MockTransportService mockTargetTransportService = ((MockTransportService) internalCluster().getInstance(
TransportService.class,
oldPrimary
));
mockTargetTransportService.addSendBehavior((connection, requestId, action, request, options) -> {
if (PeerRecoveryTargetService.Actions.HANDOFF_PRIMARY_CONTEXT.equals(action)) {
newPrimaryTranslogRepo.setSleepSeconds(5);
flushLatch.countDown();
}
connection.sendRequest(requestId, action, request, options);
});

logger.info("--> relocate the shard");
client().admin()
.cluster()
.prepareReroute()
.add(new MoveAllocationCommand(INDEX_NAME, 0, oldPrimary, newPrimary))
.execute()
.actionGet();

CountDownLatch flushDone = new CountDownLatch(1);
Thread flushThread = new Thread(() -> {
try {
flushLatch.await(2, TimeUnit.SECONDS);
oldPrimaryIndexShard.flush(new FlushRequest().waitIfOngoing(true).force(true));
newPrimaryTranslogRepo.setSleepSeconds(0);
flushDone.countDown();
} catch (InterruptedException e) {
throw new AssertionError(e);
}
});
flushThread.start();
flushDone.await(5, TimeUnit.SECONDS);
flushThread.join();

ClusterHealthResponse clusterHealthResponse = client().admin()
.cluster()
.prepareHealth()
.setWaitForStatus(ClusterHealthStatus.GREEN)
.setWaitForEvents(Priority.LANGUID)
.setWaitForNoRelocatingShards(true)
.setTimeout(TimeValue.timeValueSeconds(5))
.execute()
.actionGet();
assertFalse(clusterHealthResponse.isTimedOut());

client().admin()
.indices()
.updateSettings(
new UpdateSettingsRequest(INDEX_NAME).settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1))
)
.get();

clusterHealthResponse = client().admin()
.cluster()
.prepareHealth()
.setWaitForStatus(ClusterHealthStatus.GREEN)
.setWaitForEvents(Priority.LANGUID)
.setWaitForNoRelocatingShards(true)
.setTimeout(TimeValue.timeValueSeconds(5))
.execute()
.actionGet();
assertFalse(clusterHealthResponse.isTimedOut());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,33 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.function.IntSupplier;

public class MockFsBlobStore extends FsBlobStore {

private final boolean triggerDataIntegrityFailure;

public MockFsBlobStore(int bufferSizeInBytes, Path path, boolean readonly, boolean triggerDataIntegrityFailure) throws IOException {
private final IntSupplier sleepSecondsSupplier;

public MockFsBlobStore(
int bufferSizeInBytes,
Path path,
boolean readonly,
boolean triggerDataIntegrityFailure,
IntSupplier sleepSecondsSupplier
) throws IOException {
super(bufferSizeInBytes, path, readonly);
this.triggerDataIntegrityFailure = triggerDataIntegrityFailure;
this.sleepSecondsSupplier = sleepSecondsSupplier;
}

@Override
public BlobContainer blobContainer(BlobPath path) {
try {
if (sleepSecondsSupplier != null) {
assert triggerDataIntegrityFailure == false;
return new MockFsSlowableBlobContainer(this, path, buildAndCreate(path), sleepSecondsSupplier);
}
return new MockFsAsyncBlobContainer(this, path, buildAndCreate(path), triggerDataIntegrityFailure);
} catch (IOException ex) {
throw new OpenSearchException("failed to create blob container", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ public class MockFsRepository extends FsRepository {
false
);

public static Setting<Boolean> SLOW_CONTAINER = Setting.boolSetting("mock_fs_repository.slow_container", false);

private final boolean triggerDataIntegrityFailure;

private final boolean slowContainer;

private volatile int sleepSeconds;

public MockFsRepository(
RepositoryMetadata metadata,
Environment environment,
Expand All @@ -36,11 +42,26 @@ public MockFsRepository(
) {
super(metadata, environment, namedXContentRegistry, clusterService, recoverySettings);
triggerDataIntegrityFailure = TRIGGER_DATA_INTEGRITY_FAILURE.get(metadata.settings());
slowContainer = SLOW_CONTAINER.get(metadata.settings());
}

@Override
protected BlobStore createBlobStore() throws Exception {
FsBlobStore fsBlobStore = (FsBlobStore) super.createBlobStore();
return new MockFsBlobStore(fsBlobStore.bufferSizeInBytes(), fsBlobStore.path(), isReadOnly(), triggerDataIntegrityFailure);
return new MockFsBlobStore(
fsBlobStore.bufferSizeInBytes(),
fsBlobStore.path(),
isReadOnly(),
triggerDataIntegrityFailure,
slowContainer ? this::sleepSeconds : null
);
}

public int sleepSeconds() {
return sleepSeconds;
}

public void setSleepSeconds(Integer sleepSeconds) {
this.sleepSeconds = sleepSeconds;
}
}
Loading

0 comments on commit d0be4aa

Please sign in to comment.