Skip to content

Commit 7e11672

Browse files
tvaron3Copilot
andauthored
Cherry-pick: Fix pk_range_cache to use .item_by_rid() for correct URL fetching (#4041)
Cherry-pick of #4032 into `release/azure_data_cosmos-previews`. Fixes pk_range_cache to use `.item_by_rid()` for correct URL fetching, ensuring partition key range lookups use the correct container resource ID. Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
1 parent e40336b commit 7e11672

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

sdk/cosmos/.cspell.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"Kusto",
7474
"libazurecosmos",
7575
"linearizability",
76+
"LLZA",
7677
"makefiles",
7778
"MEMORYSTATUSEX",
7879
"moka",
@@ -105,6 +106,7 @@
105106
"perfdb",
106107
"perfresults",
107108
"PIDS",
109+
"Pigw",
108110
"Pkrange",
109111
"PKRANGE",
110112
"pkranges",

sdk/cosmos/azure_data_cosmos/src/routing/partition_key_range_cache.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ impl PartitionKeyRangeCache {
144144
)
145145
.await;
146146

147+
if let Err(ref e) = routing_map {
148+
tracing::warn!(
149+
collection_rid,
150+
error = %e,
151+
"Failed to fetch routing map for collection"
152+
);
153+
}
147154
Ok(routing_map.ok())
148155
}
149156

@@ -189,7 +196,7 @@ impl PartitionKeyRangeCache {
189196
let pk_range_link = self
190197
.database_link
191198
.feed(ResourceType::Containers)
192-
.item(collection_rid)
199+
.item_by_rid(collection_rid)
193200
.feed(ResourceType::PartitionKeyRanges);
194201
let response = self
195202
.execute_partition_key_range_read_change_feed(
@@ -673,4 +680,61 @@ mod tests {
673680
assert!(range.target_throughput.is_some());
674681
assert_eq!(range.target_throughput.unwrap(), 1000.0);
675682
}
683+
684+
// Tests verifying that the pkranges resource link uses item_by_rid() so that
685+
// collection RIDs (which are base64-encoded and can contain '=', '+', '/') are
686+
// not URL-percent-encoded. Using item() would encode '=' to '%3D', causing 404s.
687+
688+
#[test]
689+
fn pkranges_link_rid_with_equals_is_not_encoded() {
690+
// RIDs like "pLLZAIuPigw=" contain '=' which item() would encode to '%3D'.
691+
// item_by_rid() must preserve it as-is.
692+
let collection_rid = "pLLZAIuPigw=";
693+
let database_link = ResourceLink::root(ResourceType::Databases).item("perfdb");
694+
let pk_range_link = database_link
695+
.feed(ResourceType::Containers)
696+
.item_by_rid(collection_rid)
697+
.feed(ResourceType::PartitionKeyRanges);
698+
699+
// Correct: '=' preserved, not encoded to '%3D'
700+
assert_eq!(
701+
"dbs/perfdb/colls/pLLZAIuPigw=/pkranges",
702+
pk_range_link.path()
703+
);
704+
}
705+
706+
#[test]
707+
fn pkranges_link_item_encodes_equals_incorrectly() {
708+
// Demonstrates the bug: item() URL-encodes '=' to '%3D', producing a path
709+
// that Cosmos DB cannot find (404).
710+
let collection_rid = "pLLZAIuPigw=";
711+
let database_link = ResourceLink::root(ResourceType::Databases).item("perfdb");
712+
let pk_range_link_wrong = database_link
713+
.feed(ResourceType::Containers)
714+
.item(collection_rid)
715+
.feed(ResourceType::PartitionKeyRanges);
716+
717+
// Wrong: '=' is encoded to '%3D', causing 404 from Cosmos DB
718+
assert!(
719+
pk_range_link_wrong.path().contains("%3D"),
720+
"item() should URL-encode '=' to '%3D'"
721+
);
722+
assert_eq!(
723+
"dbs/perfdb/colls/pLLZAIuPigw%3D/pkranges",
724+
pk_range_link_wrong.path()
725+
);
726+
}
727+
728+
#[test]
729+
fn pkranges_link_rid_with_plus_is_not_encoded() {
730+
// RIDs may also contain '+' (base64 char). item_by_rid() must preserve it.
731+
let collection_rid = "AB+CD/EF==";
732+
let database_link = ResourceLink::root(ResourceType::Databases).item("mydb");
733+
let pk_range_link = database_link
734+
.feed(ResourceType::Containers)
735+
.item_by_rid(collection_rid)
736+
.feed(ResourceType::PartitionKeyRanges);
737+
738+
assert_eq!("dbs/mydb/colls/AB+CD/EF==/pkranges", pk_range_link.path());
739+
}
676740
}

0 commit comments

Comments
 (0)