예제 #1
0
        public async Task BatchOperationsTest(ConnectionMode mode)
        {
            Container container = await this.CreateClientAndContainer(mode, Microsoft.Azure.Cosmos.ConsistencyLevel.Eventual); // Client level consistency

            using (BatchAsyncContainerExecutor executor =
                       new BatchAsyncContainerExecutor(
                           (ContainerInlineCore)container,
                           ((ContainerInlineCore)container).ClientContext,
                           20,
                           Documents.Constants.MaxDirectModeBatchRequestBodySizeInBytes)
                   )
            {
                List <Task <TransactionalBatchOperationResult> > tasks = new List <Task <TransactionalBatchOperationResult> >();
                for (int i = 0; i < 10; i++)
                {
                    tasks.Add(executor.AddAsync(CreateItem(i.ToString()), NoOpTrace.Singleton, default));
                }

                await Task.WhenAll(tasks);
            }

            IDictionary <string, long> expectedRecordCountInOperation = new Dictionary <string, long>
            {
                { Documents.OperationType.Batch.ToString(), 1 }
            };

            await this.WaitAndAssert(expectedOperationCount : 2,
                                     expectedConsistencyLevel : Microsoft.Azure.Cosmos.ConsistencyLevel.Eventual,
                                     expectedOperationRecordCountMap : expectedRecordCountInOperation);
        }
예제 #2
0
        public async Task DoOperationsAsync()
        {
            BatchAsyncContainerExecutor executor = new BatchAsyncContainerExecutor(this.cosmosContainer, this.cosmosContainer.ClientContext, 20, Constants.MaxDirectModeBatchRequestBodySizeInBytes);

            List <Task <TransactionalBatchOperationResult> > tasks = new List <Task <TransactionalBatchOperationResult> >();

            for (int i = 0; i < 100; i++)
            {
                tasks.Add(executor.AddAsync(CreateItem(i.ToString()), null, default(CancellationToken)));
            }

            await Task.WhenAll(tasks);

            for (int i = 0; i < 100; i++)
            {
                Task <TransactionalBatchOperationResult> task   = tasks[i];
                TransactionalBatchOperationResult        result = await task;
                Assert.AreEqual(HttpStatusCode.Created, result.StatusCode);

                MyDocument document = cosmosDefaultJsonSerializer.FromStream <MyDocument>(result.ResourceStream);
                Assert.AreEqual(i.ToString(), document.id);

                ItemResponse <MyDocument> storedDoc = await this.cosmosContainer.ReadItemAsync <MyDocument>(i.ToString(), new Cosmos.PartitionKey(i.ToString()));

                Assert.IsNotNull(storedDoc.Resource);
            }

            executor.Dispose();
        }
