Externally caching file metadata #2066
Replies: 2 comments 3 replies
Implementation ThoughtsBelow are some thoughts I had about what a potential implementation could look like. In this scenario the validation checksum and all other metadata would exist in a single Cloud LayoutFor ASDF files in cloud storage the metadata and schema would be stored alongside the ASDF files themselves. The metadata sidecar for The schemas for the files in a given directory (or bucket, or other granularity level) would be stored in an Local LayoutFor local files the layout is largely the same, except that the schemas are cached in a single central directory for all files. We also add a central cache for remote file metadata. The sidecar file names are the base64-encoded remote file URIs. One potential problem with this approach is that local files are much more likely to be moved or renamed than remote files, which may result in them losing their sidecar files. Metadata File FormatIn my opinion the two most relevant formats for the metadata sidecar files are JSON and Flatbuffers. JSONJSON has the advantages of being self-describing and being supported by basically every programming language. JSON is used by Zarr and fsspec's Kerchunk. FlatbuffersFlatbuffers is a binary format like Protobuf but with zero-copy deserialization. Flatbuffers is used by Icechunk and is also the container format for Apache Arrow IPC. |
|
regarding |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
An issue we're running into with ASDF files is that they are missing certain metadata that could significantly speed up reading/processing. In general this metadata is either available from an external source (e.g. schema definitions) or can be reconstructed from the ASDF file itself (e.g. block layouts).
Rather than adding to or changing the format of ASDF files themselves, we could instead store additional metadata external to the ASDF files to improve reader performance.
Supplemental Information
Below is a non-exhaustive list of additional information that would speed up reading/validating ASDF files if available.
Cached Validation
Currently the reader needs to re-validate an ASDF file every time it is read. We could avoid some (ideally most) validations by storing some record of previous validation that can be checked by the reader.
One way this could work is:
Schema Definitions
The reader needs access to schema definitions in order to validate the structure of an ASDF file. Currently (for the Python library) this requires installing a Python package that provides a resource mapping for the schema. If we had a way to store schemas alongside the ASDF files then files could be validated without needing additional packages.
It's also worth noting that whereas all of the other metadata described in this section is per-file, schemas could potentially be shared across multiple files to avoid duplication.
Block Index
ASDF files already contain a block index, but they aren't required and it isn't guaranteed to be correct. Additionally there's not a direct way to locate the start of a block index: readers either have to search backward from the end of the file or forward from the last block.
If we stored a block index externally it would be easier to locate and could also be updated if needed without rewriting the original file. Primarily, though, the benefit is that it would facilitate the other layers of additional metadata described below.
Tag Index / Block Mapping
ASDF's YAML-based tree structure works well for some queries but there are other queries that could be sped up by storing information about the tree structure in a different format.
One example would be block lookups. If I already know I want to read an array at a specific path in the tree (e.g.
tree["foo"]["bar"]) the reader has to read the entire tree in order to parse it, then find the associated block and read that as well.If we had a mapping of blocks to their hierarchical paths in the tree it could be possible to speed up queries in situations where the user only wants a single array from the file.
Another example: in the future we're hoping to define standard structures for astronomical data in ASDF (similar to FITS). For certain tools (e.g. visualizers) it could be useful to have a flat index of which structures appear in a given file rather than potentially needing to walk the entire tree to locate all of them.
Block Chunk Layout
ASDF currently doesn't have good support for reading a slice of an array stored in a block. It's possible to compute the byte range of a slice for uncompressed blocks but not for compressed blocks. For most of the compression formats you're required to read and decompress the entire block because all of the data is stored in a single chunk. For LZ4 compression the data is stored in a sequence of 4MB chunks so it is possible to only read part of a block, but there is currently no way to map an uncompressed byte range to a set of chunks.
If we cached the locations of each chunk along with their corresponding uncompressed byte offsets then readers could efficiently read slices from LZ4 compressed blocks.
Array Metadata
ASDF files store the information needed to read and decompress a block in the block header. However, the primary use case for blocks is to store ndarrays, in which case array metadata like shape and dtype are stored in the YAML tree.
If we're already storing supplemental block/chunk layout information as described above it would be trivial to also include ndarray metadata such as dtype and shape. We wouldn't even need a special case for ndarrays since blocks that aren't ndarrays can still be represented as a flat uint8 ndarray.
All together this added information would allow efficient reading of array slices with no additional metadata needed from the ASDF file itself.
All reactions