@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
947 lines • 47.6 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import PriorityQueue from "priorityqueuejs";
import semaphore from "semaphore";
import { StatusCodes, SubStatusCodes } from "../common/statusCodes.js";
import { ErrorResponse } from "../request/ErrorResponse.js";
import { QueryRange } from "../routing/QueryRange.js";
import { SmartRoutingMapProvider } from "../routing/smartRoutingMapProvider.js";
import { DocumentProducer } from "./documentProducer.js";
import { getInitialHeader, mergeHeaders } from "./headerUtils.js";
import { RidSkipCountFilter } from "./queryFilteringStrategy/RidSkipCountFilter.js";
import { DiagnosticNodeInternal, DiagnosticNodeType, } from "../diagnostics/DiagnosticNodeInternal.js";
import { createParallelQueryResult } from "./parallelQueryResult.js";
import { isPartitionRangeActive } from "./PartitionRangeManager.js";
import { getLogicalPartitionRange, intersectPartitionKeyRange, toEffectivePartitionKeyRange, } from "./logicalPartitionRange.js";
/** @hidden */
export var ParallelQueryExecutionContextBaseStates;
(function (ParallelQueryExecutionContextBaseStates) {
ParallelQueryExecutionContextBaseStates["started"] = "started";
ParallelQueryExecutionContextBaseStates["inProgress"] = "inProgress";
ParallelQueryExecutionContextBaseStates["ended"] = "ended";
})(ParallelQueryExecutionContextBaseStates || (ParallelQueryExecutionContextBaseStates = {}));
/** @hidden */
export class ParallelQueryExecutionContextBase {
clientContext;
collectionLink;
query;
options;
partitionedQueryExecutionInfo;
correlatedActivityId;
rangeManager;
queryProcessingStrategy;
documentProducerComparator;
parsedContinuationToken;
err;
state;
static STATES = ParallelQueryExecutionContextBaseStates;
routingProvider;
requestContinuation;
respHeaders;
unfilledDocumentProducersQueue;
bufferedDocumentProducersQueue;
// TODO: update type of buffer from any --> generic can be used here
buffer;
partitionDataPatchMap = new Map();
patchCounter = 0;
updatedContinuationRanges = new Map();
splitRepairProducers = new Map();
orderByFilterSnapshots = new WeakMap();
completeDrainError;
topologyTransitions = new Set();
requiresCompleteDrainAfterSplit;
sem;
diagnosticNodeWrapper;
/**
* Provides the ParallelQueryExecutionContextBase.
* This is the base class that ParallelQueryExecutionContext and OrderByQueryExecutionContext will derive from.
*
* When handling a parallelized query, it instantiates one instance of
* DocumentProcuder per target partition key range and aggregates the result of each.
*
* @param clientContext - The service endpoint to use to create the client.
* @param collectionLink - The Collection Link
* @param options - Represents the feed options.
* @param partitionedQueryExecutionInfo - PartitionedQueryExecutionInfo
* @hidden
*/
constructor(clientContext, collectionLink, query, options, partitionedQueryExecutionInfo, correlatedActivityId, rangeManager, queryProcessingStrategy, documentProducerComparator, parsedContinuationToken) {
this.clientContext = clientContext;
this.collectionLink = collectionLink;
this.query = query;
this.options = options;
this.partitionedQueryExecutionInfo = partitionedQueryExecutionInfo;
this.correlatedActivityId = correlatedActivityId;
this.rangeManager = rangeManager;
this.queryProcessingStrategy = queryProcessingStrategy;
this.documentProducerComparator = documentProducerComparator;
this.parsedContinuationToken = parsedContinuationToken;
this.clientContext = clientContext;
this.collectionLink = collectionLink;
this.query = query;
this.options = options;
this.partitionedQueryExecutionInfo = partitionedQueryExecutionInfo;
this.correlatedActivityId = correlatedActivityId;
this.diagnosticNodeWrapper = {
consumed: false,
diagnosticNode: new DiagnosticNodeInternal(clientContext.diagnosticLevel, DiagnosticNodeType.PARALLEL_QUERY_NODE, null),
};
this.diagnosticNodeWrapper.diagnosticNode.addData({ stateful: true });
this.err = undefined;
this.state = ParallelQueryExecutionContextBase.STATES.started;
this.routingProvider = new SmartRoutingMapProvider(this.clientContext);
this.buffer = [];
const queryInfo = partitionedQueryExecutionInfo.queryInfo;
this.requiresCompleteDrainAfterSplit =
queryInfo?.hasNonStreamingOrderBy === true ||
(queryInfo?.aggregates?.length ?? 0) > 0 ||
Object.keys(queryInfo?.groupByAliasToAggregateType ?? {}).length > 0 ||
(queryInfo?.groupByExpressions?.length ?? 0) > 0;
this.requestContinuation = options
? options.continuationToken || options.continuation
: undefined;
// Validate continuation token usage immediately
if (this.requestContinuation && !this.options.enableQueryControl) {
throw new Error("Continuation tokens are supported when enableQueryControl is set true in FeedOptions");
}
// response headers of undergoing operation
this.respHeaders = getInitialHeader();
// Make priority queue for documentProducers
this.unfilledDocumentProducersQueue = new PriorityQueue((a, b) => this.compareDocumentProducersByRange(a, b));
this.bufferedDocumentProducersQueue = new PriorityQueue((a, b) => this.documentProducerComparator(b, a));
// Creating the documentProducers
this.sem = semaphore(1);
this.sem.take(() => this._initializeDocumentProducers());
}
/**
* Determine if there are still remaining resources to processs based on the value of the continuation
* token or the elements remaining on the current batch in the QueryIterator.
* @returns true if there is other elements to process in the ParallelQueryExecutionContextBase.
*/
hasMoreResults() {
return (!this.err &&
(this.buffer.length > 0 || this.state !== ParallelQueryExecutionContextBase.STATES.ended));
}
/**
* Fetches more results from the query execution context.
* @param diagnosticNode - Optional diagnostic node for tracing.
* @returns A promise that resolves to the fetched results.
* @hidden
*/
async fetchMore(diagnosticNode) {
let splitError = this.splitRepairProducers.values().next().value ?? this.completeDrainError;
let isRepairingSplit = this.splitRepairProducers.size > 0 || this.completeDrainError !== undefined;
while (true) {
const bufferCycle = await this.bufferDocumentProducers(diagnosticNode);
splitError ??= bufferCycle.splitError;
isRepairingSplit ||= bufferCycle.splitError !== undefined;
await this.fillBufferFromBufferQueue();
this.updateStates(this.err);
if (this.buffer.length > 0 || this.state === ParallelQueryExecutionContextBase.STATES.ended) {
return this.drainBufferedItems();
}
const shouldContinueScheduling = this.shouldContinueScheduling();
if (!isRepairingSplit && !shouldContinueScheduling) {
return this.drainBufferedItems();
}
if (!shouldContinueScheduling &&
bufferCycle.splitError === undefined &&
!bufferCycle.processedTrackedProducer) {
return this.drainBufferedItems();
}
if (!bufferCycle.madeProgress) {
const error = splitError ??
new ErrorResponse("Query execution made no progress while establishing an ORDER BY checkpoint.");
this.updateStates(error);
error.headers = this._getAndResetActiveResponseHeaders();
throw error;
}
if (shouldContinueScheduling) {
this.partitionDataPatchMap.clear();
this.patchCounter = 0;
}
}
}
shouldContinueScheduling() {
const hasActiveProducers = this.unfilledDocumentProducersQueue.size() > 0 ||
this.bufferedDocumentProducersQueue.size() > 0;
const isOrderByQuery = this.rangeManager.getStrategyType() === "OrderByQuery";
const hasMeaningfulCheckpoint = Array.from(this.partitionDataPatchMap.values()).some((mapping) => !isOrderByQuery || isPartitionRangeActive(mapping.continuationToken, mapping.isActive));
const canReturnMeaningfulCheckpoint = !isOrderByQuery || this.requestContinuation !== undefined;
return (this.options.enableQueryControl === true &&
this.buffer.length === 0 &&
!(hasMeaningfulCheckpoint && canReturnMeaningfulCheckpoint) &&
this.state !== ParallelQueryExecutionContextBase.STATES.ended &&
hasActiveProducers);
}
/**
* Processes buffered document producers
* @returns A promise that resolves when processing is complete.
*/
async processBufferedDocumentProducers() {
while (this.hasBufferedProducers() &&
this.shouldProcessBufferedProducers(this.isUnfilledQueueEmpty())) {
const producer = this.getNextBufferedProducer();
if (!producer)
break;
await this.processDocumentProducer(producer);
}
}
/**
* Processes a single document producer using template method pattern.
* Common structure with query-specific processing delegated to subclasses.
*/
async processDocumentProducer(producer) {
const response = await this.fetchFromProducer(producer);
this._mergeWithActiveResponseHeaders(response.headers);
if (response.result) {
this.addToBuffer(response.result);
this.handlePartitionMapping(producer, response.result);
}
// Handle producer lifecycle
if (producer.peakNextItem() !== undefined) {
this.requeueProducer(producer);
}
else if (producer.hasMoreResults()) {
this.moveToUnfilledQueue(producer);
}
else {
this.splitRepairProducers.delete(producer);
}
}
/**
* Handles partition mapping updates - implemented in base class using template method pattern.
* Child classes provide query-specific parameters through abstract methods.
*/
handlePartitionMapping(producer, result) {
this.updatePartitionMapping(this.createPartitionMapping(producer, result?.length || 0));
}
createPartitionMapping(producer, itemCount) {
const logicalRange = getLogicalPartitionRange(producer.targetPartitionKeyRange, producer.startEpk, producer.endEpk);
const filterSnapshots = this._getOrderByFilterSnapshots(producer);
return {
itemCount,
partitionKeyRange: toEffectivePartitionKeyRange(logicalRange),
continuationToken: this.getContinuationToken(producer),
previousContinuationToken: producer.previousContinuationToken,
isActive: producer.hasMoreResults(),
isOrderByCheckpoint: producer.isOrderByCheckpoint,
orderByFilter: filterSnapshots.current,
previousOrderByFilter: filterSnapshots.previous,
};
}
/**
* Gets the continuation token to use - implemented by subclasses.
*/
getContinuationToken(producer) {
const hasMoreBufferedItems = producer.peakNextItem() !== undefined;
return hasMoreBufferedItems ? producer.previousContinuationToken : producer.continuationToken;
}
/**
* Updates partition mapping - creates new entry or merges with existing for ORDER BY queries.
*/
updatePartitionMapping(mapping) {
const currentPatch = this.partitionDataPatchMap.get(this.patchCounter.toString());
const isSamePartition = currentPatch?.partitionKeyRange?.id === mapping.partitionKeyRange.id &&
currentPatch.partitionKeyRange.minInclusive === mapping.partitionKeyRange.minInclusive &&
currentPatch.partitionKeyRange.maxExclusive === mapping.partitionKeyRange.maxExclusive;
if (isSamePartition && currentPatch) {
currentPatch.itemCount += mapping.itemCount;
currentPatch.continuationToken = mapping.continuationToken;
currentPatch.isActive = mapping.isActive;
currentPatch.orderByFilter = mapping.orderByFilter;
currentPatch.previousOrderByFilter = mapping.previousOrderByFilter;
return;
}
// Create new partition mapping entry
this.partitionDataPatchMap.set((++this.patchCounter).toString(), mapping);
}
/**
* Checks if the unfilled queue is empty (used by ORDER BY for processing control).
*/
isUnfilledQueueEmpty() {
return this.unfilledDocumentProducersQueue.size() === 0;
}
/**
* Initializes document producers and fills the priority queue.
* Handles both continuation token and fresh query scenarios.
*/
async _initializeDocumentProducers() {
try {
const targetPartitionRanges = await this._onTargetPartitionRanges();
const documentProducers = this.requestContinuation
? await this._createDocumentProducersFromContinuation(targetPartitionRanges)
: this._createDocumentProducersFromFresh(targetPartitionRanges);
// Fill up our priority queue with documentProducers
this._enqueueDocumentProducers(documentProducers);
this.sem.leave();
}
catch (err) {
this.err = err;
this.sem.leave();
}
}
/**
* Creates document producers from continuation token scenario.
*/
async _createDocumentProducersFromContinuation(targetPartitionRanges) {
// Parse continuation token to get range mappings and check for split/merge scenarios
const parsedToken = this.parsedContinuationToken ?? this._parseContinuationToken(this.requestContinuation);
const continuationRanges = await this._handlePartitionRangeChanges(parsedToken);
// Use strategy to create additional query info from parsed token
const additionalQueryInfo = this.queryProcessingStrategy.createAdditionalQueryInfo(parsedToken);
const filterResult = this.rangeManager.filterPartitionRanges(targetPartitionRanges, continuationRanges, additionalQueryInfo);
// Extract ranges and tokens from the combined result
const rangeTokenPairs = filterResult.rangeTokenPairs;
// Use strategy to create filter context for continuation token processing
const filterContext = this.queryProcessingStrategy.createFilterContext(parsedToken);
return rangeTokenPairs.map((rangeTokenPair) => this._createDocumentProducerFromRangeTokenPair(rangeTokenPair, filterContext));
}
/**
* Creates document producers from fresh query scenario (no continuation token).
*/
_createDocumentProducersFromFresh(targetPartitionRanges) {
return targetPartitionRanges.map((partitionTargetRange) => this._createTargetPartitionQueryExecutionContext(partitionTargetRange, undefined));
}
/**
* Creates a document producer from a range token pair (continuation token scenario).
*/
_createDocumentProducerFromRangeTokenPair(rangeTokenPair, filterContext) {
const partitionTargetRange = rangeTokenPair.range;
const continuationToken = rangeTokenPair.continuationToken;
const filterCondition = rangeTokenPair.filteringCondition || undefined;
const logicalRange = getLogicalPartitionRange(partitionTargetRange, rangeTokenPair.epkMin, rangeTokenPair.epkMax);
const partitionFilterContext = rangeTokenPair.orderByFilter && filterContext
? {
...filterContext,
orderByItems: [...rangeTokenPair.orderByFilter.orderByItems],
rid: rangeTokenPair.orderByFilter.rid,
skipCount: rangeTokenPair.orderByFilter.skipCount,
}
: rangeTokenPair.isOrderByCheckpoint
? filterContext
: undefined;
return this._createTargetPartitionQueryExecutionContext(partitionTargetRange, continuationToken, logicalRange.minInclusive, logicalRange.maxExclusive, rangeTokenPair.epkMin !== undefined && rangeTokenPair.epkMax !== undefined, filterCondition, partitionFilterContext, rangeTokenPair.isOrderByCheckpoint);
}
/**
* Enqueues document producers into the unfilled queue.
*/
_enqueueDocumentProducers(documentProducers) {
documentProducers.forEach((documentProducer) => {
try {
this.unfilledDocumentProducersQueue.enq(documentProducer);
}
catch (e) {
this.err = e;
}
});
}
/**
* Checks if there are buffered document producers ready for processing.
* Encapsulates queue size checking.
*/
hasBufferedProducers() {
return this.bufferedDocumentProducersQueue.size() > 0;
}
/**
* Gets the next buffered document producer for processing.
* Encapsulates queue dequeuing logic.
*/
getNextBufferedProducer() {
if (this.bufferedDocumentProducersQueue.size() > 0) {
return this.bufferedDocumentProducersQueue.deq();
}
return undefined;
}
/**
* Adds items to the result buffer. Handles both single items and arrays.
*/
addToBuffer(items) {
if (Array.isArray(items)) {
if (items.length > 0) {
this.buffer.push(...items);
}
}
else if (items) {
this.buffer.push(items);
}
}
/**
* Moves a producer to the unfilled queue for later processing.
*/
moveToUnfilledQueue(producer) {
this.unfilledDocumentProducersQueue.enq(producer);
}
/**
* Re-queues a producer to the buffered queue for further processing.
*/
requeueProducer(producer) {
this.bufferedDocumentProducersQueue.enq(producer);
}
/**
* Compares two document producers based on their partition key ranges and EPK values.
* Primary comparison: minInclusive values for left-to-right range traversal
* Secondary comparison: EPK ranges when minInclusive values are identical
* @param a - First document producer
* @param b - Second document producer
* @returns Comparison result for priority queue ordering
* @hidden
*/
compareDocumentProducersByRange(a, b) {
const aMinInclusive = a.targetPartitionKeyRange.minInclusive;
const bMinInclusive = b.targetPartitionKeyRange.minInclusive;
const minInclusiveComparison = bMinInclusive.localeCompare(aMinInclusive);
// If minInclusive values are the same, check minEPK ranges if they exist
if (minInclusiveComparison === 0) {
const aMinEpk = a.startEpk;
const bMinEpk = b.startEpk;
if (aMinEpk !== undefined && bMinEpk !== undefined) {
return bMinEpk.localeCompare(aMinEpk);
}
}
return minInclusiveComparison;
}
/**
* Detects partition splits/merges by analyzing parsed continuation token ranges and comparing with current topology
* @param parsed - The continuation token containing range mappings to analyze
* @returns Array of processed ranges with EPK info
*/
async _handlePartitionRangeChanges(parsed) {
const processedRanges = [];
const rangeMappings = parsed.rangeMappings;
if (!rangeMappings || rangeMappings.length === 0) {
return [];
}
const isOrderBy = this.rangeManager.getStrategyType() === "OrderByQuery";
const hasCheckpointMetadata = rangeMappings.some((mapping) => mapping.isOrderByCheckpoint !== undefined);
for (let index = 0; index < rangeMappings.length; index++) {
const rangeWithToken = rangeMappings[index];
const isOrderByCheckpoint = rangeWithToken.isOrderByCheckpoint ??
(isOrderBy ? !hasCheckpointMetadata && index === 0 : undefined);
const range = rangeWithToken.queryRange;
const queryRange = new QueryRange(range.min, range.max, true, false);
const overlappingRanges = await this.routingProvider.getOverlappingRanges(this.collectionLink, [queryRange], this.getDiagnosticNode());
const logicalRanges = overlappingRanges
.map((partitionKeyRange) => intersectPartitionKeyRange(partitionKeyRange, range.min, range.max))
.filter((logicalRange) => !!logicalRange);
if (logicalRanges.length === 0) {
throw new ErrorResponse("Unable to map the continuation token range to the current partition topology.");
}
const topologyUnchanged = logicalRanges.length === 1 &&
logicalRanges[0].partitionKeyRange.minInclusive === range.min &&
logicalRanges[0].partitionKeyRange.maxExclusive === range.max;
if (!topologyUnchanged) {
this._recordPartitionRangeUpdate({ ...rangeWithToken, isOrderByCheckpoint }, logicalRanges);
}
processedRanges.push(...logicalRanges.map((logicalRange) => ({
range: logicalRange.partitionKeyRange,
continuationToken: rangeWithToken.continuationToken,
epkMin: logicalRange.minInclusive,
epkMax: logicalRange.maxExclusive,
isOrderByCheckpoint,
isActive: this._inferRangeActivity(rangeWithToken),
orderByFilter: rangeWithToken.orderByFilter,
})));
}
return processedRanges;
}
/**
* Parses the continuation token based on query type
* @param continuationToken - The continuation token string to parse
* @returns Parsed continuation token object (ORDER BY or Parallel query token)
* @throws ErrorResponse when continuation token is malformed or cannot be parsed
*/
_parseContinuationToken(continuationToken) {
try {
return this.queryProcessingStrategy.parseContinuationToken(continuationToken);
}
catch (e) {
throw new ErrorResponse(`Invalid continuation token format. Expected token with rangeMappings property. ` +
`Ensure the continuation token was generated by a compatible query and has not been modified.`);
}
}
_recordPartitionRangeUpdate(rangeWithToken, replacementRanges) {
const rangeKey = `${rangeWithToken.queryRange.min}-${rangeWithToken.queryRange.max}`;
this.updatedContinuationRanges.set(rangeKey, {
oldRange: {
min: rangeWithToken.queryRange.min,
max: rangeWithToken.queryRange.max,
isMinInclusive: true, // Assumption: min is always inclusive
isMaxInclusive: false, // Assumption: max is always exclusive
},
newRanges: replacementRanges.map((range) => ({
min: range.minInclusive,
max: range.maxExclusive,
isMinInclusive: true,
isMaxInclusive: false,
})),
continuationToken: rangeWithToken.continuationToken,
isActive: this._inferRangeActivity(rangeWithToken),
isOrderByCheckpoint: rangeWithToken.isOrderByCheckpoint,
orderByFilter: rangeWithToken.orderByFilter,
});
}
_inferRangeActivity(rangeWithToken) {
return (rangeWithToken.isActive ??
(this.rangeManager.getStrategyType() === "OrderByQuery"
? true
: isPartitionRangeActive(rangeWithToken.continuationToken)));
}
/**
* Handles partition merge scenario for continuation token ranges
*/
_mergeWithActiveResponseHeaders(headers) {
mergeHeaders(this.respHeaders, headers);
}
_getAndResetActiveResponseHeaders() {
const ret = this.respHeaders;
this.respHeaders = getInitialHeader();
return ret;
}
getDiagnosticNode() {
return this.diagnosticNodeWrapper.diagnosticNode;
}
async _onTargetPartitionRanges() {
// invokes the callback when the target partition ranges are ready
const parsedRanges = this.partitionedQueryExecutionInfo.queryRanges;
const queryRanges = parsedRanges.map((item) => QueryRange.parseFromDict(item));
return this.routingProvider.getOverlappingRanges(this.collectionLink, queryRanges, this.getDiagnosticNode());
}
async _getReplacementPartitionKeyRanges(effectiveRange, diagnosticNode) {
return this.routingProvider.getOverlappingRanges(this.collectionLink, [effectiveRange], diagnosticNode, true);
}
async _enqueueReplacementDocumentProducers(error, diagnosticNode, documentProducer, isCycleActive) {
if (!isCycleActive()) {
return undefined;
}
const effectiveRange = ParallelQueryExecutionContextBase._getEffectiveQueryRange(documentProducer);
const replacementPartitionKeyRanges = await this._getReplacementPartitionKeyRanges(effectiveRange, diagnosticNode);
if (!isCycleActive()) {
return undefined;
}
if (replacementPartitionKeyRanges.length === 0) {
throw error;
}
const filterCondition = documentProducer.getFilterCondition();
const filterContext = documentProducer.getFilterContext();
const replacementRanges = [];
replacementPartitionKeyRanges.forEach((partitionKeyRange) => {
const intersection = ParallelQueryExecutionContextBase._intersectPartitionRange(partitionKeyRange, effectiveRange);
if (!intersection) {
return;
}
const populateEpkRangeHeaders = true;
replacementRanges.push({
max: intersection.max,
min: intersection.min,
partitionKeyRange,
populateEpkRangeHeaders,
});
});
if (replacementRanges.length === 0) {
throw error;
}
const originalIdentity = `${documentProducer.targetPartitionKeyRange.id}:` +
`${effectiveRange.min}:${effectiveRange.max}`;
const replacementIdentities = replacementRanges
.map(({ max, min, partitionKeyRange }) => `${partitionKeyRange.id}:${min}:${max}`)
.sort();
const transition = `${originalIdentity}->${replacementIdentities.join(",")}`;
if ((replacementIdentities.length === 1 && replacementIdentities[0] === originalIdentity) ||
this.topologyTransitions.has(transition)) {
throw error;
}
if (!isCycleActive()) {
return undefined;
}
const replacementDocumentProducers = replacementRanges.map(({ max, min, partitionKeyRange, populateEpkRangeHeaders }) => this._createTargetPartitionQueryExecutionContext(partitionKeyRange, documentProducer.continuationToken, min, max, populateEpkRangeHeaders, filterCondition, filterContext, documentProducer.isOrderByCheckpoint));
if (!isCycleActive()) {
return undefined;
}
this.topologyTransitions.add(transition);
this.splitRepairProducers.delete(documentProducer);
if (this.requiresCompleteDrainAfterSplit) {
this.completeDrainError ??= error;
}
if (this.options.enableQueryControl) {
this._removeRetiredEmptyPatch(effectiveRange);
this._recordPartitionRangeUpdate(this._createQueryRangeWithContinuationToken(documentProducer), replacementRanges.map(({ max, min, partitionKeyRange }) => getLogicalPartitionRange(partitionKeyRange, min, max)));
}
replacementDocumentProducers.forEach((producer) => {
if (producer.hasMoreResults()) {
this.splitRepairProducers.set(producer, error);
this.unfilledDocumentProducersQueue.enq(producer);
}
});
return error;
}
_createQueryRangeWithContinuationToken(documentProducer) {
const partitionRange = documentProducer.targetPartitionKeyRange;
const queryRange = {
min: documentProducer.startEpk ?? partitionRange.minInclusive,
max: documentProducer.endEpk ?? partitionRange.maxExclusive,
};
const filterSnapshots = this._getOrderByFilterSnapshots(documentProducer);
return {
queryRange,
continuationToken: documentProducer.continuationToken,
isActive: documentProducer.hasMoreResults(),
isOrderByCheckpoint: documentProducer.isOrderByCheckpoint,
orderByFilter: filterSnapshots.current,
};
}
_getOrderByFilterSnapshots(documentProducer) {
const existing = this.orderByFilterSnapshots.get(documentProducer);
if (existing?.generation === documentProducer.generation) {
return existing;
}
const snapshots = {
current: ParallelQueryExecutionContextBase._toOrderByFilter(documentProducer.getFilterContext()),
generation: documentProducer.generation,
previous: ParallelQueryExecutionContextBase._toOrderByFilter(documentProducer.getPreviousFilterContext()),
};
this.orderByFilterSnapshots.set(documentProducer, snapshots);
return snapshots;
}
_removeRetiredEmptyPatch(effectiveRange) {
for (const [key, mapping] of this.partitionDataPatchMap) {
if (mapping.itemCount === 0 &&
mapping.partitionKeyRange.minInclusive === effectiveRange.min &&
mapping.partitionKeyRange.maxExclusive === effectiveRange.max) {
this.partitionDataPatchMap.delete(key);
}
}
}
static _toOrderByFilter(filterContext) {
return filterContext && typeof filterContext.rid === "string"
? {
orderByItems: [...filterContext.orderByItems],
rid: filterContext.rid,
skipCount: filterContext.skipCount,
}
: undefined;
}
static _getEffectiveQueryRange(documentProducer) {
const partitionKeyRange = documentProducer.targetPartitionKeyRange;
return new QueryRange(documentProducer.startEpk ?? partitionKeyRange.minInclusive, documentProducer.endEpk ?? partitionKeyRange.maxExclusive, true, false);
}
static _intersectPartitionRange(partitionKeyRange, effectiveRange) {
const min = partitionKeyRange.minInclusive.localeCompare(effectiveRange.min) > 0
? partitionKeyRange.minInclusive
: effectiveRange.min;
const max = partitionKeyRange.maxExclusive.localeCompare(effectiveRange.max) < 0
? partitionKeyRange.maxExclusive
: effectiveRange.max;
return min.localeCompare(max) < 0 ? { min, max } : undefined;
}
static _needPartitionKeyRangeCacheRefresh(error) {
// TODO: any error
return (error.code === StatusCodes.Gone &&
"substatus" in error &&
error["substatus"] === SubStatusCodes.PartitionKeyRangeGone);
}
/**
* Replaces the format placeholder in the rewritten query with the provided filter condition.
* Handles both string queries and SqlQuerySpec objects.
*/
_replaceFormatPlaceholder(rewrittenQuery, formatPlaceHolder, filterCondition) {
const replacement = filterCondition ?? "true";
// If rewrittenQuery has a query property, it's a SqlQuerySpec object
if (typeof rewrittenQuery === "object" && rewrittenQuery !== null && rewrittenQuery.query) {
return rewrittenQuery.query.replace(formatPlaceHolder, replacement);
}
// Otherwise, it's a string
return rewrittenQuery.replace(formatPlaceHolder, replacement);
}
/**
* Creates target partition range Query Execution Context
*/
_createTargetPartitionQueryExecutionContext(partitionKeyTargetRange, continuationToken, startEpk, endEpk, populateEpkRangeHeaders, filterCondition, filterContext, isOrderByCheckpoint) {
const rewrittenQuery = this.partitionedQueryExecutionInfo.queryInfo?.rewrittenQuery;
let sqlQuerySpec;
const query = this.query;
if (typeof query === "string") {
sqlQuerySpec = { query };
}
else {
sqlQuerySpec = query;
}
const formatPlaceHolder = "{documentdb-formattableorderbyquery-filter}";
if (rewrittenQuery) {
sqlQuerySpec = JSON.parse(JSON.stringify(sqlQuerySpec));
const replacedQuery = this._replaceFormatPlaceholder(rewrittenQuery, formatPlaceHolder, filterCondition);
sqlQuerySpec["query"] = replacedQuery;
}
const options = { ...this.options };
options.continuationToken = continuationToken;
options.continuation = continuationToken;
let filter;
if (filterContext) {
filter = new RidSkipCountFilter(filterContext);
}
return new DocumentProducer(this.clientContext, this.collectionLink, sqlQuerySpec, partitionKeyTargetRange, options, this.correlatedActivityId, startEpk, endEpk, populateEpkRangeHeaders, filter, filterCondition, isOrderByCheckpoint);
}
async drainBufferedItems() {
return new Promise((resolve, reject) => {
this.sem.take(() => {
if (this.err) {
// if there is a prior error return error
this.sem.leave();
this.err.headers = this._getAndResetActiveResponseHeaders();
reject(this.err);
return;
}
// return undefined if there is no more results
if (this.buffer.length === 0) {
this.sem.leave();
const partitionDataPatchMap = this.partitionDataPatchMap;
this.partitionDataPatchMap = new Map();
this.patchCounter = 0;
// Get and reset updated continuation ranges
const updatedContinuationRanges = Object.fromEntries(this.updatedContinuationRanges);
this.updatedContinuationRanges.clear();
const result = createParallelQueryResult([], partitionDataPatchMap, updatedContinuationRanges, undefined);
return resolve({
result: this.state === ParallelQueryExecutionContextBase.STATES.ended ? undefined : result,
headers: this._getAndResetActiveResponseHeaders(),
});
}
// draing the entire buffer object and return that in result of return object
const bufferedResults = this.buffer;
this.buffer = [];
// reset the patchToRangeMapping
const partitionDataPatchMap = this.partitionDataPatchMap;
this.partitionDataPatchMap = new Map();
this.patchCounter = 0;
// Get and reset updated continuation ranges
const updatedContinuationRanges = Object.fromEntries(this.updatedContinuationRanges);
this.updatedContinuationRanges.clear();
// release the lock before returning
this.sem.leave();
const result = createParallelQueryResult(bufferedResults, partitionDataPatchMap, updatedContinuationRanges, undefined);
return resolve({
result,
headers: this._getAndResetActiveResponseHeaders(),
});
});
});
}
/**
* Buffers document producers based on the maximum degree of parallelism.
* Moves document producers from the unfilled queue to the buffered queue.
* @param diagnosticNode - The diagnostic node for logging and tracing.
* @returns A promise that resolves when buffering is complete.
*/
async bufferDocumentProducers(diagnosticNode) {
return new Promise((resolve, reject) => {
this.sem.take(async () => {
if (this.err) {
this.sem.leave();
reject(this.err);
return;
}
this.updateStates(this.err);
if (this.state === ParallelQueryExecutionContextBase.STATES.ended) {
this.sem.leave();
resolve({ madeProgress: false, processedTrackedProducer: false });
return;
}
if (this.unfilledDocumentProducersQueue.size() === 0) {
this.sem.leave();
resolve({ madeProgress: false, processedTrackedProducer: false });
return;
}
let closed = false;
let firstError;
let splitError;
const isCycleActive = () => !closed;
const closeCycle = (error) => {
if (!isCycleActive()) {
return false;
}
closed = true;
firstError = error;
return true;
};
try {
const maxDegreeOfParallelism = this.options.maxDegreeOfParallelism === undefined ||
this.options.maxDegreeOfParallelism < 1
? this.unfilledDocumentProducersQueue.size() // number of partitions
: Math.min(this.options.maxDegreeOfParallelism, this.unfilledDocumentProducersQueue.size());
const documentProducers = [];
while (documentProducers.length < maxDegreeOfParallelism &&
this.unfilledDocumentProducersQueue.size() > 0) {
documentProducers.push(this.unfilledDocumentProducersQueue.deq());
}
const bufferDocumentProducer = async (documentProducer) => {
const isSplitRepairProducer = this.splitRepairProducers.has(documentProducer);
const shouldTrackProducerProgress = isSplitRepairProducer ||
this.completeDrainError !== undefined ||
(this.options.enableQueryControl === true &&
this.rangeManager.getStrategyType() === "OrderByQuery");
try {
const previousContinuationToken = documentProducer.continuationToken;
const headers = await documentProducer.bufferMore(diagnosticNode);
if (!isCycleActive()) {
return { madeProgress: false, processedTrackedProducer: false };
}
this._mergeWithActiveResponseHeaders(headers);
// Always track this document producer in patchToRangeMapping, even if it has no results
// This ensures we maintain a record of all partition ranges that were scanned
const nextItem = documentProducer.peakNextItem();
if (nextItem !== undefined) {
this.bufferedDocumentProducersQueue.enq(documentProducer);
return {
madeProgress: shouldTrackProducerProgress,
processedTrackedProducer: shouldTrackProducerProgress,
};
}
else {
const hasActiveContinuation = isPartitionRangeActive(documentProducer.continuationToken);
if (this.options.enableQueryControl || hasActiveContinuation) {
const mapping = this.createPartitionMapping(documentProducer, 0);
const range = mapping.partitionKeyRange;
const patchKey = `empty-${range.id}-${range.minInclusive}-${range.maxExclusive}`;
this.partitionDataPatchMap.set(patchKey, mapping);
}
const hasMoreResults = documentProducer.hasMoreResults();
if (hasMoreResults) {
this.unfilledDocumentProducersQueue.enq(documentProducer);
}
if (shouldTrackProducerProgress && !hasMoreResults) {
if (isSplitRepairProducer) {
this.splitRepairProducers.delete(documentProducer);
}
return {
madeProgress: true,
processedTrackedProducer: true,
};
}
if (shouldTrackProducerProgress &&
documentProducer.continuationToken !== previousContinuationToken) {
return {
madeProgress: true,
processedTrackedProducer: true,
};
}
}
return {
madeProgress: false,
processedTrackedProducer: shouldTrackProducerProgress,
};
}
catch (err) {
if (ParallelQueryExecutionContextBase._needPartitionKeyRangeCacheRefresh(err)) {
if (!isCycleActive()) {
return { madeProgress: false, processedTrackedProducer: false };
}
this._mergeWithActiveResponseHeaders(err.headers);
splitError ??= err;
try {
const replacementError = await this._enqueueReplacementDocumentProducers(err, diagnosticNode, documentProducer, isCycleActive);
return {
madeProgress: replacementError !== undefined,
processedTrackedProducer: shouldTrackProducerProgress,
};
}
catch (repairError) {
if (closeCycle(repairError) && repairError !== err) {
this._mergeWithActiveResponseHeaders(repairError.headers);
}
throw firstError ?? repairError;
}
}
else {
if (closeCycle(err)) {
this._mergeWithActiveResponseHeaders(err.headers);
}
throw firstError ?? err;
}
}
};
const bufferResults = await Promise.all(documentProducers.map((producer) => bufferDocumentProducer(producer)));
closed = true;
resolve({
madeProgress: bufferResults.some(({ madeProgress }) => madeProgress),
processedTrackedProducer: bufferResults.some(({ processedTrackedProducer }) => processedTrackedProducer),
splitError,
});
}
catch (err) {
if (closeCycle(err)) {
this._mergeWithActiveResponseHeaders(err.headers);
}
const terminalError = firstError ?? err;
this.updateStates(terminalError);
this.err.headers = this._getAndResetActiveResponseHeaders();
reject(terminalError);
}
finally {
this.sem.leave();
}
});
});
}
/**
* Drains the buffer of filled document producers and appends their items to the main buffer.
* Uses template method pattern - delegates actual processing to subclasses.
* @returns A promise that resolves when the buffer is filled.
*/
async fillBufferFromBufferQueue() {
return new Promise((resolve, reject) => {
this.sem.take(async () => {
if (this.err) {
// if there is a prior error return error
this.sem.leave();
this.err.headers = this._getAndResetActiveResponseHeaders();
reject(this.err);
return;
}
if (this.state === ParallelQueryExecutionContextBase.STATES.ended ||
this.bufferedDocumentProducersQueue.size() === 0) {
this.sem.leave();
resolve();
return;
}
try {
await this.processBufferedDocumentProducers();
this.updateStates(this.err);
}
catch (err) {
this.updateStates(err);
this.err.headers = this._getAndResetActiveResponseHeaders();
reject(this.err);
return;
}
finally {
// release the lock before returning
this.sem.leave();
}
resolve();
return;
});
});
}
updateStates(error) {
if (error) {
this.splitRepairProducers.clear();
this.completeDrainError = undefined;
this.topologyTransitions.clear();
this.err = error;
this.state = ParallelQueryExecutionContextBase.STATES.ended;
return;
}
if (this.state === ParallelQueryExecutionContextBase.STATES.started) {
this.state = ParallelQueryExecutionContextBase.STATES.inProgress;
}
const hasNoActiveProducers = this.unfilledDocumentProducersQueue.size() === 0 &&
this.bufferedDocumentProducersQueue.size() === 0;
if (hasNoActiveProducers) {
this.splitRepairProducers.clear();
this.completeDrainError = undefined;
this.topologyTransitions.clear();
this.state = ParallelQueryExecutionContextBase.STATES.ended;
}
}
}
//# sourceMappingURL=parallelQueryExecutionContextBase.js.map