예제 #3
0
        public async Task BatchOperationsTest(ConnectionMode mode)
        {
            Container container = await this.GetContainer(mode);

            using (BatchAsyncContainerExecutor executor =
                       new BatchAsyncContainerExecutor(
                           (ContainerInlineCore)container,
                           ((ContainerInlineCore)container).ClientContext,
                           20,
                           Documents.Constants.MaxDirectModeBatchRequestBodySizeInBytes)
                   )
            {
                List <Task <TransactionalBatchOperationResult> > tasks = new List <Task <TransactionalBatchOperationResult> >();
                for (int i = 0; i < 10; i++)
                {
                    tasks.Add(executor.AddAsync(CreateItem(i.ToString()), NoOpTrace.Singleton, default));
                }

                await Task.WhenAll(tasks);
            }

            await this.WaitAndAssert(2);
        }
        public async Task RetryOnNameStale()
        {
            ItemBatchOperation itemBatchOperation = CreateItem("test");

            Mock <CosmosClientContext> mockedContext = new Mock <CosmosClientContext>();

            mockedContext.Setup(c => c.ClientOptions).Returns(new CosmosClientOptions());
            mockedContext
            .SetupSequence(c => c.ProcessResourceOperationStreamAsync(
                               It.IsAny <string>(),
                               It.IsAny <ResourceType>(),
                               It.IsAny <OperationType>(),
                               It.IsAny <RequestOptions>(),
                               It.IsAny <ContainerInternal>(),
                               It.IsAny <Cosmos.PartitionKey?>(),
                               It.IsAny <Stream>(),
                               It.IsAny <Action <RequestMessage> >(),
                               It.IsAny <CosmosDiagnosticsContext>(),
                               It.IsAny <ITrace>(),
                               It.IsAny <CancellationToken>()))
            .Returns(this.GenerateCacheStaleResponseAsync(itemBatchOperation))
            .Returns(this.GenerateOkResponseAsync(itemBatchOperation));

            mockedContext.Setup(c => c.SerializerCore).Returns(MockCosmosUtil.Serializer);

            string link = "/dbs/db/colls/colls";
            Mock <ContainerInternal> mockContainer = new Mock <ContainerInternal>();

            mockContainer.Setup(x => x.LinkUri).Returns(link);
            mockContainer.Setup(x => x.GetPartitionKeyDefinitionAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(new PartitionKeyDefinition()
            {
                Paths = new Collection <string>()
                {
                    "/id"
                }
            }));

            CollectionRoutingMap routingMap = CollectionRoutingMap.TryCreateCompleteRoutingMap(
                new[]
            {
                Tuple.Create(new PartitionKeyRange {
                    Id = "0", MinInclusive = "", MaxExclusive = "FF"
                }, (ServiceIdentity)null)
            },
                string.Empty);

            mockContainer.Setup(x => x.GetRoutingMapAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(routingMap));
            BatchAsyncContainerExecutor       executor = new BatchAsyncContainerExecutor(mockContainer.Object, mockedContext.Object, 20, BatchAsyncContainerExecutorCache.DefaultMaxBulkRequestBodySizeInBytes);
            TransactionalBatchOperationResult result   = await executor.AddAsync(itemBatchOperation);

            Mock.Get(mockContainer.Object)
            .Verify(x => x.GetPartitionKeyDefinitionAsync(It.IsAny <CancellationToken>()), Times.Exactly(2));
            Mock.Get(mockedContext.Object)
            .Verify(c => c.ProcessResourceOperationStreamAsync(
                        It.IsAny <string>(),
                        It.IsAny <ResourceType>(),
                        It.IsAny <OperationType>(),
                        It.IsAny <RequestOptions>(),
                        It.IsAny <ContainerInternal>(),
                        It.IsAny <Cosmos.PartitionKey?>(),
                        It.IsAny <Stream>(),
                        It.IsAny <Action <RequestMessage> >(),
                        It.IsAny <CosmosDiagnosticsContext>(),
                        It.IsAny <ITrace>(),
                        It.IsAny <CancellationToken>()), Times.Exactly(2));
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsNotNull(result.DiagnosticsContext);

            string diagnosticsString = result.DiagnosticsContext.ToString();

            Assert.IsTrue(diagnosticsString.Contains("PointOperationStatistics"), "Diagnostics might be missing");
        }
        public async Task RetryOnSplit()
        {
            ItemBatchOperation itemBatchOperation = CreateItem("test");

            Mock <CosmosClientContext> mockedContext = this.MockClientContext();

            mockedContext.Setup(c => c.ClientOptions).Returns(new CosmosClientOptions());
            mockedContext
            .SetupSequence(c => c.ProcessResourceOperationStreamAsync(
                               It.IsAny <string>(),
                               It.IsAny <ResourceType>(),
                               It.IsAny <OperationType>(),
                               It.IsAny <RequestOptions>(),
                               It.IsAny <ContainerInternal>(),
                               It.IsAny <Cosmos.FeedRange>(),
                               It.IsAny <Stream>(),
                               It.IsAny <Action <RequestMessage> >(),
                               It.IsAny <ITrace>(),
                               It.IsAny <CancellationToken>()))
            .Returns(GenerateSplitResponseAsync(itemBatchOperation))
            .Returns(GenerateOkResponseAsync(itemBatchOperation));

            mockedContext.Setup(c => c.SerializerCore).Returns(MockCosmosUtil.Serializer);

            string link = "/dbs/db/colls/colls";
            Mock <ContainerInternal> mockContainer = new Mock <ContainerInternal>();

            mockContainer.Setup(x => x.LinkUri).Returns(link);
            mockContainer.Setup(x => x.GetCachedContainerPropertiesAsync(It.IsAny <bool>(), It.IsAny <ITrace>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new ContainerProperties()
            {
                PartitionKey = new PartitionKeyDefinition()
                {
                    Paths = new Collection <string>()
                    {
                        "/id"
                    }
                }
            }));
            Mock <CosmosClientContext> context = this.MockClientContext();

            mockContainer.Setup(c => c.ClientContext).Returns(context.Object);
            context.Setup(c => c.DocumentClient).Returns(new ClientWithSplitDetection());


            CollectionRoutingMap routingMap = CollectionRoutingMap.TryCreateCompleteRoutingMap(
                new[]
            {
                Tuple.Create(new PartitionKeyRange {
                    Id = "0", MinInclusive = "", MaxExclusive = "FF"
                }, (ServiceIdentity)null)
            },
                string.Empty);

            mockContainer.Setup(x => x.GetRoutingMapAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(routingMap));
            BatchAsyncContainerExecutor       executor = new BatchAsyncContainerExecutor(mockContainer.Object, mockedContext.Object, 20, BatchAsyncContainerExecutorCache.DefaultMaxBulkRequestBodySizeInBytes);
            TransactionalBatchOperationResult result   = await executor.AddAsync(itemBatchOperation, NoOpTrace.Singleton);

            Mock.Get(mockContainer.Object)
            .Verify(x => x.GetCachedContainerPropertiesAsync(It.IsAny <bool>(), It.IsAny <ITrace>(), It.IsAny <CancellationToken>()), Times.Exactly(2));
            Mock.Get(mockedContext.Object)
            .Verify(c => c.ProcessResourceOperationStreamAsync(
                        It.IsAny <string>(),
                        It.IsAny <ResourceType>(),
                        It.IsAny <OperationType>(),
                        It.IsAny <RequestOptions>(),
                        It.IsAny <ContainerInternal>(),
                        It.IsAny <Cosmos.FeedRange>(),
                        It.IsAny <Stream>(),
                        It.IsAny <Action <RequestMessage> >(),
                        It.IsAny <ITrace>(),
                        It.IsAny <CancellationToken>()), Times.Exactly(2));
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsNotNull(result.ToResponseMessage().Trace);
        }
