Beispiel #1
0
 public DefaultDocumentQueryExecutionContext(
     IDocumentQueryClient client,
     ResourceType resourceTypeEnum,
     Type resourceType,
     Expression expression,
     FeedOptions feedOptions,
     string resourceLink,
     bool isContinuationExpected,
     Guid correlatedActivityId) :
     base(
         client,
         resourceTypeEnum,
         resourceType,
         expression,
         feedOptions,
         resourceLink,
         false,
         correlatedActivityId)
 {
     this.isContinuationExpected = isContinuationExpected;
     this.fetchSchedulingMetrics = new SchedulingStopwatch();
     this.fetchSchedulingMetrics.Ready();
     this.providedRangesCache            = new Dictionary <string, IReadOnlyList <Range <string> > >();
     this.fetchExecutionRangeAccumulator = new FetchExecutionRangeAccumulator(singlePartitionKeyId);
     this.retries = -1;
     this.partitionRoutingHelper = new PartitionRoutingHelper();
 }
Beispiel #2
0
        internal override Task <QueryResponseCore> ExecuteQueryAsync(
            SqlQuerySpec querySpecForInit,
            string continuationToken,
            PartitionKeyRangeIdentity partitionKeyRange,
            bool isContinuationExpected,
            int pageSize,
            SchedulingStopwatch schedulingStopwatch,
            CancellationToken cancellationToken)
        {
            QueryRequestOptions requestOptions = null;

            if (this.queryRequestOptions != null)
            {
                requestOptions = this.queryRequestOptions.Clone();
            }

            return(this.QueryClient.ExecuteItemQueryAsync(
                       resourceUri: this.ResourceLink,
                       resourceType: this.ResourceTypeEnum,
                       operationType: this.OperationTypeEnum,
                       requestOptions: requestOptions,
                       sqlQuerySpec: querySpecForInit,
                       continuationToken: continuationToken,
                       partitionKeyRange: partitionKeyRange,
                       isContinuationExpected: isContinuationExpected,
                       pageSize: pageSize,
                       schedulingStopwatch: schedulingStopwatch,
                       cancellationToken: cancellationToken));
        }
Beispiel #3
0
 internal abstract Task <QueryResponseCore> ExecuteQueryAsync(
     SqlQuerySpec querySpecForInit,
     string continuationToken,
     PartitionKeyRangeIdentity partitionKeyRange,
     bool isContinuationExpected,
     int pageSize,
     SchedulingStopwatch schedulingStopwatch,
     CancellationToken cancellationToken);
Beispiel #4
0
        /// <summary>
        /// Initializes a new instance of the ItemProducer class.
        /// </summary>
        /// <param name="queryContext">request context</param>
        /// <param name="querySpecForInit">query spec for initialization</param>
        /// <param name="partitionKeyRange">The partition key range.</param>
        /// <param name="createRetryPolicyFunc">The callback to create the retry policy.</param>
        /// <param name="produceAsyncCompleteCallback">The callback to call once you are done fetching.</param>
        /// <param name="equalityComparer">The comparer to use to determine whether the producer has seen a new document.</param>
        /// <param name="initialPageSize">The initial page size.</param>
        /// <param name="initialContinuationToken">The initial continuation token.</param>
        public ItemProducer(
            CosmosQueryContext queryContext,
            SqlQuerySpec querySpecForInit,
            PartitionKeyRange partitionKeyRange,
            Func <IDocumentClientRetryPolicy> createRetryPolicyFunc,
            ProduceAsyncCompleteDelegate produceAsyncCompleteCallback,
            IEqualityComparer <CosmosElement> equalityComparer,
            long initialPageSize            = 50,
            string initialContinuationToken = null)
        {
            this.bufferedPages = new AsyncCollection <TryMonad <FeedResponse <CosmosElement> > >();

            // We use a binary semaphore to get the behavior of a mutex,
            // since fetching documents from the backend using a continuation token is a critical section.
            this.fetchSemaphore = new SemaphoreSlim(1, 1);
            if (partitionKeyRange == null)
            {
                throw new ArgumentNullException(nameof(partitionKeyRange));
            }

            if (createRetryPolicyFunc == null)
            {
                throw new ArgumentNullException(nameof(createRetryPolicyFunc));
            }

            if (produceAsyncCompleteCallback == null)
            {
                throw new ArgumentNullException(nameof(produceAsyncCompleteCallback));
            }

            if (equalityComparer == null)
            {
                throw new ArgumentNullException(nameof(equalityComparer));
            }

            this.queryContext                 = queryContext;
            this.querySpecForInit             = querySpecForInit;
            this.PartitionKeyRange            = partitionKeyRange;
            this.createRetryPolicyFunc        = createRetryPolicyFunc;
            this.produceAsyncCompleteCallback = produceAsyncCompleteCallback;
            this.equalityComparer             = equalityComparer;
            this.pageSize = initialPageSize;
            this.currentContinuationToken  = initialContinuationToken;
            this.BackendContinuationToken  = initialContinuationToken;
            this.PreviousContinuationToken = initialContinuationToken;
            if (!string.IsNullOrEmpty(initialContinuationToken))
            {
                this.hasStartedFetching = true;
                this.IsActive           = true;
            }

            this.fetchSchedulingMetrics = new SchedulingStopwatch();
            this.fetchSchedulingMetrics.Ready();
            this.fetchExecutionRangeAccumulator = new FetchExecutionRangeAccumulator();

            this.HasMoreResults = true;
        }
 internal abstract Task <QueryResponseCore> ExecuteItemQueryAsync <QueryRequestOptionType>(
     Uri resourceUri,
     Documents.ResourceType resourceType,
     Documents.OperationType operationType,
     QueryRequestOptionType requestOptions,
     SqlQuerySpec sqlQuerySpec,
     string continuationToken,
     Documents.PartitionKeyRangeIdentity partitionKeyRange,
     bool isContinuationExpected,
     int pageSize,
     SchedulingStopwatch schedulingStopwatch,
     CancellationToken cancellationToken);
 public DefaultDocumentQueryExecutionContext(
     DocumentQueryExecutionContextBase.InitParams constructorParams,
     bool isContinuationExpected)
     : base(constructorParams)
 {
     this.isContinuationExpected = isContinuationExpected;
     this.fetchSchedulingMetrics = new SchedulingStopwatch();
     this.fetchSchedulingMetrics.Ready();
     this.fetchExecutionRangeAccumulator = new FetchExecutionRangeAccumulator();
     this.providedRangesCache            = new Dictionary <string, IReadOnlyList <Range <string> > >();
     this.retries = -1;
     this.partitionRoutingHelper = new PartitionRoutingHelper();
 }
Beispiel #7
0
 public QueryPageDiagnostics(
     string partitionKeyRangeId,
     string queryMetricText,
     string indexUtilizationText,
     CosmosDiagnostics requestDiagnostics,
     SchedulingStopwatch schedulingStopwatch)
 {
     this.PartitionKeyRangeId  = partitionKeyRangeId ?? throw new ArgumentNullException(nameof(partitionKeyRangeId));
     this.QueryMetricText      = queryMetricText ?? string.Empty;
     this.IndexUtilizationText = indexUtilizationText ?? string.Empty;
     this.RequestDiagnostics   = requestDiagnostics;
     this.SchedulingTimeSpan   = schedulingStopwatch.Elapsed;
 }
        public CosmosGatewayQueryExecutionContext(
            CosmosQueryContext cosmosQueryContext)
        {
            if (cosmosQueryContext == null)
            {
                throw new ArgumentNullException(nameof(cosmosQueryContext));
            }

            this.queryContext           = cosmosQueryContext;
            this.fetchSchedulingMetrics = new SchedulingStopwatch();
            this.fetchSchedulingMetrics.Ready();
            this.fetchExecutionRangeAccumulator = new FetchExecutionRangeAccumulator();
            this.retries = -1;
            this.partitionRoutingHelper = new PartitionRoutingHelper();
        }
        private PipelinedDocumentQueryExecutionContext(
            IDocumentQueryExecutionComponent component,
            int actualPageSize,
            Guid correlatedActivityId)
        {
            this.component            = component;
            this.actualPageSize       = actualPageSize;
            this.correlatedActivityId = correlatedActivityId;

            this.executeNextSchedulingMetrics = new SchedulingStopwatch();
            this.executeNextSchedulingMetrics.Ready();

            DefaultTrace.TraceVerbose(string.Format(
                                          CultureInfo.InvariantCulture,
                                          "{0} Pipelined~Context, actual page size: {1}",
                                          DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture),
                                          this.actualPageSize));
        }
        /// <summary>
        /// Initializes a new instance of the DocumentProducer class.
        /// </summary>
        /// <param name="partitionKeyRange">The partition key range.</param>
        /// <param name="createRequestFunc">The callback to create a request.</param>
        /// <param name="executeRequestFunc">The callback to execute the request.</param>
        /// <param name="createRetryPolicyFunc">The callback to create the retry policy.</param>
        /// <param name="produceAsyncCompleteCallback">The callback to call once you are done fetching.</param>
        /// <param name="equalityComparer">The comparer to use to determine whether the producer has seen a new document.</param>
        /// <param name="initialPageSize">The initial page size.</param>
        /// <param name="initialContinuationToken">The initial continuation token.</param>
        public DocumentProducer(
            PartitionKeyRange partitionKeyRange,
            Func <PartitionKeyRange, string, int, DocumentServiceRequest> createRequestFunc,
            Func <DocumentServiceRequest, CancellationToken, Task <FeedResponse <CosmosElement> > > executeRequestFunc,
            Func <IDocumentClientRetryPolicy> createRetryPolicyFunc,
            ProduceAsyncCompleteDelegate produceAsyncCompleteCallback,
            IEqualityComparer <CosmosElement> equalityComparer,
            long initialPageSize            = 50,
            string initialContinuationToken = null)
        {
            this.bufferedPages  = new AsyncCollection <FeedResponse <CosmosElement> >();
            this.fetchSemaphore = new SemaphoreSlim(1, 1);
            if (partitionKeyRange == null)
            {
                throw new ArgumentNullException(nameof(partitionKeyRange));
            }

            if (createRequestFunc == null)
            {
                throw new ArgumentNullException(nameof(createRequestFunc));
            }

            if (executeRequestFunc == null)
            {
                throw new ArgumentNullException(nameof(executeRequestFunc));
            }

            if (createRetryPolicyFunc == null)
            {
                throw new ArgumentNullException(nameof(createRetryPolicyFunc));
            }

            if (produceAsyncCompleteCallback == null)
            {
                throw new ArgumentNullException(nameof(produceAsyncCompleteCallback));
            }

            if (equalityComparer == null)
            {
                throw new ArgumentNullException(nameof(equalityComparer));
            }

            this.PartitionKeyRange            = partitionKeyRange;
            this.createRequestFunc            = createRequestFunc;
            this.executeRequestFunc           = executeRequestFunc;
            this.createRetryPolicyFunc        = createRetryPolicyFunc;
            this.produceAsyncCompleteCallback = produceAsyncCompleteCallback;
            this.equalityComparer             = equalityComparer;
            this.pageSize = initialPageSize;
            this.currentContinuationToken  = initialContinuationToken;
            this.backendContinuationToken  = initialContinuationToken;
            this.previousContinuationToken = initialContinuationToken;
            if (!string.IsNullOrEmpty(initialContinuationToken))
            {
                this.hasStartedFetching = true;
                this.isActive           = true;
            }

            this.fetchSchedulingMetrics = new SchedulingStopwatch();
            this.fetchSchedulingMetrics.Ready();
            this.fetchExecutionRangeAccumulator = new FetchExecutionRangeAccumulator(this.PartitionKeyRange.Id);

            this.hasMoreResults = true;
        }
