Search in sources :

Example 6 with BackgroundIndexer

use of org.opensearch.test.BackgroundIndexer in project OpenSearch by opensearch-project.

the class RecoveryWhileUnderLoadIT method testRecoverWhileUnderLoadAllocateReplicasRelocatePrimariesTest.

public void testRecoverWhileUnderLoadAllocateReplicasRelocatePrimariesTest() throws Exception {
    logger.info("--> creating test index ...");
    int numberOfShards = numberOfShards();
    assertAcked(prepareCreate("test", 1, Settings.builder().put(SETTING_NUMBER_OF_SHARDS, numberOfShards).put(SETTING_NUMBER_OF_REPLICAS, 1).put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC)));
    final int totalNumDocs = scaledRandomIntBetween(200, 10000);
    int waitFor = totalNumDocs / 10;
    int extraDocs = waitFor;
    try (BackgroundIndexer indexer = new BackgroundIndexer("test", "type", client(), extraDocs)) {
        logger.info("--> waiting for {} docs to be indexed ...", waitFor);
        waitForDocs(waitFor, indexer);
        indexer.assertNoFailures();
        logger.info("--> {} docs indexed", waitFor);
        extraDocs = totalNumDocs / 10;
        waitFor += extraDocs;
        indexer.continueIndexing(extraDocs);
        logger.info("--> flushing the index ....");
        // now flush, just to make sure we have some data in the index, not just translog
        client().admin().indices().prepareFlush().execute().actionGet();
        logger.info("--> waiting for {} docs to be indexed ...", waitFor);
        waitForDocs(waitFor, indexer);
        indexer.assertNoFailures();
        logger.info("--> {} docs indexed", waitFor);
        extraDocs = totalNumDocs - waitFor;
        indexer.continueIndexing(extraDocs);
        logger.info("--> allow 4 nodes for index [test] ...");
        allowNodes("test", 4);
        logger.info("--> waiting for GREEN health status ...");
        assertNoTimeout(client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setTimeout("5m").setWaitForGreenStatus());
        logger.info("--> waiting for {} docs to be indexed ...", totalNumDocs);
        waitForDocs(totalNumDocs, indexer);
        indexer.assertNoFailures();
        logger.info("--> {} docs indexed", totalNumDocs);
        logger.info("--> marking and waiting for indexing threads to stop ...");
        indexer.stopAndAwaitStopped();
        logger.info("--> indexing threads stopped");
        logger.info("--> refreshing the index");
        refreshAndAssert();
        logger.info("--> verifying indexed content");
        iterateAssertCount(numberOfShards, 10, indexer.getIds());
    }
}
Also used : BackgroundIndexer(org.opensearch.test.BackgroundIndexer)

Example 7 with BackgroundIndexer

use of org.opensearch.test.BackgroundIndexer in project OpenSearch by opensearch-project.

the class SimpleBlocksIT method testAddBlockWhileIndexingDocuments.

