Example #1
0
        public async Task RunAsync(
            CTLConfig config,
            CosmosClient cosmosClient,
            ILogger logger,
            IMetrics metrics,
            string loggingContextIdentifier,
            CancellationToken cancellationToken)
        {
            logger.LogInformation("Initializing counters and metrics.");
            CounterOptions documentCounter = new CounterOptions {
                Name = "#Documents received", Context = loggingContextIdentifier
            };
            GaugeOptions leaseGauge = new GaugeOptions {
                Name = "#Leases created", Context = loggingContextIdentifier
            };

            string    leaseContainerId = Guid.NewGuid().ToString();
            Container leaseContainer   = await cosmosClient.GetDatabase(config.Database).CreateContainerAsync(leaseContainerId, "/id");

            logger.LogInformation("Created lease container {0}", leaseContainerId);

            try
            {
                ChangeFeedProcessor changeFeedProcessor = cosmosClient.GetContainer(config.Database, config.Collection)
                                                          .GetChangeFeedProcessorBuilder <SimpleItem>("ctlProcessor",
                                                                                                      (IReadOnlyCollection <SimpleItem> docs, CancellationToken token) =>
                {
                    metrics.Measure.Counter.Increment(documentCounter, docs.Count);
                    return(Task.CompletedTask);
                })
                                                          .WithLeaseContainer(leaseContainer)
                                                          .WithInstanceName(Guid.NewGuid().ToString())
                                                          .WithStartTime(DateTime.MinValue.ToUniversalTime())
                                                          .Build();

                await changeFeedProcessor.StartAsync();

                logger.LogInformation("Started change feed processor");

                await Task.Delay(config.RunningTimeDurationAsTimespan, cancellationToken);

                logger.LogInformation("Stopping change feed processor");
                await changeFeedProcessor.StopAsync();

                // List leases
                using FeedIterator <LeaseSchema> leaseIterator = leaseContainer.GetItemQueryIterator <LeaseSchema>();
                int leaseTotal          = 0;
                List <FeedRange> ranges = new List <FeedRange>();
                while (leaseIterator.HasMoreResults)
                {
                    FeedResponse <LeaseSchema> response = await leaseIterator.ReadNextAsync();

                    foreach (LeaseSchema lease in response)
                    {
                        if (lease.LeaseToken != null)
                        {
                            logger.LogInformation($"Lease for range {lease.LeaseToken} - {lease.FeedRange.EffectiveRange.Min} - {lease.FeedRange.EffectiveRange.Max}");
                            ranges.Add(lease.FeedRange.EffectiveRange);
                            leaseTotal++;
                        }
                    }
                }

                string previousMin = "";
                foreach (FeedRange sortedRange in ranges.OrderBy(range => range.Min))
                {
                    if (previousMin != sortedRange.Min)
                    {
                        logger.LogError($"Expected a sorted range with Min <{previousMin}> but encountered range <{sortedRange.Min}>:<{sortedRange.Max}>");
                    }

                    previousMin = sortedRange.Max;
                }

                metrics.Measure.Gauge.SetValue(leaseGauge, leaseTotal);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Failure during Change Feed Processor initialization");
            }
            finally
            {
                await leaseContainer.DeleteContainerAsync();

                if (this.initializationResult.CreatedDatabase)
                {
                    await cosmosClient.GetDatabase(config.Database).DeleteAsync();
                }

                if (this.initializationResult.CreatedContainer)
                {
                    await cosmosClient.GetContainer(config.Database, config.Collection).DeleteContainerAsync();
                }
            }
        }
Example #2
0
        public async Task ContainerResourcePermissionTest()
        {
            //create user
            string       userId       = Guid.NewGuid().ToString();
            UserResponse userResponse = await this.cosmosDatabase.CreateUserAsync(userId);

            Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode);
            Assert.AreEqual(userId, userResponse.Resource.Id);
            User user = userResponse.User;

            //create resource
            string            containerId       = Guid.NewGuid().ToString();
            ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerId, "/id");

            Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode);
            Container container = containerResponse.Container;

            //create permission
            string permissionId = Guid.NewGuid().ToString();
            PermissionProperties permissionProperties = new PermissionProperties(permissionId, PermissionMode.Read, container);
            PermissionResponse   permissionResponse   = await user.CreatePermissionAsync(permissionProperties);

            PermissionProperties permission = permissionResponse.Resource;

            Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode);
            Assert.AreEqual(permissionId, permission.Id);
            Assert.AreEqual(permissionProperties.PermissionMode, permission.PermissionMode);

            //delete resource with PermissionMode.Read
            using (CosmosClient tokenCosmosClient = TestCommon.CreateCosmosClient(clientOptions: null, resourceToken: permission.Token))
            {
                try
                {
                    ContainerResponse response = await tokenCosmosClient
                                                 .GetDatabase(this.cosmosDatabase.Id)
                                                 .GetContainer(containerId)
                                                 .DeleteContainerAsync();

                    Assert.Fail();
                }
                catch (CosmosException ex)
                {
                    Assert.AreEqual(HttpStatusCode.Forbidden, ex.StatusCode);
                }
            }

            //update permission to PermissionMode.All
            permissionProperties = new PermissionProperties(permissionId, PermissionMode.All, container);
            permissionResponse   = await user.GetPermission(permissionId).ReplaceAsync(permissionProperties);

            permission = permissionResponse.Resource;

            //delete resource with PermissionMode.All
            using (CosmosClient tokenCosmosClient = TestCommon.CreateCosmosClient(clientOptions: null, resourceToken: permission.Token))
            {
                ContainerResponse response = await tokenCosmosClient
                                             .GetDatabase(this.cosmosDatabase.Id)
                                             .GetContainer(containerId)
                                             .DeleteContainerAsync();

                Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
            }
        }