Beispiel #11
0
        protected ParallelDocumentQueryExecutionContextBase(
            IDocumentQueryClient client,
            ResourceType resourceTypeEnum,
            Type resourceType,
            Expression expression,
            FeedOptions feedOptions,
            string resourceLink,
            string rewrittenQuery,
            Guid correlatedActivityId,
            bool isContinuationExpected,
            bool getLazyFeedResponse,
            bool isDynamicPageSizeAllowed) :
            base(
                client,
                resourceTypeEnum,
                resourceType,
                expression,
                feedOptions,
                resourceLink,
                getLazyFeedResponse,
                correlatedActivityId)
        {
            this.DocumentProducers = new List <DocumentProducer <T> >();

            this.chargeTracker           = new RequestChargeTracker();
            this.groupedQueryMetrics     = new Dictionary <string, QueryMetrics>();
            this.partitionedQueryMetrics = new ConcurrentBag <Tuple <string, QueryMetrics> >();
            this.responseHeaders         = new StringKeyValueCollection();

            this.actualMaxBufferedItemCount            = Math.Max(this.MaxBufferedItemCount, ParallelQueryConfig.GetConfig().DefaultMaximumBufferSize);
            this.currentAverageNumberOfRequestsPerTask = 1d;

            if (!string.IsNullOrEmpty(rewrittenQuery))
            {
                this.querySpec = new SqlQuerySpec(rewrittenQuery, this.QuerySpec.Parameters);
            }

            this.TaskScheduler                   = new ComparableTaskScheduler(this.GetCurrentMaximumAllowedConcurrentTasks(0));
            this.ShouldPrefetch                  = feedOptions.MaxDegreeOfParallelism != 0;
            this.IsContinuationExpected          = isContinuationExpected;
            this.DefaultContinuationToken        = Guid.NewGuid().ToString();
            this.InitializationSchedulingMetrics = new SchedulingStopwatch();
            this.InitializationSchedulingMetrics.Ready();
            this.CurrentContinuationTokens = new SortedList <DocumentProducer <T>, string>(
                Comparer <DocumentProducer <T> > .Create((producer1, producer2) => string.CompareOrdinal(producer1.TargetRange.MinInclusive, producer2.TargetRange.MinInclusive)));
            this.actualMaxPageSize = this.MaxItemCount.GetValueOrDefault(ParallelQueryConfig.GetConfig().ClientInternalMaxItemCount);

            if (this.actualMaxBufferedItemCount < 0)
            {
                throw new OverflowException("actualMaxBufferedItemCount should never be less than 0");
            }

            if (this.actualMaxBufferedItemCount > int.MaxValue)
            {
                throw new OverflowException("actualMaxBufferedItemCount should never be greater than int.MaxValue");
            }

            if (this.actualMaxPageSize < 0)
            {
                throw new OverflowException("actualMaxPageSize should never be less than 0");
            }

            if (this.actualMaxPageSize > int.MaxValue)
            {
                throw new OverflowException("actualMaxPageSize should never be greater than int.MaxValue");
            }
        }