-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtest_virtual_ref.py
More file actions
344 lines (316 loc) · 10.3 KB
/
test_virtual_ref.py
File metadata and controls
344 lines (316 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import uuid
from datetime import UTC, datetime, timedelta
from pathlib import Path
import numpy as np
import pytest
import zarr
import zarr.core
import zarr.core.buffer
from icechunk import (
IcechunkError,
ObjectStoreConfig,
RepositoryConfig,
S3Options,
VirtualChunkContainer,
VirtualChunkSpec,
containers_credentials,
http_store,
in_memory_storage,
local_filesystem_storage,
s3_credentials,
s3_store,
)
from icechunk.repository import Repository
from tests.conftest import write_chunks_to_minio
@pytest.mark.filterwarnings("ignore:datetime.datetime.utcnow")
async def test_write_minio_virtual_refs() -> None:
prefix = str(uuid.uuid4())
etags = write_chunks_to_minio(
[
(f"{prefix}/chunk-1", b"first"),
(f"{prefix}/chunk-2", b"second"),
],
)
config = RepositoryConfig.default()
store_config = s3_store(
region="us-east-1",
endpoint_url="http://localhost:9000",
allow_http=True,
s3_compatible=True,
force_path_style=True,
)
container = VirtualChunkContainer("s3", "s3://", store_config)
config.set_virtual_chunk_container(container)
credentials = containers_credentials(
s3=s3_credentials(access_key_id="minio123", secret_access_key="minio123")
)
# Open the store
repo = Repository.open_or_create(
storage=in_memory_storage(),
config=config,
virtual_chunk_credentials=credentials,
)
session = repo.writable_session("main")
store = session.store
array = zarr.create_array(
store, shape=(5, 1, 3), chunks=(1, 1, 1), dtype="i4", compressors=None
)
# We add the virtual chunk refs without checksum, with the right etag, and with the wrong wrong etag and datetime.
# This way we can check retrieval operations that should fail
old = datetime.now(UTC) - timedelta(weeks=1)
new = datetime.now(UTC) + timedelta(minutes=1)
res = store.set_virtual_refs(
array_path="/",
validate_containers=True,
chunks=[
VirtualChunkSpec(
index=[0, 0, 0],
location=f"s3://testbucket/{prefix}/chunk-1",
offset=0,
length=4,
),
VirtualChunkSpec(
index=[1, 0, 0],
location=f"s3://testbucket/{prefix}/chunk-1",
offset=0,
length=4,
etag_checksum=etags[0],
),
VirtualChunkSpec(
index=[2, 0, 0],
location=f"s3://testbucket/{prefix}/chunk-1",
offset=0,
length=4,
etag_checksum="bad etag",
),
VirtualChunkSpec(
index=[3, 0, 0],
location=f"s3://testbucket/{prefix}/chunk-1",
offset=0,
length=4,
last_updated_at_checksum=old,
),
VirtualChunkSpec(
index=[4, 0, 0],
location=f"s3://testbucket/{prefix}/chunk-1",
offset=0,
length=4,
last_updated_at_checksum=new,
),
VirtualChunkSpec(
index=[0, 0, 1],
location=f"s3://testbucket/{prefix}/chunk-2",
offset=1,
length=4,
),
VirtualChunkSpec(
index=[1, 0, 1],
location=f"s3://testbucket/{prefix}/chunk-2",
offset=1,
length=4,
etag_checksum=etags[1],
),
VirtualChunkSpec(
index=[2, 0, 1],
location=f"s3://testbucket/{prefix}/chunk-2",
offset=1,
length=4,
etag_checksum="bad etag",
),
VirtualChunkSpec(
index=[3, 0, 1],
location=f"s3://testbucket/{prefix}/chunk-2",
offset=1,
length=4,
last_updated_at_checksum=old,
),
VirtualChunkSpec(
index=[4, 0, 1],
location=f"s3://testbucket/{prefix}/chunk-2",
offset=1,
length=4,
last_updated_at_checksum=new,
),
# we write a ref that simulates a lost chunk
VirtualChunkSpec(
index=[0, 0, 2],
location=f"s3://testbucket/{prefix}/non-existing",
offset=1,
length=4,
),
# we write one that doesn't pass container validation
VirtualChunkSpec(
index=[0, 0, 2],
location=f"bad://testbucket/{prefix}/non-existing",
offset=1,
length=4,
),
],
)
# we got the failed ref index
assert res == [(0, 0, 2)]
# can validate virtual chunk containers
with pytest.raises(IcechunkError, match="invalid chunk location"):
store.set_virtual_ref(
"c/0/0/2",
f"bad://testbucket/{prefix}/non-existing",
offset=1,
length=4,
validate_container=True,
)
buffer_prototype = zarr.core.buffer.default_buffer_prototype()
first = await store.get("c/0/0/0", prototype=buffer_prototype)
assert first is not None
assert first.to_bytes() == b"firs" # codespell:ignore firs
assert await store.get("c/1/0/0", prototype=buffer_prototype) == first
assert await store.get("c/4/0/0", prototype=buffer_prototype) == first
second = await store.get("c/0/0/1", prototype=buffer_prototype)
assert second is not None
assert second.to_bytes() == b"econ"
assert await store.get("c/1/0/1", prototype=buffer_prototype) == second
assert await store.get("c/4/0/1", prototype=buffer_prototype) == second
assert array[0, 0, 0] == 1936877926
assert array[0, 0, 1] == 1852793701
# fetch uninitialized chunk should be None
assert await store.get("c/0/0/3", prototype=buffer_prototype) is None
# fetching a virtual ref that disappeared should be an exception
with pytest.raises(IcechunkError):
# TODO: we should include the key and other info in the exception
await store.get("c/0/0/2", prototype=buffer_prototype)
all_locations = set(session.all_virtual_chunk_locations())
assert f"s3://testbucket/{prefix}/non-existing" in all_locations
assert f"s3://testbucket/{prefix}/chunk-1" in all_locations
assert f"s3://testbucket/{prefix}/chunk-2" in all_locations
# check we cannot get refs with bad etag o that were modified
with pytest.raises(IcechunkError, match="chunk has changed"):
await store.get("c/2/0/0", prototype=buffer_prototype)
with pytest.raises(IcechunkError, match="chunk has changed"):
await store.get("c/3/0/0", prototype=buffer_prototype)
with pytest.raises(IcechunkError, match="chunk has changed"):
await store.get("c/2/0/1", prototype=buffer_prototype)
with pytest.raises(IcechunkError, match="chunk has changed"):
await store.get("c/3/0/1", prototype=buffer_prototype)
_snapshot_id = session.commit("Add virtual refs")
@pytest.mark.parametrize(
"container_type,url_prefix,store_config",
[
(
"s3",
"s3://earthmover-sample-data",
ObjectStoreConfig.S3(S3Options(region="us-east-1", anonymous=True)),
),
(
"http",
"https://earthmover-sample-data.s3.amazonaws.com",
http_store(),
),
],
)
async def test_public_virtual_refs(
tmpdir: Path,
container_type: str,
url_prefix: str,
store_config: ObjectStoreConfig.S3 | ObjectStoreConfig.Http,
) -> None:
config = RepositoryConfig.default()
container = VirtualChunkContainer("sample-data", url_prefix, store_config)
config.set_virtual_chunk_container(container)
repo = Repository.open_or_create(
storage=local_filesystem_storage(f"{tmpdir}/virtual-{container_type}"),
config=config,
)
session = repo.writable_session("main")
store = session.store
root = zarr.Group.from_store(store=store, zarr_format=3)
year = root.require_array(
name="year", shape=((72,)), chunks=((72,)), dtype="float32", compressors=None
)
file_path = f"{url_prefix}/netcdf/oscar_vel2018.nc"
store.set_virtual_ref(
"year/c/0",
file_path,
offset=22306,
length=288,
)
nodes = [n async for n in store.list()]
assert "year/c/0" in nodes
year_values = year[:]
assert len(year_values) == 72
actual_values = np.array(
[
2018.0,
2018.0139,
2018.0278,
2018.0416,
2018.0555,
2018.0695,
2018.0834,
2018.0972,
2018.1111,
2018.125,
2018.1389,
2018.1528,
2018.1666,
2018.1805,
2018.1945,
2018.2084,
2018.2222,
2018.2361,
2018.25,
2018.2639,
2018.2778,
2018.2916,
2018.3055,
2018.3195,
2018.3334,
2018.3472,
2018.3611,
2018.375,
2018.3889,
2018.4028,
2018.4166,
2018.4305,
2018.4445,
2018.4584,
2018.4722,
2018.4861,
2018.5,
2018.5139,
2018.5278,
2018.5416,
2018.5555,
2018.5695,
2018.5834,
2018.5972,
2018.6111,
2018.625,
2018.6389,
2018.6528,
2018.6666,
2018.6805,
2018.6945,
2018.7084,
2018.7222,
2018.7361,
2018.75,
2018.7639,
2018.7778,
2018.7916,
2018.8055,
2018.8195,
2018.8334,
2018.8472,
2018.8611,
2018.875,
2018.8889,
2018.9028,
2018.9166,
2018.9305,
2018.9445,
2018.9584,
2018.9722,
2018.9861,
],
dtype="float32",
)
assert np.allclose(year_values, actual_values)