Repository navigation
Add basic File table for (external) files stored separate from database - #2
deruyter92 wants to merge 17 commits into
Conversation
(fixed incompletely in 62762a3)
There was a problem hiding this comment.
Nice work overall, but we might need to be careful around certain design choices, especially the current coupling of recording vs copying, see #2 (comment)
Also let's make sure to use DJ's built-in object storage for derived/recomputable data. I think the current design makes sense for raw data acquired only once, shared, and reused across labs; for the rest I think it might be simpler to lean on their existing code
| class Location(dj.Manual): | ||
| """A copy of a file in a store, at ``object_key(file_sha256)``; it exists while the copy does. | ||
|
|
||
| Every ``File`` has at least one ``Location``. Each store has one writer (the lab's |
There was a problem hiding this comment.
Is that enforced? Should it be?
There was a problem hiding this comment.
We need to think about this. May be difficult to enforce this per site since File is SHARED.
| return {"store_name": name} | ||
|
|
||
|
|
||
| def _copy_into(path: Path, root: Path) -> tuple[str, int]: |
There was a problem hiding this comment.
Should there be a cleanup for temp files?
There was a problem hiding this comment.
Onn a higher level I'm not very convinced by this copy design; an ingestion is always a hand-written object store and I think the amount of issues just in this part of the code point us towards a different design.
- "Put bytes in a store, verify, record" is a single operation; having two code paths (
register_file/record_stored) means two sets of failure modes. _copy_intohashes the source and never checks back what is actually in the store
I think first of all recording and copying should be separate. Over network you may need retries or face bandwith limits as well.
Options seem to be:
- rclone is super nice here I think, it handles some of the network annoyances, atomic writes, partial writes, and checks. Downside is it will just redo hashing (on its own) due to backend-dependent hashing, but that's a time loss mainly for a safety trade-off that may be acceptable.
- fsspec is python native but brings again the atomicity and checks burden
- Make every store S3 (run MiniO or actually Ceph on lab servers) - heavy and inconvenient imo
For semantics/prior art maybe check how git-annex did things. Not an expert, happy to help discuss the design together overall as I may have missed other options. But this is where things could get complicated if we redo everything by hand in Python.
|
(This is a rather long-term review with a lot of design requests, @deruyter92 the core design is really fine imo aside from the more minor comments I made so let me know if I should approve and move this to an issue preferably, or better indicate what can be solved easily now) |
…ck before recording - use datajoint.storage.StorageBackend - check the object exists in the store with the given size - refuse unmounted disk stores and store_kind / dj.config protocol mismatches - drop SCENE_STORE, ACQUISITION write role - File: raw data only; derived data uses DataJoint's own object storage
|
@C-Achard Thanks for your elaborate initial review. I will triage what we can put in a first working version while keeping the remaining issues open. I think it is important to make better use of what is available from DataJoint as well. And let's test it properly when we have access to the Simons s3 infrastructure. I am keeping the PR as draft for now while working on a better implementation. |
2.3.3 makes file-protocol stores safe on Windows (POSIX separators; the garbage collector could delete live files before). 2.3.6 is a security release, including path checks for every store path.
keep SHA-256 and add `content: <filepath@scene-files>`, which is DJ's canonical codec for this use. Each deployment can have it's own definition in dj.config["stores"]["scene-files"] to decide which copy it reads. `Store`, `Location`, their row-meta tables and `ensure_store` are removed. - register_file: hash, copy with DataJoint's storage backend unless present, record - record_file: record bytes a transfer job (e.g. rclone) already put in place - an `s3` extra (s3fs) and a DJ_STORES example in .env.example
DataJoint checks only that a <filepath@> file exists (and its size on fetch). verify_file re-reads the bytes, in chunks, and compares SHA-256 and size with the File row: an audit to run now and then.
Stricter and separate init_file_store; avoids wrongly initializing a new file store for File ingestion by accident (e.g. when address contains a typo). register_file and record_file now refuse a store without scene-files.json at its root. Only admin.init_file_store() writes it, once per copy of the store. It refuses a copy that is already prepared, so it cannot become a check at the start of a script, and a place that holds none of a few files the database records (unmounted, wrong location) unless new_copy=True. Also shortens the docstrings of the files modules.
Whoever registered the first file under a prefix created sha256/ab/ with their own umask (often 0755), so other users could not add files to it. init_file_store now creates sha256/00 to sha256/ff up front, on POSIX group-writable with the setgid bit, so they take the root folder's group.
Deleting a wrong-size copy first is only needed on disk, where put_file cannot overwrite on Windows (datajoint-python#1744). On S3 a PUT overwrites, so writers can do without delete permission. Document a lifecycle rule for incomplete multipart uploads.
store_file puts the bytes in the store; register_stored_file writes the File row. (can be used downstream by lab-tables for specific modalities) A strict xfail on Windows tracks datajoint-python#1744; once it passes, the remove() before put_file can go.
|
@C-Achard thanks again for the review. I reworked the PR. Let me know what you think. New design Still open:
|
File, Store, Location File table for (external) files stored separate from database
C-Achard
left a comment
There was a problem hiding this comment.
Looks good!
The only points I would ask about are:
- The File/FileContent split
- Should we figure out how to setup test S3 in CI, also to get hands-on experience (and it would likely be useful later)
Otherwise I'd rather keep this moving and test it live over further fine-tuning,so I'm approving now
| @mark_sync_authority(SyncAuthority.SHARED) | ||
| @schema | ||
| class File(dj.Manual): | ||
| """One file from outside the pipeline (recording, video, config), identified by SHA-256. | ||
|
|
||
| Immutable and the same everywhere, so not stamped as RowMeta. What the file is belongs to | ||
| the table referencing it; computed data uses DataJoint's own storage types instead. | ||
| ``content`` fetches as an ``ObjectRef`` (``read()``, ``open()``, ``download()``). | ||
| """ | ||
|
|
||
| definition = f""" | ||
| file_sha256: char(64) # lowercase hex SHA-256 of the bytes | ||
| --- | ||
| size_bytes: int64 | ||
| content: <filepath@{FILE_STORE}> # the bytes in the store, at object_key(file_sha256) | ||
| """ |
There was a problem hiding this comment.
Here I have a small request for local vs stored granularity, could we do a shape more like:
@schema
class File(dj.Manual): # no store needed
definition = """
file_sha256: char(64)
---
size_bytes: int64
"""
@schema
class FileContent(dj.Manual): # added later, optionally
definition = f"""
-> File
---
content: <filepath@{FILE_STORE}>
"""Note that this is more of a feature request, but it matters for e.g. human data and local deployment I think.
There was a problem hiding this comment.
On the granularity topic:
- For live files: a
<filepath@>store over a lab's live folder references files in place, but DataJoint records path, size and timestamp only, and reads whatever is there. Might need a separateFileLocation(-> File, store, path, seen_at), re-hashed before use, over aFileContentrow. - For local backup first, S3 later: A synced
FileContentrow would point to bytes the central deployment cannot read before syncing, so "bytes check then sync rows" would need enforcing in sync.
There was a problem hiding this comment.
Note this also unlocks size/SHA checks without needing the store, which is also quite useful imo
There was a problem hiding this comment.
I completely agree with the separation, good suggestion. I was thinking making a separate syncing category NEVER / NOSYNC or similar - meaning, the FileContent reference always stays on the machine, and never syncs instead of syncing the FileContent, we can re-register the reference by create the FileContent entry on the central DB separately. This means that the central database can never lie about having FileContent that is not actually in the store.
There was a problem hiding this comment.
Regarding CI - I have some notebooks for testing it locally. I will have a look for configuring something in CI. Thanks!
…thority.NOSYNC A file can now be registered by hash while its bytes stay elsewhere (register_file(path, store=False), register_file_hash). FileContent (-> File, <filepath@scene-files>) holds THIS database's store copy of the bytes. A new SyncAuthority.NOSYNC marks the FileContent to be never synchronized.
Adds a shared
Filetable for files from outside the pipeline (recordings, videos, configs). The bytes live in aDataJoint store outside the database; the database keeps their SHA-256 and a
FileContententry references to the store.Design
File: identity only (file_sha256,size_bytes), can be registered by hash only.FileContentwithcontent: <filepath@scene-files>: the bytes in this deployment's store. Never synced (newSyncAuthority.NOSYNC).scene-files. Each machine maps it indj.config["stores"](a lab disk, an S3 bucket).Files sit at the content-addressed path
sha256/ab/<sha256>in every copy, so copies replicate withrclone copy --checksumand a row reads from whichever copy the machine is configured for.<npy@>,<object@>,<blob@>)in its own tables.
Store,Locationand their row-meta tables (as was the case in an earlier proposed version).Helpers (
scene_pipelines.ingestion.files)register_file: store and register a local file (the two steps below);store=Falseregisters the hash only.register_file_hash: write theFilerow only (bytes kept elsewhere).register_stored_file: writeFileandFileContentfor bytes already in the storestore_file: copy into the store with DataJoint'sput_fileunless present; no database write.verify_file: re-read the stored bytes and check the SHA-256admin.init_file_store: prepare a store copy once, by hand. It writesscene-files.jsonas top-level marker in the store. Without it the other helpers refuse to store files.Also
datajoint>=2.3.6; news3extra (s3fs).StorageBackend.put_filecannot overwrite on Windows; concurrent writes share one temp file datajoint/datajoint-python#1744:put_filecannot overwrite on Windows; we delete a wrong-size copy first.A strict xfail (Windows only) tells us when the workaround can go.
StorageBackend.put_filedatajoint/datajoint-python#1745: no checksum while uploading; until then, store only finished files.