Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pyiceberg/catalog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ def delete_files(io: FileIO, files_to_delete: set[str], file_type: str) -> None:
for file in files_to_delete:
try:
io.delete(file)
except FileNotFoundError:
# The file is already gone, for example because a REST catalog server removed it
# during the commit. The desired end state is reached, so don't warn about it.
logger.debug(f"{file_type.capitalize()} file {file} was already deleted")
except OSError:
logger.warning(f"Failed to delete {file_type} file {file}", exc_info=logger.isEnabledFor(logging.DEBUG))

Expand Down
53 changes: 52 additions & 1 deletion tests/catalog/test_catalog_behaviors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""

import json
import logging
import os
from collections.abc import Callable, Generator
from pathlib import Path
Expand All @@ -31,7 +32,7 @@
from pytest_lazy_fixtures import lf
from sqlalchemy.exc import IntegrityError

from pyiceberg.catalog import Catalog
from pyiceberg.catalog import Catalog, delete_files
from pyiceberg.exceptions import (
CommitFailedException,
NamespaceAlreadyExistsError,
Expand All @@ -40,6 +41,7 @@
NoSuchTableError,
TableAlreadyExistsError,
)
from pyiceberg.io import FileIO, InputFile, OutputFile
from pyiceberg.io.pyarrow import _dataframe_to_data_files, schema_to_pyarrow
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionField, PartitionSpec
from pyiceberg.schema import Schema
Expand Down Expand Up @@ -741,6 +743,55 @@ def test_delete_metadata_multiple(catalog: Catalog, table_schema_nested: Schema,
assert os.path.exists(updated_metadata_2.metadata_file[len("file://") :])


def test_delete_metadata_already_removed_by_server_does_not_warn(
catalog: Catalog, table_schema_nested: Schema, random_table_identifier: Identifier, caplog: pytest.LogCaptureFixture
) -> None:
"""A REST catalog server may remove old metadata files during the commit, before the client tries."""
namespace = Catalog.namespace_from(random_table_identifier)
catalog.create_namespace(namespace)
table = catalog.create_table(
random_table_identifier,
table_schema_nested,
properties={
TableProperties.METADATA_PREVIOUS_VERSIONS_MAX: "2",
TableProperties.METADATA_DELETE_AFTER_COMMIT_ENABLED: "true",
},
)

removed_by_server = 0
with caplog.at_level(logging.WARNING, logger="pyiceberg.catalog"):
for i in range(6):
# With previous-versions-max=2, a commit on a full log drops the oldest entry.
# Simulate the server deleting that file as part of the commit.
if len(table.metadata.metadata_log) >= 2:
oldest = table.metadata.metadata_log[0].metadata_file.replace("file://", "")
os.remove(oldest)
removed_by_server += 1

with table.update_schema() as update:
update.add_column(path=f"new_column_{i}", field_type=IntegerType())

assert removed_by_server > 0
assert "Failed to delete metadata file" not in caplog.text


def test_delete_files_still_warns_on_other_os_errors(caplog: pytest.LogCaptureFixture) -> None:
class PermissionDeniedIO(FileIO):
def new_input(self, location: str) -> InputFile:
raise NotImplementedError

def new_output(self, location: str) -> OutputFile:
raise NotImplementedError

def delete(self, location: str | InputFile | OutputFile) -> None:
raise PermissionError(f"denied: {location}")

with caplog.at_level(logging.WARNING, logger="pyiceberg.catalog"):
delete_files(PermissionDeniedIO(), {"file:///warehouse/00000.metadata.json"}, "metadata")

assert "Failed to delete metadata file file:///warehouse/00000.metadata.json" in caplog.text


# Table properties tests


Expand Down
Loading