예제 #6
0
        public async Task DoesNotRecalculatePartitionKeyRangeOnNoSplits()
        {
            ItemBatchOperation itemBatchOperation = CreateItem("test");

            Mock <CosmosClientContext> mockedContext = new Mock <CosmosClientContext>();

            mockedContext.Setup(c => c.ClientOptions).Returns(new CosmosClientOptions());
            mockedContext
            .Setup(c => c.ProcessResourceOperationStreamAsync(
                       It.IsAny <Uri>(),
                       It.IsAny <ResourceType>(),
                       It.IsAny <OperationType>(),
                       It.IsAny <RequestOptions>(),
                       It.IsAny <ContainerInternal>(),
                       It.IsAny <Cosmos.PartitionKey?>(),
                       It.IsAny <Stream>(),
                       It.IsAny <Action <RequestMessage> >(),
                       It.IsAny <CosmosDiagnosticsContext>(),
                       It.IsAny <CancellationToken>()))
            .Returns(this.GenerateOkResponseAsync(itemBatchOperation));

            mockedContext.Setup(c => c.SerializerCore).Returns(MockCosmosUtil.Serializer);

            Uri link = new Uri($"/dbs/db/colls/colls", UriKind.Relative);
            Mock <ContainerInternal> mockContainer = new Mock <ContainerInternal>();

            mockContainer.Setup(x => x.LinkUri).Returns(link);
            mockContainer.Setup(x => x.GetPartitionKeyDefinitionAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(new PartitionKeyDefinition()
            {
                Paths = new Collection <string>()
                {
                    "/id"
                }
            }));

            CollectionRoutingMap routingMap = CollectionRoutingMap.TryCreateCompleteRoutingMap(
                new[]
            {
                Tuple.Create(new PartitionKeyRange {
                    Id = "0", MinInclusive = "", MaxExclusive = "FF"
                }, (ServiceIdentity)null)
            },
                string.Empty);

            mockContainer.Setup(x => x.GetRoutingMapAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(routingMap));
            BatchAsyncContainerExecutor       executor = new BatchAsyncContainerExecutor(mockContainer.Object, mockedContext.Object, 20, Constants.MaxDirectModeBatchRequestBodySizeInBytes, 1);
            TransactionalBatchOperationResult result   = await executor.AddAsync(itemBatchOperation);

            Mock.Get(mockContainer.Object)
            .Verify(x => x.GetPartitionKeyDefinitionAsync(It.IsAny <CancellationToken>()), Times.Once);
            Mock.Get(mockedContext.Object)
            .Verify(c => c.ProcessResourceOperationStreamAsync(
                        It.IsAny <Uri>(),
                        It.IsAny <ResourceType>(),
                        It.IsAny <OperationType>(),
                        It.IsAny <RequestOptions>(),
                        It.IsAny <ContainerInternal>(),
                        It.IsAny <Cosmos.PartitionKey?>(),
                        It.IsAny <Stream>(),
                        It.IsAny <Action <RequestMessage> >(),
                        It.IsAny <CosmosDiagnosticsContext>(),
                        It.IsAny <CancellationToken>()), Times.Once);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
        }
        public async Task RetryOnSplit()
        {
            ItemBatchOperation itemBatchOperation = CreateItem("test");

            Mock <CosmosClientContext> mockedContext = new Mock <CosmosClientContext>();

            mockedContext.Setup(c => c.ClientOptions).Returns(new CosmosClientOptions());
            mockedContext
            .SetupSequence(c => c.ProcessResourceOperationStreamAsync(
                               It.IsAny <Uri>(),
                               It.IsAny <ResourceType>(),
                               It.IsAny <OperationType>(),
                               It.IsAny <RequestOptions>(),
                               It.IsAny <ContainerCore>(),
                               It.IsAny <Cosmos.PartitionKey?>(),
                               It.IsAny <Stream>(),
                               It.IsAny <Action <RequestMessage> >(),
                               It.IsAny <CancellationToken>()))
            .Returns(this.GenerateSplitResponseAsync(itemBatchOperation))
            .Returns(this.GenerateOkResponseAsync(itemBatchOperation));

            mockedContext.Setup(c => c.CosmosSerializer).Returns(new CosmosJsonDotNetSerializer());

            Uri link = new Uri($"/dbs/db/colls/colls", UriKind.Relative);
            Mock <ContainerCore> mockContainer = new Mock <ContainerCore>();

            mockContainer.Setup(x => x.LinkUri).Returns(link);
            mockContainer.Setup(x => x.GetPartitionKeyDefinitionAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(new PartitionKeyDefinition()
            {
                Paths = new Collection <string>()
                {
                    "/id"
                }
            }));

            CollectionRoutingMap routingMap = CollectionRoutingMap.TryCreateCompleteRoutingMap(
                new[]
            {
                Tuple.Create(new PartitionKeyRange {
                    Id = "0", MinInclusive = "", MaxExclusive = "FF"
                }, (ServiceIdentity)null)
            },
                string.Empty);

            mockContainer.Setup(x => x.GetRoutingMapAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(routingMap));
            BatchAsyncContainerExecutor       executor = new BatchAsyncContainerExecutor(mockContainer.Object, mockedContext.Object, 20, Constants.MaxDirectModeBatchRequestBodySizeInBytes, 1);
            TransactionalBatchOperationResult result   = await executor.AddAsync(itemBatchOperation);

            Mock.Get(mockContainer.Object)
            .Verify(x => x.GetPartitionKeyDefinitionAsync(It.IsAny <CancellationToken>()), Times.Exactly(2));
            Mock.Get(mockedContext.Object)
            .Verify(c => c.ProcessResourceOperationStreamAsync(
                        It.IsAny <Uri>(),
                        It.IsAny <ResourceType>(),
                        It.IsAny <OperationType>(),
                        It.IsAny <RequestOptions>(),
                        It.IsAny <ContainerCore>(),
                        It.IsAny <Cosmos.PartitionKey?>(),
                        It.IsAny <Stream>(),
                        It.IsAny <Action <RequestMessage> >(),
                        It.IsAny <CancellationToken>()), Times.Exactly(2));
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.IsNotNull(result.Diagnostics);

            int    diagnosticsLines  = 0;
            string diagnosticsString = result.Diagnostics.ToString();
            int    index             = diagnosticsString.IndexOf(Environment.NewLine);

            while (index > -1)
            {
                diagnosticsLines++;
                index = diagnosticsString.IndexOf(Environment.NewLine, index + 1);
            }

            Assert.IsTrue(diagnosticsLines > 1, "Diagnostics might be missing");
        }