Example #3
0
        public async Task ItemTest(bool directMode)
        {
            CosmosClient client    = directMode ? DirectCosmosClient : GatewayCosmosClient;
            Database     database  = client.GetDatabase(DatabaseId);
            Container    container = await database.CreateContainerAsync(Guid.NewGuid().ToString(), "/pk");

            List <string> createdIds = new List <string>()
            {
                "BasicQueryItem",
                "BasicQueryItem2",
                "BasicQueryItem3"
            };

            List <dynamic> queryResults = await this.ToListAsync(
                container.GetItemQueryStreamIterator,
                container.GetItemQueryIterator <dynamic>,
                "select * from T where STARTSWITH(T.id, \"BasicQueryItem\")",
                CosmosBasicQueryTests.RequestOptions);

            if (queryResults.Count < 3)
            {
                foreach (string id in createdIds)
                {
                    dynamic item = new
                    {
                        id = id,
                        pk = id,
                    };

                    ItemResponse <dynamic> createResponse = await container.CreateItemAsync <dynamic>(item : item);
                }

                queryResults = await this.ToListAsync(
                    container.GetItemQueryStreamIterator,
                    container.GetItemQueryIterator <dynamic>,
                    "select * from T where STARTSWITH(T.id, \"BasicQueryItem\")",
                    CosmosBasicQueryTests.RequestOptions);
            }

            List <string> ids = queryResults.Select(x => (string)x.id).ToList();

            CollectionAssert.AreEquivalent(createdIds, ids);

            //Read All
            List <dynamic> results = await this.ToListAsync(
                container.GetItemQueryStreamIterator,
                container.GetItemQueryIterator <dynamic>,
                null,
                CosmosBasicQueryTests.RequestOptions);


            ids = results.Select(x => (string)x.id).ToList();
            CollectionAssert.IsSubsetOf(createdIds, ids);

            //Read All with partition key
            results = await this.ToListAsync(
                container.GetItemQueryStreamIterator,
                container.GetItemQueryIterator <dynamic>,
                null,
                new QueryRequestOptions()
            {
                MaxItemCount = 1,
                PartitionKey = new PartitionKey("BasicQueryItem")
            });

            Assert.AreEqual(1, results.Count);

            //Read All with partition key
            results = container.GetItemLinqQueryable <dynamic>(
                allowSynchronousQueryExecution: true,
                requestOptions: new QueryRequestOptions()
            {
                MaxItemCount = 1,
                PartitionKey = new PartitionKey("BasicQueryItem")
            }).ToList();

            Assert.AreEqual(1, results.Count);

            // LINQ to feed iterator Read All with partition key
            FeedIterator <dynamic> iterator = container.GetItemLinqQueryable <dynamic>(
                allowSynchronousQueryExecution: true,
                requestOptions: new QueryRequestOptions()
            {
                MaxItemCount = 1,
                PartitionKey = new PartitionKey("BasicQueryItem")
            }).ToFeedIterator();

            List <dynamic> linqResults = new List <dynamic>();

            while (iterator.HasMoreResults)
            {
                linqResults.AddRange(await iterator.ReadNextAsync());
            }

            Assert.AreEqual(1, linqResults.Count);
            Assert.AreEqual("BasicQueryItem", linqResults.First().pk.ToString());
        }
