Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CreateDocumentCollectionIfNotExistsAsync does not always work

I am trying to create a collection if it does not exist.

First, I create my DocumentClient by:

public DocumentClient CreateClient(ConnectionPolicy connectionPolicy = null)
{
   return new DocumentClient(new Uri(this.AccountEndpoint), this.AccountKey, connectionPolicy);
}

Now that I have my document client (let's call it documentDbClient), I attempt to use CreateDocumentCollectionIfNotExistsAsync:

var documentDbClient = dbConnection.CreateClient();

await documentDbClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("mydatabase"),
            new DocumentCollection { Id = "testcollection" });

Now, I would expect this to create a collection named testcollection under my database named mydatabase. However, nothing happens. No collection is created, no exceptions are thrown.

I thought perhaps it was just slow, and so I waited 30 minutes and went back to the portal to see if my collection had been made...

It hadn't.

What is wrong?

UPDATE

Okay, so I have discovered that if I change the name of the collection that I am attempting to create, it works. I have previously created and deleted the collection many times and now it appears that Azure is failing to create it 90% of the time.

like image 776
pookie Avatar asked Sep 22 '26 21:09

pookie


2 Answers

So, you're likely running into a throttling situation when repeatedly creating and destroying collections. Here's an excerpt from the Table API section of the FAQ (which applies equally to document collections):

The rapid rate of creation of tables is detected and throttled. We recommend that you look at the rate of creation of tables and lower it to less than 5 per minute.

The current throttle rate is 5 creates / minute. So, for a unit-testing scenario, you'd need to work around this (e.g. keeping a collection, but just emptying its contents after each test run).

Note: I notified the product team, that the throttle mention is only in the Table API section, and to update the FAQ accordingly.

like image 166
David Makogon Avatar answered Sep 25 '26 10:09

David Makogon


You should add a .Wait() to the end of database creation and then the collection creation. This will wait for both tasks to complete.

_client.CreateDatabaseIfNotExistsAsync(new Database { Id = "dbName" }).Wait();

_client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("dbName"), new DocumentCollection { Id = "collectionName" }).Wait();
like image 28
Njrmp Avatar answered Sep 25 '26 12:09

Njrmp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!