From 82786d2061b1b92d4deab775aaacb79b520b8ecc Mon Sep 17 00:00:00 2001 From: Harsh Garg Date: Thu, 3 Oct 2024 23:36:46 +0530 Subject: [PATCH] Separating metadata filtering for pagination strategies Signed-off-by: Harsh Garg --- .../pagination/IndexPaginationStrategy.java | 31 +++++++------- .../pagination/ShardPaginationStrategy.java | 41 +++++++++++++++++-- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/org/opensearch/action/pagination/IndexPaginationStrategy.java b/server/src/main/java/org/opensearch/action/pagination/IndexPaginationStrategy.java index 869ae736296d8..b4a7b18f8ec18 100644 --- a/server/src/main/java/org/opensearch/action/pagination/IndexPaginationStrategy.java +++ b/server/src/main/java/org/opensearch/action/pagination/IndexPaginationStrategy.java @@ -54,8 +54,7 @@ public IndexPaginationStrategy(PageParams pageParams, ClusterState clusterState) clusterState, pageParams.getSort(), Objects.isNull(requestedToken) ? null : requestedToken.lastIndexName, - Objects.isNull(requestedToken) ? null : requestedToken.lastIndexCreationTime, - false + Objects.isNull(requestedToken) ? null : requestedToken.lastIndexCreationTime ); // Trim sortedIndicesList to get the list of indices metadata to be sent as response @@ -69,12 +68,11 @@ public IndexPaginationStrategy(PageParams pageParams, ClusterState clusterState) ); } - protected static List getEligibleIndices( + private static List getEligibleIndices( ClusterState clusterState, String sortOrder, String lastIndexName, - Long lastIndexCreationTime, - boolean includeLastIndex + Long lastIndexCreationTime ) { if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) { return PaginationStrategy.getSortedIndexMetadata( @@ -84,25 +82,28 @@ protected static List getEligibleIndices( } return PaginationStrategy.getSortedIndexMetadata( clusterState, - getMetadataFilter(sortOrder, lastIndexName, lastIndexCreationTime, includeLastIndex), + getMetadataFilter(sortOrder, lastIndexName, lastIndexCreationTime), PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR ); } - protected static Predicate getMetadataFilter( - String sortOrder, - String lastIndexName, - Long lastIndexCreationTime, - boolean includeLastIndex - ) { + private static Predicate getMetadataFilter(String sortOrder, String lastIndexName, Long lastIndexCreationTime) { if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) { return indexMetadata -> true; } - boolean isAscendingSort = sortOrder.equals(PageParams.PARAM_ASC_SORT_VALUE); return metadata -> { if (metadata.getIndex().getName().equals(lastIndexName)) { - return includeLastIndex; - } else if (metadata.getCreationDate() == lastIndexCreationTime) { + return false; + } else { + return getIndexCreateTimeFilter(sortOrder, lastIndexName, lastIndexCreationTime).test(metadata); + } + }; + } + + protected static Predicate getIndexCreateTimeFilter(String sortOrder, String lastIndexName, Long lastIndexCreationTime) { + boolean isAscendingSort = sortOrder.equals(PageParams.PARAM_ASC_SORT_VALUE); + return metadata -> { + if (metadata.getCreationDate() == lastIndexCreationTime) { return isAscendingSort ? metadata.getIndex().getName().compareTo(lastIndexName) > 0 : metadata.getIndex().getName().compareTo(lastIndexName) < 0; diff --git a/server/src/main/java/org/opensearch/action/pagination/ShardPaginationStrategy.java b/server/src/main/java/org/opensearch/action/pagination/ShardPaginationStrategy.java index 7201578a87aca..768f2993f454b 100644 --- a/server/src/main/java/org/opensearch/action/pagination/ShardPaginationStrategy.java +++ b/server/src/main/java/org/opensearch/action/pagination/ShardPaginationStrategy.java @@ -19,6 +19,10 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.function.Predicate; + +import static org.opensearch.action.pagination.IndexPaginationStrategy.ASC_COMPARATOR; +import static org.opensearch.action.pagination.IndexPaginationStrategy.DESC_COMPARATOR; /** * This strategy can be used by the Rest APIs wanting to paginate the responses based on Shards. @@ -35,12 +39,11 @@ public class ShardPaginationStrategy implements PaginationStrategy public ShardPaginationStrategy(PageParams pageParams, ClusterState clusterState) { ShardStrategyToken shardStrategyToken = getShardStrategyToken(pageParams.getRequestedToken()); // Get list of indices metadata sorted by their creation time and filtered by the last sent index - List filteredIndices = IndexPaginationStrategy.getEligibleIndices( + List filteredIndices = getEligibleIndices( clusterState, pageParams.getSort(), Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexName, - Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexCreationTime, - true + Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexCreationTime ); // Get the list of shards and indices belonging to current page. this.pageData = getPageData( @@ -51,6 +54,38 @@ public ShardPaginationStrategy(PageParams pageParams, ClusterState clusterState) ); } + private static List getEligibleIndices( + ClusterState clusterState, + String sortOrder, + String lastIndexName, + Long lastIndexCreationTime + ) { + if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) { + return PaginationStrategy.getSortedIndexMetadata( + clusterState, + PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR + ); + } + return PaginationStrategy.getSortedIndexMetadata( + clusterState, + getMetadataFilter(sortOrder, lastIndexName, lastIndexCreationTime), + PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR + ); + } + + private static Predicate getMetadataFilter(String sortOrder, String lastIndexName, Long lastIndexCreationTime) { + if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) { + return indexMetadata -> true; + } + return metadata -> { + if (metadata.getIndex().getName().equals(lastIndexName)) { + return true; + } else { + return IndexPaginationStrategy.getIndexCreateTimeFilter(sortOrder, lastIndexName, lastIndexCreationTime).test(metadata); + } + }; + } + /** * Will be used to get the list of shards and respective indices to which they belong, * which are to be displayed in a page.