Example #4
0
        public async Task PermissionTests(bool directMode)
        {
            CosmosClient  client               = directMode ? DirectCosmosClient : GatewayCosmosClient;
            Database      database             = client.GetDatabase(DatabaseId);
            List <string> createdPermissionIds = new List <string>();
            List <string> createdContainerIds  = new List <string>();
            string        userId               = Guid.NewGuid().ToString();
            UserCore      user = null;

            try
            {
                UserResponse createUserResponse = await database.CreateUserAsync(userId);

                Assert.AreEqual(HttpStatusCode.Created, createUserResponse.StatusCode);
                user = (UserInlineCore)createUserResponse.User;

                ContainerResponse createContainerResponse = await database.CreateContainerIfNotExistsAsync(Guid.NewGuid().ToString(), partitionKeyPath : "/pk");

                Container          container          = createContainerResponse.Container;
                PermissionResponse permissionResponse = await user.CreatePermissionAsync(new PermissionProperties("BasicQueryPermission1", PermissionMode.All, container));

                createdContainerIds.Add(createContainerResponse.Container.Id);
                createdPermissionIds.Add(permissionResponse.Permission.Id);


                createContainerResponse = await database.CreateContainerIfNotExistsAsync(Guid.NewGuid().ToString(), partitionKeyPath : "/pk");

                container          = createContainerResponse.Container;
                permissionResponse = await user.CreatePermissionAsync(new PermissionProperties("BasicQueryPermission2", PermissionMode.All, container));

                createdContainerIds.Add(createContainerResponse.Container.Id);
                createdPermissionIds.Add(permissionResponse.Permission.Id);

                createContainerResponse = await database.CreateContainerIfNotExistsAsync(Guid.NewGuid().ToString(), partitionKeyPath : "/pk");

                container          = createContainerResponse.Container;
                permissionResponse = await user.CreatePermissionAsync(new PermissionProperties("BasicQueryPermission3", PermissionMode.All, container));

                createdContainerIds.Add(createContainerResponse.Container.Id);
                createdPermissionIds.Add(permissionResponse.Permission.Id);

                //Read All
                List <PermissionProperties> results = await this.ToListAsync(
                    user.GetPermissionQueryStreamIterator,
                    user.GetPermissionQueryIterator <PermissionProperties>,
                    null,
                    CosmosBasicQueryTests.RequestOptions
                    );

                CollectionAssert.IsSubsetOf(createdPermissionIds, results.Select(x => x.Id).ToList());

                //Basic query
                List <PermissionProperties> queryResults = await this.ToListAsync(
                    user.GetPermissionQueryStreamIterator,
                    user.GetPermissionQueryIterator <PermissionProperties>,
                    "select * from T where STARTSWITH(T.id, \"BasicQueryPermission\")",
                    CosmosBasicQueryTests.RequestOptions
                    );

                CollectionAssert.AreEquivalent(createdPermissionIds, queryResults.Select(x => x.Id).ToList());
            }
            finally
            {
                foreach (string id in createdPermissionIds)
                {
                    await user.GetPermission(id).DeleteAsync();
                }
                foreach (string id in createdContainerIds)
                {
                    await database.GetContainer(id).DeleteContainerAsync();
                }
                await user?.DeleteAsync();
            }
        }
