Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
2950e20
merge from main and resolve conflicts
Aug 14, 2024
7a1a1eb
remove async keyword from changeFeed query in aio package
Aug 18, 2024
b6c53fb
refactor
Aug 18, 2024
5f16b14
refactor
Aug 18, 2024
36990ef
fix pylint
Aug 20, 2024
3c569e8
added public surface methods
tvaron3 Aug 20, 2024
7479b0c
pylint fix
Aug 20, 2024
2e76620
fix
Aug 21, 2024
56bbb9e
added functionality for merging session tokens from logical pk
tvaron3 Aug 21, 2024
8c0aa46
fix mypy
Aug 21, 2024
28394b9
added tests for basic merge and split
tvaron3 Aug 21, 2024
25c3363
resolve comments
Aug 27, 2024
cecdfa5
resolve comments
Aug 28, 2024
65ed132
resolve comments
Aug 28, 2024
4bb30d2
resolve comments
Aug 28, 2024
5addcdc
fix pylint
Aug 29, 2024
59814d7
fix mypy
Aug 29, 2024
ec79b94
merge feed range changes
tvaron3 Aug 22, 2024
66c3f7b
fix tests
Sep 4, 2024
1e7a268
merged with feed range branch
tvaron3 Sep 4, 2024
997b6b0
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-python …
tvaron3 Sep 4, 2024
7eda72f
Merge branch 'main' into addFeedRangeSupportInChangeFeed
Sep 4, 2024
3a2e4e1
add tests
Sep 5, 2024
0883dac
fix pylint
Sep 5, 2024
b7d1210
Merge branch 'addFeedRangeSupportInChangeFeed' of https://github.com/…
tvaron3 Sep 5, 2024
195c47c
fix and resolve comments
Sep 6, 2024
246b1be
fix and resolve comments
Sep 6, 2024
10fe387
Added isSubsetFeedRange logic
tvaron3 Sep 9, 2024
6498311
Added request context to crud operations, session token helpers
tvaron3 Sep 11, 2024
5a13ddf
Merge branch 'addFeedRangeSupportInChangeFeed' of https://github.com/…
tvaron3 Sep 11, 2024
f5d0d7b
Merge branch 'main' into addFeedRangeSupportInChangeFeed
Sep 13, 2024
5cde59b
revert unnecessary change
Sep 13, 2024
a494346
Added more tests
tvaron3 Sep 20, 2024
0d75607
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-python …
tvaron3 Sep 20, 2024
c8c099f
Merge branch 'addFeedRangeSupportInChangeFeed' of https://github.com/…
tvaron3 Sep 20, 2024
ad3ae4f
Added more tests
tvaron3 Oct 5, 2024
8f466a1
merge with main
tvaron3 Oct 6, 2024
5249d0a
Changed tests to use new public feed range and more test coverage for…
tvaron3 Oct 6, 2024
40523f5
Added more tests
tvaron3 Oct 7, 2024
9f88b4e
Fix tests and add changelog
tvaron3 Oct 7, 2024
7c23e87
fix spell checks
tvaron3 Oct 7, 2024
4d0b058
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-python …
tvaron3 Oct 7, 2024
d7c598e
Added tests and pushed request context to client level
tvaron3 Oct 8, 2024
8698098
Added async methods and removed feed range from request context
tvaron3 Oct 8, 2024
c252d88
fix tests
tvaron3 Oct 9, 2024
51e721b
fix tests and pylint
tvaron3 Oct 9, 2024
923055b
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-python …
tvaron3 Oct 9, 2024
104e341
Reacting to comments
tvaron3 Oct 10, 2024
5552912
Reacting to comments
tvaron3 Oct 10, 2024
1bbbd0f
pylint and added hpk tests
tvaron3 Oct 10, 2024
a9299ab
reacting to comments
tvaron3 Oct 11, 2024
2155016
fix tests and mypy
tvaron3 Oct 11, 2024
0436355
fix mypy
tvaron3 Oct 11, 2024
103eb41
fix mypy
tvaron3 Oct 11, 2024
76451df
reacting to comments
tvaron3 Oct 15, 2024
7b0f4b7
reacting to comments
tvaron3 Oct 15, 2024
5d7b978
reacting to comments
tvaron3 Oct 15, 2024
d54992f
fix cspell
tvaron3 Oct 15, 2024
fa16830
rename method to get_latest_session_token
tvaron3 Oct 16, 2024
b2ac9d8
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-python …
tvaron3 Oct 17, 2024
6914a20
reacting to reverted feed range
tvaron3 Oct 17, 2024
ab9723a
change based on the api review
Oct 23, 2024
8a4305d
Reacting to API review and adding samples.
tvaron3 Oct 25, 2024
3a1f160
Reacting to API review and adding samples.
tvaron3 Oct 25, 2024
4bc16b1
Merge branch 'main' into tvaron3/sessionTokenHelper
tvaron3 Oct 25, 2024
900d001
Fixed pylint
tvaron3 Oct 25, 2024
96a165f
Merge branch 'tvaron3/sessionTokenHelper' of https://github.com/tvaro…
tvaron3 Oct 25, 2024
eab1822
Reacting to comments
tvaron3 Oct 28, 2024
97ffec7
Reacting to comments
tvaron3 Oct 28, 2024
2264465
Reacting to comments
tvaron3 Oct 29, 2024
35588fa
Reacting to comments
tvaron3 Oct 29, 2024
c42966f
Fix pydoc
tvaron3 Oct 30, 2024
786e357
Fix pydoc
tvaron3 Oct 31, 2024
0de21b4
reacting to comments
tvaron3 Oct 31, 2024
d32a6f1
reacting to comments
tvaron3 Oct 31, 2024
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
Prev Previous commit
Next Next commit
Added more tests
  • Loading branch information
