Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
540a16d
Initial changes
aayush3011 Apr 24, 2024
6f49c75
Initial changes
aayush3011 Apr 25, 2024
97509eb
Merge branch 'main' into users/akataria/nonStreamingOrderBy
aayush3011 Apr 25, 2024
528a0eb
[Cosmos][VectorIndex]Adding changes for vectorIndex and vectorEmbeddi…
aayush3011 May 2, 2024
86b36d3
Merge branch 'main' into users/akataria/nonStreamingOrderBy
aayush3011 May 3, 2024
a979c11
Initial changes
aayush3011 May 3, 2024
e2756a5
Initial changes
aayush3011 May 3, 2024
8be2277
Initial changes
aayush3011 May 3, 2024
e491b9d
Resolving comments
aayush3011 May 7, 2024
151bb50
Fixing build issues
aayush3011 May 7, 2024
73afd5b
Merge branch 'Azure:main' into users/akataria/nonStreamingOrderBy
aayush3011 May 8, 2024
148cba5
[Cosmos][VectorSearch] Non Streaming Order By Query (#40085)
aayush3011 May 8, 2024
3b0d751
Merge branch 'Azure:main' into users/akataria/nonStreamingOrderBy
aayush3011 May 9, 2024
87572f7
Merge branch 'feature/vector_search' into users/akataria/nonStreaming…
aayush3011 May 9, 2024
df7e838
[Cosmos][VectorSearch] Non Streaming Order By Query (#40096)
aayush3011 May 9, 2024
179f904
Initial changes
aayush3011 May 9, 2024
5602e33
Merge branch 'users/akataria/nonStreamingOrderBy' of github.com:aayus…
aayush3011 May 9, 2024
36ab9b7
Merge branch 'feature/vector_search' into users/akataria/nonStreaming…
aayush3011 May 9, 2024
70639b5
Initial changes
aayush3011 May 10, 2024
c45c3a5
Fixes
aayush3011 May 10, 2024
6c255ee
Merge branch 'Azure:main' into users/akataria/nonStreamingOrderBy
aayush3011 May 10, 2024
9d427e6
Users/akataria/vectorindexing (#40117)
aayush3011 May 10, 2024
1cadb1b
Merge branch 'feature/VectorSearch' into users/akataria/nonStreamingO…
aayush3011 May 10, 2024
0f1be0c
Users/akataria/non streaming order by (#40118)
aayush3011 May 10, 2024
d4dcad2
Fixing some merge issues
aayush3011 May 10, 2024
cdaa5bc
Fixing some merge issues
aayush3011 May 10, 2024
dfa8b64
Fixing some merge issues
aayush3011 May 10, 2024
7549cbe
Resolving comments
aayush3011 May 14, 2024
c6e2376
Merge branch 'feature/VectorSearch' into users/akataria/nonStreamingO…
aayush3011 May 14, 2024
30d8370
Users/akataria/vectorindexing (#40158)
aayush3011 May 14, 2024
8279358
Users/akataria/non streaming order by (#40159)
aayush3011 May 14, 2024
dd9d13b
Fixing build issue
aayush3011 May 14, 2024
d966af4
Merge branch 'feature/VectorSearch' into users/akataria/nonStreamingO…
aayush3011 May 14, 2024
9eb9208
Fixing build issue
aayush3011 May 14, 2024
1aaecef
Merge branch 'users/akataria/nonStreamingOrderBy' of github.com:aayus…
aayush3011 May 14, 2024
8bf4f8d
Merge branch 'main' into users/akataria/nonStreamingOrderBy
aayush3011 May 16, 2024
943cc4c
Adding tests
aayush3011 May 16, 2024
1db9b31
Adding capability for CI pipeline
aayush3011 May 16, 2024
2770b0f
Updating the PQ logic
aayush3011 May 17, 2024
7002362
Resolving comments, adding new test cases
aayush3011 May 17, 2024
508e94a
Adding argument to run emulator tests
aayush3011 May 17, 2024
822bd67
fixing emulator test pipeline
aayush3011 May 17, 2024
46fe7cb
fixing emulator test pipeline
aayush3011 May 17, 2024
5657b75
Adding logging for variable AZURE_COSMOS_DISABLE_NON_STREAMING_ORDER_BY
aayush3011 May 17, 2024
015a77c
Adding logging for variable AZURE_COSMOS_DISABLE_NON_STREAMING_ORDER_BY
aayush3011 May 17, 2024
f87be45
fixing emulator test pipeline
aayush3011 May 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Resolving comments
  • Loading branch information
aayush3011 committed May 7, 2024
commit e491b9d88acb7d37a2ce48384d83679de282e965
2 changes: 1 addition & 1 deletion sdk/cosmos/azure-cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#### Features Added

* Added a new non-streaming OrderBy query pipeline and a query feature`NonStreamingOrderBy` to support Vector Search queries. - See [PR 39897](https://github.com/Azure/azure-sdk-for-java/pull/39897/)
* Added support for non-streaming OrderBy query and a query feature `NonStreamingOrderBy` to support Vector Search queries. - See [PR 39897](https://github.com/Azure/azure-sdk-for-java/pull/39897/)

#### Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ public class Configs {
public static final String MAX_RETRIES_IN_LOCAL_REGION_WHEN_REMOTE_REGION_PREFERRED = "COSMOS.MAX_RETRIES_IN_LOCAL_REGION_WHEN_REMOTE_REGION_PREFERRED";
private static final int DEFAULT_MAX_RETRIES_IN_LOCAL_REGION_WHEN_REMOTE_REGION_PREFERRED = 1;

private static final String MAX_ITEM_SIZE_FOR_VECTOR_SEARCH = "COSMOS.MAX_ITEM_SIZE_FOR_VECTOR_SEARCH";
private static final int DEFAULT_MAX_ITEM_SIZE_FOR_VECTOR_SEARCH = 50000;

private static final String MAX_ITEM_SIZE_FOR_VECTOR_SEARCH_ENABLED = "COSMOS.MAX_ITEM_SIZE_FOR_VECTOR_SEARCH_ENABLED";

private static final boolean DEFAULT_MAX_ITEM_SIZE_FOR_VECTOR_SEARCH_ENABLED = true;

public static final int MIN_MAX_RETRIES_IN_LOCAL_REGION_WHEN_REMOTE_REGION_PREFERRED = 1;

public static final String TCP_CONNECTION_ACQUISITION_TIMEOUT_IN_MS = "COSMOS.TCP_CONNECTION_ACQUISITION_TIMEOUT_IN_MS";
Expand Down Expand Up @@ -484,6 +491,14 @@ public static int getMaxRetriesInLocalRegionWhenRemoteRegionPreferred() {
MIN_MAX_RETRIES_IN_LOCAL_REGION_WHEN_REMOTE_REGION_PREFERRED);
}

public static int getMaxItemSizeForVectorSearch() {
return getJVMConfigAsInt(MAX_ITEM_SIZE_FOR_VECTOR_SEARCH, DEFAULT_MAX_ITEM_SIZE_FOR_VECTOR_SEARCH);
}

public static boolean getMaxItemSizeForVectorSearchEnabled() {
return getJVMConfigAsBoolean(MAX_ITEM_SIZE_FOR_VECTOR_SEARCH_ENABLED, DEFAULT_MAX_ITEM_SIZE_FOR_VECTOR_SEARCH_ENABLED);
}

public static Duration getMinRetryTimeInLocalRegionWhenRemoteRegionPreferred() {
return
Duration.ofMillis(Math.max(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public final class CosmosQueryRequestOptionsImpl extends CosmosQueryRequestOptio
private boolean queryPlanRetrievalDisallowed;
private boolean emptyPageDiagnosticsEnabled;
private String queryName;
private Integer maxItemSizeForVectorSearch;
private List<CosmosDiagnostics> cancelledRequestDiagnosticsTracker = new ArrayList<>();

/**
Expand Down Expand Up @@ -62,6 +63,7 @@ public CosmosQueryRequestOptionsImpl(CosmosQueryRequestOptionsImpl options) {
this.queryName = options.queryName;
this.feedRange = options.feedRange;
this.cancelledRequestDiagnosticsTracker = options.cancelledRequestDiagnosticsTracker;
this.maxItemSizeForVectorSearch = options.maxItemSizeForVectorSearch;
}

/**
Expand Down Expand Up @@ -196,6 +198,26 @@ public CosmosQueryRequestOptionsImpl setMaxItemCount(Integer maxItemCount) {
return this;
}

/**
* Gets the maximum item size to fetch during non-streaming order by queries.
*
* @return the max number of items for vector search.
*/
public Integer getMaxItemSizeForVectorSearch() {
return maxItemSizeForVectorSearch;
}

/**
* Sets the maximum item size to fetch during non-streaming order by queries.
*
* @param maxItemSizeForVectorSearch the max number of items for vector search.
* return the CosmosQueryRequestOptions.
*/
public CosmosQueryRequestOptionsImpl setMaxItemSizeForVectorSearch(Integer maxItemSizeForVectorSearch) {
this.maxItemSizeForVectorSearch = maxItemSizeForVectorSearch;
return this;
}

/**
* Gets the request continuation token.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.azure.cosmos.BridgeInternal;
import com.azure.cosmos.implementation.BadRequestException;
import com.azure.cosmos.implementation.Configs;
import com.azure.cosmos.implementation.Constants;
import com.azure.cosmos.implementation.DiagnosticsClientContext;
import com.azure.cosmos.implementation.DocumentCollection;
Expand Down Expand Up @@ -240,7 +241,8 @@ private static boolean canCacheQuery(QueryInfo queryInfo) {
&& !queryInfo.hasTop()
&& !queryInfo.hasOffset()
&& !queryInfo.hasDCount()
&& !queryInfo.hasOrderBy();
&& !queryInfo.hasOrderBy()
&& !queryInfo.hasNonStreamingOrderBy();
}

private static boolean isScopedToSinglePartition(CosmosQueryRequestOptions cosmosQueryRequestOptions) {
Expand Down Expand Up @@ -360,19 +362,33 @@ public static <T> Flux<? extends IDocumentQueryExecutionContext<T>> createSpecia
boolean getLazyFeedResponse = queryInfo.hasTop();

// We need to compute the optimal initial age size for non-streaming order-by queries
if (queryInfo.hasNonStreamingOrderBy()) {
if (queryInfo.hasNonStreamingOrderBy() && Configs.getMaxItemSizeForVectorSearchEnabled()) {
// Validate the TOP or LIMIT for non-streaming order-by queries
if (!queryInfo.hasTop() || !queryInfo.hasLimit() || queryInfo.getTop() < 0 || queryInfo.getLimit() < 0) {
if (!queryInfo.hasTop() && !queryInfo.hasLimit() && queryInfo.getTop() < 0 && queryInfo.getLimit() < 0) {
throw new NonStreamingOrderByBadRequestException(HttpConstants.StatusCodes.BADREQUEST,
"Executing a vector search query without TOP or LIMIT can consume a large number of RUs" +
"very fast and have long runtimes. Please ensure you are using one of the above two filters" +
"with you vector search query.");
}

// Validate the size of TOP or LIMIT against MaxItemSizeForVectorSearch
int maxLimit = Math.max(queryInfo.hasTop() ? queryInfo.getTop() : 0,
queryInfo.hasLimit() ? queryInfo.getLimit() : 0);
int maxItemSizeForVectorSearch = Math.max(Configs.getMaxItemSizeForVectorSearch(),
ModelBridgeInternal.getMaxItemSizeForVectorSearchFromQueryRequestOptions(cosmosQueryRequestOptions));
if (maxLimit > maxItemSizeForVectorSearch) {
throw new NonStreamingOrderByBadRequestException(HttpConstants.StatusCodes.BADREQUEST,
"Executing a vector search query with TOP or LIMIT larger than the maxItemSizeForVectorSearch " +
"is not allowed");
}
// Set initialPageSize based on the smallest of TOP or LIMIT
if (queryInfo.hasTop() || queryInfo.hasLimit() ) {
initialPageSize = Math.min(queryInfo.hasTop() ? queryInfo.getTop() : Integer.MAX_VALUE,
if (queryInfo.hasTop() || queryInfo.hasLimit()) {
int pageSizeWithTopOrLimit = Math.min(queryInfo.hasTop() ? queryInfo.getTop() : Integer.MAX_VALUE,
queryInfo.hasLimit() ? queryInfo.getLimit() : Integer.MAX_VALUE);
if (initialPageSize > 0) {
initialPageSize = Math.min(pageSizeWithTopOrLimit, initialPageSize);
} else {
initialPageSize = pageSizeWithTopOrLimit;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,20 @@ public NonStreamingOrderByDocumentQueryExecutionContext(
OrderbyRowComparer<Document> consumeComparer,
UUID correlatedActivityId,
boolean hasSelectValue,
final AtomicBoolean isQueryCancelledOnTimeout,
int maxPageSizePerPartition) {
final AtomicBoolean isQueryCancelledOnTimeout) {
super(diagnosticsClientContext, client, resourceTypeEnum, Document.class, query, cosmosQueryRequestOptions,
resourceLink, rewrittenQuery, correlatedActivityId, hasSelectValue, isQueryCancelledOnTimeout);
this.consumeComparer = consumeComparer;
this.tracker = new RequestChargeTracker();
this.queryMetricMap = new ConcurrentHashMap<>();
this.clientSideRequestStatistics = ConcurrentHashMap.newKeySet();
this.maxPageSizePerPartition = maxPageSizePerPartition;
}

public static Flux<IDocumentQueryExecutionComponent<Document>> createAsync(
DiagnosticsClientContext diagnosticsClientContext,
IDocumentQueryClient client,
PipelinedDocumentQueryParams<Document> initParams,
DocumentCollection collection,
int maxPageSizePerPartition) {
DocumentCollection collection) {

QueryInfo queryInfo = initParams.getQueryInfo();

Expand All @@ -102,8 +99,7 @@ public static Flux<IDocumentQueryExecutionComponent<Document>> createAsync(
new OrderbyRowComparer<>(queryInfo.getOrderBy()),
initParams.getCorrelatedActivityId(),
queryInfo.hasSelectValue(),
initParams.isQueryCancelledOnTimeout(),
maxPageSizePerPartition);
initParams.isQueryCancelledOnTimeout());

context.setTop(initParams.getTop());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.cosmos.implementation.query;

import com.azure.cosmos.BridgeInternal;
import com.azure.cosmos.implementation.ClientSideRequestStatistics;
import com.azure.cosmos.implementation.Configs;
import com.azure.cosmos.implementation.Document;
import com.azure.cosmos.implementation.ImplementationBridgeHelpers;
import com.azure.cosmos.implementation.QueryMetrics;
import com.azure.cosmos.implementation.RequestChargeTracker;
import com.azure.cosmos.implementation.Resource;
import com.azure.cosmos.implementation.query.orderbyquery.OrderByRowResult;
import com.azure.cosmos.implementation.query.orderbyquery.OrderbyRowComparer;
import com.azure.cosmos.models.ModelBridgeInternal;
import reactor.core.publisher.Flux;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.function.Function;

public class NonStreamingOrderByUtils {
Expand All @@ -38,7 +39,7 @@ public static <T extends Resource> Flux<OrderByRowResult<Document>> nonStreaming
toNonStreamingOrderByQueryResultObservable(producer, tracker, queryMetricsMap, initialPageSize,
consumeComparer, clientSideRequestStatistics))
.toArray(Flux[]::new);
return Flux.mergeOrdered(consumeComparer, fluxes);
return Flux.mergeComparingDelayError(1,consumeComparer, fluxes);
}

private static Flux<OrderByRowResult<Document>> toNonStreamingOrderByQueryResultObservable(DocumentProducer<Document> producer,
Expand Down Expand Up @@ -73,7 +74,7 @@ private PageToItemTransformer(RequestChargeTracker tracker, Map<String, QueryMet

@Override
public Flux<OrderByRowResult<Document>> apply(Flux<DocumentProducer<Document>.DocumentProducerFeedResponse> source) {
PriorityQueue<OrderByRowResult<Document>> priorityQueue = new PriorityQueue<>(consumeComparer);
PriorityBlockingQueue<OrderByRowResult<Document>> priorityQueue = new PriorityBlockingQueue<>(initialPageSize, consumeComparer);

return source.flatMap(documentProducerFeedResponse -> {
clientSideRequestStatistics.addAll(
Expand All @@ -88,12 +89,17 @@ public Flux<OrderByRowResult<Document>> apply(Flux<DocumentProducer<Document>.Do
r.toJson(),
documentProducerFeedResponse.sourceFeedRange,
null);
if (priorityQueue.size() < initialPageSize) {
priorityQueue.add(orderByRowResult);
if (Configs.getMaxItemSizeForVectorSearchEnabled()) {
if (priorityQueue.size() < initialPageSize) {
priorityQueue.add(orderByRowResult);
} else {
priorityQueue.add(orderByRowResult);
priorityQueue.poll();
}
} else {
priorityQueue.add(orderByRowResult);
priorityQueue.poll();
}

});
tracker.addCharge(documentProducerFeedResponse.pageResult.getRequestCharge());
// Returning an empty Flux since we are only processing and managing state here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import java.util.function.BiFunction;

import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkNotNull;

/**
* While this class is public, but it is not part of our published public APIs.
* This is meant to be internally used only by our sdk.
Expand Down Expand Up @@ -54,9 +56,10 @@ private static BiFunction<String, PipelinedDocumentQueryParams<Document>, Flux<I
CosmosQueryRequestOptions orderByCosmosQueryRequestOptions =
qryOptAccessor.clone(requestOptions);
if (queryInfo.hasNonStreamingOrderBy()) {
checkNotNull(continuationToken, "Can not use a continuation token for a vector search query ");
qryOptAccessor.getImpl(orderByCosmosQueryRequestOptions).setCustomItemSerializer(null);
documentQueryParams.setCosmosQueryRequestOptions(orderByCosmosQueryRequestOptions);
return NonStreamingOrderByDocumentQueryExecutionContext.createAsync(diagnosticsClientContext, client, documentQueryParams, collection, 1000);
return NonStreamingOrderByDocumentQueryExecutionContext.createAsync(diagnosticsClientContext, client, documentQueryParams, collection);
} else {
ModelBridgeInternal.setQueryRequestOptionsContinuationToken(orderByCosmosQueryRequestOptions, continuationToken);
qryOptAccessor.getImpl(orderByCosmosQueryRequestOptions).setCustomItemSerializer(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,26 @@ CosmosQueryRequestOptions setMaxItemCount(Integer maxItemCount) {
return this;
}

/**
* Gets the maximum item size to fetch during non-streaming order by queries.
*
* @return the max number of items for vector search.
*/
public Integer getMaxItemSizeForVectorSearch() {
return this.actualRequestOptions.getMaxItemSizeForVectorSearch();
}

/**
* Sets the maximum item size to fetch during non-streaming order by queries.
*
* @param maxItemSizeForVectorSearch the max number of items for vector search.
* return the CosmosQueryRequestOptions.
*/
public CosmosQueryRequestOptions setMaxItemSizeForVectorSearch(Integer maxItemSizeForVectorSearch) {
this.actualRequestOptions.setMaxItemSizeForVectorSearch(maxItemSizeForVectorSearch);
return this;
}

/**
* Gets the request continuation token.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,11 @@ public static Integer getMaxItemCountFromQueryRequestOptions(CosmosQueryRequestO
return options.getMaxItemCount();
}

@Warning(value = INTERNAL_USE_ONLY_WARNING)
public static Integer getMaxItemSizeForVectorSearchFromQueryRequestOptions(CosmosQueryRequestOptions options) {
return options.getMaxItemSizeForVectorSearch();
}

@Warning(value = INTERNAL_USE_ONLY_WARNING)
public static String getRequestContinuationFromQueryRequestOptions(CosmosQueryRequestOptions options) {
return options.getRequestContinuation();
Expand Down