Example #5
0
        private async Task VerifyItemOperations(
            Cosmos.PartitionKey partitionKey,
            string partitionKeySerialized,
            dynamic testItem,
            ItemRequestOptions requestOptions = null)
        {
            ResponseMessage response            = null;
            HttpStatusCode  httpStatusCode      = HttpStatusCode.OK;
            int             testHandlerHitCount = 0;
            TestHandler     testHandler         = new TestHandler((request, cancellationToken) =>
            {
                Assert.IsTrue(request.RequestUri.OriginalString.StartsWith(@"dbs/testdb/colls/testcontainer"));
                Assert.AreEqual(requestOptions, request.RequestOptions);
                Assert.AreEqual(ResourceType.Document, request.ResourceType);
                Assert.IsNotNull(request.Headers.PartitionKey);
                Assert.AreEqual(partitionKeySerialized, request.Headers.PartitionKey);
                testHandlerHitCount++;
                response         = new ResponseMessage(httpStatusCode, request, errorMessage: null);
                response.Content = request.Content;
                return(Task.FromResult(response));
            });

            CosmosClient client = MockCosmosUtil.CreateMockCosmosClient(
                (builder) => builder.AddCustomHandlers(testHandler));

            Container container = client.GetDatabase("testdb")
                                  .GetContainer("testcontainer");

            ItemResponse <dynamic> itemResponse = await container.CreateItemAsync <dynamic>(
                item : testItem,
                requestOptions : requestOptions);

            Assert.IsNotNull(itemResponse);
            Assert.AreEqual(httpStatusCode, itemResponse.StatusCode);

            itemResponse = await container.ReadItemAsync <dynamic>(
                partitionKey : partitionKey,
                id : testItem.id,
                requestOptions : requestOptions);

            Assert.IsNotNull(itemResponse);
            Assert.AreEqual(httpStatusCode, itemResponse.StatusCode);

            itemResponse = await container.UpsertItemAsync <dynamic>(
                item : testItem,
                requestOptions : requestOptions);

            Assert.IsNotNull(itemResponse);
            Assert.AreEqual(httpStatusCode, itemResponse.StatusCode);

            itemResponse = await container.ReplaceItemAsync <dynamic>(
                id : testItem.id,
                item : testItem,
                requestOptions : requestOptions);

            Assert.IsNotNull(itemResponse);
            Assert.AreEqual(httpStatusCode, itemResponse.StatusCode);

            itemResponse = await container.DeleteItemAsync <dynamic>(
                partitionKey : partitionKey,
                id : testItem.id,
                requestOptions : requestOptions);

            Assert.IsNotNull(itemResponse);
            Assert.AreEqual(httpStatusCode, itemResponse.StatusCode);

            Assert.AreEqual(5, testHandlerHitCount, "An operation did not make it to the handler");

            CosmosJsonSerializerCore jsonSerializer = new CosmosJsonSerializerCore();

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                using (ResponseMessage streamResponse = await container.CreateItemStreamAsync(
                           partitionKey: partitionKey,
                           streamPayload: itemStream,
                           requestOptions: requestOptions))
                {
                    Assert.IsNotNull(streamResponse);
                    Assert.AreEqual(httpStatusCode, streamResponse.StatusCode);
                }
            }

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                using (ResponseMessage streamResponse = await container.ReadItemStreamAsync(
                           partitionKey: partitionKey,
                           id: testItem.id,
                           requestOptions: requestOptions))
                {
                    Assert.IsNotNull(streamResponse);
                    Assert.AreEqual(httpStatusCode, streamResponse.StatusCode);
                }
            }

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                using (ResponseMessage streamResponse = await container.UpsertItemStreamAsync(
                           partitionKey: partitionKey,
                           streamPayload: itemStream,
                           requestOptions: requestOptions))
                {
                    Assert.IsNotNull(streamResponse);
                    Assert.AreEqual(httpStatusCode, streamResponse.StatusCode);
                }
            }

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                using (ResponseMessage streamResponse = await container.ReplaceItemStreamAsync(
                           partitionKey: partitionKey,
                           id: testItem.id,
                           streamPayload: itemStream,
                           requestOptions: requestOptions))
                {
                    Assert.IsNotNull(streamResponse);
                    Assert.AreEqual(httpStatusCode, streamResponse.StatusCode);
                }
            }

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                using (ResponseMessage streamResponse = await container.DeleteItemStreamAsync(
                           partitionKey: partitionKey,
                           id: testItem.id,
                           requestOptions: requestOptions))
                {
                    Assert.IsNotNull(streamResponse);
                    Assert.AreEqual(httpStatusCode, streamResponse.StatusCode);
                }
            }

            Assert.AreEqual(10, testHandlerHitCount, "A stream operation did not make it to the handler");
        }
 public RobotInfosRepository(
     CosmosClient cosmosClient
     )
 {
     _robotsContainer = cosmosClient.GetDatabase("turboco").GetContainer("robots");
 }
Example #7
0
        public static async void Run([CosmosDBTrigger(
                                          databaseName: "contoso",
                                          collectionName: "orders-aggregate",
                                          ConnectionStringSetting = "CosmosConnectionString",
                                          LeaseCollectionName = "leases")] IReadOnlyList <Microsoft.Azure.Documents.Document> input, ILogger log)
        {
            if (input != null && input.Count > 0)
            {
                log.LogInformation("Documents modified " + input.Count);
                log.LogInformation("First document Id " + input[0].Id);

                string       connStr               = Environment.GetEnvironmentVariable("CosmosConnectionString");
                CosmosClient cosmosClient          = new CosmosClient(connStr);
                Microsoft.Azure.Cosmos.Database db = cosmosClient.GetDatabase("contoso");
                Container container = db.GetContainer("orders-for-hour");

                // custQuery is an IEnumerable<IGrouping<string, Customer>>
                var dateQuery =
                    from o in input
                    group o by o.GetPropertyValue <string>("OrderDateHour") into orderDateGroup
                    select orderDateGroup;

                log.LogInformation(dateQuery.Count().ToString());

                foreach (var dq in dateQuery)
                {
                    log.LogInformation("DateQuery: " + dq.Key);

                    var sqlQueryText = $"SELECT * FROM c WHERE c.id = '{dq.Key}'";

                    log.LogInformation("Running query: {0}\n", sqlQueryText);

                    QueryDefinition           queryDefinition        = new QueryDefinition(sqlQueryText);
                    FeedIterator <OrderEvent> queryResultSetIterator = container.GetItemQueryIterator <OrderEvent>(queryDefinition);

                    List <OrderEvent> orders = new List <OrderEvent>();

                    while (queryResultSetIterator.HasMoreResults)
                    {
                        log.LogInformation("In loop");
                        FeedResponse <OrderEvent> currentResultSet = await queryResultSetIterator.ReadNextAsync();

                        foreach (OrderEvent order in currentResultSet)
                        {
                            orders.Add(order);
                            log.LogInformation("\tRead {0}\n", order);
                        }
                    }

                    log.LogInformation("Orders Count: " + orders.Count);

                    OrderEvent currentOrder = new OrderEvent()
                    {
                        id = dq.Key, Count = dq.Count(), Total = dq.Sum(x => x.GetPropertyValue <decimal>("Total"))
                    };

                    if (orders.Count == 0)
                    {
                        log.LogInformation("Addin entry");
                        try
                        {
                            ItemResponse <OrderEvent> orderResponse = await container.CreateItemAsync <OrderEvent>(currentOrder);
                        }
                        catch (Exception ex)
                        {
                            log.LogInformation("Exception: " + ex.Message);
                        }
                    }
                    else
                    {
                        ItemResponse <OrderEvent> orderResponse = await container.ReadItemAsync <OrderEvent>(dq.Key, new PartitionKey(dq.Key));

                        int     currentCount = orderResponse.Resource.Count;
                        decimal currentTotal = orderResponse.Resource.Total;

                        OrderEvent newOrder = new OrderEvent()
                        {
                            id = dq.Key, Count = currentCount + currentOrder.Count, Total = currentTotal + currentOrder.Total
                        };
                        orderResponse = await container.ReplaceItemAsync <OrderEvent>(newOrder, newOrder.id);
                    }
                }
            }
        }