tvaron3 committed Oct 7, 2024
commit 40523f5a5edc51474588bc7caab90b71c93ca81b
113 changes: 48 additions & 65 deletions sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
from urllib3.util.retry import Retry

from azure.core import PipelineClient
from ._session_token_helpers import is_compound_session_token, merge_session_tokens
from ._session_token_helpers import is_compound_session_token, merge_session_tokens, split_compound_session_tokens, \
merge_session_tokens_with_same_pkrangeid
from ._vector_session_token import VectorSessionToken
from azure.core.credentials import TokenCredential
from azure.core.paging import ItemPaged
Expand Down Expand Up @@ -3337,15 +3338,17 @@ def _get_partition_key_definition(self, collection_link: str) -> Optional[Dict[s
return partition_key_definition

def _get_updated_session_token(self, feed_ranges_to_session_tokens, target_feed_range):
target_feed_range_normalized = target_feed_range.get_normalized_range()
target_feed_range_normalized = target_feed_range._feed_range_internal.get_normalized_range()
# filter out tuples that overlap with target_feed_range and normalizes all the ranges
overlapping_ranges = [(feed_range[0].get_normalized_range(), feed_range[1]) for feed_range in feed_ranges_to_session_tokens if
Range.overlaps(target_feed_range_normalized, feed_range[0].get_normalized_range())]
overlapping_ranges = [(feed_range_to_session_token[0]._feed_range_internal.get_normalized_range(), feed_range_to_session_token[1])
for feed_range_to_session_token in feed_ranges_to_session_tokens if Range.overlaps(
target_feed_range_normalized, feed_range_to_session_token[0]._feed_range_internal.get_normalized_range())]
# Is there a feed_range that is a superset of some of the other feed_ranges excluding tuples
# with compound session tokens?
if overlapping_ranges == 0:
if len(overlapping_ranges) == 0:
raise ValueError('There were no overlapping feed ranges with the target.')

# merge any session tokens that are the same exact feed range
i = 0
j = 1
while i < len(overlapping_ranges) and j < len(overlapping_ranges):
Expand All @@ -3368,7 +3371,6 @@ def _get_updated_session_token(self, feed_ranges_to_session_tokens, target_feed_


updated_session_token = ""
remaining_session_tokens = []
done_overlapping_ranges = []
while len(overlapping_ranges) != 0:
feed_range_cmp, session_token_cmp = overlapping_ranges[0]
Expand All @@ -3393,75 +3395,56 @@ def _get_updated_session_token(self, feed_ranges_to_session_tokens, target_feed_
merged_range = subsets[j][0]
session_tokens = [subsets[j][1]]
merged_indices = [subsets[j][2]]
for k in range(len(subsets)):
if j == k:
continue
if merged_range.can_merge(subsets[k][0]):
merged_range = merged_range.merge(subsets[k][0])
session_tokens.append(subsets[k][1])
merged_indices.append(subsets[k][2])
if feed_range_cmp == merged_range:
# if it is the bigger one remove the smaller ranges
# if it is the smaller ranges remove the bigger range
# if it is neither compound
child_lsns_larger = True
child_lsns_smaller = True
for session_token in session_tokens:
tokens = session_token.split(":")
vector_session_token = VectorSessionToken.create(tokens[1])
if vector_session_token.global_lsn < vector_session_token_cmp.global_lsn:
child_lsns_smaller = False
else:
child_lsns_larger = False
feed_ranges_to_remove = [overlapping_ranges[i] for i in merged_indices]
for feed_range_to_remove in feed_ranges_to_remove:
overlapping_ranges.remove(feed_range_to_remove)
if child_lsns_larger:
session_tokens.remove(session_token_cmp)
overlapping_ranges.append((merged_range, ','.join(map(str, session_tokens))))
overlapping_ranges.remove(overlapping_ranges[0])
elif child_lsns_smaller:
overlapping_ranges.append((merged_range, ','.join(map(str, session_tokens))))
overlapping_ranges.remove(overlapping_ranges[0])

not_found = False
break
if len(subsets) == 1:
tokens = session_tokens[0].split(":")
vector_session_token = VectorSessionToken.create(tokens[1])
if vector_session_token_cmp.is_greater(vector_session_token):
overlapping_ranges.remove(overlapping_ranges[merged_indices[0]])
else:
for k in range(len(subsets)):
if j == k:
continue
if merged_range.can_merge(subsets[k][0]):
merged_range = merged_range.merge(subsets[k][0])
session_tokens.append(subsets[k][1])
merged_indices.append(subsets[k][2])
if feed_range_cmp == merged_range:
# if it is the bigger one remove the smaller ranges
# if it is the smaller ranges remove the bigger range
# if it is neither compound
child_lsns_larger = True
for session_token in session_tokens:
tokens = session_token.split(":")
vector_session_token = VectorSessionToken.create(tokens[1])
if vector_session_token_cmp.is_greater(vector_session_token):
child_lsns_larger = False
feed_ranges_to_remove = [overlapping_ranges[i] for i in merged_indices]
for feed_range_to_remove in feed_ranges_to_remove:
overlapping_ranges.remove(feed_range_to_remove)
if child_lsns_larger:
overlapping_ranges.append((merged_range, ','.join(map(str, session_tokens))))
overlapping_ranges.remove(overlapping_ranges[0])
not_found = False
break

j += 1

done_overlapping_ranges.append(overlapping_ranges[0])
overlapping_ranges.remove(overlapping_ranges[0])

for _, session_token in done_overlapping_ranges:
# here break up session tokens that are compound
if is_compound_session_token(session_token):
tokens = session_token.split(",")
for token in tokens:
remaining_session_tokens.append(token)
else:
remaining_session_tokens.append(session_token)
# break up session tokens that are compound
remaining_session_tokens = split_compound_session_tokens(done_overlapping_ranges)

if len(remaining_session_tokens) == 1:
return remaining_session_tokens[0]
new_session_tokens = []
# merging any session tokens with same pkrangeid
remaining_session_tokens = merge_session_tokens_with_same_pkrangeid(remaining_session_tokens)

# compound the remaining session tokens
for i in range(len(remaining_session_tokens)):
for j in range(i + 1, len(remaining_session_tokens)):
tokens1 = remaining_session_tokens[i].split(":")
tokens2 = remaining_session_tokens[j].split(":")
pk_range_id1 = tokens1[0]
pk_range_id2 = tokens2[0]
if pk_range_id1 == pk_range_id2:
vector_session_token1 = VectorSessionToken.create(tokens1[1])
vector_session_token2 = VectorSessionToken.create(tokens2[1])
vector_session_token = vector_session_token1.merge(vector_session_token2)
new_session_tokens.append(pk_range_id1 + ":" + vector_session_token.session_token)
remaining_session_tokens.remove(remaining_session_tokens[i])
remaining_session_tokens.remove(remaining_session_tokens[j])
new_session_tokens.extend(remaining_session_tokens)
for i in range(len(new_session_tokens)):
if i == len(new_session_tokens) - 1:
updated_session_token += new_session_tokens[i]
if i == len(remaining_session_tokens) - 1:
updated_session_token += remaining_session_tokens[i]
else:
updated_session_token += new_session_tokens[i] + ","
updated_session_token += remaining_session_tokens[i] + ","

return updated_session_token
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def _compare_helper(a, b):

@staticmethod
def overlaps(range1, range2):

if range1 is None or range2 is None:
return False
if range1.isEmpty() or range2.isEmpty():
Expand All @@ -195,7 +194,7 @@ def overlaps(range1, range2):
cmp1 = Range._compare_helper(range1.min, range2.max)
cmp2 = Range._compare_helper(range2.min, range1.max)

if cmp1 <= 0 or cmp2 <= 0:
if cmp1 <= 0 and cmp2 <= 0:
if (cmp1 == 0 and not (range1.isMinInclusive and range2.isMaxInclusive)) or (
cmp2 == 0 and not (range2.isMinInclusive and range1.isMaxInclusive)
):
Expand Down Expand Up @@ -225,4 +224,4 @@ def is_subset(self, parent_range) -> bool:
normalized_child_range = self.to_normalized_range()
return normalized_parent_range.contains(normalized_child_range.min) and \
(normalized_parent_range.contains(normalized_child_range.max)
or normalized_parent_range.max == normalized_child_range.max)
or normalized_parent_range.max == normalized_child_range.max)
35 changes: 34 additions & 1 deletion sdk/cosmos/azure-cosmos/azure/cosmos/_session_token_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,45 @@ def merge_session_tokens(session_token1, session_token2):
pk_range_id = pk_range_id1
if pk_range_id1 != pk_range_id2:
pk_range_id = pk_range_id1 \
if vector_session_token1.global_lsn > vector_session_token2.global_lsn else pk_range_id2
if vector_session_token1.is_greater(vector_session_token2) else pk_range_id2
vector_session_token = vector_session_token1.merge(vector_session_token2)
return pk_range_id + ":" + vector_session_token.session_token

def is_compound_session_token(session_token):
return "," in session_token

def split_compound_session_tokens(compound_session_tokens):
session_tokens = []
for _, session_token in compound_session_tokens:
if is_compound_session_token(session_token):
tokens = session_token.split(",")
for token in tokens:
session_tokens.append(token)
else:
session_tokens.append(session_token)
return session_tokens

def merge_session_tokens_with_same_pkrangeid(session_tokens):
new_session_tokens = []
i = 0
while i < len(session_tokens):
j = i + 1
while j < len(session_tokens):
tokens1 = session_tokens[i].split(":")
tokens2 = session_tokens[j].split(":")
pk_range_id1 = tokens1[0]
pk_range_id2 = tokens2[0]
if pk_range_id1 == pk_range_id2:
vector_session_token1 = VectorSessionToken.create(tokens1[1])
vector_session_token2 = VectorSessionToken.create(tokens2[1])
vector_session_token = vector_session_token1.merge(vector_session_token2)
new_session_tokens.append(pk_range_id1 + ":" + vector_session_token.session_token)
remove_session_tokens = [session_tokens[i], session_tokens[j]]
for token in remove_session_tokens:
session_tokens.remove(token)
i = -1
j += 1
i += 1

new_session_tokens.extend(session_tokens)
return new_session_tokens
4 changes: 1 addition & 3 deletions sdk/cosmos/azure-cosmos/azure/cosmos/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
_deserialize_throughput,
_replace_throughput,
GenerateGuidId,
_set_properties_cache,
ParsePaths,
TrimBeginningAndEndingSlashes
_set_properties_cache
)
from ._cosmos_client_connection import CosmosClientConnection
from ._feed_range import FeedRange, FeedRangeEpk
Expand Down
79 changes: 52 additions & 27 deletions sdk/cosmos/azure-cosmos/test/test_feed_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import test_config
from azure.cosmos._feed_range import FeedRangeEpk
from azure.cosmos._routing.routing_range import Range
from test.test_config import TestConfig



@pytest.fixture(scope="class")
def setup():
Expand All @@ -23,13 +20,58 @@ def setup():
"You must specify your Azure Cosmos account values for "
"'masterKey' and 'host' at the top of this class to run the "
"tests.")
test_client = cosmos_client.CosmosClient(TestFeedRange.host, TestConfig.masterKey),
test_client = cosmos_client.CosmosClient(TestFeedRange.host, test_config.TestConfig.masterKey),
created_db = test_client[0].get_database_client(TestFeedRange.TEST_DATABASE_ID)
return {
"created_db": created_db,
"created_collection": created_db.get_container_client(TestFeedRange.TEST_CONTAINER_ID)
}

test_subset_ranges = [(Range("", "FF", True, False),
Range("3F", "7F", True, False),
True),
(Range("3F", "7F", True, False),
Range("", "FF", True, False),
False),
(Range("3F", "7F", True, False),
Range("", "5F", True, False),
False),
(Range("3F", "7F", True, True),
Range("3F", "7F", True, True),
True),
(Range("3F", "7F", False, True),
Range("3F", "7F", True, True),
False),
(Range("3F", "7F", True, False),
Range("3F", "7F", True, True),
False),
(Range("3F", "7F", True, False),
Range("", "2F", True, False),
False)]


test_overlaps_ranges = [(Range("", "FF", True, False),
Range("3F", "7F", True, False),
True),
(Range("3F", "7F", True, False),
Range("", "FF", True, False),
True),
(Range("3F", "7F", True, False),
Range("", "5F", True, False),
True),
(Range("3F", "7F", True, False),
Range("3F", "7F", True, False),
True),
(Range("3F", "7F", True, False),
Range("", "2F", True, False),
False),
(Range("3F", "7F", True, False),
Range("6F", "FF", True, False),
True),
(Range("AA", "BB", True, False),
Range("CC", "FF", True, False),
False)]

@pytest.mark.cosmosEmulator
@pytest.mark.unittest
@pytest.mark.usefixtures("setup")
Expand All @@ -53,29 +95,7 @@ def test_partition_key_to_feed_range(self, setup):
"3c80b1b7310bb39f29cc4ea05bdd461f", True, False)
setup["created_db"].delete_container(created_container)

