Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session Token Not on Writes #40102

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/cosmos/azure-cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Bugs Fixed
* Fixed bug preventing health check in some scenarios. See [PR 39647](https://github.com/Azure/azure-sdk-for-python/pull/39647)
* Fixed `partition_key` filter for `query_items_change_feed` API. See [PR 39895](https://github.com/Azure/azure-sdk-for-python/pull/39895)
* Session tokens are no longer sent for writes for accounts with single write region. See [PR 39895](https://github.com/Azure/azure-sdk-for-python/pull/39895)

#### Other Changes
* Moved endpoint health check to the background for async APIs. See [PR 39647](https://github.com/Azure/azure-sdk-for-python/pull/39647)
Expand Down
7 changes: 6 additions & 1 deletion sdk/cosmos/azure-cosmos/azure/cosmos/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from urllib.parse import quote as urllib_quote
from urllib.parse import urlsplit
from azure.core import MatchConditions
from azure.cosmos.documents import _OperationType

from . import documents
from . import http_constants
Expand Down Expand Up @@ -185,7 +186,11 @@ def GetHeaders( # pylint: disable=too-many-statements,too-many-branches
is_session_consistency = consistency_level == documents.ConsistencyLevel.Session

# set session token if required
if is_session_consistency is True and not IsMasterResource(resource_type):
# is_session_consistency is wrong - if neither client default is set nor request option then the default
# account consistency level needs to be taken into account. That is missing here.
if (is_session_consistency is True and not IsMasterResource(resource_type)
and (_OperationType.IsReadOnlyOperation(operation_type) or
cosmos_client_connection._global_endpoint_manager.get_use_multiple_write_locations())): # pylint: disable=protected-access:
# if there is a token set via option, then use it to override default
if options.get("sessionToken"):
headers[http_constants.HttpHeaders.SessionToken] = options["sessionToken"]
Expand Down
11 changes: 11 additions & 0 deletions sdk/cosmos/azure-cosmos/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ def _MockRequest(self, global_endpoint_manager, request_params, connection_polic
return self._OriginalRequest(global_endpoint_manager, request_params, connection_policy, pipeline_client,
request)

def test_session_token_not_sent_for_writes(self):
self._OriginalRequest = synchronized_request._Request
try:
synchronized_request._Request = self._MockRequest
self.created_collection.create_item(body={'id': '1' + str(uuid.uuid4()), 'pk': 'mypk'})
self.assertEqual(self.last_session_token_sent, None)
self.created_collection.upsert_item(body={'id': '1' + str(uuid.uuid4()), 'pk': 'mypk'})
self.assertEqual(self.last_session_token_sent, None)
finally:
synchronized_request._Request = self._OriginalRequest

def test_session_token_not_sent_for_master_resource_ops(self):
self._OriginalRequest = synchronized_request._Request
synchronized_request._Request = self._MockRequest
Expand Down