Example #8
0
        private async Task VerifyItemNullPartitionKeyExpectations(
            dynamic testItem,
            ItemRequestOptions requestOptions = null)
        {
            TestHandler testHandler = new TestHandler((request, cancellationToken) =>
            {
                Assert.IsNotNull(request.Headers.PartitionKey);
                Assert.AreEqual(Documents.Routing.PartitionKeyInternal.Undefined.ToString(), request.Headers.PartitionKey.ToString());

                return(Task.FromResult(new ResponseMessage(HttpStatusCode.OK)));
            });

            CosmosClient client = MockCosmosUtil.CreateMockCosmosClient(
                (cosmosClientBuilder) => cosmosClientBuilder.AddCustomHandlers(testHandler));

            Container container = client.GetDatabase("testdb")
                                  .GetContainer("testcontainer");

            await container.CreateItemAsync <dynamic>(
                item : testItem,
                requestOptions : requestOptions);

            await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
            {
                await container.ReadItemAsync <dynamic>(
                    partitionKey: null,
                    id: testItem.id,
                    requestOptions: requestOptions);
            }, "ReadItemAsync should throw ArgumentNullException without the correct request option set.");

            await container.UpsertItemAsync <dynamic>(
                item : testItem,
                requestOptions : requestOptions);

            await container.ReplaceItemAsync <dynamic>(
                id : testItem.id,
                item : testItem,
                requestOptions : requestOptions);

            await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
            {
                await container.DeleteItemAsync <dynamic>(
                    partitionKey: null,
                    id: testItem.id,
                    requestOptions: requestOptions);
            }, "DeleteItemAsync should throw ArgumentNullException without the correct request option set.");

            CosmosJsonSerializerCore jsonSerializer = new CosmosJsonSerializerCore();

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
                {
                    await container.CreateItemStreamAsync(
                        partitionKey: null,
                        streamPayload: itemStream,
                        requestOptions: requestOptions);
                }, "CreateItemAsync should throw ArgumentNullException without the correct request option set.");

                await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
                {
                    await container.ReadItemStreamAsync(
                        partitionKey: null,
                        id: testItem.id,
                        requestOptions: requestOptions);
                }, "ReadItemAsync should throw ArgumentNullException without the correct request option set.");

                await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
                {
                    await container.UpsertItemStreamAsync(
                        partitionKey: null,
                        streamPayload: itemStream,
                        requestOptions: requestOptions);
                }, "UpsertItemAsync should throw ArgumentNullException without the correct request option set.");

                await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
                {
                    await container.ReplaceItemStreamAsync(
                        partitionKey: null,
                        id: testItem.id,
                        streamPayload: itemStream,
                        requestOptions: requestOptions);
                }, "ReplaceItemAsync should throw ArgumentNullException without the correct request option set.");

                await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
                {
                    await container.DeleteItemStreamAsync(
                        partitionKey: null,
                        id: testItem.id,
                        requestOptions: requestOptions);
                }, "DeleteItemAsync should throw ArgumentNullException without the correct request option set.");
            }
        }
