Commit e708bc0
Slim PartitionKeyRange cache (#4393)
## Summary
Slims the cached `PartitionKeyRange` from 14 fields to 6, mirroring
memory optimization #2 from [Azure SDK for Python PR
#46297](Azure/azure-sdk-for-python#46297). The
wire contract is unchanged — the dropped JSON keys are silently ignored
by serde on deserialization.
## Motivation
The routing-map cache holds one `PartitionKeyRange` per `(container,
range)` pair. With many containers — or several `CosmosClient` instances
against the same account — that footprint dominates the per-container
heap. The Cosmos service returns ~14 fields on `/pkranges`, but the
routing layer reads only a handful.
This PR slims the cached struct from **14 fields (~232 B)** to **6
fields (112 B on 64-bit)**.
## Changes
### `azure_data_cosmos_driver`
**`PartitionKeyRange` slimmed:** the cached struct now retains the four
fields the routing layer consults (`id`, `min_inclusive`,
`max_exclusive`, `status`) plus `throughput_fraction` and `parents`,
kept for downstream consumers that read them directly. Dropped:
`resource_id`, `self_link`, `etag`, `timestamp`, `rid_prefix`,
`target_throughput`, `lsn`, `owned_archival_pk_range_ids`.
- `status` is the only retained field beyond the geometry triple
consulted on the routing hot path: `validate_and_build_index` reads it
to compute `highest_non_offline_pk_range_id` for split detection.
- `throughput_fraction` and `parents` are retained on the cached
representation for downstream consumers; the routing layer itself does
not consult them.
**`PartialEq` / `Hash` relaxed** to the routing-relevant identity tuple
`(id, min_inclusive, max_exclusive)` — `_rid` is no longer in the
comparison key. A grep across the workspace confirmed
`PartitionKeyRange` is never used as a `HashMap` / `HashSet` /
`BTreeMap` / `BTreeSet` *key* in this crate.
### Tests
- `cached_size_stays_small` — asserts `size_of::<PartitionKeyRange>() <=
120 B` (re-bloat tripwire) AND `== 112 B` on 64-bit (silent
layout-regression tripwire).
- `deserialization_ignores_stripped_metadata_fields` — asserts that a
service payload carrying every field still parses, that
`throughput_fraction` is read as `0.5`, and that the dropped fields are
silently absorbed.
- `try_combine_online_to_offline_recomputes_highest_non_offline` —
extended with the recovery flip (Online → Offline → Online round-trip)
so split-detection semantics are pinned across the field reduction.
## Validation
- `cargo fmt --check` clean
- `cargo clippy -p azure_data_cosmos_driver --all-features --all-targets
-- -D warnings` clean
- `cargo test -p azure_data_cosmos_driver --lib --all-features` — **1439
passed**
- `cspell` clean
## Reviewer notes
The slim struct is a public-API breaking change (8 `pub` fields removed,
`PartialEq` / `Hash` semantics relaxed); CHANGELOG documents both in a
single entry. There is no behavioral change to routing or to the wire
contract.
A microbenchmark for the cache hot path was prototyped during
development but is **not part of this PR** — it will be submitted
separately under the `azure_data_cosmos_benchmarks` crate so reviewers
of the model change can focus on the breaking-change surface here.
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 09d85ce commit e708bc0
3 files changed
Lines changed: 210 additions & 99 deletions
File tree
- sdk/cosmos/azure_data_cosmos_driver
- src
- driver/cache
- models
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
Lines changed: 93 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
348 | 348 | | |
349 | 349 | | |
350 | 350 | | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
367 | 354 | | |
368 | 355 | | |
369 | 356 | | |
| |||
619 | 606 | | |
620 | 607 | | |
621 | 608 | | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
622 | 699 | | |
623 | 700 | | |
624 | 701 | | |
| |||
0 commit comments