Skip to content

Add excluded locations on client and request levels#40298

Merged
allenkim0129 merged 33 commits intoAzure:mainfrom
allenkim0129:user/allekim/feature/addExcludedLocations
Apr 17, 2025
Merged

Add excluded locations on client and request levels#40298
allenkim0129 merged 33 commits intoAzure:mainfrom
allenkim0129:user/allekim/feature/addExcludedLocations

Conversation

@allenkim0129
Copy link
Copy Markdown
Contributor

@allenkim0129 allenkim0129 commented Mar 31, 2025

Changes

This change will add excluded locations on client and request levels.

Design doc: https://microsoft-my.sharepoint.com/:w:/p/allekim/EWf4tc65_f9GiOv9aD2XkLMBD9h2ZAAXCcFTKghjutvqcg?e=AlD8w1

On Client level:

def excluded_locations_client_level_sample():
    preferred_locations = ['West US 3', 'West US', 'East US 2']
    excluded_locations = ['West US 3', 'West US']
    client = CosmosClient(
                HOST,
                MASTER_KEY,
                preferred_locations=preferred_locations,
                excluded_locations=excluded_locations
    )

    db = client.create_database(DATABASE_ID)
    container = db.create_container(id=CONTAINER_ID, partition_key=PARTITION_KEY)

    # For write operations with single master account, write endpoint will be the default endpoint,
    # since preferred_locations or excluded_locations are ignored and used
    container.create_item(get_test_item(0))

    # For read operations, read endpoints will be 'preferred_locations' - 'excluded_locations'.
    # In our sample, ['West US 3', 'West US', 'East US 2'] - ['West US 3', 'West US'] => ['East US 2'],
    # therefore 'East US 2' will be the read endpoint, and items will be read from 'East US 2' location
    item = container.read_item(item='Item_0', partition_key='Item_0')
  • Note: During client creation, excluded locations can be set by using the excluded_locations parameter. This set will filter any existing preferred locations. If excluded locations filtered all preferred locations, Cosmos DB will use the default endpoints.

On Request level:

def excluded_locations_request_level_sample():
    preferred_locations = ['West US 3', 'West US', 'East US 2']
    excluded_locations_on_client = ['West US 3', 'West US']
    excluded_locations_on_request = ['West US 3']
    client = CosmosClient(
                HOST,
                MASTER_KEY,
                preferred_locations=preferred_locations,
                excluded_locations=excluded_locations_on_client
    )

    db = client.create_database(DATABASE_ID)
    container = db.create_container(id=CONTAINER_ID, partition_key=PARTITION_KEY)

    # For write operations with single master account, write endpoint will be the default endpoint,
    # since preferred_locations or excluded_locations are ignored and used
    container.create_item(get_test_item(0))

    # For read operations, read endpoints will be 'preferred_locations' - 'excluded_locations'.
    # However, in our sample, since the excluded_locations` were passed with the read request, the `excluded_location`
    # will be replaced with the locations from request, ['West US 3']. The `excluded_locations` on request always takes
    # the highest priority!
    # With the excluded_locations on request, the read endpoints will be ['West US', 'East US 2']
    #   ['West US 3', 'West US', 'East US 2'] - ['West US 3'] => ['West US', 'East US 2']
    # Therefore, items will be read from 'West US' or 'East US 2' location
    item = container.read_item(item='Item_0', partition_key='Item_0', excluded_locations=excluded_locations_on_request)
  • Note: The excluded locations on requests level can be used by passing excluded_locations parameters along with any other parameters for Container level APIs. This excluded_location parameter will override the existing exlcuded locations on client level. This means also means that if a user can manupilate the client level excluded locations for the specific request. Moreover, if a user wants to skip all client level excluded locations, the user can simply pass an empty list [] of excluded locations to skip all excluded location filters.

Client level API works with excluded_location(11 APIs):

  • read_item
  • read_all_items
  • query_items_change_feed
  • query_items
  • replace_item
  • upsert_item
  • create_item
  • patch_item
  • execute_item_batch
  • delete_item
  • delete_all_items_by_partition_key

Other Changes:

Bug Fix

  • Fixed can_use_multiple_write_locations_for_request check method, because PartitionKey resource type was not included during the check.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