Example #9
0
        public async Task ItemResourcePermissionTest()
        {
            //create user
            string       userId       = Guid.NewGuid().ToString();
            UserResponse userResponse = await this.cosmosDatabase.CreateUserAsync(userId);

            Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode);
            Assert.AreEqual(userId, userResponse.Resource.Id);
            User user = userResponse.User;

            //create resource
            string            containerId       = Guid.NewGuid().ToString();
            ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerId, "/id");

            Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode);
            Container              container    = containerResponse.Container;
            string                 itemId       = Guid.NewGuid().ToString();
            PartitionKey           partitionKey = new PartitionKey(itemId);
            ItemResponse <dynamic> itemRespnose = await container.CreateItemAsync <dynamic>(new { id = itemId }, partitionKey);

            Assert.AreEqual(HttpStatusCode.Created, itemRespnose.StatusCode);

            //create permission
            string permissionId = Guid.NewGuid().ToString();
            PermissionProperties permissionProperties = new PermissionProperties(permissionId, PermissionMode.Read, container, partitionKey, itemId);
            PermissionResponse   permissionResponse   = await user.CreatePermissionAsync(permissionProperties);

            PermissionProperties permission = permissionResponse.Resource;

            Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode);
            Assert.AreEqual(permissionId, permission.Id);
            Assert.AreEqual(permissionProperties.PermissionMode, permission.PermissionMode);

            //delete resource with PermissionMode.Read
            using (CosmosClient tokenCosmosClient = TestCommon.CreateCosmosClient(clientOptions: null, resourceToken: permission.Token))
            {
                Container tokenContainer = tokenCosmosClient.GetContainer(this.cosmosDatabase.Id, containerId);
                ItemResponse <dynamic> readPermissionItem = await tokenContainer.ReadItemAsync <dynamic>(itemId, partitionKey);

                Assert.AreEqual(itemId, readPermissionItem.Resource.id.ToString());

                try
                {
                    ItemResponse <dynamic> response = await tokenContainer.DeleteItemAsync <dynamic>(
                        itemId,
                        partitionKey);

                    Assert.Fail();
                }
                catch (CosmosException ex)
                {
                    Assert.AreEqual(HttpStatusCode.Forbidden, ex.StatusCode);
                }
            }

            //update permission to PermissionMode.All
            permissionProperties = new PermissionProperties(permissionId, PermissionMode.All, container);
            permissionResponse   = await user.GetPermission(permissionId).ReplaceAsync(permissionProperties);

            permission = permissionResponse.Resource;

            //delete resource with PermissionMode.All
            using (CosmosClient tokenCosmosClient = TestCommon.CreateCosmosClient(clientOptions: null, resourceToken: permission.Token))
            {
                FeedIterator <dynamic> feed = tokenCosmosClient
                                              .GetDatabase(this.cosmosDatabase.Id)
                                              .GetContainer(containerId)
                                              .GetItemQueryIterator <dynamic>(new QueryDefinition("select * from t"));

                while (feed.HasMoreResults)
                {
                    FeedResponse <dynamic> response = await feed.ReadNextAsync();

                    Assert.IsNotNull(response);
                }
            }
        }
