-
Notifications
You must be signed in to change notification settings - Fork 376
feat(catalog): Implement update_table for Hive metastore catalog #1894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vladson
wants to merge
1
commit into
apache:main
Choose a base branch
from
vladson:hms_update_table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,7 +50,14 @@ pub const THRIFT_TRANSPORT_BUFFERED: &str = "buffered"; | |
| /// HMS Catalog warehouse location | ||
| pub const HMS_CATALOG_PROP_WAREHOUSE: &str = "warehouse"; | ||
|
|
||
| /// Builder for [`RestCatalog`]. | ||
| ///HMS Hive Locks Disabled | ||
| pub const HMS_HIVE_LOCKS_DISABLED: &str = "hive_locks_disabled"; | ||
|
|
||
| /// HMS Environment Context | ||
| const HMS_EXPECTED_PARAMETER_KEY: &str = "expected_parameter_key"; | ||
| const HMS_EXPECTED_PARAMETER_VALUE: &str = "expected_parameter_value"; | ||
|
|
||
| /// Builder for [`HmsCatalog`]. | ||
| #[derive(Debug)] | ||
| pub struct HmsCatalogBuilder(HmsCatalogConfig); | ||
|
|
||
|
|
@@ -167,6 +174,43 @@ impl Debug for HmsCatalog { | |
| } | ||
| } | ||
|
|
||
| /// RAII guard for HMS table locks. Automatically releases the lock when dropped. | ||
| struct HmsLockGuard { | ||
| client: ThriftHiveMetastoreClient, | ||
| lockid: i64, | ||
| } | ||
|
|
||
| impl HmsLockGuard { | ||
| async fn acquire( | ||
| client: &ThriftHiveMetastoreClient, | ||
| db_name: &str, | ||
| tbl_name: &str, | ||
| ) -> Result<Self> { | ||
| let lock = client | ||
| .lock(create_lock_request(db_name, tbl_name)) | ||
| .await | ||
| .map(from_thrift_exception) | ||
| .map_err(from_thrift_error)??; | ||
|
|
||
| Ok(Self { | ||
| client: client.clone(), | ||
| lockid: lock.lockid, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for HmsLockGuard { | ||
| fn drop(&mut self) { | ||
| let client = self.client.clone(); | ||
| let lockid = self.lockid; | ||
| tokio::spawn(async move { | ||
| let _ = client | ||
| .unlock(hive_metastore::UnlockRequest { lockid }) | ||
| .await; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| impl HmsCatalog { | ||
| /// Create a new hms catalog. | ||
| fn new(config: HmsCatalogConfig) -> Result<Self> { | ||
|
|
@@ -208,6 +252,64 @@ impl HmsCatalog { | |
| pub fn file_io(&self) -> FileIO { | ||
| self.file_io.clone() | ||
| } | ||
|
|
||
| /// Applies a commit to a table and prepares the update for HMS. | ||
| /// # Returns | ||
| /// A tuple of (staged_table, new_hive_table) ready for HMS alter_table operation | ||
| async fn apply_and_prepare_update( | ||
| &self, | ||
| commit: TableCommit, | ||
| db_name: &str, | ||
| tbl_name: &str, | ||
| hive_table: &hive_metastore::Table, | ||
| ) -> Result<(Table, hive_metastore::Table)> { | ||
| let metadata_location = get_metadata_location(&hive_table.parameters)?; | ||
|
|
||
| let metadata = TableMetadata::read_from(&self.file_io, &metadata_location).await?; | ||
|
|
||
| let cur_table = Table::builder() | ||
| .file_io(self.file_io()) | ||
| .metadata_location(metadata_location) | ||
| .metadata(metadata) | ||
| .identifier(TableIdent::new( | ||
| NamespaceIdent::new(db_name.to_string()), | ||
| tbl_name.to_string(), | ||
| )) | ||
| .build()?; | ||
|
|
||
| let staged_table = commit.apply(cur_table)?; | ||
| staged_table | ||
| .metadata() | ||
| .write_to( | ||
| staged_table.file_io(), | ||
| staged_table.metadata_location_result()?, | ||
| ) | ||
| .await?; | ||
|
|
||
| let new_hive_table = update_hive_table_from_table(hive_table, &staged_table)?; | ||
|
|
||
| Ok((staged_table, new_hive_table)) | ||
| } | ||
|
|
||
| /// Builds an EnvironmentContext for optimistic locking with HMS. | ||
| /// | ||
| /// The context includes the expected metadata_location, which HMS will use | ||
| /// to validate that the table hasn't been modified concurrently. | ||
| fn build_environment_context(metadata_location: &str) -> hive_metastore::EnvironmentContext { | ||
| let mut env_context_properties = pilota::AHashMap::new(); | ||
| env_context_properties.insert( | ||
| HMS_EXPECTED_PARAMETER_KEY.into(), | ||
| "metadata_location".into(), | ||
| ); | ||
| env_context_properties.insert( | ||
| HMS_EXPECTED_PARAMETER_VALUE.into(), | ||
| pilota::FastStr::from_string(metadata_location.to_string()), | ||
| ); | ||
|
|
||
| hive_metastore::EnvironmentContext { | ||
| properties: Some(env_context_properties), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
|
|
@@ -603,10 +705,94 @@ impl Catalog for HmsCatalog { | |
| )) | ||
| } | ||
|
|
||
| async fn update_table(&self, _commit: TableCommit) -> Result<Table> { | ||
| Err(Error::new( | ||
| ErrorKind::FeatureUnsupported, | ||
| "Updating a table is not supported yet", | ||
| )) | ||
| /// Updates an existing table by applying a commit operation. | ||
| /// | ||
| /// This method supports two update strategies depending on the catalog configuration: | ||
| /// | ||
| /// **Optimistic Locking** (when `hive_locks_disabled` is set): | ||
| /// - Retrieves the current table state from HMS without acquiring locks | ||
| /// - Constructs an `EnvironmentContext` with the expected metadata location | ||
| /// - Uses `alter_table_with_environment_context` to perform an atomic | ||
| /// compare-and-swap operation. | ||
| /// - HMS will reject the update if the metadata location has changed, | ||
| /// indicating a concurrent modification | ||
| /// | ||
| /// **Traditional Locking** (default): | ||
| /// - Acquires an exclusive HMS lock on the table before making changes | ||
| /// - Retrieves the current table state | ||
| /// - Applies the commit and writes new metadata | ||
| /// - Updates the table in HMS using `alter_table` | ||
| /// - Releases the lock after the operation completes | ||
| /// | ||
| /// # Returns | ||
| /// A `Result` wrapping the updated `Table` object with new metadata. | ||
| /// | ||
| /// # Errors | ||
| /// This function may return an error in several scenarios: | ||
| /// - Failure to validate the namespace or table identifier | ||
| /// - Inability to acquire a lock (traditional locking mode) | ||
| /// - Failure to retrieve the table from HMS | ||
| /// - Errors reading or writing table metadata | ||
| /// - HMS rejects the update due to concurrent modification (optimistic locking) | ||
| /// - Errors from the underlying Thrift communication with HMS | ||
| async fn update_table(&self, commit: TableCommit) -> Result<Table> { | ||
| let ident = commit.identifier().clone(); | ||
| let db_name = validate_namespace(ident.namespace())?; | ||
| let tbl_name = ident.name.clone(); | ||
|
|
||
| if self.config.props.contains_key(HMS_HIVE_LOCKS_DISABLED) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a hive expert, but I would suggest to start with pessimistic version only first. |
||
| // Optimistic locking path: read first, then validate with EnvironmentContext | ||
| let hive_table = self | ||
| .client | ||
| .0 | ||
| .get_table(db_name.clone().into(), tbl_name.clone().into()) | ||
| .await | ||
| .map(from_thrift_exception) | ||
| .map_err(from_thrift_error)??; | ||
|
|
||
| let metadata_location = get_metadata_location(&hive_table.parameters)?; | ||
| let env_context = Self::build_environment_context(&metadata_location); | ||
|
|
||
| let (staged_table, new_hive_table) = self | ||
| .apply_and_prepare_update(commit, &db_name, &tbl_name, &hive_table) | ||
| .await?; | ||
|
|
||
| self.client | ||
| .0 | ||
| .alter_table_with_environment_context( | ||
| db_name.into(), | ||
| tbl_name.into(), | ||
| new_hive_table, | ||
| env_context, | ||
| ) | ||
| .await | ||
| .map_err(from_thrift_error)?; | ||
|
|
||
| Ok(staged_table) | ||
| } else { | ||
| // Traditional locking path: acquire lock first, then read | ||
| let _guard = HmsLockGuard::acquire(&self.client.0, &db_name, &tbl_name).await?; | ||
|
|
||
| let hive_table = self | ||
| .client | ||
| .0 | ||
| .get_table(db_name.clone().into(), tbl_name.clone().into()) | ||
| .await | ||
| .map(from_thrift_exception) | ||
| .map_err(from_thrift_error)??; | ||
|
|
||
| let (staged_table, new_hive_table) = self | ||
| .apply_and_prepare_update(commit, &db_name, &tbl_name, &hive_table) | ||
| .await?; | ||
|
|
||
| self.client | ||
| .0 | ||
| .alter_table(db_name.into(), tbl_name.into(), new_hive_table) | ||
| .await | ||
| .map_err(from_thrift_error)?; | ||
|
|
||
| Ok(staged_table) | ||
| // Lock automatically released here via Drop | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We manage dependencies in workspace's root cargo.tom.