Example #1
0
        private static async Task <List <CosmosElement> > DrainWithStateAsync(string query, IDocumentContainer documentContainer, int pageSize = 10)
        {
            IQueryPipelineStage pipelineStage;
            CosmosElement       state = null;

            List <CosmosElement> elements = new List <CosmosElement>();

            do
            {
                pipelineStage = CreatePipeline(documentContainer, query, pageSize, state);

                if (!await pipelineStage.MoveNextAsync())
                {
                    break;
                }

                TryCatch <QueryPage> tryGetQueryPage = pipelineStage.Current;
                tryGetQueryPage.ThrowIfFailed();

                elements.AddRange(tryGetQueryPage.Result.Documents);
                state = tryGetQueryPage.Result.State?.Value;
            }while (state != null);

            return(elements);
        }
Example #2
0
        public async Task Tracing()
        {
            List <CosmosObject> documents = new List <CosmosObject>();

            for (int i = 0; i < 250; i++)
            {
                documents.Add(CosmosObject.Parse($"{{\"pk\" : {i} }}"));
            }

            IDocumentContainer documentContainer = await CreateDocumentContainerAsync(documents);

            IQueryPipelineStage pipelineStage = CreatePipeline(documentContainer, "SELECT * FROM c", pageSize: 10);

            Trace rootTrace;
            int   numTraces = 1;

            using (rootTrace = Trace.GetRootTrace("Cross Partition Query"))
            {
                while (await pipelineStage.MoveNextAsync(rootTrace))
                {
                    TryCatch <QueryPage> tryGetQueryPage = pipelineStage.Current;
                    tryGetQueryPage.ThrowIfFailed();

                    numTraces++;
                }
            }

            string traceString = TraceWriter.TraceToText(rootTrace);

            Console.WriteLine(traceString);

            Assert.AreEqual(numTraces, rootTrace.Children.Count);
        }
        public static T Deserialize <T>(ReadOnlyMemory <byte> buffer)
        {
            TryCatch <T> tryDeserialize = CosmosElementDeserializer.Monadic.Deserialize <T>(buffer);

            tryDeserialize.ThrowIfFailed();
            return(tryDeserialize.Result);
        }
Example #4
0
        public static SqlQuery Parse(string text)
        {
            TryCatch <SqlQuery> monadicParse = Monadic.Parse(text);

            monadicParse.ThrowIfFailed();
            return(monadicParse.Result);
        }
        public async Task Tracing()
        {
            List <CosmosObject> documents = new List <CosmosObject>();

            for (int i = 0; i < 250; i++)
            {
                documents.Add(CosmosObject.Parse($"{{\"pk\" : {i} }}"));
            }

            IDocumentContainer documentContainer = await CreateDocumentContainerAsync(documents);

            IQueryPipelineStage pipelineStage = await CreatePipelineAsync(documentContainer, "SELECT * FROM c", pageSize : 10);

            Trace rootTrace;
            int   numTraces = (await documentContainer.GetFeedRangesAsync(NoOpTrace.Singleton, default)).Count;

            using (rootTrace = Trace.GetRootTrace("Cross Partition Query"))
            {
                while (await pipelineStage.MoveNextAsync(rootTrace))
                {
                    TryCatch <QueryPage> tryGetQueryPage = pipelineStage.Current;
                    tryGetQueryPage.ThrowIfFailed();

                    numTraces++;
                }
            }

            Assert.AreEqual(numTraces, rootTrace.Children.Count);
        }
Example #6
0
        public static CosmosElement Parse(string json)
        {
            TryCatch <CosmosElement> tryParse = CosmosElement.Monadic.Parse(json);

            tryParse.ThrowIfFailed();

            return(tryParse.Result);
        }
Example #7
0
        public static PartitionKeyHashRanges Create(IEnumerable <PartitionKeyHashRange> partitionKeyHashRanges)
        {
            TryCatch <PartitionKeyHashRanges> tryCreateMonad = Monadic.Create(partitionKeyHashRanges);

            tryCreateMonad.ThrowIfFailed();

            return(tryCreateMonad.Result);
        }
Example #8
0
        public static TCosmosElement Parse <TCosmosElement>(string json)
            where TCosmosElement : CosmosElement
        {
            TryCatch <TCosmosElement> tryParse = CosmosElement.Monadic.Parse <TCosmosElement>(json);

            tryParse.ThrowIfFailed();

            return(tryParse.Result);
        }
Example #9
0
        public static TCosmosElement CreateFromBuffer <TCosmosElement>(ReadOnlyMemory <byte> buffer)
            where TCosmosElement : CosmosElement
        {
            TryCatch <TCosmosElement> tryCreateFromBuffer = CosmosElement.Monadic.CreateFromBuffer <TCosmosElement>(buffer);

            tryCreateFromBuffer.ThrowIfFailed();

            return(tryCreateFromBuffer.Result);
        }
