|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import asyncio |
5 | 6 | import json |
6 | 7 | import re |
7 | 8 | import warnings |
|
20 | 21 | upgrade_array_document, |
21 | 22 | ) |
22 | 23 | from zarr.core.metadata.v3 import RectilinearChunkGridMetadata, RegularChunkGridMetadata |
| 24 | +from zarr.core.sync import sync |
23 | 25 | from zarr.dtype import Int16 |
24 | 26 | from zarr.errors import ZarrUserWarning |
25 | 27 |
|
@@ -345,6 +347,64 @@ def store_legacy(doc: dict[str, Any]) -> None: |
345 | 347 | np.testing.assert_array_equal(reopened[...], np.concatenate([data, block])) |
346 | 348 |
|
347 | 349 |
|
| 350 | +@pytest.mark.parametrize( |
| 351 | + ("zarr_format", "shape", "inner", "expected"), |
| 352 | + [(2, (3,), None, (3,)), (3, (3,), None, (3,)), (3, (10,), (4,), (12,))], |
| 353 | + ids=["v2", "v3", "v3-sharded"], |
| 354 | +) |
| 355 | +@pytest.mark.parametrize("api", ["sync", "async", "async-concurrent"]) |
| 356 | +def test_write_stores_upgraded_metadata_first( |
| 357 | + tmp_path: Path, |
| 358 | + zarr_format: Literal[2, 3], |
| 359 | + shape: tuple[int, ...], |
| 360 | + inner: tuple[int, ...] | None, |
| 361 | + expected: tuple[int, ...], |
| 362 | + api: str, |
| 363 | +) -> None: |
| 364 | + """Writing chunks to an array read from an upgraded document first stores the |
| 365 | + upgraded metadata, so readers that do not upgrade (or read it differently) see |
| 366 | + the chunks the write stored.""" |
| 367 | + path = tmp_path / "legacy.zarr" |
| 368 | + zarr.create_array( |
| 369 | + store=path, |
| 370 | + shape=shape, |
| 371 | + chunks=inner or expected, |
| 372 | + shards=expected if inner else None, |
| 373 | + dtype="int16", |
| 374 | + fill_value=0, |
| 375 | + zarr_format=zarr_format, |
| 376 | + ) |
| 377 | + _rewrite_doc( |
| 378 | + path, |
| 379 | + zarr_format, |
| 380 | + lambda doc: ( |
| 381 | + doc.update(chunks=[0]) |
| 382 | + if zarr_format == 2 |
| 383 | + else doc["chunk_grid"]["configuration"].update(chunk_shape=[0]) |
| 384 | + ), |
| 385 | + ) |
| 386 | + data = np.arange(1, shape[0] + 1, dtype="int16") |
| 387 | + with pytest.warns(ZarrUserWarning, match="is read as"): |
| 388 | + arr = zarr.open_array(store=path, mode="r+") |
| 389 | + if api == "sync": |
| 390 | + arr[:] = data |
| 391 | + elif api == "async": |
| 392 | + sync(arr.async_array.setitem(slice(None), data)) |
| 393 | + else: |
| 394 | + |
| 395 | + async def write_twice() -> None: |
| 396 | + # Both writes find the metadata not yet stored, and both store it. |
| 397 | + await asyncio.gather(*(arr.async_array.setitem(slice(None), data) for _ in "ab")) |
| 398 | + |
| 399 | + sync(write_twice()) |
| 400 | + |
| 401 | + with warnings.catch_warnings(): |
| 402 | + warnings.simplefilter("error", ZarrUserWarning) |
| 403 | + reopened = zarr.open_array(store=path, mode="r") |
| 404 | + assert (reopened.shards or reopened.chunks) == expected |
| 405 | + np.testing.assert_array_equal(reopened[...], data) |
| 406 | + |
| 407 | + |
348 | 408 | @pytest.mark.filterwarnings("ignore:Consolidated metadata is currently not part:UserWarning") |
349 | 409 | @pytest.mark.parametrize("zarr_format", [2, 3]) |
350 | 410 | def test_legacy_chunk_size_consolidated(tmp_path: Path, zarr_format: Literal[2, 3]) -> None: |
|
0 commit comments