Skip to content

Commit d0d25e2

Browse files
authored
More hypothesis stateful testing fixes (#2355)
Follow-up on #2349, also detected in nightly runs There are two fixes in this PR: - a test case where `reset_branch` revived commits after expiration ran, and model predicted one extra deletion. ``` state.initialize(spec_version=1) state.set_and_commit(ncommits=7) state.reset_branch(branch=branches_0, commit=commits_5) state.expire_snapshots(delete_expired_branches=True, delete_expired_tags=True) state.reset_branch(branch=branches_0, commit=commits_6) state.garbage_collect() ``` - an upstream zarr bug, will open a PR there: ``` a = zarr.create_array({}, shape=(4, 4), chunks=(2, 2), shards=(2, 4), dtype="int64") a.oindex[np.array([0, 1]), np.array([2, 3])] = np.zeros((2, 2), dtype="int64") # ValueError: shape mismatch: value array of shape (2,2) could not be # broadcast to indexing result of shape (4,) ``` There is a probe like `ZARR_RECREATES_ARRAYS` in #2349 that can be dropped once it is fixed upstream.
1 parent dca841e commit d0d25e2

2 files changed

Lines changed: 86 additions & 6 deletions

File tree

‎icechunk-python/tests/test_stateful_repo_ops.py‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,14 @@ def expire_snapshots(
466466
# V2's released_snapshots only contains snapshots truly removed from
467467
# the repo info, so all can be popped.
468468
ref_pointees = set(self.refs_iter()) if self.spec_version < 2 else set()
469+
470+
# V1 expiration rewrites one snapshot file per ref, so a snapshot no ref
471+
# reaches keeps its parent: a later reset_branch to it makes that expired
472+
# ancestor reachable again. V2 rewrites every snapshot in the repo info.
473+
rewritable = (
474+
self.reachable_snapshots() if self.spec_version < 2 else set(self.commits)
475+
)
476+
469477
for id in expired_snaps:
470478
# notice we don't delete from self.ondisk_snaps, those can still be deleted by GC
471479
# however we do pop from `commits` since that is a list of unexpired snaps
@@ -495,8 +503,8 @@ def expire_snapshots(
495503

496504
# we reparent to the initial snapshot for simplicity. This should be good enough to make
497505
# self.reachable_snapshots() accurate.
498-
for c in self.commits.values():
499-
if c.parent_id in expired_snaps:
506+
for cid, c in self.commits.items():
507+
if cid in rewritable and c.parent_id in expired_snaps:
500508
c.parent_id = self.initial_snapshot_id
501509

502510
if delete_expired_tags:
@@ -530,11 +538,16 @@ def expire_snapshots(
530538

531539
def reachable_snapshots(self) -> set[str]:
532540
assert self.initial_snapshot_id is not None
541+
# V1 walks parent pointers in the snapshot files, so a snapshot dropped
542+
# from `commits` by expiration is still reachable while its file lives.
543+
# V2 walks the repo info ancestry, which only holds `commits`.
544+
snaps = self.ondisk_snaps if self.spec_version < 2 else self.commits
533545
reachable_snaps: set[str] = set((self.initial_snapshot_id,))
534-
for commit_id in self.refs_iter():
535-
while commit_id is not None:
546+
for ref_id in self.refs_iter():
547+
commit_id: str | None = ref_id
548+
while commit_id is not None and commit_id in snaps:
536549
reachable_snaps.add(commit_id)
537-
commit_id = self.commits[commit_id].parent_id # type: ignore[assignment]
550+
commit_id = snaps[commit_id].parent_id
538551
return reachable_snaps
539552

540553
def garbage_collect(

‎icechunk-python/tests/test_zarr/test_stateful.py‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from collections.abc import Callable
66
from typing import Any, TypeVar
77

8+
import hypothesis.extra.numpy as npst
89
import hypothesis.strategies as st
910
import numpy as np
1011
import pytest
@@ -30,11 +31,12 @@
3031
from icechunk.testing.trees import absolute, valid_moves
3132
from icechunk.testing.utils import update_paths_after_move
3233
from zarr import Array
34+
from zarr.codecs import ShardingCodec
3335
from zarr.codecs.bytes import BytesCodec
3436
from zarr.core.buffer import default_buffer_prototype
3537
from zarr.testing.stateful import ZarrHierarchyStateMachine, split_prefix_name
3638
from zarr.testing.strategies import arrays as zarr_arrays
37-
from zarr.testing.strategies import node_names
39+
from zarr.testing.strategies import node_names, orthogonal_indices
3840

3941
PROTOTYPE = default_buffer_prototype()
4042

@@ -45,6 +47,28 @@
4547
# store if we drove it ourselves — so only override against the newer API.
4648
ZARR_RECREATES_ARRAYS = "open_mode" in inspect.signature(zarr_arrays).parameters
4749

50+
51+
def _writes_sharded_oindex() -> bool:
52+
"""Whether zarr can write an orthogonal selection to a sharded array.
53+
54+
An integer array on any axis reaches the sharding codec in broadcast
55+
(``np.ix_``) form. That codec's partial-encode path reads the selection
56+
back as a pointwise one and raises. The probe writes four points inside one
57+
shard. A one-point selection still writes, because both shapes then hold
58+
one element.
59+
"""
60+
arr = zarr.create_array({}, shape=(4, 4), chunks=(2, 2), shards=(2, 4), dtype="int64")
61+
try:
62+
arr.oindex[np.array([0, 1]), np.array([2, 3])] = np.zeros((2, 2), dtype="int64")
63+
except (ValueError, IndexError):
64+
return False
65+
return True
66+
67+
68+
# Probed rather than pinned to a version, so the override below disappears on
69+
# its own once zarr releases the fix.
70+
ZARR_WRITES_SHARDED_OINDEX = _writes_sharded_oindex()
71+
4872
Frequency = TypeVar("Frequency", bound=Callable[..., Any])
4973

5074

@@ -70,6 +94,17 @@ def storage_chunk_sizes(arr: "Array[Any]") -> tuple[tuple[int, ...], ...]:
7094
)
7195

7296

97+
def is_sharded(arr: "Array[Any]") -> bool:
98+
"""True when the array stores shards.
99+
100+
``Array.shards`` raises on a rectilinear chunk grid. The newer zarr
101+
strategies draw those grids, so this reads the codec chain instead.
102+
These arrays are always zarr format 3. Format 2 cannot shard.
103+
"""
104+
codecs = getattr(arr.metadata, "codecs", ())
105+
return any(isinstance(codec, ShardingCodec) for codec in codecs)
106+
107+
73108
# pytestmark = [
74109
# pytest.mark.filterwarnings(
75110
# "ignore::zarr.core.dtype.common.UnstableSpecificationWarning"
@@ -192,6 +227,38 @@ def add_array(self, data: st.DataObject, name: str) -> None:
192227
arr[:] = a[:]
193228
self.all_arrays.add(path)
194229

230+
if not ZARR_WRITES_SHARDED_OINDEX:
231+
232+
@precondition(lambda self: bool(self.all_arrays))
233+
@rule(data=st.data())
234+
def overwrite_array_orthogonal_indexing(self, data: st.DataObject) -> None:
235+
"""Copy of zarr's rule with the broken sharded writes skipped.
236+
237+
An integer array on any axis reaches the sharding codec in
238+
broadcast (``np.ix_``) form. That codec's partial-encode path reads
239+
the selection back as a pointwise one. The write then raises
240+
``ValueError: shape mismatch`` unless it covers a single point. It
241+
fails on the model store, before icechunk sees it. A selection of
242+
slices always works, and so does any unsharded target.
243+
"""
244+
array = data.draw(st.sampled_from(sorted(self.all_arrays)))
245+
model_array = zarr.open_array(path=array, store=self.model)
246+
store_array = zarr.open_array(path=array, store=self.store)
247+
indexer, _ = data.draw(orthogonal_indices(shape=model_array.shape))
248+
assume(
249+
not is_sharded(model_array)
250+
or not any(isinstance(dim_sel, np.ndarray) for dim_sel in indexer)
251+
)
252+
note(f"overwriting array orthogonal {indexer=}")
253+
new_data = data.draw(
254+
npst.arrays(
255+
shape=model_array.oindex[indexer].shape, # type: ignore[union-attr]
256+
dtype=model_array.dtype,
257+
)
258+
)
259+
model_array.oindex[indexer] = new_data
260+
store_array.oindex[indexer] = new_data
261+
195262
@precondition(
196263
lambda self: (
197264
Version(self.ic.__version__).major >= 2

0 commit comments

Comments
 (0)