question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Cosmos DB] Robust usage of PreferredLocations / equivalent of ApplicationRegion

See original GitHub issue
  • Package Name: azure-cosmos
  • Package Version: 4.2.0
  • Operating System: Linux
  • Python Version: 3.8

Describe the bug This is probably as much as feature request as a bug report:

For different app environments (dev,test,prod) we are using different Cosmos DB accounts. Prod and test are geo-replicated (multiple read-regions, only one write region). Dev, however, is not replicated at all.

Now we would like our app to connect automatically to the closest available region. In the .NET SDK this seems to be available with the ApplicationRegion parameter. I didn’t find this in the python SDK?! So instead we tried to use PreferredLocations. However, that crashes if you specify a region in there, which is not in the replicated regions for this account (even if another region in that list does exist).

To Reproduce

        self.cosmos_client = CosmosClient(
            url=DB_URL,
            credential=credentials,
            preferred_locations=["UK West", "UK South"]

Whereas the Cosmos DB does exist in UK south but was not replicated to UK West.

Traceback (most recent call last):
[...]
  File "./app/database/database.py", line 60, in __init__
    self.cosmos_client = CosmosClient(
  File "/usr/local/lib/python3.8/site-packages/azure/cosmos/cosmos_client.py", line 167, in __init__
    self.client_connection = CosmosClientConnection(
  File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_cosmos_client_connection.py", line 208, in __init__
    self._global_endpoint_manager.force_refresh(database_account)
  File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_global_endpoint_manager.py", line 89, in force_refresh
    self.refresh_endpoint_list(database_account)
  File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_global_endpoint_manager.py", line 99, in refresh_endpoint_list
    raise e
  File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_global_endpoint_manager.py", line 97, in refresh_endpoint_list
    self._refresh_endpoint_list_private(database_account, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_global_endpoint_manager.py", line 107, in _refresh_endpoint_list_private
    self.location_cache.should_refresh_endpoints()
  File "/usr/local/lib/python3.8/site-packages/azure/cosmos/_location_cache.py", line 164, in should_refresh_endpoints
    most_preferred_read_endpoint = self.available_read_endpoint_by_locations[most_preferred_location]
KeyError: 'UK West'

Expected behavior The next region in the list should be automatically tried without the entire connection failing.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sebadercommented, Dec 7, 2020

Is the use case to support your three environments with a single client options object?

basically, yes 😃 For now we worked around this, but it would have felt more natural if the SDK will automatically retry if one of the regions is invalid. Ideally, even if none of the preferred_locations, is valid, it should just fall back to the main/default region. After all, it’s called “preferred” not “ever-only-one-of-these” locations 😃

0reactions
sebadercommented, Dec 7, 2020

ok, fair enough. thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate your application to use the Azure Cosmos DB .NET ...
Azure Cosmos DB package) for API for NoSQL. ... a new CosmosClientBuilder with a strong ConsistencyLevel and a list of preferred locations:.
Read more >
Azure - Configure disaster RECOVERY and automatic failover ...
The CosmosClientOptions.ConnectionPolicy.preferredLocations parameter in JS SDK. This preference configuration also determines which regions ...
Read more >
Understanding Cosmos DB | James Serra's Blog
Azure Cosmos DB accounts that are configured to use strong consistency cannot associate more than one Azure region with their Azure Cosmos DB...
Read more >
Exam AZ-204 topic 3 question 28 discussion - ExamTopics
Update Azure Cosmos DB to use the Strong consistency level. Add indexed properties to the container to indicate region. C. Update the ...
Read more >
Availability and best practices for the Azure Cosmos DB .NET ...
In this episode, Mark sits down with Matias Quaranta. We'll do a deep dive into the SDK on connectivity, understand how each connection...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found