public void testAddBlockWhileIndexingDocuments() throws Exception {
    final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
    createIndex(indexName);
    final APIBlock block = randomAddableBlock();
    int nbDocs = 0;
    try {
        try (BackgroundIndexer indexer = new BackgroundIndexer(indexName, "_doc", client(), 1000)) {
            indexer.setFailureAssertion(t -> {
                Throwable cause = ExceptionsHelper.unwrapCause(t);
                assertThat(cause, instanceOf(ClusterBlockException.class));
                ClusterBlockException e = (ClusterBlockException) cause;
                assertThat(e.blocks(), hasSize(1));
                assertTrue(e.blocks().stream().allMatch(b -> b.id() == block.getBlock().id()));
            });
            waitForDocs(randomIntBetween(10, 50), indexer);
            assertAcked(client().admin().indices().prepareAddBlock(block, indexName));
            indexer.stopAndAwaitStopped();
            nbDocs += indexer.totalIndexedDocs();
        }
        assertIndexHasBlock(block, indexName);
    } finally {
        disableIndexBlock(indexName, block);
    }
    refresh(indexName);
    assertHitCount(client().prepareSearch(indexName).setSize(0).setTrackTotalHitsUpTo(TRACK_TOTAL_HITS_ACCURATE).get(), nbDocs);
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) IntStream(java.util.stream.IntStream) APIBlock(org.opensearch.cluster.metadata.IndexMetadata.APIBlock) Arrays(java.util.Arrays) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndexResponse(org.opensearch.action.index.IndexResponse) HashMap(java.util.HashMap) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) AddIndexBlockRequestBuilder(org.opensearch.action.admin.indices.readonly.AddIndexBlockRequestBuilder) ArrayList(java.util.ArrayList) ClusterState(org.opensearch.cluster.ClusterState) OpenSearchAssertions.assertHitCount(org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount) Locale(java.util.Locale) Matchers.hasSize(org.hamcrest.Matchers.hasSize) BackgroundIndexer(org.opensearch.test.BackgroundIndexer) TRACK_TOTAL_HITS_ACCURATE(org.opensearch.search.internal.SearchContext.TRACK_TOTAL_HITS_ACCURATE) IndicesOptions.lenientExpandOpen(org.opensearch.action.support.IndicesOptions.lenientExpandOpen) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) OpenSearchAssertions.assertBlocked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertBlocked) Collections.emptySet(java.util.Collections.emptySet) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ExceptionsHelper(org.opensearch.ExceptionsHelper) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) Settings(org.opensearch.common.settings.Settings) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) ShardRouting(org.opensearch.cluster.routing.ShardRouting) Consumer(java.util.function.Consumer) SETTING_READ_ONLY(org.opensearch.cluster.metadata.IndexMetadata.SETTING_READ_ONLY) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) SETTING_BLOCKS_WRITE(org.opensearch.cluster.metadata.IndexMetadata.SETTING_BLOCKS_WRITE) Matchers.equalTo(org.hamcrest.Matchers.equalTo) UpdateSettingsRequestBuilder(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequestBuilder) Matchers.is(org.hamcrest.Matchers.is) IndicesExistsResponse(org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) Matchers.containsString(org.hamcrest.Matchers.containsString) BackgroundIndexer(org.opensearch.test.BackgroundIndexer) APIBlock(org.opensearch.cluster.metadata.IndexMetadata.APIBlock) Matchers.containsString(org.hamcrest.Matchers.containsString) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException)

Example 8 with BackgroundIndexer

use of org.opensearch.test.BackgroundIndexer in project OpenSearch by opensearch-project.

the class OpenSearchMockAPIBasedRepositoryIntegTestCase method testRequestStats.

