Source code for multistorageclient.generators.manifest_metadata

  1# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
  2# SPDX-License-Identifier: Apache-2.0
  3#
  4# Licensed under the Apache License, Version 2.0 (the "License");
  5# you may not use this file except in compliance with the License.
  6# You may obtain a copy of the License at
  7#
  8# http://www.apache.org/licenses/LICENSE-2.0
  9#
 10# Unless required by applicable law or agreed to in writing, software
 11# distributed under the License is distributed on an "AS IS" BASIS,
 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13# See the License for the specific language governing permissions and
 14# limitations under the License.
 15
 16import json
 17from concurrent.futures import ThreadPoolExecutor
 18from typing import Optional
 19
 20from multistorageclient.types import ObjectMetadata
 21from multistorageclient.utils import calculate_worker_processes_and_threads
 22
 23from .. import StorageClient
 24from ..providers.manifest_metadata import DEFAULT_MANIFEST_BASE_DIR, ManifestMetadataProvider
 25
 26
[docs] 27class ManifestMetadataGenerator: 28 """ 29 Generates a file metadata manifest for use with a :py:class:`multistorageclient.providers.ManifestMetadataProvider`. 30 """ 31 32 @staticmethod 33 def _generate_manifest_part_body(object_metadata: list[ObjectMetadata]) -> bytes: 34 return "\n".join( 35 [ 36 json.dumps({**metadata_dict, "size_bytes": metadata_dict.pop("content_length")}) 37 for metadata in object_metadata 38 for metadata_dict in [metadata.to_dict()] 39 ] 40 ).encode(encoding="utf-8") 41
[docs] 42 @staticmethod 43 def generate_and_write_manifest( 44 data_storage_client: StorageClient, 45 manifest_storage_client: StorageClient, 46 partition_keys: Optional[list[str]] = None, 47 ) -> None: 48 """ 49 Generates a file metadata manifest. 50 51 The data storage client's base path should be set to the root path for data objects (e.g. ``my-bucket/my-data-prefix``). 52 53 The manifest storage client's base path should be set to the root path for manifest objects (e.g. ``my-bucket/my-manifest-prefix``). 54 55 The following manifest objects will be written with the destination storage client (with the total number of manifest parts being variable):: 56 57 .msc_manifests/ 58 ├── msc_manifest_index.json 59 └── parts/ 60 ├── msc_manifest_part000001.jsonl 61 ├── ... 62 └── msc_manifest_part999999.jsonl 63 64 :param data_storage_client: Storage client for reading data objects. 65 :param manifest_storage_client: Storage client for writing manifest objects. 66 :param partition_keys: Optional list of keys to partition the listing operation. If provided, objects will be listed concurrently using these keys as boundaries. 67 """ 68 # Get respective StorageProviders. A StorageClient will always have a StorageProvider 69 # TODO: Cleanup by exposing APIs from the client 70 data_storage_provider = data_storage_client._storage_provider 71 manifest_storage_provider = manifest_storage_client._storage_provider 72 73 # Create a ManifestMetadataProvider for writing manifest, configure manifest storage provider 74 # TODO(NGCDP-3018): Opportunity to split up the responsibilities of MetadataProvider 75 manifest_metadata_provider = ManifestMetadataProvider( 76 storage_provider=manifest_storage_provider, manifest_path="", writable=True 77 ) 78 79 if partition_keys is not None: 80 _, num_worker_threads = calculate_worker_processes_and_threads() 81 82 boundaries = list(zip([""] + partition_keys, partition_keys + [None])) 83 84 def process_partition(boundary): 85 start_after, end_at = boundary 86 for object_metadata in data_storage_provider.list_objects( 87 prefix="", start_after=start_after, end_at=end_at 88 ): 89 if DEFAULT_MANIFEST_BASE_DIR not in object_metadata.key.split("/"): # Do not track manifest files 90 manifest_metadata_provider.add_file(path=object_metadata.key, metadata=object_metadata) 91 92 with ThreadPoolExecutor(max_workers=num_worker_threads) as executor: 93 futures = [executor.submit(process_partition, boundary) for boundary in boundaries] 94 for future in futures: 95 future.result() 96 else: 97 for object_metadata in data_storage_provider.list_objects(prefix=""): 98 if DEFAULT_MANIFEST_BASE_DIR not in object_metadata.key.split("/"): # Do not track manifest files 99 manifest_metadata_provider.add_file(path=object_metadata.key, metadata=object_metadata) 100 101 manifest_metadata_provider.commit_updates()