test_ranges = [(Range("", "FF", True, False),
Range("3F", "7F", True, False),
True),
(Range("3F", "7F", True, False),
Range("", "FF", True, False),
False),
(Range("3F", "7F", True, False),
Range("", "5F", True, False),
False),
(Range("3F", "7F", True, True),
Range("3F", "7F", True, True),
True),
(Range("3F", "7F", False, True),
Range("3F", "7F", True, True),
False),
(Range("3F", "7F", True, False),
Range("3F", "7F", True, True),
False),
(Range("3F", "7F", True, False),
Range("", "2F", True, False),
False)]

@pytest.mark.parametrize("parent_feed_range, child_feed_range, is_subset", test_ranges)
@pytest.mark.parametrize("parent_feed_range, child_feed_range, is_subset", test_subset_ranges)
def test_feed_range_is_subset(self, setup, parent_feed_range, child_feed_range, is_subset):
epk_parent_feed_range = FeedRangeEpk(parent_feed_range)
epk_child_feed_range = FeedRangeEpk(child_feed_range)
Expand All @@ -86,5 +106,10 @@ def test_feed_range_is_subset_from_pk(self, setup):
epk_child_feed_range = setup["created_collection"].feed_range_from_partition_key("1")
assert setup["created_collection"].is_feed_range_subset(epk_parent_feed_range, epk_child_feed_range)