public void testRequestStats() throws Exception {
    final String repository = createRepository(randomName());
    final String index = "index-no-merges";
    createIndex(index, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build());
    final long nbDocs = randomLongBetween(10_000L, 20_000L);
    try (BackgroundIndexer indexer = new BackgroundIndexer(index, "_doc", client(), (int) nbDocs)) {
        waitForDocs(nbDocs, indexer);
    }
    flushAndRefresh(index);
    ForceMergeResponse forceMerge = client().admin().indices().prepareForceMerge(index).setFlush(true).setMaxNumSegments(1).get();
    assertThat(forceMerge.getSuccessfulShards(), equalTo(1));
    assertHitCount(client().prepareSearch(index).setSize(0).setTrackTotalHits(true).get(), nbDocs);
    final String snapshot = "snapshot";
    assertSuccessfulSnapshot(client().admin().cluster().prepareCreateSnapshot(repository, snapshot).setWaitForCompletion(true).setIndices(index));
    assertAcked(client().admin().indices().prepareDelete(index));
    assertSuccessfulRestore(client().admin().cluster().prepareRestoreSnapshot(repository, snapshot).setWaitForCompletion(true));
    ensureGreen(index);
    assertHitCount(client().prepareSearch(index).setSize(0).setTrackTotalHits(true).get(), nbDocs);
    assertAcked(client().admin().cluster().prepareDeleteSnapshot(repository, snapshot).get());
    final RepositoryStats repositoryStats = StreamSupport.stream(internalCluster().getInstances(RepositoriesService.class).spliterator(), false).map(repositoriesService -> {
        try {
            return repositoriesService.repository(repository);
        } catch (RepositoryMissingException e) {
            return null;
        }
    }).filter(Objects::nonNull).map(Repository::stats).reduce(RepositoryStats::merge).get();
    Map<String, Long> sdkRequestCounts = repositoryStats.requestCounts;
    final Map<String, Long> mockCalls = getMockRequestCounts();
    String assertionErrorMsg = String.format("SDK sent [%s] calls and handler measured [%s] calls", sdkRequestCounts, mockCalls);
    assertEquals(assertionErrorMsg, mockCalls, sdkRequestCounts);
}
Also used : ForceMergeResponse(org.opensearch.action.admin.indices.forcemerge.ForceMergeResponse) Repository(org.opensearch.repositories.Repository) BackgroundIndexer(org.opensearch.test.BackgroundIndexer) RepositoriesService(org.opensearch.repositories.RepositoriesService) RepositoryStats(org.opensearch.repositories.RepositoryStats) RepositoryMissingException(org.opensearch.repositories.RepositoryMissingException)

Example 9 with BackgroundIndexer

use of org.opensearch.test.BackgroundIndexer in project OpenSearch by opensearch-project.

the class OpenSearchMockAPIBasedRepositoryIntegTestCase method testSnapshotWithLargeSegmentFiles.

/**
 * Test the snapshot and restore of an index which has large segments files.
 */
public void testSnapshotWithLargeSegmentFiles() throws Exception {
    final String repository = createRepository(randomName());
    final String index = "index-no-merges";
    createIndex(index, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build());
    final long nbDocs = randomLongBetween(10_000L, 20_000L);
    try (BackgroundIndexer indexer = new BackgroundIndexer(index, "_doc", client(), (int) nbDocs)) {
        waitForDocs(nbDocs, indexer);
    }
    flushAndRefresh(index);
    ForceMergeResponse forceMerge = client().admin().indices().prepareForceMerge(index).setFlush(true).setMaxNumSegments(1).get();
    assertThat(forceMerge.getSuccessfulShards(), equalTo(1));
    assertHitCount(client().prepareSearch(index).setSize(0).setTrackTotalHits(true).get(), nbDocs);
    final String snapshot = "snapshot";
    assertSuccessfulSnapshot(client().admin().cluster().prepareCreateSnapshot(repository, snapshot).setWaitForCompletion(true).setIndices(index));
    assertAcked(client().admin().indices().prepareDelete(index));
    assertSuccessfulRestore(client().admin().cluster().prepareRestoreSnapshot(repository, snapshot).setWaitForCompletion(true));
    ensureGreen(index);
    assertHitCount(client().prepareSearch(index).setSize(0).setTrackTotalHits(true).get(), nbDocs);
    assertAcked(client().admin().cluster().prepareDeleteSnapshot(repository, snapshot).get());
}
Also used : ForceMergeResponse(org.opensearch.action.admin.indices.forcemerge.ForceMergeResponse) BackgroundIndexer(org.opensearch.test.BackgroundIndexer)

Example 10 with BackgroundIndexer

use of org.opensearch.test.BackgroundIndexer in project OpenSearch by opensearch-project.

the class IndexRecoveryIT method testReplicaRecovery.

public void testReplicaRecovery() throws Exception {
    final String nodeA = internalCluster().startNode();
    createIndex(INDEX_NAME, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, SHARD_COUNT).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, REPLICA_COUNT).build());
    ensureGreen(INDEX_NAME);
    final int numOfDocs = scaledRandomIntBetween(0, 200);
    try (BackgroundIndexer indexer = new BackgroundIndexer(INDEX_NAME, "_doc", client(), numOfDocs)) {
        waitForDocs(numOfDocs, indexer);
    }
    refresh(INDEX_NAME);
    assertHitCount(client().prepareSearch(INDEX_NAME).setSize(0).get(), numOfDocs);
    final boolean closedIndex = randomBoolean();
    if (closedIndex) {
        assertAcked(client().admin().indices().prepareClose(INDEX_NAME));
        ensureGreen(INDEX_NAME);
    }
    // force a shard recovery from nodeA to nodeB
    final String nodeB = internalCluster().startNode();
    assertAcked(client().admin().indices().prepareUpdateSettings(INDEX_NAME).setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)));
    ensureGreen(INDEX_NAME);
    final RecoveryResponse response = client().admin().indices().prepareRecoveries(INDEX_NAME).execute().actionGet();
    // we should now have two total shards, one primary and one replica
    List<RecoveryState> recoveryStates = response.shardRecoveryStates().get(INDEX_NAME);
    assertThat(recoveryStates.size(), equalTo(2));
    List<RecoveryState> nodeAResponses = findRecoveriesForTargetNode(nodeA, recoveryStates);
    assertThat(nodeAResponses.size(), equalTo(1));
    List<RecoveryState> nodeBResponses = findRecoveriesForTargetNode(nodeB, recoveryStates);
    assertThat(nodeBResponses.size(), equalTo(1));
    // validate node A recovery
    final RecoveryState nodeARecoveryState = nodeAResponses.get(0);
    final RecoverySource expectedRecoverySource;
    if (closedIndex == false) {
        expectedRecoverySource = RecoverySource.EmptyStoreRecoverySource.INSTANCE;
    } else {
        expectedRecoverySource = RecoverySource.ExistingStoreRecoverySource.INSTANCE;
    }
    assertRecoveryState(nodeARecoveryState, 0, expectedRecoverySource, true, Stage.DONE, null, nodeA);
    validateIndexRecoveryState(nodeARecoveryState.getIndex());
    // validate node B recovery
    final RecoveryState nodeBRecoveryState = nodeBResponses.get(0);
    assertRecoveryState(nodeBRecoveryState, 0, PeerRecoverySource.INSTANCE, false, Stage.DONE, nodeA, nodeB);
    validateIndexRecoveryState(nodeBRecoveryState.getIndex());
    internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodeA));
    if (closedIndex) {
        assertAcked(client().admin().indices().prepareOpen(INDEX_NAME));
    }
    assertHitCount(client().prepareSearch(INDEX_NAME).setSize(0).get(), numOfDocs);
}
Also used : BackgroundIndexer(org.opensearch.test.BackgroundIndexer) RecoveryResponse(org.opensearch.action.admin.indices.recovery.RecoveryResponse) SnapshotRecoverySource(org.opensearch.cluster.routing.RecoverySource.SnapshotRecoverySource) RecoverySource(org.opensearch.cluster.routing.RecoverySource) PeerRecoverySource(org.opensearch.cluster.routing.RecoverySource.PeerRecoverySource)

Aggregations

BackgroundIndexer (org.opensearch.test.BackgroundIndexer)14 ClusterState (org.opensearch.cluster.ClusterState)4 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ShardRouting (org.opensearch.cluster.routing.ShardRouting)3 HashMap (java.util.HashMap)2 List (java.util.List)2 IntStream (java.util.stream.IntStream)2 Matchers.hasSize (org.hamcrest.Matchers.hasSize)2 ActionRequestValidationException (org.opensearch.action.ActionRequestValidationException)2 ForceMergeResponse (org.opensearch.action.admin.indices.forcemerge.ForceMergeResponse)2 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)2 ClusterBlockException (org.opensearch.cluster.block.ClusterBlockException)2 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)2 MoveAllocationCommand (org.opensearch.cluster.routing.allocation.command.MoveAllocationCommand)2 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1