Skip to content

[FEA] Add precaching support within file-opening functions #885

Open

Description

@rjzamora

Background

The recently-added open_parquet_file function introduces a new variation on the existing AbstractBufferedFile caching strategies. That is, it compliments the KnownPartsOfAFile ("parts") strategy by performing format-aware "precaching" before the file is open (and before the "parts" cache is initialized).

The new open_parquet_file function is very powerful. However, toggling between the specialized function and fs.open in downstream libraries like Dask is a bit painful. I propose that we add new logic in fsspec's file-opening utilities to trigger the same precaching logic before the AbstractBufferedFile objects are created.

At the moment, I feel that there are two general solutions that may make sense:

Proposed API v1

The first possible solution here is to support the case that the user supplies "precache" and "precache_options" fields within the cache_options dictionary that is allready supported by AbstractBufferedFile:

fs.open(
    path,
    cache_type="parts",
    cache_options={"precache": "parquet", "precache_options": {"columns": ...}},
)

The value passes with the "precache" field ("parquet" in the example above) may be a string id for an "official" method or a callable function. The specification for this callable is that (1) it accepts the kwargs defined in precache_options as inputs, and (2) it outputs the data dictionary required for the "parts" cache.

Notes

  • These new cache_options keys will only be supported for the "parts" cache (for now, at least)
  • The new fields will need to be detected and acted upon in the open-related functions, and ignored in the KnownPartsOfAFile logic itself. That is, the file-opening functions will be expected to copy the cache_options, and populate the "data" field for the kwargs that are passed down to fs.open. Since cache_options is processed when KnownPartsOfAFile is initialized, it may seen tempting to move all precaching logic into the caching class itself. Unfortunately, this approach has the following problems:
    • The KnownPartsOfAFile object will only have access to a single file, and optimal/flexible precaching may require us to collect bytes for many files at once
    • The KnownPartsOfAFile object will not have direct access to the file system and path. The constrained fetcher approach is not ideal for the types of data transfer desired in the precaching stage

Concerns

The only component of this first proposal that feels a bit icky to me is the fact that we are adding fields to "cache_options", but we are not actually acting on the new fields in the location where "cache_options" were originally intended to be processed. For this reason, I am also strongly considering the following alternative...

Proposed API v2

fs.open(
    path,
    cache_type="parts",
    precache="parquet",
    precache_options={"columns": ..., ...},
)

Here, we add support for new precache and precache_options arguments that are completely distinct from cache_options. My attraction to this solution is that we are no longer using cache_options to store arguments that will not be processed in the cache object itself. The downside, however, is that we are adding distinct arguments that are currently only applicable for cache_type="parts".

One possible mitigation for this "downside" is to require the "precache" callable to accept a cache_type= kwarg, and to output a nested dictionary of values to be added to cache_options (with the outer-most keys being file paths). These requirements would allow the precaching logic to check that the desired caching strategy is consistent with the precaching output, and would also allow the precaching logic to determine the necessary cache_options addition(s) needed for each file.

Desired Feedback

  1. **Do either of these proposals sound reasonable? If so which one?
    • v1: Adding "precache" and "precache_options" support within "cache_options"
    • v2: Adding new precache and precache_options arguments to file-opening functions

Feedback is VERY welcome here :)
cc @martindurant

Activity

  1. rjzamora commented on Jan 12, 2022

    @rjzamora
    ContributorAuthor

    Note that I recently revised dask#8339 with API v1 in mind. However, my preference is now leaning closer to API v2. Although precaching is obviously coupled tightly with caching, it seems clear that precaching must be performed before any KnownPartsOfAFile objects are initialized.

    Perhaps an even better alternative (call it API v2b) is to add a precache classmethod to BaseCache:

    class BaseCache(object):
        ...
        def __init__(self, blocksize, fetcher, size):
            ...
            self.precaches = {}
    
        @classmethod
        def precache(cls, paths, fs, method=None, **kwargs):
            if method is None:
                return cache_options
            if isinstance(method, str):
                try:
                    method = self.precaches[method]
                except KeyError:
                    raise ValueError(f"{method} not a supported precache for {cls}")
            return method(paths, fs, **kwargs)

    This way the precache-cache compatibility check is handled within the cache class itself. So, for KnownPartsOfAFile, we would just need to add self.precaches = {"parquet": parquet_precache} to __init__, and define parquet_precache appropriately. This would also allow us to get away with adding only a precache_options argument to the file-open functions, with the precaching method/logic being specified with the "method" key.

    So, if the user calls fs.open using precache_options:

    fs.open(
        path,
        cache_type="parts",
        precache_options={"method": "parquet", "columns": ..., ...},
    )

    The logic within fs.open should be able to pass the precache_options along to the appropriate classmethod to populate dictionary of new cache_option elements (to be used to update a copy of the input cache_options):

    new_cache_options = caches[cache_type].precache([path], self, **precache_options)[path]
  2. martindurant commented on Jan 13, 2022

    @martindurant
    Member

    I do like the idea of associating the precache with the target cache type, so the class method does indeed make sense to me. It means that the code lives next to the code it's preprocessing for, and open is changed minimally. Note that open is overridden by exactly one subclass as far as I know: reference. That should probably have its code moved to _open as with other implementations, so that you shouldn't worry about it (also, reference doesn't make real file objects anyway, it expects to work via cat/get alone).

    One caveat occurred to me: all of this machinery will only work with files that derive from AbstractBufferedFile, which is not all the files fsspec can make. E.g., a local filesystem makes a LocalFileOpener thin wrapper around a real file. In such cases, we should presumably not process the cache options at all, as it will repeat work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions