Skip to content

Commit

Permalink
Adding UTs for shard based pagination
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Garg <[email protected]>
  • Loading branch information
Harsh Garg committed Sep 30, 2024
1 parent 6c855ce commit cbc4c06
Show file tree
Hide file tree
Showing 5 changed files with 512 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
*/
public class RestShardsListAction extends RestShardsAction {

private static final int MAX_SUPPORTED_LIST_SHARDS_PAGE_SIZE_STRING = 20000;
private static final int MIN_SUPPORTED_LIST_SHARDS_PAGE_SIZE_STRING = 2000;
protected static final int MAX_SUPPORTED_LIST_SHARDS_PAGE_SIZE = 20000;
protected static final int MIN_SUPPORTED_LIST_SHARDS_PAGE_SIZE = 2000;

@Override
public List<Route> routes() {
Expand All @@ -55,10 +55,10 @@ public boolean isActionPaginated() {
protected PageParams validateAndGetPageParams(RestRequest restRequest) {
PageParams pageParams = super.validateAndGetPageParams(restRequest);
// validate max supported pageSize
if (pageParams.getSize() < MIN_SUPPORTED_LIST_SHARDS_PAGE_SIZE_STRING) {
throw new IllegalArgumentException("size should at least be [" + MIN_SUPPORTED_LIST_SHARDS_PAGE_SIZE_STRING + "]");
} else if (pageParams.getSize() > MAX_SUPPORTED_LIST_SHARDS_PAGE_SIZE_STRING) {
throw new IllegalArgumentException("size should be less than [" + MAX_SUPPORTED_LIST_SHARDS_PAGE_SIZE_STRING + "]");
if (pageParams.getSize() < MIN_SUPPORTED_LIST_SHARDS_PAGE_SIZE) {
throw new IllegalArgumentException("size should at least be [" + MIN_SUPPORTED_LIST_SHARDS_PAGE_SIZE + "]");
} else if (pageParams.getSize() > MAX_SUPPORTED_LIST_SHARDS_PAGE_SIZE) {
throw new IllegalArgumentException("size should be less than [" + MAX_SUPPORTED_LIST_SHARDS_PAGE_SIZE + "]");
}
// Next Token in the request will be validated by the ShardStrategyToken itself.
if (Objects.nonNull(pageParams.getRequestedToken())) {
Expand All @@ -68,6 +68,6 @@ protected PageParams validateAndGetPageParams(RestRequest restRequest) {
}

protected int defaultPageSize() {
return MIN_SUPPORTED_LIST_SHARDS_PAGE_SIZE_STRING;
return MIN_SUPPORTED_LIST_SHARDS_PAGE_SIZE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
import org.opensearch.common.Table;
import org.opensearch.index.shard.DocsStats;
import org.opensearch.index.shard.ShardPath;
import org.opensearch.rest.pagination.PageToken;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.rest.FakeRestRequest;
import org.junit.Before;

import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -64,14 +66,18 @@

public class RestShardsActionTests extends OpenSearchTestCase {

public void testBuildTable() {
private final DiscoveryNode localNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
private List<ShardRouting> shardRoutings = new ArrayList<>();
private Map<ShardRouting, ShardStats> shardStatsMap = new HashMap<>();
private ClusterStateResponse state;
private IndicesStatsResponse stats;

@Before
public void setup() {
final int numShards = randomIntBetween(1, 5);
long numDocs = randomLongBetween(0, 10000);
long numDeletedDocs = randomLongBetween(0, 100);
DiscoveryNode localNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);

List<ShardRouting> shardRoutings = new ArrayList<>(numShards);
Map<ShardRouting, ShardStats> shardStatsMap = new HashMap<>();
String index = "index";
for (int i = 0; i < numShards; i++) {
ShardRoutingState shardRoutingState = ShardRoutingState.fromValue((byte) randomIntBetween(2, 3));
Expand All @@ -97,23 +103,43 @@ public void testBuildTable() {
when(indexStats.getPrimaries()).thenReturn(new CommonStats());
when(indexStats.getTotal()).thenReturn(new CommonStats());

IndicesStatsResponse stats = mock(IndicesStatsResponse.class);
stats = mock(IndicesStatsResponse.class);
when(stats.asMap()).thenReturn(shardStatsMap);

DiscoveryNodes discoveryNodes = mock(DiscoveryNodes.class);
when(discoveryNodes.get(localNode.getId())).thenReturn(localNode);

ClusterStateResponse state = mock(ClusterStateResponse.class);
state = mock(ClusterStateResponse.class);
RoutingTable routingTable = mock(RoutingTable.class);
when(routingTable.allShards()).thenReturn(shardRoutings);
ClusterState clusterState = mock(ClusterState.class);
when(clusterState.routingTable()).thenReturn(routingTable);
when(clusterState.nodes()).thenReturn(discoveryNodes);
when(state.getState()).thenReturn(clusterState);
}

public void testBuildTable() {
final RestShardsAction action = new RestShardsAction();
final Table table = action.buildTable(new FakeRestRequest(), state, stats, state.getState().routingTable().allShards(), null);
assertTable(table);
}

public void testBuildTableWithPageToken() {
final RestShardsAction action = new RestShardsAction();
final Table table = action.buildTable(
new FakeRestRequest(),
state,
stats,
state.getState().routingTable().allShards(),
new PageToken("foo", "test")
);
assertTable(table);
assertNotNull(table.getPageToken());
assertEquals("foo", table.getPageToken().getNextToken());
assertEquals("test", table.getPageToken().getPaginatedEntity());
}

private void assertTable(Table table) {
// now, verify the table is correct
List<Table.Cell> headers = table.getHeaders();
assertThat(headers.get(0).value, equalTo("index"));
Expand All @@ -128,7 +154,7 @@ public void testBuildTable() {
assertThat(headers.get(79).value, equalTo("docs.deleted"));

final List<List<Table.Cell>> rows = table.getRows();
assertThat(rows.size(), equalTo(numShards));
assertThat(rows.size(), equalTo(shardRoutings.size()));

Iterator<ShardRouting> shardRoutingsIt = shardRoutings.iterator();
for (final List<Table.Cell> row : rows) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.rest.action.list;

import org.opensearch.OpenSearchParseException;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.pagination.PageParams;
import org.opensearch.rest.pagination.PaginationStrategy;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.rest.FakeRestRequest;

import java.util.HashMap;
import java.util.Map;

import static org.opensearch.rest.action.list.RestShardsListAction.MAX_SUPPORTED_LIST_SHARDS_PAGE_SIZE;
import static org.opensearch.rest.action.list.RestShardsListAction.MIN_SUPPORTED_LIST_SHARDS_PAGE_SIZE;
import static org.opensearch.rest.pagination.PageParams.PARAM_ASC_SORT_VALUE;

public class RestShardsListActionTests extends OpenSearchTestCase {

private final RestShardsListAction action = new RestShardsListAction();

public void testShardsListActionIsPaginated() {
assertTrue(action.isActionPaginated());
}

public void testValidateAndGetPageParamsWithDefaultParams() {
Map<String, String> params = new HashMap<>();
RestRequest restRequest = new FakeRestRequest(params);
PageParams pageParams = action.validateAndGetPageParams(restRequest);
assertEquals(MIN_SUPPORTED_LIST_SHARDS_PAGE_SIZE, pageParams.getSize());
assertEquals(PARAM_ASC_SORT_VALUE, pageParams.getSort());
assertNull(pageParams.getRequestedToken());
}

public void testValidateAndGetPageParamsWithSizeBelowMin() {
Map<String, String> params = new HashMap<>();
params.put("size", String.valueOf(MIN_SUPPORTED_LIST_SHARDS_PAGE_SIZE - 1));
RestRequest restRequest = new FakeRestRequest(params);
assertThrows(IllegalArgumentException.class, () -> action.validateAndGetPageParams(restRequest));
}

public void testValidateAndGetPageParamsWithSizeAboveRange() {
Map<String, String> params = new HashMap<>();
params.put("size", String.valueOf(MAX_SUPPORTED_LIST_SHARDS_PAGE_SIZE * 10));
RestRequest restRequest = new FakeRestRequest(params);
assertThrows(IllegalArgumentException.class, () -> action.validateAndGetPageParams(restRequest));
}

public void testValidateAndGetPageParamsWithInvalidRequestToken() {
Map<String, String> params = new HashMap<>();
params.put("next_token", PaginationStrategy.encryptStringToken("1|-1|test"));
RestRequest restRequest = new FakeRestRequest(params);
assertThrows(OpenSearchParseException.class, () -> action.validateAndGetPageParams(restRequest));
}

}
Loading

0 comments on commit cbc4c06

Please sign in to comment.