Skip to content

Commit 1ecc447

Browse files
committed
Add unit test
1 parent 1c75721 commit 1ecc447

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

server/src/test/java/org/elasticsearch/action/search/TransportSearchActionTests.java

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import org.elasticsearch.cluster.node.VersionInformation;
4242
import org.elasticsearch.cluster.project.TestProjectResolvers;
4343
import org.elasticsearch.cluster.routing.IndexRoutingTable;
44+
import org.elasticsearch.cluster.routing.OperationRouting;
45+
import org.elasticsearch.cluster.routing.ShardIterator;
4446
import org.elasticsearch.cluster.routing.ShardRouting;
4547
import org.elasticsearch.cluster.routing.ShardRoutingState;
4648
import org.elasticsearch.cluster.routing.TestShardRouting;
@@ -133,6 +135,8 @@
133135
import static org.hamcrest.Matchers.hasSize;
134136
import static org.mockito.ArgumentMatchers.any;
135137
import static org.mockito.ArgumentMatchers.anyBoolean;
138+
import static org.mockito.ArgumentMatchers.eq;
139+
import static org.mockito.ArgumentMatchers.nullable;
136140
import static org.mockito.Mockito.mock;
137141
import static org.mockito.Mockito.when;
138142

@@ -1812,4 +1816,106 @@ public void onFailure(Exception ex) {
18121816
assertTrue(ESTestCase.terminate(threadPool));
18131817
}
18141818
}
1819+
1820+
public void testSkippedIndicesWithRefreshBlock() {
1821+
final ProjectId projectId = randomProjectIdOrDefault();
1822+
1823+
String normalIndexName = "test-normal";
1824+
String blockedIndexName = "test-blocked";
1825+
final String[] indexNames = {normalIndexName, blockedIndexName};
1826+
final Index normalIndex = new Index(normalIndexName, UUIDs.randomBase64UUID());
1827+
final Index blockedIndex = new Index(blockedIndexName, UUIDs.randomBase64UUID());
1828+
final int numberOfShards = randomIntBetween(1, 3);
1829+
final int numberOfReplicas = randomIntBetween(0, 1);
1830+
final int totalShards = numberOfShards + numberOfShards * numberOfReplicas;
1831+
1832+
ClusterState clusterState = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(
1833+
projectId,
1834+
indexNames,
1835+
numberOfShards,
1836+
numberOfReplicas
1837+
);
1838+
ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(clusterState.blocks());
1839+
blocksBuilder.addIndexBlock(projectId, "test-blocked", IndexMetadata.INDEX_REFRESH_BLOCK);
1840+
clusterState = ClusterState.builder(clusterState).blocks(blocksBuilder).build();
1841+
List<ShardIterator> shardIts = new ArrayList<>();
1842+
for (int i = 0; i < totalShards; i++) {
1843+
shardIts.add(new ShardIterator(new ShardId(normalIndex, randomInt()), Collections.emptyList()));
1844+
shardIts.add(new ShardIterator(new ShardId(blockedIndex, randomInt()), Collections.emptyList()));
1845+
}
1846+
final OperationRouting operationRouting = mock(OperationRouting.class);
1847+
when(
1848+
operationRouting.searchShards(
1849+
eq(clusterState.projectState(projectId)),
1850+
eq(indexNames),
1851+
any(),
1852+
nullable(String.class),
1853+
any(),
1854+
any()
1855+
)
1856+
).thenReturn(shardIts);
1857+
ClusterService clusterService = mock(ClusterService.class);
1858+
when(clusterService.state()).thenReturn(clusterState);
1859+
when(clusterService.getSettings()).thenReturn(Settings.EMPTY);
1860+
when(clusterService.operationRouting()).thenReturn(operationRouting);
1861+
1862+
Settings settings = Settings.builder()
1863+
.put("node.name", TransportSearchAction.class.getSimpleName())
1864+
.build();
1865+
TransportVersion transportVersion = TransportVersionUtils.getNextVersion(TransportVersions.MINIMUM_CCS_VERSION, true);
1866+
ThreadPool threadPool = new ThreadPool(settings, MeterRegistry.NOOP, new DefaultBuiltInExecutorBuilders());
1867+
try {
1868+
TransportService transportService = MockTransportService.createNewService(
1869+
Settings.EMPTY,
1870+
VersionInformation.CURRENT,
1871+
transportVersion,
1872+
threadPool
1873+
);
1874+
NodeClient client = new NodeClient(settings, threadPool);
1875+
SearchService searchService = mock(SearchService.class);
1876+
when(searchService.getRewriteContext(any(), any(), any(), anyBoolean())).thenReturn(
1877+
new QueryRewriteContext(null, null, null, null, null, null)
1878+
);
1879+
1880+
TransportSearchAction transportSearchAction = new TransportSearchAction(
1881+
threadPool,
1882+
new NoneCircuitBreakerService(),
1883+
transportService,
1884+
searchService,
1885+
null,
1886+
new SearchTransportService(transportService, client, null),
1887+
null,
1888+
clusterService,
1889+
new ActionFilters(Collections.emptySet()),
1890+
TestProjectResolvers.usingRequestHeader(threadPool.getThreadContext()),
1891+
TestIndexNameExpressionResolver.newInstance(threadPool.getThreadContext()),
1892+
null,
1893+
null,
1894+
new SearchResponseMetrics(TelemetryProvider.NOOP.getMeterRegistry()),
1895+
client,
1896+
new UsageService()
1897+
);
1898+
1899+
SearchRequest searchRequest = new SearchRequest(indexNames);
1900+
searchRequest.allowPartialSearchResults(true);
1901+
List<SearchShardIterator> searchShardIts = transportSearchAction.getLocalShardsIterator(
1902+
clusterState.projectState(projectId),
1903+
searchRequest,
1904+
searchRequest.getLocalClusterAlias(),
1905+
new HashSet<>(),
1906+
indexNames
1907+
);
1908+
1909+
assertThat(searchShardIts.size(), equalTo(shardIts.size()));
1910+
for (SearchShardIterator searchShardIt : searchShardIts) {
1911+
if (searchShardIt.skip()) {
1912+
assertThat(searchShardIt.shardId().getIndexName(), equalTo("test-blocked"));
1913+
} else {
1914+
assertThat(searchShardIt.shardId().getIndexName(), equalTo("test-normal"));
1915+
}
1916+
}
1917+
} finally {
1918+
assertTrue(ESTestCase.terminate(threadPool));
1919+
}
1920+
}
18151921
}

0 commit comments

Comments
 (0)