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);
}
}
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);
}
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
}
}
}
}
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);
}
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());
}
}
}
}
Aggregations