0
votes

I am trying to add new items into the cosmos db using my local settings.

This is my code to insert bulk:

private async Task AddSubscription(EnableOrDisableSubscriptionCommand command, SubscriptionAction subscriptionAction, IList<int> notificationCategoryTypes)
        {
            List<Task> bulkOperations = new List<Task>();
            foreach (var notificationCategory in notificationCategoryTypes)
            {
                var notificationTypes = Utility.GetNotificationTypes((NotificationCategoryType)notificationCategory);

                foreach (var notificationType in notificationTypes)
                {
                    foreach (var payerAccountSubscriptions in command.Subscriptions)
                    {
                        if (payerAccountSubscriptions.AccountNumbers?.Any() ?? false)
                        {
                            foreach (var accountNumber in payerAccountSubscriptions.AccountNumbers.Where(a => !string.IsNullOrEmpty(a)))
                            {
                                bulkOperations.Add(_repository.Create(subscriptionAction, notificationType,
                                      payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, accountNumber, command.UserRole,
                                      command.UserId));
                            }
                        }
                        else
                        {
                            bulkOperations.Add(_repository.Create(subscriptionAction, notificationType,
                                payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, null, command.UserRole,
                                command.UserId));

                        }
                    }
                }
            }
            await Task.WhenAll(bulkOperations);
        }

 public Task<ItemResponse<T>> CreateItemAsync(T item, string partitionKeyValue)
        {
            return _container.CreateItemAsync<T>(item, new PartitionKey(partitionKeyValue));
        }

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    //localhost
    "CosmosDbId": "Notifications",
    "CosmoDbAuthKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
    "CosmoDbEndpoint": "https://localhost:8081/",
}
}

I have already installed Azure Cosmos DB Emulator.

I get following error that does not provide much information?

[8/31/2020 6:49:10 AM] System.Private.CoreLib: Exception while executing function: EnableOrDisableSubscriptions. Microsoft.Azure.Cosmos.Client: Response status code does not indicate success: ServiceUnavailable (503); Substatus: 0; Activity

2
Also it might be the emulator is failing to initialize the network stack. Check to see if you have the Pulse secure client or Juniper networks client installed, as their network filter drivers may cause the problem. Uninstalling third-party network filter drivers typically fixes the issue. Alternatively, start the emulator with /DisableRIO, which will switch the emulator network communication to regular Winsock.krishg
Can you show the complete stack trace of the exception?Matias Quaranta
Issue got resolved when un-install Azure Cosmos DB emulator older version and install new version 2.11.2.Rakesh Kumar

2 Answers

2
votes

I got the same issue following Cosmos DB SQL API getting started guide.

The fix was to specify ConnectionMode = ConnectionMode.Gateway

var cosmosClient = new CosmosClient(EndpointUrl, AuthorizationKey, new CosmosClientOptions() { AllowBulkExecution = true, ConnectionMode = ConnectionMode.Gateway });
0
votes

Please ensure below points are taken care in .NET SDK:

  • Initialize a singleton DocumentClient
  • Use Direct connectivity and TCP protocol (ConnectionMode.Direct and ConnectionProtocol.Tcp)
  • Use 100s of Tasks in parallel (depends on your hardware)
  • Increase the MaxConnectionLimit in the DocumentClient constructor to a high value, say 1000 connections
  • Turn gcServer on
  • Make sure your collection has the appropriate provisioned throughput (and a good partition key)

You can now use the bulk executor library directly for bulk operations - https://docs.microsoft.com/en-us/azure/cosmos-db/bulk-executor-dot-net