|
2 | 2 |
|
3 | 3 | import asyncio |
4 | 4 | import pickle |
| 5 | +import time |
5 | 6 | from abc import abstractmethod |
6 | 7 | from typing import TYPE_CHECKING, Self |
7 | 8 |
|
|
10 | 11 | from zarr.storage import WrapperStore |
11 | 12 |
|
12 | 13 | if TYPE_CHECKING: |
| 14 | + from collections.abc import AsyncIterator, Iterable, Sequence |
13 | 15 | from typing import Any |
14 | 16 |
|
15 | 17 | from zarr.core.buffer.core import BufferPrototype |
@@ -718,7 +720,10 @@ def set_latency(self) -> float: |
718 | 720 | return max(0.0, np.random.normal(loc=self._set_latency[0], scale=self._set_latency[1])) |
719 | 721 |
|
720 | 722 | def _with_store(self, store: Store) -> Self: |
721 | | - return type(self)(store, get_latency=self.get_latency, set_latency=self.set_latency) |
| 723 | + # Pass the raw latency config, not the sampled `get_latency`/`set_latency` |
| 724 | + # properties — sampling would freeze a `(loc, scale)` distribution into |
| 725 | + # one fixed float on derived stores (e.g. via `with_read_only`). |
| 726 | + return type(self)(store, get_latency=self._get_latency, set_latency=self._set_latency) |
722 | 727 |
|
723 | 728 | async def set(self, key: str, value: Buffer) -> None: |
724 | 729 | """ |
@@ -763,3 +768,76 @@ async def get( |
763 | 768 | """ |
764 | 769 | await asyncio.sleep(self.get_latency) |
765 | 770 | return await self._store.get(key, prototype=prototype, byte_range=byte_range) |
| 771 | + |
| 772 | + def get_sync( |
| 773 | + self, |
| 774 | + key: str, |
| 775 | + *, |
| 776 | + prototype: BufferPrototype | None = None, |
| 777 | + byte_range: ByteRequest | None = None, |
| 778 | + ) -> Buffer | None: |
| 779 | + """Add latency to `get_sync`. |
| 780 | +
|
| 781 | + Sleeps `self.get_latency` on the calling thread (the sync path runs on |
| 782 | + worker threads, not the event loop) before delegating to the wrapped |
| 783 | + store. |
| 784 | + """ |
| 785 | + time.sleep(self.get_latency) |
| 786 | + return super().get_sync(key, prototype=prototype, byte_range=byte_range) |
| 787 | + |
| 788 | + def set_sync(self, key: str, value: Buffer) -> None: |
| 789 | + """Add latency to `set_sync`. |
| 790 | +
|
| 791 | + Sleeps `self.set_latency` on the calling thread (the sync path runs on |
| 792 | + worker threads, not the event loop) before delegating to the wrapped |
| 793 | + store. |
| 794 | + """ |
| 795 | + time.sleep(self.set_latency) |
| 796 | + super().set_sync(key, value) |
| 797 | + |
| 798 | + async def get_ranges( |
| 799 | + self, |
| 800 | + key: str, |
| 801 | + byte_ranges: Sequence[ByteRequest | None], |
| 802 | + *, |
| 803 | + prototype: BufferPrototype, |
| 804 | + max_concurrency: int | None = None, |
| 805 | + max_gap_bytes: int | None = None, |
| 806 | + max_coalesced_bytes: int | None = None, |
| 807 | + ) -> AsyncIterator[Sequence[tuple[int, Buffer | None]]]: |
| 808 | + """Byte-range reads built on `self.get`, so each fetch pays latency. |
| 809 | +
|
| 810 | + Routes through the coalescing `Store.get_ranges` default instead of the |
| 811 | + `WrapperStore` delegation, which would bypass this wrapper's `get` and |
| 812 | + therefore the synthetic latency. `None` for a coalescing kwarg means |
| 813 | + "use the `Store` default". |
| 814 | + """ |
| 815 | + kwargs: dict[str, int] = {} |
| 816 | + if max_concurrency is not None: |
| 817 | + kwargs["max_concurrency"] = max_concurrency |
| 818 | + if max_gap_bytes is not None: |
| 819 | + kwargs["max_gap_bytes"] = max_gap_bytes |
| 820 | + if max_coalesced_bytes is not None: |
| 821 | + kwargs["max_coalesced_bytes"] = max_coalesced_bytes |
| 822 | + async for group in Store.get_ranges(self, key, byte_ranges, prototype=prototype, **kwargs): |
| 823 | + yield group |
| 824 | + |
| 825 | + async def get_partial_values( |
| 826 | + self, |
| 827 | + prototype: BufferPrototype, |
| 828 | + key_ranges: Iterable[tuple[str, ByteRequest | None]], |
| 829 | + ) -> list[Buffer | None]: |
| 830 | + """Partial-value reads built on `self.get`, so each fetch pays latency. |
| 831 | +
|
| 832 | + Issues one `self.get` per `(key, byte_range)` pair instead of the |
| 833 | + `WrapperStore` delegation, which would bypass this wrapper's `get` and |
| 834 | + therefore the synthetic latency. |
| 835 | + """ |
| 836 | + return list( |
| 837 | + await asyncio.gather( |
| 838 | + *( |
| 839 | + self.get(key, prototype=prototype, byte_range=byte_range) |
| 840 | + for key, byte_range in key_ranges |
| 841 | + ) |
| 842 | + ) |
| 843 | + ) |
0 commit comments