Example #10
0
        private static async Task BulkLoadContainer()
        {
            string dbname        = null;
            string cname         = null;
            string infile        = null;
            int    maxBatchCount = 0;
            int    batchSize     = 500;
            int    batchCount    = 0;
            int    taskCount     = 0;
            long   startEpoch    = 0;
            int    exceptions    = 0;

            try {
                dbname        = cliArgs[1];
                cname         = cliArgs[2];
                infile        = cliArgs[3];
                maxBatchCount = Int32.Parse(cliArgs[4]);

                Console.WriteLine($"dbname:   {dbname}");
                Console.WriteLine($"cname:    {cname}");
                Console.WriteLine($"infile:   {infile}");
                Console.WriteLine($"mbc:      {maxBatchCount}");

                cosmosClient = CosmosClientFactory.BulkLoadingClient();
                Database    database  = cosmosClient.GetDatabase(dbname);
                Container   container = database.GetContainer(cname);
                List <Task> tasks     = new List <Task>(batchSize);
                Console.WriteLine(
                    $"LoadContainer - db: {dbname}, container: {cname}, infile: {infile}, maxBatchCount: {maxBatchCount}");
                startEpoch = EpochMsTime();

                using (FileStream fs = File.Open(infile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                    using (BufferedStream bs = new BufferedStream(fs)) {
                        using (StreamReader sr = new StreamReader(bs)) {
                            string jsonLine;
                            while ((jsonLine = sr.ReadLine()) != null)
                            {
                                if (batchCount < maxBatchCount)
                                {
                                    // Parse the JSON line into a "dynamic" object, like a python dict
                                    dynamic jsonDoc = JsonConvert.DeserializeObject <ExpandoObject>(
                                        jsonLine.Trim(), new ExpandoObjectConverter());
                                    // jsonDoc is an instance of class System.Dynamic.ExpandoObject (dynamic)

                                    // Set some additional attributes on the dynamic jsonDoc
                                    DateTimeOffset now = DateTimeOffset.UtcNow;
                                    jsonDoc.doc_epoch = now.ToUnixTimeMilliseconds();
                                    jsonDoc.doc_time  = now.ToString("yyyy/MM/dd-HH:mm:ss");
                                    jsonDoc.id        = Guid.NewGuid().ToString();

                                    // Add an UpsertItemAsync task to the list of tasks for a batch
                                    tasks.Add(container.UpsertItemAsync(jsonDoc, new PartitionKey(jsonDoc.pk)));
                                    taskCount++;

                                    if (tasks.Count == batchSize)
                                    {
                                        // Display the last jsonDoc in this batch
                                        Console.WriteLine(JsonConvert.SerializeObject(jsonDoc));

                                        batchCount++;
                                        Console.WriteLine($"writing batch {batchCount} ({tasks.Count}) at {EpochMsTime()}");

                                        // execute all of the Tasks in this batch, and await them
                                        await Task.WhenAll(tasks);

                                        // reset the tasks list to an empty list for the next batch
                                        tasks = new List <Task>(batchSize);
                                    }
                                }
                                else
                                {
                                    return;
                                }
                            }
                        }
                    }
                }
                if (tasks.Count > 0)
                {
                    // execute the last batch if there are any tasks
                    batchCount++;
                    Console.WriteLine($"writing batch {batchCount} ({tasks.Count}) at {EpochMsTime()}");
                    await Task.WhenAll(tasks);
                }
            }
            catch (Exception e) {
                Console.WriteLine($"EXCEPTION: {e.Message}");
                exceptions++;
                throw;
            }
            finally {
                long   finishEpoch = EpochMsTime();
                long   elapsedMs   = finishEpoch - startEpoch;
                double elapsedSec  = (double)elapsedMs / (double)1000;
                double elapsedMin  = elapsedSec / (double)60.0;
                double docsPerSec  = taskCount / elapsedSec;
                Console.WriteLine("");
                Console.WriteLine("EOJ Totals:");
                Console.WriteLine($"  Database:             {dbname}");
                Console.WriteLine($"  Container:            {cname}");
                Console.WriteLine($"  Input Filename:       {infile}");
                Console.WriteLine($"  Max Batch Count:      {maxBatchCount}");
                Console.WriteLine($"  BulkLoad startEpoch:  {startEpoch}");
                Console.WriteLine($"  BulkLoad finishEpoch: {finishEpoch}");
                Console.WriteLine($"  BulkLoad elapsedMs:   {elapsedMs}");
                Console.WriteLine($"  BulkLoad elapsedSec:  {elapsedSec}");
                Console.WriteLine($"  BulkLoad elapsedMin:  {elapsedMin}");
                Console.WriteLine($"  Batch Size:           {batchSize}");
                Console.WriteLine($"  Batch Count:          {batchCount}");
                Console.WriteLine($"  Exceptions:           {exceptions}");
                Console.WriteLine($"  Document/Task count:  {taskCount}");
                Console.WriteLine($"  Document per Second:  {docsPerSec}");
                Console.WriteLine("");
            }
            await Task.Delay(0);
        }
        public void VerifySynchronizationContextDoesNotLock()
        {
            string databaseId = Guid.NewGuid().ToString();
            SynchronizationContext prevContext = SynchronizationContext.Current;

            try
            {
                TestSynchronizationContext syncContext = new TestSynchronizationContext();
                SynchronizationContext.SetSynchronizationContext(syncContext);
                syncContext.Post(_ =>
                {
                    using (CosmosClient client = TestCommon.CreateCosmosClient())
                    {
                        Cosmos.Database database = client.CreateDatabaseAsync(databaseId).GetAwaiter().GetResult();
                        database = client.CreateDatabaseIfNotExistsAsync(databaseId).GetAwaiter().GetResult();

                        database.ReadStreamAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                        database.ReadAsync().ConfigureAwait(false).GetAwaiter().GetResult();

                        QueryDefinition databaseQuery = new QueryDefinition("select * from T where T.id = @id").WithParameter("@id", databaseId);
                        FeedIterator <DatabaseProperties> databaseIterator = client.GetDatabaseQueryIterator <DatabaseProperties>(databaseQuery);
                        while (databaseIterator.HasMoreResults)
                        {
                            databaseIterator.ReadNextAsync().GetAwaiter().GetResult();
                        }

                        Container container = database.CreateContainerAsync(Guid.NewGuid().ToString(), "/status").GetAwaiter().GetResult();
                        container           = database.CreateContainerIfNotExistsAsync(container.Id, "/status").GetAwaiter().GetResult();

                        ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity();
                        ItemResponse <ToDoActivity> response = container.CreateItemAsync <ToDoActivity>(item: testItem).ConfigureAwait(false).GetAwaiter().GetResult();
                        Assert.IsNotNull(response);

                        // Test read feed
                        container.GetItemLinqQueryable <ToDoActivity>(
                            allowSynchronousQueryExecution: true,
                            requestOptions: new QueryRequestOptions()
                        {
                        }).ToList();

                        FeedIterator feedIterator = container.GetItemLinqQueryable <ToDoActivity>()
                                                    .ToStreamIterator();

                        while (feedIterator.HasMoreResults)
                        {
                            feedIterator.ReadNextAsync().GetAwaiter().GetResult();
                        }

                        FeedIterator <ToDoActivity> feedIteratorTyped = container.GetItemLinqQueryable <ToDoActivity>()
                                                                        .ToFeedIterator <ToDoActivity>();

                        while (feedIteratorTyped.HasMoreResults)
                        {
                            feedIteratorTyped.ReadNextAsync().GetAwaiter().GetResult();
                        }

                        // Test query
                        container.GetItemLinqQueryable <ToDoActivity>(
                            allowSynchronousQueryExecution: true,
                            requestOptions: new QueryRequestOptions()
                        {
                        }).Where(item => item.id != "").ToList();

                        FeedIterator queryIterator = container.GetItemLinqQueryable <ToDoActivity>()
                                                     .Where(item => item.id != "").ToStreamIterator();

                        while (queryIterator.HasMoreResults)
                        {
                            queryIterator.ReadNextAsync().GetAwaiter().GetResult();
                        }

                        FeedIterator <ToDoActivity> queryIteratorTyped = container.GetItemLinqQueryable <ToDoActivity>()
                                                                         .Where(item => item.id != "").ToFeedIterator <ToDoActivity>();

                        while (queryIteratorTyped.HasMoreResults)
                        {
                            queryIteratorTyped.ReadNextAsync().GetAwaiter().GetResult();
                        }

                        double costAsync = container.GetItemLinqQueryable <ToDoActivity>()
                                           .Select(x => x.cost).SumAsync().GetAwaiter().GetResult();

                        double cost = container.GetItemLinqQueryable <ToDoActivity>(
                            allowSynchronousQueryExecution: true).Select(x => x.cost).Sum();

                        ItemResponse <ToDoActivity> deleteResponse = container.DeleteItemAsync <ToDoActivity>(partitionKey: new Cosmos.PartitionKey(testItem.status), id: testItem.id).ConfigureAwait(false).GetAwaiter().GetResult();
                        Assert.IsNotNull(deleteResponse);
                    }
                }, state: null);
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(prevContext);
                using (CosmosClient client = TestCommon.CreateCosmosClient())
                {
                    client.GetDatabase(databaseId).DeleteAsync().GetAwaiter().GetResult();
                }
            }
        }
        //Helper method to run query
        static async Task RunQuery(string sqlQueryText, int maxItemCountPerPage = 100, int maxConcurrency = -1, bool useQueryOptions = false)
        {
            Console.BackgroundColor = ConsoleColor.Blue;

            Console.WriteLine($"Running query: \"{sqlQueryText}\" against container {ContainerName}\n");
            Console.WriteLine("");

            if (useQueryOptions)
            {
                Console.WriteLine($"Using MaxConcurrency: {maxConcurrency}");
                Console.WriteLine($"Using MaxItemCountPerPage: {maxItemCountPerPage}");
            }
            Console.ResetColor();

            double          totalRequestCharge = 0;
            QueryDefinition queryDefinition    = new QueryDefinition(sqlQueryText);

            // Run query against Cosmos DB
            var container = cosmosClient.GetDatabase(DatabaseName).GetContainer(ContainerName);

            QueryRequestOptions requestOptions;

            if (useQueryOptions)
            {
                requestOptions = new QueryRequestOptions()
                {
                    MaxItemCount   = maxItemCountPerPage,
                    MaxConcurrency = maxConcurrency,
                };
            }
            else
            {
                requestOptions = new QueryRequestOptions(); //use all default query options
            }

            // Time the query
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            FeedIterator <dynamic> queryResultSetIterator = container.GetItemQueryIterator <dynamic>(queryDefinition, requestOptions: requestOptions);
            List <dynamic>         reviews = new List <dynamic>();

            while (queryResultSetIterator.HasMoreResults)
            {
                FeedResponse <dynamic> currentResultSet = await queryResultSetIterator.ReadNextAsync();

                totalRequestCharge += currentResultSet.RequestCharge;
                //Console.WriteLine("another page");
                foreach (var item in currentResultSet)
                {
                    reviews.Add(item);
                    Console.WriteLine(item);
                }
                if (useQueryOptions)
                {
                    Console.WriteLine($"Result count: {reviews.Count}");
                }
            }

            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;

            //Print results
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                               ts.Hours, ts.Minutes, ts.Seconds,
                                               ts.Milliseconds / 10);

            Console.ForegroundColor = ConsoleColor.Green;

            Console.WriteLine($"\tQuery returned {reviews.Count} results");
            Console.WriteLine($"\tTotal time: {elapsedTime}");
            Console.WriteLine($"\tTotal Request Units consumed: {totalRequestCharge}\n");
            Console.WriteLine("\n\n\n");
            Console.ResetColor();
        }
Example #13
0
 private void GetDatabase()
 {
     database = cosmosClient.GetDatabase(settings.DatabaseName);
 }