@pytest.mark.parametrize("range1, range2, overlaps", test_overlaps_ranges)
def test_overlaps(self, setup, range1, range2, overlaps):
assert Range.overlaps(range1, range2) == overlaps


if __name__ == '__main__':
unittest.main()
7 changes: 2 additions & 5 deletions sdk/cosmos/azure-cosmos/test/test_request_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import azure.cosmos.cosmos_client as cosmos_client
import test_config
from azure.cosmos._routing.routing_range import Range
from test.test_config import TestConfig



@pytest.fixture(scope="class")
def setup():
Expand All @@ -21,7 +18,7 @@ def setup():
"You must specify your Azure Cosmos account values for "
"'masterKey' and 'host' at the top of this class to run the "
"tests.")
test_client = cosmos_client.CosmosClient(TestRequestContext.host, TestConfig.masterKey),
test_client = cosmos_client.CosmosClient(TestRequestContext.host, test_config.TestConfig.masterKey),
created_db = test_client[0].get_database_client(TestRequestContext.TEST_DATABASE_ID)
return {
"created_db": created_db,
Expand Down Expand Up @@ -52,7 +49,7 @@ def createItem(id = 'item' + str(uuid.uuid4()), pk='A', name='sample'):
@pytest.mark.unittest
@pytest.mark.usefixtures("setup")
class TestRequestContext:
"""Tests to verify methods for operations on feed ranges
"""Tests to verify request context gets populated correctly
"""

host = test_config.TestConfig.host
Expand Down
Loading