@azure-sdk
Copy link
Copy Markdown
Collaborator

API change check

APIView has identified API level changes in this PR and created following API reviews.

azure-cosmos

@allenkim0129
Copy link
Copy Markdown
Contributor Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@allenkim0129
Copy link
Copy Markdown
Contributor Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@allenkim0129
Copy link
Copy Markdown
Contributor Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@allenkim0129
Copy link
Copy Markdown
Contributor Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@allenkim0129
Copy link
Copy Markdown
Contributor Author

I created a new PR since there were some conflict with branch merges which ended up including more reviewer than required.

Previous PR with comments: #40298

@allenkim0129
Copy link
Copy Markdown
Contributor Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@allenkim0129
Copy link
Copy Markdown
Contributor Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@allenkim0129 allenkim0129 requested a review from a team as a code owner April 14, 2025 18:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 28 out of 28 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

sdk/cosmos/azure-cosmos/azure/cosmos/_request_object.py:80

  • [nitpick] Consider adding type validation in set_excluded_location_from_options to ensure that options['excludedLocations'] is actually a list of strings.
def set_excluded_location_from_options(self, options: Mapping[str, Any]) -> None:

sdk/cosmos/azure-cosmos/azure/cosmos/_location_cache.py:168

  • [nitpick] Although returning a fallback endpoint keeps the service available, consider logging a warning to highlight that all regional endpoints were excluded and the fallback is being used.
if not applicable_endpoints:

@allenkim0129
Copy link
Copy Markdown
Contributor Author

This PR was created to remove unnecesary commits from the last PR. #40022

The previous PR will have all related comments from previous commits.

@allenkim0129
Copy link
Copy Markdown
Contributor Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Copy Markdown
Member

@simorenoh simorenoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great overall, thanks Allen! Just had a couple of comments/ questions 👍

Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/documents.py Outdated
Comment thread sdk/cosmos/azure-cosmos/samples/excluded_locations.py
Comment thread sdk/cosmos/azure-cosmos/tests/test_query.py
Copy link
Copy Markdown
Member

@kushagraThapar kushagraThapar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work @allenkim0129
Great development + test coverage. Thanks for the efforts on this.
One thing though, we should add a section in the readme.md as well, noting this feature and pointing to the sample from the readme.md

Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_global_endpoint_manager.py
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_location_cache.py
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_request_object.py
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/documents.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_location_cache.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_location_cache.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py
Copy link
Copy Markdown
Member

@tvaron3 tvaron3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@allenkim0129
Copy link
Copy Markdown
Contributor Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

@allenkim0129
Copy link
Copy Markdown
Contributor Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@allenkim0129
Copy link
Copy Markdown
Contributor Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@allenkim0129 allenkim0129 merged commit f4ce81d into Azure:main Apr 17, 2025
31 checks passed
cRui861 pushed a commit that referenced this pull request May 14, 2025
* Add Excluded Locations Feature

* Added multi-region tests

* Fix _AddParitionKey to pass options to sub methods

* Added initial live tests

* Updated live-platform-matrix for multi-region tests

* Add cosmosQuery mark to TestQuery

* Correct spelling

* Fixed live platform matrix syntax

* Changed Multi-regions

* Added client level ExcludedLocation for async

* Update Live test settings

* Added Async tests

* Add more live tests for all other Python versions

* Fix Async test failure

* Fix live test failures

* Fix live test failures

* Fix live test failures

* Add test_delete_all_items_by_partition_key

* Remove test_delete_all_items_by_partition_key

* Added missing doc for excluded_locations in async client

* Remove duplicate functions

* Fix live tests with multi write locations

* Fixed bug with endpoint routing with multi write region partition key API calls

* Adding emulator tests for delete_all_items_by_partition_key API

* minimized duplicate codes

* Added Async emulator tests

* Nit: Changed test names

* Addressed comments about documents

* Address comments about method naming

* Updated document to add more details of request level excluded_locations

---------

Co-authored-by: Kushagra Thapar <kuthapar@microsoft.com>
@allenkim0129 allenkim0129 deleted the user/allekim/feature/addExcludedLocations branch June 16, 2025 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants