Search in sources :

Example 1 with APIBlock

use of org.opensearch.cluster.metadata.IndexMetadata.APIBlock in project OpenSearch by opensearch-project.

the class SimpleBlocksIT method testSameBlockTwice.

public void testSameBlockTwice() throws Exception {
    final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
    createIndex(indexName);
    if (randomBoolean()) {
        indexRandom(randomBoolean(), false, randomBoolean(), IntStream.range(0, randomIntBetween(1, 10)).mapToObj(i -> client().prepareIndex(indexName).setId(String.valueOf(i)).setSource("num", i)).collect(toList()));
    }
    final APIBlock block = randomAddableBlock();
    try {
        assertAcked(client().admin().indices().prepareAddBlock(block, indexName));
        assertIndexHasBlock(block, indexName);
        // Second add block should be acked too, even if it was a METADATA block
        assertAcked(client().admin().indices().prepareAddBlock(block, indexName));
        assertIndexHasBlock(block, indexName);
    } finally {
        disableIndexBlock(indexName, block);
    }
}
Also used : APIBlock(org.opensearch.cluster.metadata.IndexMetadata.APIBlock) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with APIBlock

use of org.opensearch.cluster.metadata.IndexMetadata.APIBlock 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 3 with APIBlock

use of org.opensearch.cluster.metadata.IndexMetadata.APIBlock in project OpenSearch by opensearch-project.

the class SimpleBlocksIT method testAddBlockWhileDeletingIndices.

public void testAddBlockWhileDeletingIndices() throws Exception {
    final String[] indices = new String[randomIntBetween(3, 10)];
    for (int i = 0; i < indices.length; i++) {
        final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
        createIndex(indexName);
        if (randomBoolean()) {
            indexRandom(randomBoolean(), false, randomBoolean(), IntStream.range(0, 10).mapToObj(n -> client().prepareIndex(indexName).setId(String.valueOf(n)).setSource("num", n)).collect(toList()));
        }
        indices[i] = indexName;
    }
    assertThat(client().admin().cluster().prepareState().get().getState().metadata().indices().size(), equalTo(indices.length));
    final List<Thread> threads = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final APIBlock block = randomAddableBlock();
    Consumer<Exception> exceptionConsumer = t -> {
        Throwable cause = ExceptionsHelper.unwrapCause(t);
        if (cause instanceof ClusterBlockException) {
            ClusterBlockException e = (ClusterBlockException) cause;
            assertThat(e.blocks(), hasSize(1));
            assertTrue(e.blocks().stream().allMatch(b -> b.id() == block.getBlock().id()));
        } else {
            assertThat(cause, instanceOf(IndexNotFoundException.class));
        }
    };
    try {
        for (final String indexToDelete : indices) {
            threads.add(new Thread(() -> {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    throw new AssertionError(e);
                }
                try {
                    assertAcked(client().admin().indices().prepareDelete(indexToDelete));
                } catch (final Exception e) {
                    exceptionConsumer.accept(e);
                }
            }));
        }
        for (final String indexToBlock : indices) {
            threads.add(new Thread(() -> {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    throw new AssertionError(e);
                }
                try {
                    client().admin().indices().prepareAddBlock(block, indexToBlock).get();
                } catch (final Exception e) {
                    exceptionConsumer.accept(e);
                }
            }));
        }
        for (Thread thread : threads) {
            thread.start();
        }
        latch.countDown();
        for (Thread thread : threads) {
            thread.join();
        }
    } finally {
        for (final String indexToBlock : indices) {
            try {
                disableIndexBlock(indexToBlock, block);
            } catch (IndexNotFoundException infe) {
            // ignore
            }
        }
    }
}
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) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) APIBlock(org.opensearch.cluster.metadata.IndexMetadata.APIBlock) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Example 4 with APIBlock

use of org.opensearch.cluster.metadata.IndexMetadata.APIBlock in project OpenSearch by opensearch-project.

the class SimpleBlocksIT method testAddIndexBlock.

public void testAddIndexBlock() throws Exception {
    final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
    createIndex(indexName);
    final int nbDocs = randomIntBetween(0, 50);
    indexRandom(randomBoolean(), false, randomBoolean(), IntStream.range(0, nbDocs).mapToObj(i -> client().prepareIndex(indexName).setId(String.valueOf(i)).setSource("num", i)).collect(toList()));
    final APIBlock block = randomAddableBlock();
    try {
        assertAcked(client().admin().indices().prepareAddBlock(block, indexName));
        assertIndexHasBlock(block, indexName);
    } finally {
        disableIndexBlock(indexName, block);
    }
    client().admin().indices().prepareRefresh(indexName).get();
    assertHitCount(client().prepareSearch(indexName).setSize(0).get(), nbDocs);
}
Also used : APIBlock(org.opensearch.cluster.metadata.IndexMetadata.APIBlock) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 5 with APIBlock

use of org.opensearch.cluster.metadata.IndexMetadata.APIBlock in project OpenSearch by opensearch-project.

the class SimpleBlocksIT method testAddBlocksWhileExistingBlocks.

public void testAddBlocksWhileExistingBlocks() {
    createIndex("test");
    ensureGreen("test");
    for (APIBlock otherBlock : APIBlock.values()) {
        if (otherBlock == APIBlock.READ_ONLY_ALLOW_DELETE) {
            continue;
        }
        for (APIBlock block : Arrays.asList(APIBlock.READ, APIBlock.WRITE)) {
            try {
                enableIndexBlock("test", block.settingName());
                // Adding a block is not blocked
                AcknowledgedResponse addBlockResponse = client().admin().indices().prepareAddBlock(otherBlock, "test").get();
                assertAcked(addBlockResponse);
            } finally {
                disableIndexBlock("test", otherBlock.settingName());
                disableIndexBlock("test", block.settingName());
            }
        }
        for (APIBlock block : Arrays.asList(APIBlock.READ_ONLY, APIBlock.METADATA, APIBlock.READ_ONLY_ALLOW_DELETE)) {
            boolean success = false;
            try {
                enableIndexBlock("test", block.settingName());
                // Adding a block is blocked when there is a metadata block and the new block to be added is not a metadata block
                if (block.getBlock().contains(ClusterBlockLevel.METADATA_WRITE) && otherBlock.getBlock().contains(ClusterBlockLevel.METADATA_WRITE) == false) {
                    assertBlocked(client().admin().indices().prepareAddBlock(otherBlock, "test"));
                } else {
                    assertAcked(client().admin().indices().prepareAddBlock(otherBlock, "test"));
                    success = true;
                }
            } finally {
                if (success) {
                    disableIndexBlock("test", otherBlock.settingName());
                }
                disableIndexBlock("test", block.settingName());
            }
        }
    }
}
Also used : APIBlock(org.opensearch.cluster.metadata.IndexMetadata.APIBlock) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse)

Aggregations

APIBlock (org.opensearch.cluster.metadata.IndexMetadata.APIBlock)9 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ClusterState (org.opensearch.cluster.ClusterState)5 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Consumer (java.util.function.Consumer)4 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)4 ClusterBlockLevel (org.opensearch.cluster.block.ClusterBlockLevel)4 Settings (org.opensearch.common.settings.Settings)4 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)4 Collections.emptySet (java.util.Collections.emptySet)3 Locale (java.util.Locale)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Collectors.toList (java.util.stream.Collectors.toList)3 IntStream (java.util.stream.IntStream)3 Matchers.equalTo (org.hamcrest.Matchers.equalTo)3 Matchers.hasSize (org.hamcrest.Matchers.hasSize)3 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)3