@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
1,022 lines (1,021 loc) • 43.5 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var parallelQueryExecutionContextBase_exports = {};
__export(parallelQueryExecutionContextBase_exports, {
ParallelQueryExecutionContextBase: () => ParallelQueryExecutionContextBase,
ParallelQueryExecutionContextBaseStates: () => ParallelQueryExecutionContextBaseStates
});
module.exports = __toCommonJS(parallelQueryExecutionContextBase_exports);
var import_priorityqueuejs = __toESM(require("priorityqueuejs"));
var import_semaphore = __toESM(require("semaphore"));
var import_statusCodes = require("../common/statusCodes.js");
var import_ErrorResponse = require("../request/ErrorResponse.js");
var import_QueryRange = require("../routing/QueryRange.js");
var import_smartRoutingMapProvider = require("../routing/smartRoutingMapProvider.js");
var import_documentProducer = require("./documentProducer.js");
var import_headerUtils = require("./headerUtils.js");
var import_RidSkipCountFilter = require("./queryFilteringStrategy/RidSkipCountFilter.js");
var import_DiagnosticNodeInternal = require("../diagnostics/DiagnosticNodeInternal.js");
var import_parallelQueryResult = require("./parallelQueryResult.js");
var import_PartitionRangeManager = require("./PartitionRangeManager.js");
var import_logicalPartitionRange = require("./logicalPartitionRange.js");
var ParallelQueryExecutionContextBaseStates = /* @__PURE__ */ ((ParallelQueryExecutionContextBaseStates2) => {
ParallelQueryExecutionContextBaseStates2["started"] = "started";
ParallelQueryExecutionContextBaseStates2["inProgress"] = "inProgress";
ParallelQueryExecutionContextBaseStates2["ended"] = "ended";
return ParallelQueryExecutionContextBaseStates2;
})(ParallelQueryExecutionContextBaseStates || {});
class ParallelQueryExecutionContextBase {
/**
* 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 import_DiagnosticNodeInternal.DiagnosticNodeInternal(
clientContext.diagnosticLevel,
import_DiagnosticNodeInternal.DiagnosticNodeType.PARALLEL_QUERY_NODE,
null
)
};
this.diagnosticNodeWrapper.diagnosticNode.addData({ stateful: true });
this.err = void 0;
this.state = ParallelQueryExecutionContextBase.STATES.started;
this.routingProvider = new import_smartRoutingMapProvider.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 : void 0;
if (this.requestContinuation && !this.options.enableQueryControl) {
throw new Error(
"Continuation tokens are supported when enableQueryControl is set true in FeedOptions"
);
}
this.respHeaders = (0, import_headerUtils.getInitialHeader)();
this.unfilledDocumentProducersQueue = new import_priorityqueuejs.default(
(a, b) => this.compareDocumentProducersByRange(a, b)
);
this.bufferedDocumentProducersQueue = new import_priorityqueuejs.default(
(a, b) => this.documentProducerComparator(b, a)
);
this.sem = (0, import_semaphore.default)(1);
this.sem.take(() => this._initializeDocumentProducers());
}
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 = /* @__PURE__ */ new Map();
patchCounter = 0;
updatedContinuationRanges = /* @__PURE__ */ new Map();
splitRepairProducers = /* @__PURE__ */ new Map();
orderByFilterSnapshots = /* @__PURE__ */ new WeakMap();
completeDrainError;
topologyTransitions = /* @__PURE__ */ new Set();
requiresCompleteDrainAfterSplit;
sem;
diagnosticNodeWrapper;
/**
* 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 !== void 0;
while (true) {
const bufferCycle = await this.bufferDocumentProducers(diagnosticNode);
splitError ??= bufferCycle.splitError;
isRepairingSplit ||= bufferCycle.splitError !== void 0;
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 === void 0 && !bufferCycle.processedTrackedProducer) {
return this.drainBufferedItems();
}
if (!bufferCycle.madeProgress) {
const error = splitError ?? new import_ErrorResponse.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 || (0, import_PartitionRangeManager.isPartitionRangeActive)(mapping.continuationToken, mapping.isActive)
);
const canReturnMeaningfulCheckpoint = !isOrderByQuery || this.requestContinuation !== void 0;
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);
}
if (producer.peakNextItem() !== void 0) {
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 = (0, import_logicalPartitionRange.getLogicalPartitionRange)(
producer.targetPartitionKeyRange,
producer.startEpk,
producer.endEpk
);
const filterSnapshots = this._getOrderByFilterSnapshots(producer);
return {
itemCount,
partitionKeyRange: (0, import_logicalPartitionRange.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() !== void 0;
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;
}
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);
this._enqueueDocumentProducers(documentProducers);
this.sem.leave();
} catch (err) {
this.err = err;
this.sem.leave();
}
}
/**
* Creates document producers from continuation token scenario.
*/
async _createDocumentProducersFromContinuation(targetPartitionRanges) {
const parsedToken = this.parsedContinuationToken ?? this._parseContinuationToken(this.requestContinuation);
const continuationRanges = await this._handlePartitionRangeChanges(parsedToken);
const additionalQueryInfo = this.queryProcessingStrategy.createAdditionalQueryInfo(parsedToken);
const filterResult = this.rangeManager.filterPartitionRanges(
targetPartitionRanges,
continuationRanges,
additionalQueryInfo
);
const rangeTokenPairs = filterResult.rangeTokenPairs;
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, void 0)
);
}
/**
* 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 || void 0;
const logicalRange = (0, import_logicalPartitionRange.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 : void 0;
return this._createTargetPartitionQueryExecutionContext(
partitionTargetRange,
continuationToken,
logicalRange.minInclusive,
logicalRange.maxExclusive,
rangeTokenPair.epkMin !== void 0 && rangeTokenPair.epkMax !== void 0,
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 void 0;
}
/**
* 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 (minInclusiveComparison === 0) {
const aMinEpk = a.startEpk;
const bMinEpk = b.startEpk;
if (aMinEpk !== void 0 && bMinEpk !== void 0) {
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 !== void 0
);
for (let index = 0; index < rangeMappings.length; index++) {
const rangeWithToken = rangeMappings[index];
const isOrderByCheckpoint = rangeWithToken.isOrderByCheckpoint ?? (isOrderBy ? !hasCheckpointMetadata && index === 0 : void 0);
const range = rangeWithToken.queryRange;
const queryRange = new import_QueryRange.QueryRange(range.min, range.max, true, false);
const overlappingRanges = await this.routingProvider.getOverlappingRanges(
this.collectionLink,
[queryRange],
this.getDiagnosticNode()
);
const logicalRanges = overlappingRanges.map(
(partitionKeyRange) => (0, import_logicalPartitionRange.intersectPartitionKeyRange)(partitionKeyRange, range.min, range.max)
).filter((logicalRange) => !!logicalRange);
if (logicalRanges.length === 0) {
throw new import_ErrorResponse.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 import_ErrorResponse.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 : (0, import_PartitionRangeManager.isPartitionRangeActive)(rangeWithToken.continuationToken));
}
/**
* Handles partition merge scenario for continuation token ranges
*/
_mergeWithActiveResponseHeaders(headers) {
(0, import_headerUtils.mergeHeaders)(this.respHeaders, headers);
}
_getAndResetActiveResponseHeaders() {
const ret = this.respHeaders;
this.respHeaders = (0, import_headerUtils.getInitialHeader)();
return ret;
}
getDiagnosticNode() {
return this.diagnosticNodeWrapper.diagnosticNode;
}
async _onTargetPartitionRanges() {
const parsedRanges = this.partitionedQueryExecutionInfo.queryRanges;
const queryRanges = parsedRanges.map((item) => import_QueryRange.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 void 0;
}
const effectiveRange = ParallelQueryExecutionContextBase._getEffectiveQueryRange(documentProducer);
const replacementPartitionKeyRanges = await this._getReplacementPartitionKeyRanges(
effectiveRange,
diagnosticNode
);
if (!isCycleActive()) {
return void 0;
}
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 void 0;
}
const replacementDocumentProducers = replacementRanges.map(
({ max, min, partitionKeyRange, populateEpkRangeHeaders }) => this._createTargetPartitionQueryExecutionContext(
partitionKeyRange,
documentProducer.continuationToken,
min,
max,
populateEpkRangeHeaders,
filterCondition,
filterContext,
documentProducer.isOrderByCheckpoint
)
);
if (!isCycleActive()) {
return void 0;
}
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 }) => (0, import_logicalPartitionRange.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
} : void 0;
}
static _getEffectiveQueryRange(documentProducer) {
const partitionKeyRange = documentProducer.targetPartitionKeyRange;
return new import_QueryRange.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 } : void 0;
}
static _needPartitionKeyRangeCacheRefresh(error) {
return error.code === import_statusCodes.StatusCodes.Gone && "substatus" in error && error["substatus"] === import_statusCodes.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 (typeof rewrittenQuery === "object" && rewrittenQuery !== null && rewrittenQuery.query) {
return rewrittenQuery.query.replace(formatPlaceHolder, replacement);
}
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 import_RidSkipCountFilter.RidSkipCountFilter(filterContext);
}
return new import_documentProducer.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) {
this.sem.leave();
this.err.headers = this._getAndResetActiveResponseHeaders();
reject(this.err);
return;
}
if (this.buffer.length === 0) {
this.sem.leave();
const partitionDataPatchMap2 = this.partitionDataPatchMap;
this.partitionDataPatchMap = /* @__PURE__ */ new Map();
this.patchCounter = 0;
const updatedContinuationRanges2 = Object.fromEntries(
this.updatedContinuationRanges
);
this.updatedContinuationRanges.clear();
const result2 = (0, import_parallelQueryResult.createParallelQueryResult)(
[],
partitionDataPatchMap2,
updatedContinuationRanges2,
void 0
);
return resolve({
result: this.state === ParallelQueryExecutionContextBase.STATES.ended ? void 0 : result2,
headers: this._getAndResetActiveResponseHeaders()
});
}
const bufferedResults = this.buffer;
this.buffer = [];
const partitionDataPatchMap = this.partitionDataPatchMap;
this.partitionDataPatchMap = /* @__PURE__ */ new Map();
this.patchCounter = 0;
const updatedContinuationRanges = Object.fromEntries(
this.updatedContinuationRanges
);
this.updatedContinuationRanges.clear();
this.sem.leave();
const result = (0, import_parallelQueryResult.createParallelQueryResult)(
bufferedResults,
partitionDataPatchMap,
updatedContinuationRanges,
void 0
);
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 === void 0 || this.options.maxDegreeOfParallelism < 1 ? this.unfilledDocumentProducersQueue.size() : 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 !== void 0 || 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);
const nextItem = documentProducer.peakNextItem();
if (nextItem !== void 0) {
this.bufferedDocumentProducersQueue.enq(documentProducer);
return {
madeProgress: shouldTrackProducerProgress,
processedTrackedProducer: shouldTrackProducerProgress
};
} else {
const hasActiveContinuation = (0, import_PartitionRangeManager.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 !== void 0,
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) {
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 {
this.sem.leave();
}
resolve();
return;
});
});
}
updateStates(error) {
if (error) {
this.splitRepairProducers.clear();
this.completeDrainError = void 0;
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 = void 0;
this.topologyTransitions.clear();
this.state = ParallelQueryExecutionContextBase.STATES.ended;
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ParallelQueryExecutionContextBase,
ParallelQueryExecutionContextBaseStates
});
//# sourceMappingURL=parallelQueryExecutionContextBase.js.map