Skip to content

Commit

Permalink
Change block check type for index clear cache APIs (opensearch-projec…
Browse files Browse the repository at this point in the history
…t#7538)

Signed-off-by: Kunal Kotwani <[email protected]>
  • Loading branch information
kotwanikunal authored May 12, 2023
1 parent b1871df commit 58edd18
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed
- Enable `./gradlew build` on MacOS by disabling bcw tests ([#7303](https://github.com/opensearch-project/OpenSearch/pull/7303))
- Moved concurrent-search from sandbox plugin to server module behind feature flag ([#7203](https://github.com/opensearch-project/OpenSearch/pull/7203))
- Allow access to indices cache clear APIs for read only indexes ([#7303](https://github.com/opensearch-project/OpenSearch/pull/7303))

### Deprecated

Expand All @@ -120,4 +121,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Security

[Unreleased 3.0]: https://github.com/opensearch-project/OpenSearch/compare/2.x...HEAD
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.7...2.x
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.7...2.x
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ public void testClearIndicesCacheWithBlocks() {
NumShards numShards = getNumShards("test");

// Request is not blocked
for (String blockSetting : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE)) {
for (String blockSetting : Arrays.asList(
SETTING_BLOCKS_READ,
SETTING_BLOCKS_WRITE,
SETTING_READ_ONLY,
SETTING_READ_ONLY_ALLOW_DELETE
)) {
try {
enableIndexBlock("test", blockSetting);
ClearIndicesCacheResponse clearIndicesCacheResponse = client().admin()
Expand All @@ -73,7 +78,7 @@ public void testClearIndicesCacheWithBlocks() {
}
}
// Request is blocked
for (String blockSetting : Arrays.asList(SETTING_READ_ONLY, SETTING_BLOCKS_METADATA, SETTING_READ_ONLY_ALLOW_DELETE)) {
for (String blockSetting : Arrays.asList(SETTING_BLOCKS_METADATA)) {
try {
enableIndexBlock("test", blockSetting);
assertBlocked(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ protected ShardsIterator shards(ClusterState clusterState, ClearIndicesCacheRequ

@Override
protected ClusterBlockException checkGlobalBlock(ClusterState state, ClearIndicesCacheRequest request) {
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE);
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ);
}

@Override
protected ClusterBlockException checkRequestBlock(ClusterState state, ClearIndicesCacheRequest request, String[] concreteIndices) {
return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_WRITE, concreteIndices);
return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_READ, concreteIndices);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.action.admin.indices.cache.clear;

import org.opensearch.action.support.ActionFilters;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.block.ClusterBlock;
import org.opensearch.cluster.block.ClusterBlockLevel;
import org.opensearch.cluster.block.ClusterBlocks;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.indices.IndicesService;
import org.opensearch.rest.RestStatus;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.transport.TransportService;

import java.util.EnumSet;

import static org.mockito.Mockito.mock;

public class TransportClearIndicesCacheActionTests extends OpenSearchTestCase {
private final TransportClearIndicesCacheAction action = new TransportClearIndicesCacheAction(
mock(ClusterService.class),
mock(TransportService.class),
mock(IndicesService.class),
mock(ActionFilters.class),
mock(IndexNameExpressionResolver.class)
);

private final ClusterBlock writeClusterBlock = new ClusterBlock(
1,
"uuid",
"",
true,
true,
true,
RestStatus.OK,
EnumSet.of(ClusterBlockLevel.METADATA_WRITE)
);

private final ClusterBlock readClusterBlock = new ClusterBlock(
1,
"uuid",
"",
true,
true,
true,
RestStatus.OK,
EnumSet.of(ClusterBlockLevel.METADATA_READ)
);

public void testGlobalBlockCheck() {
ClusterBlocks.Builder builder = ClusterBlocks.builder();
builder.addGlobalBlock(writeClusterBlock);
ClusterState metadataWriteBlockedState = ClusterState.builder(ClusterState.EMPTY_STATE).blocks(builder).build();
assertNull(action.checkGlobalBlock(metadataWriteBlockedState, new ClearIndicesCacheRequest()));

builder = ClusterBlocks.builder();
builder.addGlobalBlock(readClusterBlock);
ClusterState metadataReadBlockedState = ClusterState.builder(ClusterState.EMPTY_STATE).blocks(builder).build();
assertNotNull(action.checkGlobalBlock(metadataReadBlockedState, new ClearIndicesCacheRequest()));
}

public void testIndexBlockCheck() {
String indexName = "test";
ClusterBlocks.Builder builder = ClusterBlocks.builder();
builder.addIndexBlock(indexName, writeClusterBlock);
ClusterState metadataWriteBlockedState = ClusterState.builder(ClusterState.EMPTY_STATE).blocks(builder).build();
assertNull(action.checkRequestBlock(metadataWriteBlockedState, new ClearIndicesCacheRequest(), new String[] { indexName }));

builder = ClusterBlocks.builder();
builder.addIndexBlock(indexName, readClusterBlock);
ClusterState metadataReadBlockedState = ClusterState.builder(ClusterState.EMPTY_STATE).blocks(builder).build();
assertNotNull(action.checkRequestBlock(metadataReadBlockedState, new ClearIndicesCacheRequest(), new String[] { indexName }));
}
}

0 comments on commit 58edd18

Please sign in to comment.