Example #10
0
        private static void ParseUsingNativeParser(SqlQuerySpec sqlQuerySpec)
        {
            TryCatch <PartitionedQueryExecutionInfo> tryGetQueryPlan = QueryPartitionProvider.TryGetPartitionedQueryExecutionInfo(
                querySpec: sqlQuerySpec,
                partitionKeyDefinition: PartitionKeyDefinition,
                requireFormattableOrderByQuery: true,
                isContinuationExpected: false,
                allowNonValueAggregateQuery: true,
                hasLogicalPartitionKey: false);

            tryGetQueryPlan.ThrowIfFailed();
        }
        public static ReadFeedCrossFeedRangeState Parse(string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            TryCatch <ReadFeedCrossFeedRangeState> monadicParse = Monadic.Parse(text);

            monadicParse.ThrowIfFailed();

            return(monadicParse.Result);
        }
        private static void ParseUsingNativeParser(SqlQuerySpec sqlQuerySpec)
        {
            TryCatch <PartitionedQueryExecutionInfo> tryGetQueryPlan = QueryPartitionProvider.TryGetPartitionedQueryExecutionInfo(
                querySpecJsonString: JsonConvert.SerializeObject(sqlQuerySpec),
                partitionKeyDefinition: PartitionKeyDefinition,
                requireFormattableOrderByQuery: true,
                isContinuationExpected: false,
                allowNonValueAggregateQuery: true,
                hasLogicalPartitionKey: false,
                allowDCount: true,
                useSystemPrefix: false);

            tryGetQueryPlan.ThrowIfFailed();
        }
Example #13
0
        private static async Task <(int, ReadFeedCrossFeedRangeState?)> DrainOnePageAsync(IAsyncEnumerable <TryCatch <ReadFeedPage> > asyncEnumerable)
        {
            IAsyncEnumerator <TryCatch <ReadFeedPage> > enumerator = asyncEnumerable.GetAsyncEnumerator();

            await enumerator.MoveNextAsync();

            TryCatch <ReadFeedPage> monadicPage = enumerator.Current;

            monadicPage.ThrowIfFailed();

            ReadFeedPage page = monadicPage.Result;
            ReadFeedCrossFeedRangeState?state = page.State;
            int totalCount = page.Documents.Count;

            return(totalCount, state);
        }
Example #14
0
        private static async Task <List <CosmosElement> > DrainWithoutStateAsync(string query, IDocumentContainer documentContainer, int pageSize = 10)
        {
            IQueryPipelineStage pipelineStage = CreatePipeline(documentContainer, query, pageSize);

            List <CosmosElement> elements = new List <CosmosElement>();

            while (await pipelineStage.MoveNextAsync())
            {
                TryCatch <QueryPage> tryGetQueryPage = pipelineStage.Current;
                tryGetQueryPage.ThrowIfFailed();

                elements.AddRange(tryGetQueryPage.Result.Documents);
            }

            return(elements);
        }
Example #15
0
        public async Task TestMerge()
        {
            List <CosmosObject> documents = Enumerable
                                            .Range(0, 100)
                                            .Select(x => CosmosObject.Parse($"{{\"pk\" : {x} }}"))
                                            .ToList();

            MergeTestUtil mergeTest = new MergeTestUtil();

            mergeTest.DocumentContainer = await CreateDocumentContainerAsync(
                documents : documents,
                numPartitions : 2,
                failureConfigs : new FlakyDocumentContainer.FailureConfigs(
                    inject429s: false,
                    injectEmptyPages: false,
                    shouldReturnFailure: mergeTest.ShouldReturnFailure));

            string query    = "SELECT * FROM c ORDER BY c._ts";
            int    pageSize = 10;
            IQueryPipelineStage pipelineStage = CreatePipeline(mergeTest.DocumentContainer, query, pageSize);

            List <CosmosElement> elements = new List <CosmosElement>();
            int iteration = 0;

            while (await pipelineStage.MoveNextAsync())
            {
                TryCatch <QueryPage> tryGetQueryPage = pipelineStage.Current;
                tryGetQueryPage.ThrowIfFailed();

                elements.AddRange(tryGetQueryPage.Result.Documents);
                ++iteration;

                if (iteration == 1)
                {
                    mergeTest.ShouldMerge = MergeTestUtil.TriState.Ready;
                }
            }

            Assert.AreEqual(expected: documents.Count, actual: elements.Count);
        }
            public async Task TestSplitAndMergeImplementationAsync(bool useState, bool allowSplits, bool allowMerges)
            {
                int numItems = 1000;
                IDocumentContainer inMemoryCollection = await this.CreateDocumentContainerAsync(numItems);

                IAsyncEnumerator <TryCatch <CrossFeedRangePage <ReadFeedPage, ReadFeedState> > > enumerator = this.CreateEnumerator(inMemoryCollection);
                HashSet <string> identifiers = new HashSet <string>();
                Random           random      = new Random();

                while (await enumerator.MoveNextAsync())
                {
                    TryCatch <CrossFeedRangePage <ReadFeedPage, ReadFeedState> > tryGetPage = enumerator.Current;
                    tryGetPage.ThrowIfFailed();

                    IReadOnlyList <Record> records = this.GetRecordsFromPage(tryGetPage.Result);
                    foreach (Record record in records)
                    {
                        identifiers.Add(record.Payload["pk"].ToString());
                    }

                    if (useState)
                    {
                        if (tryGetPage.Result.State == null)
                        {
                            break;
                        }

                        enumerator = this.CreateEnumerator(inMemoryCollection, tryGetPage.Result.State);
                    }

                    if (random.Next() % 2 == 0)
                    {
                        if (allowSplits && (random.Next() % 2 == 0))
                        {
                            // Split
                            await inMemoryCollection.RefreshProviderAsync(NoOpTrace.Singleton, cancellationToken : default);

                            List <FeedRangeEpk> ranges = await inMemoryCollection.GetFeedRangesAsync(
                                trace : NoOpTrace.Singleton,
                                cancellationToken : default);