Something went wrong. Try again.
Manage a distributed indexed dataset of files and metadata, using a Dolt database to track who has copies of what.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220#!/usr/bin/env python# -*- coding: utf-8 -*-
import pathlibfrom typing import Generatorimport pytestimport random
import pytest_asyncio
from dolt_annex.datatypes.async_utils import as_acmfrom dolt_annex.datatypes.common import TableRowfrom dolt_annex.config.base import Configfrom dolt_annex.datatypes.errors import ContentAddressableStorageErrorfrom dolt_annex.file_keys.base import FileKeyfrom dolt_annex.file_keys import Sha256Efrom dolt_annex.filestore.annexfs import AnnexFSModelfrom dolt_annex.filestore.archivefs import ArchiveFS, ArchiveFSModelfrom dolt_annex.filestore.base import FileStoreModelfrom dolt_annex.filestore.cas import ContentAddressableStoragefrom dolt_annex.filestore.filestore_test import SftpWrappedFilestoreModel, SimpleSftpFilestoreModelfrom dolt_annex.filestore.leveldb import LevelDBModel, plyvel_importedfrom dolt_annex.filestore.memory import MemoryFSModelfrom dolt_annex.filestore.sqlite import SQLiteModelfrom dolt_annex.datatypes.file_stream import bytes_file_sourcefrom dolt_annex.replicated_db.dolt import DatabaseConnectionfrom dolt_annex.sync import move_datasetfrom dolt_annex.util.test_util import EnvironmentForTest, test_dataset_schema
# Try every combination of two and from types.def all_filestore_types(prefix: pathlib.Path) -> Generator[FileStoreModel]: if plyvel_imported: yield LevelDBModel(root=prefix / "leveldb") yield SQLiteModel(root=prefix / "sqlite") yield AnnexFSModel(root=prefix / "annexfs") yield ArchiveFSModel(num_workers=4, root=prefix / "archivefs" / "archives", secondary=SQLiteModel(root=prefix / "archivefs" / "secondary")) yield SftpWrappedFilestoreModel( remote_file_store_model=ArchiveFSModel( num_workers=4, root=prefix / "archivefs" / "archives", secondary=SQLiteModel(root=prefix / "archivefs" / "secondary") ) ) yield SimpleSftpFilestoreModel()
def all_filestore_type_parameters(prefix: pathlib.Path): for fs in all_filestore_types(prefix): yield pytest.param(fs, id=fs.type_name())
@pytest_asyncio.fixtureasync def added_file_keys(local_filestore: ContentAddressableStorage) -> list[FileKey]: # Create random files to move in parallel NUM_FILES = 5 file_keys: list[FileKey] = [] for i in range(NUM_FILES): file_bytes = random.randbytes(1024*i) file_key, _ = await local_filestore.put_file(bytes_file_source(file_bytes, Sha256E)) file_keys.append(file_key) await local_filestore.file_store.flush() return file_keys
file_key = Sha256E.from_bytes(b"existing data")
@pytest.mark.asyncio@pytest.mark.parametrize("local_filestore_model", [pytest.param(MemoryFSModel(files={bytes(file_key): b"corrupted data"}), id=pytest.HIDDEN_PARAM)])@pytest.mark.parametrize("remote_filestore_model", [pytest.param(MemoryFSModel(), id="")])async def test_detect_corruption( setup: EnvironmentForTest,): from_repo = setup.local_repo to_repo = setup.remote_repo FILTERS = [] async with ( DatabaseConnection.open(setup.config) as conn, as_acm(conn.open_dataset(test_dataset_schema)) as dataset, dataset.with_repo(from_repo.uuid) as from_dataset_replica, dataset.with_repo(to_repo.uuid), ): # Add entries to from_repo database from_table = from_dataset_replica.get_table("test_table") await from_table.insert( TableRow({"path": "0", "file_key": file_key}), )
await from_table.flush() with pytest.RaisesGroup(ContentAddressableStorageError, flatten_subgroups=True, allow_unwrapped=True): await move_dataset( dataset, from_repo, to_repo, FILTERS, )
@pytest.mark.asyncio@pytest.mark.parametrize("local_filestore_model", list(all_filestore_type_parameters(pathlib.Path("from"))))@pytest.mark.parametrize("remote_filestore_model", list(all_filestore_type_parameters(pathlib.Path("to"))))async def test_async_move( test_config: Config, setup: EnvironmentForTest, added_file_keys: list[Sha256E],): from_repo = setup.local_repo to_repo = setup.remote_repo FILTERS = [] async with ( DatabaseConnection.open(test_config) as conn, as_acm(conn.open_dataset(test_dataset_schema)) as dataset, dataset.with_repo(from_repo.uuid) as from_dataset_replica, dataset.with_repo(to_repo.uuid), ): # Add entries to from_repo database from_table = from_dataset_replica.get_table("test_table") for i, file_key in enumerate(added_file_keys): path = f"{i}" await from_table.insert(TableRow({"path": path,"file_key": file_key}))
await from_table.flush() await move_dataset( dataset, from_repo, to_repo, FILTERS, ) await to_repo.file_store.flush() # Check that files have been moved for file_key in added_file_keys: await to_repo.verify_file(file_key) # Check that db entries have been updated to_table = from_dataset_replica.get_table("test_table") for row in to_table.get_rows(): path = row["path"] file_key = row["file_key"] assert bytes(added_file_keys[int(path)]) == bytes(file_key, encoding='utf-8') # If the destination filestore is ArchiveFS, ensure that files are in multiple archives if isinstance(to_repo.file_store, ArchiveFS): archive_ids = set() for file_key in added_file_keys: file_location = await to_repo.file_store.secondary.get_file_bytes(file_key) print(file_location) name, offset, size = file_location.split(b":") archive_ids.add(name) # assert len(archive_ids) > 1
@pytest.mark.asyncio@pytest.mark.parametrize("local_filestore_model", [pytest.param(MemoryFSModel(), id=pytest.HIDDEN_PARAM)])@pytest.mark.parametrize("remote_filestore_model", [pytest.param(MemoryFSModel(), id="")])async def test_diff_types( test_config: Config, setup: EnvironmentForTest,): from_repo = setup.local_repo to_repo = setup.remote_repo FILTERS = [] async with ( DatabaseConnection.open(test_config) as conn, as_acm(conn.open_dataset(test_dataset_schema)) as dataset, dataset.with_repo(from_repo.uuid) as from_dataset_replica, dataset.with_repo(to_repo.uuid) as to_dataset_replica, ): file_bytes = random.randbytes(1024**2) # 1 MB file_key, _ = await setup.local_repo.put_file(bytes_file_source(file_bytes, Sha256E)) table_row = TableRow({"path": "test_path","file_key": file_key}) # Add entries to from_repo database from_table = from_dataset_replica.get_table("test_table") await from_table.insert(table_row)
await from_table.flush() await move_dataset( dataset, from_repo, to_repo, FILTERS, ) # Check that files have been moved await to_repo.verify_file(file_key) # Check that db entries have been updated to_table = to_dataset_replica.get_table("test_table") rows = list(to_table.get_rows()) assert len(rows) == 1 assert rows[0]["path"] == "test_path" assert rows[0]["file_key"] == str(file_key) # Modify the table row await from_table.remove(table_row) new_file_bytes = random.randbytes(1024**2) # 1 MB new_file_key, _ = await setup.local_repo.put_file(bytes_file_source(new_file_bytes, Sha256E)) new_table_row = TableRow({"path": "test_path", "file_key": new_file_key}) await from_table.insert(new_table_row)
await from_table.flush() await move_dataset( dataset, from_repo, to_repo, FILTERS, )
# Check that files have been moved await to_repo.verify_file(new_file_key) # Check that db entries have been updated rows = list(to_table.get_rows()) assert len(rows) == 1 assert rows[0]["path"] == "test_path" assert rows[0]["file_key"] == str(new_file_key)
# Delete the table row await from_table.remove(new_table_row)
await from_table.flush() await move_dataset( dataset, from_repo, to_repo, FILTERS, )
rows = list(to_table.get_rows()) assert len(rows) == 0