forked from lance-format/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_namespace_integration.py
More file actions
840 lines (662 loc) · 26.2 KB
/
test_namespace_integration.py
File metadata and controls
840 lines (662 loc) · 26.2 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
"""
Integration tests for Lance Namespace with S3 and credential refresh.
This test simulates a namespace server that returns incrementing credentials
and verifies that the credential refresh mechanism works correctly.
See DEVELOPMENT.md under heading "Integration Tests" for more information.
"""
import copy
import time
import uuid
from threading import Lock
from typing import Dict
import lance
import pyarrow as pa
import pytest
from lance.namespace import (
CreateEmptyTableRequest,
CreateEmptyTableResponse,
DeclareTableRequest,
DeclareTableResponse,
DescribeTableRequest,
DescribeTableResponse,
LanceNamespace,
)
# These are all keys that are accepted by storage_options
CONFIG = {
"allow_http": "true",
"aws_access_key_id": "ACCESS_KEY",
"aws_secret_access_key": "SECRET_KEY",
"aws_endpoint": "http://localhost:4566",
"aws_region": "us-east-1",
}
def get_boto3_client(*args, **kwargs):
import boto3
return boto3.client(
*args,
region_name=CONFIG["aws_region"],
aws_access_key_id=CONFIG["aws_access_key_id"],
aws_secret_access_key=CONFIG["aws_secret_access_key"],
**kwargs,
)
@pytest.fixture(scope="module")
def s3_bucket():
s3 = get_boto3_client("s3", endpoint_url=CONFIG["aws_endpoint"])
bucket_name = "lance-namespace-integtest"
# if bucket exists, delete it
try:
delete_bucket(s3, bucket_name)
except s3.exceptions.NoSuchBucket:
pass
s3.create_bucket(Bucket=bucket_name)
yield bucket_name
delete_bucket(s3, bucket_name)
def delete_bucket(s3, bucket_name):
# Delete all objects first
try:
for obj in s3.list_objects(Bucket=bucket_name).get("Contents", []):
s3.delete_object(Bucket=bucket_name, Key=obj["Key"])
s3.delete_bucket(Bucket=bucket_name)
except Exception:
pass
class TrackingNamespace(LanceNamespace):
"""Mock namespace that wraps DirectoryNamespace and tracks API calls."""
def __init__(
self,
bucket_name: str,
storage_options: Dict[str, str],
credential_expires_in_seconds: int = 60,
):
from lance.namespace import DirectoryNamespace
self.bucket_name = bucket_name
self.base_storage_options = storage_options
self.credential_expires_in_seconds = credential_expires_in_seconds
self.describe_call_count = 0
self.create_call_count = 0
self.lock = Lock()
# Create underlying DirectoryNamespace with storage options
dir_props = {f"storage.{k}": v for k, v in storage_options.items()}
if bucket_name.startswith("/") or bucket_name.startswith("file://"):
dir_props["root"] = f"{bucket_name}/namespace_root"
else:
dir_props["root"] = f"s3://{bucket_name}/namespace_root"
self.inner = DirectoryNamespace(**dir_props)
def get_describe_call_count(self) -> int:
with self.lock:
return self.describe_call_count
def get_create_call_count(self) -> int:
with self.lock:
return self.create_call_count
def namespace_id(self) -> str:
return f"TrackingNamespace {{ inner: {self.inner.namespace_id()} }}"
def _modify_storage_options(
self, storage_options: Dict[str, str], count: int
) -> Dict[str, str]:
"""Add incrementing credentials with expiration timestamp."""
modified = copy.deepcopy(storage_options) if storage_options else {}
modified["aws_access_key_id"] = f"AKID_{count}"
modified["aws_secret_access_key"] = f"SECRET_{count}"
modified["aws_session_token"] = f"TOKEN_{count}"
expires_at_millis = int(
(time.time() + self.credential_expires_in_seconds) * 1000
)
modified["expires_at_millis"] = str(expires_at_millis)
# Set refresh offset to 1 second (1000ms) for short-lived credential tests
modified["refresh_offset_millis"] = "1000"
return modified
def create_empty_table(
self, request: CreateEmptyTableRequest
) -> CreateEmptyTableResponse:
with self.lock:
self.create_call_count += 1
count = self.create_call_count
response = self.inner.create_empty_table(request)
response.storage_options = self._modify_storage_options(
response.storage_options, count
)
return response
def declare_table(self, request: DeclareTableRequest) -> DeclareTableResponse:
with self.lock:
self.create_call_count += 1
count = self.create_call_count
response = self.inner.declare_table(request)
response.storage_options = self._modify_storage_options(
response.storage_options, count
)
return response
def describe_table(self, request: DescribeTableRequest) -> DescribeTableResponse:
with self.lock:
self.describe_call_count += 1
count = self.describe_call_count
response = self.inner.describe_table(request)
response.storage_options = self._modify_storage_options(
response.storage_options, count
)
return response
@pytest.mark.integration
def test_namespace_open_dataset(s3_bucket: str):
"""Test creating and opening datasets through namespace with credential tracking."""
storage_options = copy.deepcopy(CONFIG)
namespace = TrackingNamespace(
bucket_name=s3_bucket,
storage_options=storage_options,
credential_expires_in_seconds=3600,
)
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 10, "b": 20}])
table_name = uuid.uuid4().hex
table_id = ["test_ns", table_name]
assert namespace.get_create_call_count() == 0
assert namespace.get_describe_call_count() == 0
ds = lance.write_dataset(
table1, namespace=namespace, table_id=table_id, mode="create"
)
assert len(ds.versions()) == 1
assert ds.count_rows() == 2
assert namespace.get_create_call_count() == 1
ds_from_namespace = lance.dataset(
namespace=namespace,
table_id=table_id,
)
assert namespace.get_describe_call_count() == 1
assert ds_from_namespace.count_rows() == 2
result = ds_from_namespace.to_table()
assert result == table1
# Test credential caching
call_count_before_reads = namespace.get_describe_call_count()
for _ in range(3):
assert ds_from_namespace.count_rows() == 2
assert namespace.get_describe_call_count() == call_count_before_reads
@pytest.mark.integration
def test_namespace_with_refresh(s3_bucket: str):
"""Test credential refresh when credentials expire."""
storage_options = copy.deepcopy(CONFIG)
namespace = TrackingNamespace(
bucket_name=s3_bucket,
storage_options=storage_options,
credential_expires_in_seconds=3,
)
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 10, "b": 20}])
table_name = uuid.uuid4().hex
table_id = ["test_ns", table_name]
assert namespace.get_create_call_count() == 0
assert namespace.get_describe_call_count() == 0
ds = lance.write_dataset(
table1,
namespace=namespace,
table_id=table_id,
mode="create",
)
assert ds.count_rows() == 2
assert namespace.get_create_call_count() == 1
ds_from_namespace = lance.dataset(
namespace=namespace,
table_id=table_id,
)
initial_call_count = namespace.get_describe_call_count()
assert initial_call_count == 1
assert ds_from_namespace.count_rows() == 2
result = ds_from_namespace.to_table()
assert result == table1
call_count_after_initial_reads = namespace.get_describe_call_count()
time.sleep(5)
assert ds_from_namespace.count_rows() == 2
result2 = ds_from_namespace.to_table()
assert result2 == table1
final_call_count = namespace.get_describe_call_count()
assert final_call_count == call_count_after_initial_reads + 1
@pytest.mark.integration
def test_namespace_append_through_namespace(s3_bucket: str):
"""Test appending to dataset through namespace."""
storage_options = copy.deepcopy(CONFIG)
namespace = TrackingNamespace(
bucket_name=s3_bucket,
storage_options=storage_options,
credential_expires_in_seconds=3600,
)
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}])
table_name = uuid.uuid4().hex
table_id = ["test_ns", table_name]
assert namespace.get_create_call_count() == 0
assert namespace.get_describe_call_count() == 0
ds = lance.write_dataset(
table1, namespace=namespace, table_id=table_id, mode="create"
)
assert ds.count_rows() == 1
assert len(ds.versions()) == 1
assert namespace.get_create_call_count() == 1
initial_describe_count = namespace.get_describe_call_count()
table2 = pa.Table.from_pylist([{"a": 10, "b": 20}])
ds = lance.write_dataset(
table2, namespace=namespace, table_id=table_id, mode="append"
)
assert ds.count_rows() == 2
assert len(ds.versions()) == 2
assert namespace.get_create_call_count() == 1
assert namespace.get_describe_call_count() == initial_describe_count + 1
ds_from_namespace = lance.dataset(
namespace=namespace,
table_id=table_id,
)
assert ds_from_namespace.count_rows() == 2
assert len(ds_from_namespace.versions()) == 2
assert namespace.get_describe_call_count() == initial_describe_count + 2
@pytest.mark.integration
def test_namespace_write_create_mode(s3_bucket: str):
"""Test writing dataset through namespace in CREATE mode."""
storage_options = copy.deepcopy(CONFIG)
namespace = TrackingNamespace(
bucket_name=s3_bucket,
storage_options=storage_options,
credential_expires_in_seconds=3600,
)
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 10, "b": 20}])
table_name = uuid.uuid4().hex
assert namespace.get_create_call_count() == 0
assert namespace.get_describe_call_count() == 0
ds = lance.write_dataset(
table1,
namespace=namespace,
table_id=["test_ns", table_name],
mode="create",
)
assert namespace.get_create_call_count() == 1
assert ds.count_rows() == 2
assert len(ds.versions()) == 1
result = ds.to_table()
assert result == table1
@pytest.mark.integration
def test_namespace_write_append_mode(s3_bucket: str):
"""Test writing dataset through namespace in APPEND mode."""
storage_options = copy.deepcopy(CONFIG)
namespace = TrackingNamespace(
bucket_name=s3_bucket,
storage_options=storage_options,
credential_expires_in_seconds=3600,
)
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}])
table_name = uuid.uuid4().hex
table_id = ["test_ns", table_name]
assert namespace.get_create_call_count() == 0
assert namespace.get_describe_call_count() == 0
ds = lance.write_dataset(
table1, namespace=namespace, table_id=table_id, mode="create"
)
assert ds.count_rows() == 1
assert namespace.get_create_call_count() == 1
assert namespace.get_describe_call_count() == 0
table2 = pa.Table.from_pylist([{"a": 10, "b": 20}])
ds = lance.write_dataset(
table2,
namespace=namespace,
table_id=table_id,
mode="append",
)
assert namespace.get_create_call_count() == 1
describe_count_after_append = namespace.get_describe_call_count()
assert describe_count_after_append == 1
assert ds.count_rows() == 2
assert len(ds.versions()) == 2
call_count_before_reads = namespace.get_describe_call_count()
for _ in range(3):
assert ds.count_rows() == 2
assert namespace.get_describe_call_count() == call_count_before_reads
@pytest.mark.integration
def test_namespace_write_overwrite_mode(s3_bucket: str):
"""Test writing dataset through namespace in OVERWRITE mode."""
storage_options = copy.deepcopy(CONFIG)
namespace = TrackingNamespace(
bucket_name=s3_bucket,
storage_options=storage_options,
credential_expires_in_seconds=3600,
)
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}])
table_name = uuid.uuid4().hex
table_id = ["test_ns", table_name]
assert namespace.get_create_call_count() == 0
assert namespace.get_describe_call_count() == 0
ds = lance.write_dataset(
table1, namespace=namespace, table_id=table_id, mode="create"
)
assert ds.count_rows() == 1
assert namespace.get_create_call_count() == 1
assert namespace.get_describe_call_count() == 0
table2 = pa.Table.from_pylist([{"a": 10, "b": 20}, {"a": 100, "b": 200}])
ds = lance.write_dataset(
table2,
namespace=namespace,
table_id=table_id,
mode="overwrite",
)
assert namespace.get_create_call_count() == 1
describe_count_after_overwrite = namespace.get_describe_call_count()
assert describe_count_after_overwrite == 1
assert ds.count_rows() == 2
assert len(ds.versions()) == 2
result = ds.to_table()
assert result == table2
call_count_before_reads = namespace.get_describe_call_count()
for _ in range(3):
assert ds.count_rows() == 2
assert namespace.get_describe_call_count() == call_count_before_reads
@pytest.mark.integration
def test_namespace_distributed_write(s3_bucket: str):
"""Test distributed write pattern through namespace."""
storage_options = copy.deepcopy(CONFIG)
namespace = TrackingNamespace(
bucket_name=s3_bucket,
storage_options=storage_options,
credential_expires_in_seconds=3600,
)
table_name = uuid.uuid4().hex
table_id = ["test_ns", table_name]
request = DeclareTableRequest(id=table_id, location=None)
response = namespace.declare_table(request)
assert namespace.get_create_call_count() == 1
assert namespace.get_describe_call_count() == 0
table_uri = response.location
assert table_uri is not None
from lance.namespace import LanceNamespaceStorageOptionsProvider
namespace_storage_options = response.storage_options
assert namespace_storage_options is not None
storage_options_provider = LanceNamespaceStorageOptionsProvider(
namespace=namespace, table_id=table_id
)
merged_options = dict(storage_options)
merged_options.update(namespace_storage_options)
from lance.fragment import write_fragments
fragment1_data = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 3, "b": 4}])
fragment1 = write_fragments(
fragment1_data,
table_uri,
storage_options=merged_options,
storage_options_provider=storage_options_provider,
)
fragment2_data = pa.Table.from_pylist([{"a": 10, "b": 20}, {"a": 30, "b": 40}])
fragment2 = write_fragments(
fragment2_data,
table_uri,
storage_options=merged_options,
storage_options_provider=storage_options_provider,
)
fragment3_data = pa.Table.from_pylist([{"a": 100, "b": 200}])
fragment3 = write_fragments(
fragment3_data,
table_uri,
storage_options=merged_options,
storage_options_provider=storage_options_provider,
)
all_fragments = fragment1 + fragment2 + fragment3
operation = lance.LanceOperation.Overwrite(fragment1_data.schema, all_fragments)
ds = lance.LanceDataset.commit(
table_uri,
operation,
storage_options=merged_options,
storage_options_provider=storage_options_provider,
)
assert ds.count_rows() == 5
assert len(ds.versions()) == 1
result = ds.to_table().sort_by("a")
expected = pa.Table.from_pylist(
[
{"a": 1, "b": 2},
{"a": 3, "b": 4},
{"a": 10, "b": 20},
{"a": 30, "b": 40},
{"a": 100, "b": 200},
]
)
assert result == expected
ds_from_namespace = lance.dataset(
namespace=namespace,
table_id=table_id,
)
assert ds_from_namespace.count_rows() == 5
@pytest.mark.integration
def test_file_writer_with_storage_options_provider(s3_bucket: str):
"""Test LanceFileWriter with storage_options_provider and credential refresh."""
from lance import LanceNamespaceStorageOptionsProvider
from lance.file import LanceFileReader, LanceFileWriter
storage_options = copy.deepcopy(CONFIG)
namespace = TrackingNamespace(
bucket_name=s3_bucket,
storage_options=storage_options,
credential_expires_in_seconds=3,
)
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 10, "b": 20}])
table_name = uuid.uuid4().hex
table_id = ["test_ns", table_name]
assert namespace.get_create_call_count() == 0
assert namespace.get_describe_call_count() == 0
ds = lance.write_dataset(
table1, namespace=namespace, table_id=table_id, mode="create"
)
assert ds.count_rows() == 2
assert namespace.get_create_call_count() == 1
describe_response = namespace.describe_table(
DescribeTableRequest(id=table_id, version=None)
)
namespace_storage_options = describe_response.storage_options
provider = LanceNamespaceStorageOptionsProvider(
namespace=namespace, table_id=table_id
)
initial_describe_count = namespace.get_describe_call_count()
file_uri = f"s3://{s3_bucket}/{table_name}_file_test.lance"
schema = pa.schema([pa.field("x", pa.int64()), pa.field("y", pa.int64())])
writer = LanceFileWriter(
file_uri,
schema=schema,
storage_options=namespace_storage_options,
storage_options_provider=provider,
)
batch = pa.RecordBatch.from_pydict({"x": [1, 2, 3], "y": [4, 5, 6]}, schema=schema)
writer.write_batch(batch)
batch2 = pa.RecordBatch.from_pydict(
{"x": [7, 8, 9], "y": [10, 11, 12]}, schema=schema
)
writer.write_batch(batch2)
writer.close()
describe_count_after_write = namespace.get_describe_call_count()
assert describe_count_after_write == initial_describe_count
reader = LanceFileReader(
file_uri,
storage_options=namespace_storage_options,
storage_options_provider=provider,
)
result = reader.read_all(batch_size=1024)
result_table = result.to_table()
assert result_table.num_rows == 6
assert result_table.schema == schema
expected_table = pa.table(
{"x": [1, 2, 3, 7, 8, 9], "y": [4, 5, 6, 10, 11, 12]}, schema=schema
)
assert result_table == expected_table
time.sleep(5)
file_uri2 = f"s3://{s3_bucket}/{table_name}_file_test2.lance"
writer2 = LanceFileWriter(
file_uri2,
schema=schema,
storage_options=namespace_storage_options,
storage_options_provider=provider,
)
batch3 = pa.RecordBatch.from_pydict(
{"x": [100, 200], "y": [300, 400]}, schema=schema
)
writer2.write_batch(batch3)
writer2.close()
final_describe_count = namespace.get_describe_call_count()
assert final_describe_count == describe_count_after_write + 1
reader2 = LanceFileReader(
file_uri2,
storage_options=namespace_storage_options,
storage_options_provider=provider,
)
result2 = reader2.read_all(batch_size=1024)
result_table2 = result2.to_table()
assert result_table2.num_rows == 2
expected_table2 = pa.table({"x": [100, 200], "y": [300, 400]}, schema=schema)
assert result_table2 == expected_table2
@pytest.mark.integration
def test_file_reader_with_storage_options_provider(s3_bucket: str):
"""Test LanceFileReader with storage_options_provider and credential refresh."""
from lance import LanceNamespaceStorageOptionsProvider
from lance.file import LanceFileReader, LanceFileWriter
storage_options = copy.deepcopy(CONFIG)
namespace = TrackingNamespace(
bucket_name=s3_bucket,
storage_options=storage_options,
credential_expires_in_seconds=3,
)
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 10, "b": 20}])
table_name = uuid.uuid4().hex
table_id = ["test_ns", table_name]
ds = lance.write_dataset(
table1, namespace=namespace, table_id=table_id, mode="create"
)
assert ds.count_rows() == 2
describe_response = namespace.describe_table(
DescribeTableRequest(id=table_id, version=None)
)
namespace_storage_options = describe_response.storage_options
provider = LanceNamespaceStorageOptionsProvider(
namespace=namespace, table_id=table_id
)
file_uri = f"s3://{s3_bucket}/{table_name}_file_reader_test.lance"
schema = pa.schema([pa.field("x", pa.int64()), pa.field("y", pa.int64())])
# Write a file first (without provider to keep it simple)
writer = LanceFileWriter(
file_uri,
schema=schema,
storage_options=namespace_storage_options,
)
batch = pa.RecordBatch.from_pydict({"x": [1, 2, 3], "y": [4, 5, 6]}, schema=schema)
writer.write_batch(batch)
writer.close()
# Get fresh credentials for reading
describe_response = namespace.describe_table(
DescribeTableRequest(id=table_id, version=None)
)
namespace_storage_options = describe_response.storage_options
initial_describe_count = namespace.get_describe_call_count()
# First read should work without needing refresh
reader = LanceFileReader(
file_uri,
storage_options=namespace_storage_options,
storage_options_provider=provider,
)
result = reader.read_all(batch_size=1024)
result_table = result.to_table()
assert result_table.num_rows == 3
assert result_table.schema == schema
describe_count_after_first_read = namespace.get_describe_call_count()
assert describe_count_after_first_read == initial_describe_count
# Wait for credentials to expire
time.sleep(5)
# Write a second file
file_uri2 = f"s3://{s3_bucket}/{table_name}_file_reader_test2.lance"
writer2 = LanceFileWriter(
file_uri2,
schema=schema,
storage_options=namespace_storage_options,
)
batch2 = pa.RecordBatch.from_pydict(
{"x": [100, 200], "y": [300, 400]}, schema=schema
)
writer2.write_batch(batch2)
writer2.close()
# Second read should trigger credential refresh
reader2 = LanceFileReader(
file_uri2,
storage_options=namespace_storage_options,
storage_options_provider=provider,
)
result2 = reader2.read_all(batch_size=1024)
result_table2 = result2.to_table()
assert result_table2.num_rows == 2
expected_table2 = pa.table({"x": [100, 200], "y": [300, 400]}, schema=schema)
assert result_table2 == expected_table2
final_describe_count = namespace.get_describe_call_count()
assert final_describe_count == describe_count_after_first_read + 1
@pytest.mark.integration
def test_file_session_with_storage_options_provider(s3_bucket: str):
"""Test LanceFileSession with storage_options_provider and credential refresh."""
from lance import LanceNamespaceStorageOptionsProvider
from lance.file import LanceFileSession
storage_options = copy.deepcopy(CONFIG)
namespace = TrackingNamespace(
bucket_name=s3_bucket,
storage_options=storage_options,
credential_expires_in_seconds=3,
)
table1 = pa.Table.from_pylist([{"a": 1, "b": 2}, {"a": 10, "b": 20}])
table_name = uuid.uuid4().hex
table_id = ["test_ns", table_name]
ds = lance.write_dataset(
table1, namespace=namespace, table_id=table_id, mode="create"
)
assert ds.count_rows() == 2
describe_response = namespace.describe_table(
DescribeTableRequest(id=table_id, version=None)
)
namespace_storage_options = describe_response.storage_options
provider = LanceNamespaceStorageOptionsProvider(
namespace=namespace, table_id=table_id
)
initial_describe_count = namespace.get_describe_call_count()
# Create session with storage_options_provider
session = LanceFileSession(
f"s3://{s3_bucket}/{table_name}_session",
storage_options=namespace_storage_options,
storage_options_provider=provider,
)
# Test contains method
assert not session.contains("session_test.lance")
# Test list method
files = session.list()
assert isinstance(files, list)
schema = pa.schema([pa.field("x", pa.int64()), pa.field("y", pa.int64())])
# Write using session - should not trigger credential refresh
writer = session.open_writer(
"session_test.lance",
schema=schema,
)
batch = pa.RecordBatch.from_pydict({"x": [1, 2, 3], "y": [4, 5, 6]}, schema=schema)
writer.write_batch(batch)
writer.close()
describe_count_after_first_write = namespace.get_describe_call_count()
assert describe_count_after_first_write == initial_describe_count
# Test contains method after write
assert session.contains("session_test.lance")
# Read using session - should not trigger credential refresh
reader = session.open_reader("session_test.lance")
result = reader.read_all(batch_size=1024)
result_table = result.to_table()
assert result_table.num_rows == 3
assert result_table.schema == schema
expected_table = pa.table({"x": [1, 2, 3], "y": [4, 5, 6]}, schema=schema)
assert result_table == expected_table
describe_count_after_first_read = namespace.get_describe_call_count()
assert describe_count_after_first_read == describe_count_after_first_write
# Wait for credentials to expire
time.sleep(5)
# Write again, should trigger credential refresh
writer2 = session.open_writer(
"session_test2.lance",
schema=schema,
)
batch2 = pa.RecordBatch.from_pydict(
{"x": [100, 200], "y": [300, 400]}, schema=schema
)
writer2.write_batch(batch2)
writer2.close()
describe_count_after_second_write = namespace.get_describe_call_count()
assert describe_count_after_second_write == describe_count_after_first_read + 1
# Read the second file - should not trigger another refresh since we just refreshed
reader2 = session.open_reader("session_test2.lance")
result2 = reader2.read_all(batch_size=1024)
result_table2 = result2.to_table()
assert result_table2.num_rows == 2
expected_table2 = pa.table({"x": [100, 200], "y": [300, 400]}, schema=schema)
assert result_table2 == expected_table2
final_describe_count = namespace.get_describe_call_count()
assert final_describe_count == describe_count_after_second_write