"""Row-count manifest of a directory of Parquet files.
Reading the footer of every Parquet file of an entity to learn its row-group sizes costs one storage request per file on
every rank. The manifest ``_row_counts.json`` is written once, next to the data files, and holds the same information::
{"version": 2,
"files": {"part-00000-of-00002.parquet": {"row_groups": [100000, 100000, 523], "size_bytes": 81234567},
...}}
Keys are file names relative to the directory. Each value holds:
- ``row_groups``: the row counts of the file's row groups, in order.
- ``size_bytes``: the file's size. Reading the manifest compares it with the size in the directory listing, so a file
rewritten after the manifest was written fails the read unless its size is unchanged. Listings already return sizes,
so the check costs no extra storage requests.
The name does not end in ``.parquet``, so file listings of the data never include it.
The data preprocessor writes one directory per node type and per edge type. The manifest sits next to the data files:
- Node type ``user``::
gs://<temp bucket>/<job name>/data_preprocess/staging/transformed_node_features_dir/user/parquet/_row_counts.json
gs://<temp bucket>/<job name>/data_preprocess/staging/transformed_node_features_dir/user/parquet/part-00000-of-00042.parquet
gs://<temp bucket>/<job name>/data_preprocess/staging/transformed_node_features_dir/user/parquet/part-00001-of-00042.parquet
- Edge type ``user-follows-user``. The directory below the edge type is the edge usage type: ``main`` for the graph
edges, ``positive`` and ``negative`` for label edges::
gs://<temp bucket>/<job name>/data_preprocess/staging/transformed_edge_features_dir/user-follows-user/main/parquet/_row_counts.json
gs://<temp bucket>/<job name>/data_preprocess/staging/transformed_edge_features_dir/user-follows-user/main/parquet/part-00000-of-00042.parquet
"""
import json
import os
from concurrent.futures import ThreadPoolExecutor
from datetime import timedelta
from typing import Any, Final
import pyarrow as pa
import pyarrow.fs as pafs
import pyarrow.parquet as pq
from gigl.common import GcsUri, Uri
from gigl.common.data.parquet_format import PARQUET_FILE_SUFFIX
from gigl.common.logger import Logger
from gigl.common.utils.retry import retry
[docs]
MANIFEST_FILE_NAME: Final[str] = "_row_counts.json"
[docs]
MANIFEST_VERSION: Final[int] = 2
[docs]
GCS_RETRY_TIME_LIMIT: Final[timedelta] = timedelta(seconds=300)
_DEFAULT_THREADS: Final[int] = 32
[docs]
def filesystem_and_path(uri: Uri) -> tuple[pafs.FileSystem, str]:
"""Returns the filesystem for ``uri`` and the path of ``uri`` within it.
GCS URIs get a GCS filesystem with a longer retry limit, and the path is ``<bucket>/<object path>``. Any other URI
is resolved by pyarrow.
Args:
uri (Uri): The location of the directory or file.
Returns:
tuple[pafs.FileSystem, str]: The filesystem and the scheme-less path within it, such as ``my-bucket/my_job/parquet/``.
"""
if isinstance(uri, GcsUri):
fs = pafs.GcsFileSystem(retry_time_limit=GCS_RETRY_TIME_LIMIT)
return fs, f"{uri.bucket}/{uri.path}"
return pafs.FileSystem.from_uri(uri.uri)
[docs]
def list_parquet_files(fs: pafs.FileSystem, path: str) -> list[pafs.FileInfo]:
"""Returns the Parquet data files directly under ``path``, sorted by path.
Args:
fs (pafs.FileSystem): The filesystem that holds ``path``.
path (str): The scheme-less directory path within ``fs``.
Returns:
list[pafs.FileInfo]: The files that end in ``.parquet``, sorted by full path. Each has the ``path`` and
``size`` from the listing. The manifest itself is not included.
"""
selector = pafs.FileSelector(path.rstrip("/"), recursive=False)
infos = fs.get_file_info(selector)
parquet_files: list[pafs.FileInfo] = []
for info in infos:
if info.type == pafs.FileType.File and info.path.endswith(PARQUET_FILE_SUFFIX):
parquet_files.append(info)
return sorted(parquet_files, key=lambda info: info.path)
[docs]
def manifest_path(path: str) -> str:
"""Returns the path of the manifest in the directory ``path``.
Args:
path (str): The scheme-less directory path, with or without a trailing slash.
Returns:
str: ``path`` joined with the manifest file name.
"""
return path.rstrip("/") + "/" + MANIFEST_FILE_NAME
[docs]
def read_manifest_row_groups(
fs: pafs.FileSystem, path: str, files: list[pafs.FileInfo]
) -> list[list[int]]:
"""Returns the row-group row counts of ``files`` (as listed under ``path``) from the manifest under ``path``.
Args:
fs (pafs.FileSystem): The filesystem that holds ``path``.
path (str): The scheme-less directory path within ``fs``.
files (list[pafs.FileInfo]): The data files listed under ``path``, as returned by ``list_parquet_files``.
Returns:
list[list[int]]: One list of row-group row counts per entry of ``files``, in the same order.
Raises:
FileNotFoundError: If there is no manifest.
ValueError: If the manifest is malformed, has an unknown version, does not list exactly ``files``, or lists a
size that differs from a file's listed size.
"""
if fs.get_file_info(manifest_path(path)).type == pafs.FileType.NotFound:
raise FileNotFoundError(manifest_path(path))
with fs.open_input_file(manifest_path(path)) as file:
manifest = json.loads(file.read())
if manifest.get("version") != MANIFEST_VERSION:
raise ValueError(f"unsupported manifest version {manifest.get('version')!r}")
manifest_files = manifest["files"]
names = [os.path.basename(file.path) for file in files]
if set(names) != set(manifest_files):
raise ValueError(
f"manifest lists {len(manifest_files)} files but {len(names)} were found"
)
row_groups_per_file: list[list[int]] = []
for name, file in zip(names, files):
entry = manifest_files[name]
if file.size is None or file.size != entry["size_bytes"]:
raise ValueError(
f"manifest lists {entry['size_bytes']} bytes for {file.path} but the listing has {file.size}"
)
row_groups_per_file.append(entry["row_groups"])
return row_groups_per_file
@retry(
exception_to_check=(OSError, pa.ArrowException),
tries=2,
delay_s=1,
backoff=2,
should_throw_original_exception=True,
)
def _read_row_groups(fs: pafs.FileSystem, file: str) -> list[int]:
"""Reads the row counts of the row groups of a Parquet file from its footer.
Transient read failures are retried. Up to three attempts are made, one second apart and then two.
Args:
fs (pafs.FileSystem): The filesystem that holds ``file``.
file (str): The scheme-less path of the Parquet file within ``fs``.
Returns:
list[int]: The row count of each row group, in file order.
Raises:
OSError, pa.ArrowException: If the footer cannot be read after all attempts.
"""
metadata = pq.read_metadata(file, filesystem=fs)
row_counts: list[int] = []
for row_group_index in range(metadata.num_row_groups):
row_counts.append(metadata.row_group(row_group_index).num_rows)
return row_counts
[docs]
def build_manifest(
uri_prefix: Uri, num_threads: int = _DEFAULT_THREADS
) -> dict[str, Any]:
"""Builds the manifest of the Parquet files under ``uri_prefix`` by reading each footer once.
Args:
uri_prefix (Uri): The directory that holds the Parquet files.
num_threads (int): The number of threads that read footers in parallel.
Returns:
dict[str, Any]: The manifest, with the keys ``version`` and ``files``.
Raises:
ValueError: If the listing does not report a file's size.
"""
fs, path = filesystem_and_path(uri_prefix)
files = list_parquet_files(fs, path)
for file in files:
if file.size is None:
raise ValueError(f"The listing of {file.path} has no size")
with ThreadPoolExecutor(num_threads) as pool:
row_groups = list(pool.map(lambda file: _read_row_groups(fs, file.path), files))
manifest_files: dict[str, dict[str, Any]] = {}
for file, file_row_groups in zip(files, row_groups):
manifest_files[os.path.basename(file.path)] = {
"row_groups": file_row_groups,
"size_bytes": file.size,
}
return {"version": MANIFEST_VERSION, "files": manifest_files}
[docs]
def write_manifest(
uri_prefix: Uri, num_threads: int = _DEFAULT_THREADS
) -> dict[str, Any]:
"""Builds the manifest of the Parquet files under ``uri_prefix`` and writes it there.
Args:
uri_prefix (Uri): The directory that holds the Parquet files.
num_threads (int): The number of threads that read footers in parallel.
Returns:
dict[str, Any]: The manifest that was written.
Raises:
ValueError: If the listing does not report a file's size.
"""
manifest = build_manifest(uri_prefix, num_threads)
fs, path = filesystem_and_path(uri_prefix)
with fs.open_output_stream(manifest_path(path)) as stream:
stream.write(json.dumps(manifest).encode())
logger.info(
f"Wrote row-count manifest of {len(manifest['files'])} files to {manifest_path(uri_prefix.uri)}"
)
return manifest