Fix Store tokenization including file manager's random id #11831
All Vouch review confidence checks passed
Residual risk 0.26 = Change Risk (0.65) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.65 · 330 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.347
Shallow Review Depth
shallow_review_depth
0.289
Sensitive Paths
sensitive_paths
0.065
Real PR Conversation Timeline
1 comments
"Review active on #11831"
general
weight: 0.35
djhoese
Description
This PR fixes the tokenization of Store objects so that they are unique (non-random) for read mode cases. See #11814 for details. This is already the case for when a Path/str is passed directly to
open_dataset, but if you pass a Store object then you would always get random tokens. This means that the same file opened with a store and then passed toopen_datasetwithchunks=defined to use dask would get a different dask token every time it was opened. This would cause dask to recompute/reload the data from the file when it could be sharing the computation.The main problem starts with
open_datasettokenizing the input str/Path/object:https://github.com/pydata/xarray/blob/c753f1ed366d3b97cf67abea4dee5f3d0eb20be3/xarray/backends/api.py#L269
For a filename this is a simple tokenization and the mtime is included. For a Store object it ends up going to dask, getting pickled, and then tokenized based on those bytes. However, this includes the underlying file manager object that some stores have and file manager's include a random id value. This means every instance gets a new token instead of sharing the same token like they could (see the minimal example in the related issue).
The fix is tokenize the store and file manager's state which is a tuple and dask will tokenize recursively. By customizing the file manager's tokenization to not include the id, or the lock, or anything that isn't specific to what is being read and how it is read this now produces the same token for every input. This can't be done reliably for all store types, but for the usual NetCDF and HDF5-based ones it works. Zarr would be nice but you'd have to somehow manage the modification time of all the individual zarr files and usually (?) I'd expect zarr to be used for remote files so that wouldn't work anyway.
#11826 should probably be merged before this and I can rebase after that.
Checklist
<!-- Feel free to remove check-list items aren't relevant to your change -->
whats-new.rstAI Disclosure
<!--- Please review our AI & contribution guidelines: https://docs.xarray.dev/en/stable/contribute/ai-policy.html. Remove this section if your PR does not contain AI-generated content. --->
- [x] I have tested any AI-generated content in my PR.
- [x] I take responsibility for any AI-generated content in my PR.
<!--- If you used AI to generate code, please specify the tool used and the prompt below. --->
Tools: Claude (Opus)