Skip to content

Commit 1203cfe

Browse files
authored
Revert "Consolidate Container Properties cache in Client Instance (#35293)" (#35642)
This reverts commit 35f9733.
1 parent c60d542 commit 1203cfe

13 files changed

+53
-267
lines changed

sdk/cosmos/azure-cosmos/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#### Bugs Fixed
1111
* Fixed a bug where change feed query in Async client was not returning all pages due to case-sensitive response headers. See [PR 35090](https://github.com/Azure/azure-sdk-for-python/pull/35090)
1212
* Fixed a bug when a retryable exception occurs in the first page of a query execution causing query to return 0 results. See [PR 35090](https://github.com/Azure/azure-sdk-for-python/pull/35090).
13-
* Consolidated Container Properties Cache to be in the Client to cache partition key definition and container rid to avoid unnecessary container reads. See [PR 35293](https://github.com/Azure/azure-sdk-for-python/pull/35293)
1413

1514

1615
#### Other Changes

sdk/cosmos/azure-cosmos/azure/cosmos/_base.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,3 @@ def _format_batch_operations(
853853
final_operations.append(operation)
854854

855855
return final_operations
856-
857-
858-
def _set_properties_cache(properties: Dict[str, Any]) -> Dict[str, Any]:
859-
return {
860-
"_self": properties.get("_self", None), "_rid": properties.get("_rid", None),
861-
"partitionKey": properties.get("partitionKey", None)
862-
}

sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
)
4646

4747
from . import _base as base
48-
from ._base import _set_properties_cache
4948
from . import documents
5049
from .documents import ConnectionPolicy, DatabaseAccount
5150
from ._constants import _Constants as Constants
@@ -144,7 +143,7 @@ def __init__(
144143

145144
self.connection_policy = connection_policy or ConnectionPolicy()
146145
self.partition_resolvers: Dict[str, RangePartitionResolver] = {}
147-
self.__container_properties_cache: Dict[str, Dict[str, Any]] = {}
146+
self.partition_key_definition_cache: Dict[str, Any] = {}
148147
self.default_headers: Dict[str, Any] = {
149148
http_constants.HttpHeaders.CacheControl: "no-cache",
150149
http_constants.HttpHeaders.Version: http_constants.Versions.CurrentVersion,
@@ -231,26 +230,6 @@ def __init__(
231230
self.session: Optional[_session.Session] = None
232231
self._set_client_consistency_level(database_account, consistency_level)
233232

234-
@property
235-
def _container_properties_cache(self) -> Dict[str, Dict[str, Any]]:
236-
"""Gets the container properties cache from the client.
237-
:returns: the container properties cache for the client.
238-
:rtype: Dict[str, Dict[str, Any]]"""
239-
return self.__container_properties_cache
240-
241-
def _set_container_properties_cache(self, container_link: str, properties: Optional[Dict[str, Any]]) -> None:
242-
"""Sets the container properties cache for the specified container.
243-
244-
This will only update the properties cache for a specified container.
245-
:param container_link: The container link will be used as the key to cache the container properties.
246-
:type container_link: str
247-
:param properties: These are the container properties to cache.
248-
:type properties: Optional[Dict[str, Any]]"""
249-
if properties:
250-
self.__container_properties_cache[container_link] = properties
251-
else:
252-
self.__container_properties_cache[container_link] = {}
253-
254233
def _set_client_consistency_level(
255234
self,
256235
database_account: DatabaseAccount,
@@ -1282,6 +1261,7 @@ def CreateItem(
12821261

12831262
if base.IsItemContainerLink(database_or_container_link):
12841263
options = self._AddPartitionKey(database_or_container_link, document, options)
1264+
12851265
return self.Create(document, path, "docs", collection_id, None, options, **kwargs)
12861266

12871267
def UpsertItem(
@@ -3290,14 +3270,13 @@ def _UpdateSessionIfRequired(
32903270
self.session.update_session(response_result, response_headers)
32913271

32923272
def _get_partition_key_definition(self, collection_link: str) -> Optional[Dict[str, Any]]:
3293-
partition_key_definition: Optional[Dict[str, Any]]
3273+
partition_key_definition = None
32943274
# If the document collection link is present in the cache, then use the cached partitionkey definition
3295-
if collection_link in self.__container_properties_cache:
3296-
cached_container: Dict[str, Any] = self.__container_properties_cache.get(collection_link, {})
3297-
partition_key_definition = cached_container.get("partitionKey")
3275+
if collection_link in self.partition_key_definition_cache:
3276+
partition_key_definition = self.partition_key_definition_cache.get(collection_link)
32983277
# Else read the collection from backend and add it to the cache
32993278
else:
3300-
container = self.ReadContainer(collection_link)
3301-
partition_key_definition = container.get("partitionKey")
3302-
self.__container_properties_cache[collection_link] = _set_properties_cache(container)
3279+
collection = self.ReadContainer(collection_link)
3280+
partition_key_definition = collection.get("partitionKey")
3281+
self.partition_key_definition_cache[collection_link] = partition_key_definition
33033282
return partition_key_definition

sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
validate_cache_staleness_value,
3737
_deserialize_throughput,
3838
_replace_throughput,
39-
GenerateGuidId,
40-
_set_properties_cache
39+
GenerateGuidId
4140
)
4241
from ..exceptions import CosmosResourceNotFoundError
4342
from ..http_constants import StatusCodes
@@ -82,21 +81,19 @@ def __init__(
8281
) -> None:
8382
self.client_connection = client_connection
8483
self.id = id
84+
self._properties = properties
8585
self.database_link = database_link
8686
self.container_link = "{}/colls/{}".format(database_link, self.id)
8787
self._is_system_key: Optional[bool] = None
8888
self._scripts: Optional[ScriptsProxy] = None
89-
if properties:
90-
self.client_connection._set_container_properties_cache(self.container_link,
91-
_set_properties_cache(properties)) # pylint: disable=protected-access, line-too-long
9289

9390
def __repr__(self) -> str:
9491
return "<ContainerProxy [{}]>".format(self.container_link)[:1024]
9592

9693
async def _get_properties(self) -> Dict[str, Any]:
97-
if self.container_link not in self.client_connection._container_properties_cache: # pylint: disable=protected-access, line-too-long
98-
await self.read()
99-
return self.client_connection._container_properties_cache[self.container_link] # pylint: disable=protected-access, line-too-long
94+
if self._properties is None:
95+
self._properties = await self.read()
96+
return self._properties
10097

10198
@property
10299
async def is_system_key(self) -> bool:
@@ -170,10 +167,12 @@ async def read(
170167
request_options["populatePartitionKeyRangeStatistics"] = populate_partition_key_range_statistics
171168
if populate_quota_info is not None:
172169
request_options["populateQuotaInfo"] = populate_quota_info
173-
container = await self.client_connection.ReadContainer(self.container_link, options=request_options, **kwargs)
174-
# Only cache Container Properties that will not change in the lifetime of the container
175-
self.client_connection._set_container_properties_cache(self.container_link, _set_properties_cache(container)) # pylint: disable=protected-access, line-too-long
176-
return container
170+
171+
collection_link = self.container_link
172+
self._properties = await self.client_connection.ReadContainer(
173+
collection_link, options=request_options, **kwargs
174+
)
175+
return self._properties
177176

178177
@distributed_trace_async
179178
async def create_item(

sdk/cosmos/azure-cosmos/azure/cosmos/aio/_cosmos_client_connection_async.py

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
ProxyPolicy)
4848

4949
from .. import _base as base
50-
from .._base import _set_properties_cache
5150
from .. import documents
5251
from .._routing import routing_range
5352
from ..documents import ConnectionPolicy, DatabaseAccount
@@ -145,7 +144,7 @@ def __init__(
145144

146145
self.connection_policy = connection_policy or ConnectionPolicy()
147146
self.partition_resolvers: Dict[str, RangePartitionResolver] = {}
148-
self.__container_properties_cache: Dict[str, Dict[str, Any]] = {}
147+
self.partition_key_definition_cache: Dict[str, Any] = {}
149148
self.default_headers: Dict[str, Any] = {
150149
http_constants.HttpHeaders.CacheControl: "no-cache",
151150
http_constants.HttpHeaders.Version: http_constants.Versions.CurrentVersion,
@@ -227,26 +226,6 @@ def __init__(
227226
# Routing map provider
228227
self._routing_map_provider: SmartRoutingMapProvider = SmartRoutingMapProvider(self)
229228

230-
@property
231-
def _container_properties_cache(self) -> Dict[str, Dict[str, Any]]:
232-
"""Gets the container properties cache from the client.
233-
:returns: the container properties cache for the client.
234-
:rtype: Dict[str, Dict[str, Any]]"""
235-
return self.__container_properties_cache
236-
237-
def _set_container_properties_cache(self, container_link: str, properties: Optional[Dict[str, Any]]) -> None:
238-
"""Sets the container properties cache for the specified container.
239-
240-
This will only update the properties cache for a specified container.
241-
:param container_link: The container link will be used as the key to cache the container properties.
242-
:type container_link: str
243-
:param properties: These are the container properties to cache.
244-
:type properties: Optional[Dict[str, Any]]"""
245-
if properties:
246-
self.__container_properties_cache[container_link] = properties
247-
else:
248-
self.__container_properties_cache[container_link] = {}
249-
250229
@property
251230
def _Session(self) -> Optional[_session.Session]:
252231
"""Gets the session object from the client.
@@ -3055,14 +3034,13 @@ async def _AddPartitionKey(self, collection_link, document, options):
30553034
# TODO: Refresh the cache if partition is extracted automatically and we get a 400.1001
30563035

30573036
# If the document collection link is present in the cache, then use the cached partitionkey definition
3058-
if collection_link in self.__container_properties_cache:
3059-
cached_container = self.__container_properties_cache.get(collection_link)
3060-
partitionKeyDefinition = cached_container.get("partitionKey")
3037+
if collection_link in self.partition_key_definition_cache:
3038+
partitionKeyDefinition = self.partition_key_definition_cache.get(collection_link)
30613039
# Else read the collection from backend and add it to the cache
30623040
else:
3063-
container = await self.ReadContainer(collection_link)
3064-
partitionKeyDefinition = container.get("partitionKey")
3065-
self.__container_properties_cache[collection_link] = _set_properties_cache(container)
3041+
collection = await self.ReadContainer(collection_link)
3042+
partitionKeyDefinition = collection.get("partitionKey")
3043+
self.partition_key_definition_cache[collection_link] = partitionKeyDefinition
30663044

30673045
# If the collection doesn't have a partition key definition, skip it as it's a legacy collection
30683046
if partitionKeyDefinition:

sdk/cosmos/azure-cosmos/azure/cosmos/container.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
validate_cache_staleness_value,
3737
_deserialize_throughput,
3838
_replace_throughput,
39-
GenerateGuidId,
40-
_set_properties_cache
39+
GenerateGuidId
4140
)
4241
from .exceptions import CosmosResourceNotFoundError
4342
from .http_constants import StatusCodes
@@ -84,19 +83,17 @@ def __init__(
8483
self.id = id
8584
self.container_link = "{}/colls/{}".format(database_link, self.id)
8685
self.client_connection = client_connection
86+
self._properties = properties
8787
self._is_system_key: Optional[bool] = None
8888
self._scripts: Optional[ScriptsProxy] = None
89-
if properties:
90-
self.client_connection._set_container_properties_cache(self.container_link,
91-
_set_properties_cache(properties)) # pylint: disable=protected-access, line-too-long
9289

9390
def __repr__(self) -> str:
9491
return "<ContainerProxy [{}]>".format(self.container_link)[:1024]
9592

9693
def _get_properties(self) -> Dict[str, Any]:
97-
if self.container_link not in self.client_connection._container_properties_cache: # pylint: disable=protected-access, line-too-long
98-
self.read()
99-
return self.client_connection._container_properties_cache[self.container_link] # pylint: disable=protected-access, line-too-long
94+
if self._properties is None:
95+
self._properties = self.read()
96+
return self._properties
10097

10198
@property
10299
def is_system_key(self) -> bool:
@@ -176,10 +173,11 @@ def read( # pylint:disable=docstring-missing-param
176173
request_options["populatePartitionKeyRangeStatistics"] = populate_partition_key_range_statistics
177174
if populate_quota_info is not None:
178175
request_options["populateQuotaInfo"] = populate_quota_info
179-
container = self.client_connection.ReadContainer(self.container_link, options=request_options, **kwargs)
180-
# Only cache Container Properties that will not change in the lifetime of the container
181-
self.client_connection._set_container_properties_cache(self.container_link, _set_properties_cache(container)) # pylint: disable=protected-access, line-too-long
182-
return container
176+
collection_link = self.container_link
177+
self._properties = self.client_connection.ReadContainer(
178+
collection_link, options=request_options, **kwargs
179+
)
180+
return self._properties
183181

184182
@distributed_trace
185183
def read_item( # pylint:disable=docstring-missing-param

sdk/cosmos/azure-cosmos/test/test_container_properties_cache.py

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)