Skip to content

Commit 06ac03d

Browse files
authored
Partition Key Feed Range Bug (Azure#40677)
* Feed range bug fix * add test and move logic to overlaps * fix cspell * reacting to comments * reacting to comments * reacting to comments * fix tests * fix tests
1 parent 6f78dc7 commit 06ac03d

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

sdk/cosmos/azure-cosmos/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#### Breaking Changes
1010

1111
#### Bugs Fixed
12+
* Fixed bug where change feed requests would not respect the partition key filter. See [PR 40677](https://github.com/Azure/azure-sdk-for-python/pull/40677).
1213
* Fixed how the environment variables in the sdk are parsed. See [PR 40303](https://github.com/Azure/azure-sdk-for-python/pull/40303).
1314
* Fixed health check to check the first write region when it is not specified in the preferred regions. See [PR 40588](https://github.com/Azure/azure-sdk-for-python/pull/40588).
1415
* Fixed bug where writes were being retried for 5xx status codes for patch and replace. See [PR 40672](https://github.com/Azure/azure-sdk-for-python/pull/40672).

sdk/cosmos/azure-cosmos/azure/cosmos/_routing/routing_range.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def __init__(self, range_min, range_max, isMinInclusive, isMaxInclusive):
5050
if range_max is None:
5151
raise ValueError("max is missing")
5252

53-
self.min = range_min
54-
self.max = range_max
53+
self.min = range_min.upper()
54+
self.max = range_max.upper()
5555
self.isMinInclusive = isMinInclusive
5656
self.isMaxInclusive = isMaxInclusive
5757

@@ -67,8 +67,8 @@ def contains(self, value):
6767
@classmethod
6868
def PartitionKeyRangeToRange(cls, partition_key_range):
6969
self = cls(
70-
partition_key_range[PartitionKeyRange.MinInclusive],
71-
partition_key_range[PartitionKeyRange.MaxExclusive],
70+
partition_key_range[PartitionKeyRange.MinInclusive].upper(),
71+
partition_key_range[PartitionKeyRange.MaxExclusive].upper(),
7272
True,
7373
False,
7474
)
@@ -77,8 +77,8 @@ def PartitionKeyRangeToRange(cls, partition_key_range):
7777
@classmethod
7878
def ParseFromDict(cls, range_as_dict):
7979
self = cls(
80-
range_as_dict[Range.MinPath],
81-
range_as_dict[Range.MaxPath],
80+
range_as_dict[Range.MinPath].upper(),
81+
range_as_dict[Range.MaxPath].upper(),
8282
range_as_dict[Range.IsMinInclusivePath],
8383
range_as_dict[Range.IsMaxInclusivePath],
8484
)
@@ -125,7 +125,7 @@ def add_to_effective_partition_key(self, effective_partition_key: str, value: in
125125
break
126126
byte_array[i] = 255
127127

128-
return binascii.hexlify(byte_array).decode()
128+
return binascii.hexlify(byte_array).decode().upper()
129129

130130
def hex_binary_to_byte_array(self, hex_binary_string: str):
131131
if hex_binary_string is None:
@@ -180,7 +180,7 @@ def __eq__(self, other):
180180
)
181181

182182
@staticmethod
183-
def _compare_helper(a, b):
183+
def _compare_helper(a: str, b: str):
184184
# python 3 compatible
185185
return (a > b) - (a < b)
186186

sdk/cosmos/azure-cosmos/tests/routing/test_collection_routing_map.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ def get_range_id(r):
8484
expectedRanges = ['2', '1', '3', '6', '7', '5']
8585
self.assertEqual(expectedRanges, list(map(get_range_id, filteredRanges)))
8686

87+
def test_point_range_mapping(self):
88+
partition_key_ranges = [{u'id': u'0', u'minInclusive': u'', u'maxExclusive': u'1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF'},
89+
{u'id': u'1', u'minInclusive': u'1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', u'maxExclusive': u'FF'}]
90+
partitionRangeWithInfo = [(r, True) for r in partition_key_ranges]
91+
expected_partition_key_range = routing_range.Range("", "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", True, False)
92+
pk_range = routing_range.Range("1EC0C2CBE45DBC919CF2B65D399C2673", "1EC0C2CBE45DBC919CF2B65D399C2673", True, True)
93+
normalized_pk_range = pk_range.to_normalized_range()
94+
95+
collection_routing_map = CollectionRoutingMap.CompleteRoutingMap(partitionRangeWithInfo, 'sample collection id')
96+
overlapping_partition_key_ranges = collection_routing_map.get_overlapping_ranges(normalized_pk_range)
97+
98+
# a partition key feed range should only map to one physical partition
99+
self.assertEqual(1, len(overlapping_partition_key_ranges))
100+
self.assertEqual(expected_partition_key_range, routing_range.Range.PartitionKeyRangeToRange(overlapping_partition_key_ranges[0]))
101+
87102
def test_collection_routing_map(self):
88103
Id = 'id'
89104
MinInclusive = 'minInclusive'

sdk/cosmos/azure-cosmos/tests/test_feed_range.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def setup():
4141
True),
4242
(Range("3F", "7F", False, True),
4343
Range("3F", "7F", True, True),
44-
False),
44+
True),
4545
(Range("3F", "7F", True, False),
4646
Range("3F", "7F", True, True),
4747
False),

0 commit comments

Comments
 (0)