skrawcz commented on code in PR #931: URL: https://github.com/apache/burr/pull/931#discussion_r4054073768
########## burr/core/artifacts.py: ########## @@ -0,0 +1,242 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Blob/object storage abstraction for large values held in :py:class:`burr.core.state.State`. + +Rather than embedding large binary payloads (files, dataframes, images, etc...) directly in +state -- which gets fully re-serialized to the persistence layer after every action -- store the +payload in an :py:class:`ArtifactStore` and keep a small, content-addressed :py:class:`ArtifactRef` +in state instead. Deserializing a ref does *not* eagerly fetch the underlying bytes -- callers +must explicitly call :py:meth:`ArtifactRef.read` (or ``store.get_artifact``) to do so. + +See :doc:`../reference/serde` and :doc:`../concepts/state-persistence` for background on how +state serialization interacts with this module. +""" + +import abc +import hashlib +import os +import tempfile +from dataclasses import dataclass +from typing import Optional + +from burr.core import serde + + +@dataclass(frozen=True) +class ArtifactRef: + """A lightweight, content-addressed reference to a blob stored in an :py:class:`ArtifactStore`. + + This is what you should store in :py:class:`burr.core.state.State` -- *not* the raw bytes/object. + It is registered with :py:mod:`burr.core.serde` so it serializes/deserializes automatically as + part of normal state (de)serialization; deserializing only recreates this reference, it never + re-fetches the underlying bytes on its own. + + :param key: The store-specific key/path the blob is stored under. + :param size_bytes: Size of the blob, in bytes. + :param digest: SHA-256 hex digest of the blob's contents, used to verify integrity on read + and to detect duplicate content. + :param media_type: Optional MIME type (e.g. ``"application/pdf"``) for the caller's own use. + """ + + key: str + size_bytes: int + digest: str + media_type: Optional[str] = None + + def read(self, store: "ArtifactStore", verify: bool = True) -> bytes: + """Lazily fetches the underlying bytes for this reference from ``store``. + + :param store: The store this artifact was originally written to. + :param verify: Whether to verify the fetched bytes match ``self.digest``. Defaults to True. + :return: The raw bytes of the artifact. + """ + return store.get_artifact(self, verify=verify) + + +class ArtifactStore(abc.ABC): + """Base class for blob/object storage backends. + Review Comment: **Blocking: we need to handle the async Python case.** Async Python is a first-class execution model in Burr, but this API is synchronous and the S3 implementation performs blocking network I/O through boto3. Using this store from an async action would therefore block the event loop for the duration of an artifact upload or download. Please add an async artifact-store contract and a genuinely async S3 implementation, and define how that store is configured and exposed to async applications. The alternative would be to restrict artifact storage to synchronous Burr explicitly, which would leave a significant part of Burr unsupported. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
