Skip to content
Merged
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
34 changes: 30 additions & 4 deletions pyiceberg/catalog/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
import getpass
import socket
import time
from types import TracebackType
from typing import (
Expand All @@ -34,10 +35,17 @@
AlreadyExistsException,
FieldSchema,
InvalidOperationException,
LockComponent,
LockLevel,
LockRequest,
LockResponse,
LockState,
LockType,
MetaException,
NoSuchObjectException,
SerDeInfo,
StorageDescriptor,
UnlockRequest,
)
from hive_metastore.ttypes import Database as HiveDatabase
from hive_metastore.ttypes import Table as HiveTable
Expand All @@ -56,6 +64,7 @@
PropertiesUpdateSummary,
)
from pyiceberg.exceptions import (
CommitFailedException,
NamespaceAlreadyExistsError,
NamespaceNotEmptyError,
NoSuchIcebergTableError,
Expand Down Expand Up @@ -331,6 +340,15 @@ def register_table(self, identifier: Union[str, Identifier], metadata_location:
"""
raise NotImplementedError

def _create_lock_request(self, database_name: str, table_name: str) -> LockRequest:
lock_component: LockComponent = LockComponent(
level=LockLevel.TABLE, type=LockType.EXCLUSIVE, dbname=database_name, tablename=table_name, isTransactional=True
)

lock_request: LockRequest = LockRequest(component=[lock_component], user=getpass.getuser(), hostname=socket.gethostname())

return lock_request

def _commit_table(self, table_request: CommitTableRequest) -> CommitTableResponse:
"""Update the table.

Expand Down Expand Up @@ -363,15 +381,23 @@ def _commit_table(self, table_request: CommitTableRequest) -> CommitTableRespons
self._write_metadata(updated_metadata, current_table.io, new_metadata_location)

# commit to hive
try:
with self._client as open_client:
# https://github.com/apache/hive/blob/master/standalone-metastore/metastore-common/src/main/thrift/hive_metastore.thrift#L1232
with self._client as open_client:
lock: LockResponse = open_client.lock(self._create_lock_request(database_name, table_name))

try:
if lock.state != LockState.ACQUIRED:
raise CommitFailedException(f"Failed to acquire lock for {table_request.identifier}, state: {lock.state}")

tbl = open_client.get_table(dbname=database_name, tbl_name=table_name)
tbl.parameters = _construct_parameters(
metadata_location=new_metadata_location, previous_metadata_location=current_table.metadata_location
)
open_client.alter_table(dbname=database_name, tbl_name=table_name, new_tbl=tbl)
except NoSuchObjectException as e:
raise NoSuchTableError(f"Table does not exist: {table_name}") from e
except NoSuchObjectException as e:
raise NoSuchTableError(f"Table does not exist: {table_name}") from e
finally:
open_client.unlock(UnlockRequest(lockid=lock.lockid))
Comment on lines +397 to +400
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't consider this a blocker, but it'd be nice to have a test to verify that in case of any exception thrown we do actually perform the unlock.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really hard one because it requires that we've already acquired the lock, but then something happened (timeout?) and the the lock state changed. I'd have to think though how we could replicate that scenario.


return CommitTableResponse(metadata=updated_metadata, metadata_location=new_metadata_location)

Expand Down
26 changes: 25 additions & 1 deletion tests/integration/test_reads.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

import pyarrow.parquet as pq
import pytest
from hive_metastore.ttypes import LockRequest, LockResponse, LockState, UnlockRequest
from pyarrow.fs import S3FileSystem

from pyiceberg.catalog import Catalog, load_catalog
from pyiceberg.exceptions import NoSuchTableError
from pyiceberg.catalog.hive import HiveCatalog, _HiveClient
from pyiceberg.exceptions import CommitFailedException, NoSuchTableError
from pyiceberg.expressions import (
And,
EqualTo,
Expand Down Expand Up @@ -467,3 +469,25 @@ def test_null_list_and_map(catalog: Catalog) -> None:
# assert arrow_table["col_list_with_struct"].to_pylist() == [None, [{'test': 1}]]
# Once https://github.com/apache/arrow/issues/38809 has been fixed
assert arrow_table["col_list_with_struct"].to_pylist() == [[], [{'test': 1}]]


@pytest.mark.integration
def test_hive_locking(catalog_hive: HiveCatalog) -> None:
table = create_table(catalog_hive)

database_name: str
table_name: str
_, database_name, table_name = table.identifier

hive_client: _HiveClient = _HiveClient(catalog_hive.properties["uri"])
blocking_lock_request: LockRequest = catalog_hive._create_lock_request(database_name, table_name)

with hive_client as open_client:
# Force a lock on the test table
lock: LockResponse = open_client.lock(blocking_lock_request)
assert lock.state == LockState.ACQUIRED
try:
with pytest.raises(CommitFailedException, match="(Failed to acquire lock for).*"):
table.transaction().set_properties(lock="fail").commit_transaction()
finally:
open_client.unlock(UnlockRequest(lock.lockid))