This is an automated email from the ASF dual-hosted git repository.
deniskuzZ pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git
The following commit(s) were added to refs/heads/master by this push:
new 974259bd91a HIVE-30229: Iceberg: Statistics updates and ALTER TABLE
without the HMS lock can revert a concurrent commit (#6930)
974259bd91a is described below
commit 974259bd91a6b2898392da872f3d4f5cd956b1c0
Author: Denys Kuzmenko <[email protected]>
AuthorDate: Thu Nov 5 13:21:14 2026 +0100
HIVE-30229: Iceberg: Statistics updates and ALTER TABLE without the HMS
lock can revert a concurrent commit (#6930)
---
.../iceberg/mr/hive/HiveIcebergMetaHook.java | 65 +++++++++++++++++++---
.../iceberg/mr/hive/TestHiveCatalogEncryption.java | 15 +++++
.../iceberg/mr/hive/TestHiveIcebergStatistics.java | 45 +++++++++++++++
.../hive/TestHiveIcebergStorageHandlerNoScan.java | 43 +++++++++++++-
.../concurrent/HiveIcebergStorageHandlerStub.java | 39 +++++++++++++
.../hadoop/hive/ql/stats/ColStatsProcessor.java | 10 +++-
6 files changed, 204 insertions(+), 13 deletions(-)
diff --git
a/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
b/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
index 1f25959ba46..1d1a8343d6b 100644
---
a/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
+++
b/iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
@@ -27,6 +27,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
+import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -52,7 +53,6 @@
import org.apache.hadoop.hive.metastore.client.ThriftHiveMetaStoreClient;
import org.apache.hadoop.hive.metastore.partition.spec.PartitionSpecProxy;
import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
-import org.apache.hadoop.hive.ql.QueryState;
import org.apache.hadoop.hive.ql.ddl.misc.sortoder.SortFieldDesc;
import org.apache.hadoop.hive.ql.ddl.table.AlterTableType;
import org.apache.hadoop.hive.ql.exec.SerializationUtilities;
@@ -67,7 +67,6 @@
import org.apache.hadoop.hive.ql.parse.TransformSpec;
import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
import org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc;
-import org.apache.hadoop.hive.ql.plan.HiveOperation;
import org.apache.hadoop.hive.ql.session.SessionState;
import org.apache.hadoop.hive.ql.session.SessionStateUtil;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
@@ -132,6 +131,7 @@
import org.apache.iceberg.relocated.com.google.common.base.Splitter;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;
@@ -155,6 +155,8 @@ public class HiveIcebergMetaHook extends
BaseHiveIcebergMetaHook {
FileFormat.PARQUET.name().toLowerCase(),
FileFormat.ORC.name().toLowerCase(),
FileFormat.AVRO.name().toLowerCase());
+ private static final Set<String> METADATA_FILE_PROPS = ImmutableSet.of(
+ BaseMetastoreTableOperations.METADATA_LOCATION_PROP,
BaseMetastoreTableOperations.METADATA_HASH_PROP);
private static final PartitionDropOptions DROP_OPTIONS = new
PartitionDropOptions().deleteData(false).ifExists(true);
private static final List<org.apache.commons.lang3.tuple.Pair<Integer,
byte[]>> EMPTY_FILTER =
Lists.newArrayList(org.apache.commons.lang3.tuple.Pair.of(1, new
byte[0]));
@@ -307,24 +309,32 @@ public void
preAlterTable(org.apache.hadoop.hive.metastore.api.Table hmsTable, E
}
}
+ /**
+ * An alter without an operation type (e.g. a stats update) commits nothing
to Iceberg: the metadata_location CAS
+ * protects it instead of the lock.
+ */
private HiveLock lockObject(org.apache.hadoop.hive.metastore.api.Table
hmsTable) {
- if (!HiveTableOperations.hiveLockEnabled(hmsTable.getParameters(), conf) ||
- SessionStateUtil.getQueryState(conf)
- .map(QueryState::getHiveOperation)
- .filter(opType -> HiveOperation.QUERY == opType)
- .isPresent()) {
+ if (currentAlterTableOp == null ||
!HiveTableOperations.hiveLockEnabled(hmsTable.getParameters(), conf)) {
return new NoLock();
} else {
return new MetastoreLock(
conf,
- new CachedClientPool(conf, Maps.fromProperties(tableProperties)),
+ newClientPool(),
tableProperties.getProperty(Catalogs.NAME), hmsTable.getDbName(),
hmsTable.getTableName());
}
}
+ private CachedClientPool newClientPool() {
+ return new CachedClientPool(conf, Maps.fromProperties(tableProperties));
+ }
+
private void doPreAlterTable(org.apache.hadoop.hive.metastore.api.Table
hmsTable, EnvironmentContext context)
- throws MetaException {
+ throws TException {
+ if (currentAlterTableOp == null) {
+ refreshMetadataLocation(hmsTable, context);
+ return;
+ }
try {
icebergTable = IcebergTableUtil.getTable(conf, tableProperties, true);
} catch (NoSuchTableException nte) {
@@ -429,6 +439,43 @@ private void
assertNotCrossTableMetadataLocationChange(Map<String, String> tblPa
"this request with the refreshed metadata_location in order to
preserve the concurrent commit.",
newMetadataLocation, currentMetadata.metadataFileLocation());
}
+ setExpectedMetadataLocation(currentMetadata.metadataFileLocation(),
context);
+ }
+ }
+
+ /**
+ * Makes the alter carry the current metadata location, read from HMS as
HiveTableOperations does, with the metadata
+ * hash that belongs to it.
+ */
+ private void
refreshMetadataLocation(org.apache.hadoop.hive.metastore.api.Table hmsTable,
EnvironmentContext context)
+ throws TException {
+ Map<String, String> params = hmsTable.getParameters();
+ if
(!params.containsKey(BaseMetastoreTableOperations.METADATA_LOCATION_PROP)) {
+ return;
+ }
+ Map<String, String> currentParams;
+ try {
+ currentParams = newClientPool()
+ .run(client -> client.getTable(hmsTable.getDbName(),
hmsTable.getTableName()))
+ .getParameters();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException("Interrupted during refresh", e);
+ }
+ params.putAll(Maps.filterKeys(currentParams,
METADATA_FILE_PROPS::contains));
+ String metadataLocation =
currentParams.get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP);
+ setExpectedMetadataLocation(metadataLocation, context);
+ }
+
+ /**
+ * Without a lock, HMS applies the alter only if no commit moved the
metadata location since it was read.
+ * No-op under the HMS lock: the lock and the CAS are alternatives, as in
HiveTableOperations.doCommit.
+ */
+ private void setExpectedMetadataLocation(String currentMetadataLocation,
EnvironmentContext context) {
+ if (commitLock instanceof NoLock) {
+ context.putToProperties(hive_metastoreConstants.EXPECTED_PARAMETER_KEY,
+ BaseMetastoreTableOperations.METADATA_LOCATION_PROP);
+
context.putToProperties(hive_metastoreConstants.EXPECTED_PARAMETER_VALUE,
currentMetadataLocation);
}
}
diff --git
a/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveCatalogEncryption.java
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveCatalogEncryption.java
index 2aa287ae630..54e6be76b34 100644
---
a/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveCatalogEncryption.java
+++
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveCatalogEncryption.java
@@ -28,6 +28,7 @@
import org.apache.hadoop.fs.ChecksumFileSystem;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.iceberg.AppendFiles;
import org.apache.iceberg.AssertHelpers;
import org.apache.iceberg.BaseTable;
@@ -120,6 +121,20 @@ public void testWriteAndReadEncryptedTable() {
Assert.assertEquals("d", rowsAfterDelete.get(1)[1]);
}
+ @Test
+ public void testInsertWithStatsGatheringKeepsTableReadable() {
+ // the statistics alters carry the metadata hash of the metadata file they
point to
+ shell.setHiveSessionValue(HiveConf.ConfVars.HIVE_STATS_AUTOGATHER.varname,
true);
+ TableIdentifier identifier = TableIdentifier.of("default",
"encrypted_stats_table");
+ shell.executeStatement(String.format(
+ "CREATE EXTERNAL TABLE %s (id bigint, data string) STORED BY iceberg
%s",
+ identifier.name(), encryptionProps()));
+ shell.executeStatement(String.format("INSERT INTO %s VALUES (1, 'a'), (2,
'b')", identifier.name()));
+
+ List<Object[]> rows = shell.executeStatement("SELECT * FROM " +
identifier.name() + " ORDER BY id");
+ Assert.assertEquals(2, rows.size());
+ }
+
@Test
public void testManifestEncryption() {
TableIdentifier identifier = TableIdentifier.of("default",
"manifest_check");
diff --git
a/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergStatistics.java
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergStatistics.java
index 526630f9658..ce3a74c5045 100644
---
a/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergStatistics.java
+++
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergStatistics.java
@@ -32,6 +32,8 @@
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -79,6 +81,8 @@
import org.apache.iceberg.mr.hive.stats.IcebergStoredStats;
import org.apache.iceberg.mr.hive.test.TestTables;
import org.apache.iceberg.mr.hive.test.TestTables.TestTableType;
+import
org.apache.iceberg.mr.hive.test.concurrent.HiveIcebergStorageHandlerStub;
+import org.apache.iceberg.mr.hive.test.concurrent.WithMockedStorageHandler;
import
org.apache.iceberg.mr.hive.test.utils.HiveIcebergStorageHandlerTestUtils;
import org.apache.iceberg.mr.hive.test.utils.HiveIcebergTestUtils;
import org.apache.iceberg.puffin.Blob;
@@ -231,6 +235,47 @@ private TableIdentifier
getTableIdentifierWithPessimisticLock(String hiveLockEna
return identifier;
}
+ /**
+ * Another client inserts a row between the meta hook and the HMS call of
this insert's column statistics state
+ * update: both rows survive, and the insert does not fail.
+ */
+ @Test
+ @WithMockedStorageHandler
+ public void testStatsUpdateDoesNotRevertConcurrentInsert() {
+ // the column statistics state is updated through the meta hook only when
Iceberg stores the statistics
+ assumeParquetHiveCatalogIceberg();
+ shell.setHiveSessionValue(HiveConf.ConfVars.HIVE_STATS_AUTOGATHER.varname,
true);
+ testTables.createTable(shell, "customers",
HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA,
+ PartitionSpec.unpartitioned(), fileFormat, ImmutableList.of(),
formatVersion,
+ ImmutableMap.of(TableProperties.HIVE_LOCK_ENABLED, "false"),
+ "'" + HiveIcebergStorageHandlerStub.class.getName() + "'");
+ shell.executeStatement("INSERT INTO customers VALUES (0, 'Alice',
'Brown')");
+
+ // the column statistics state update is the insert's second alter, after
the basic statistics one
+ HiveIcebergStorageHandlerStub.setPreAlterTablePostAction(() -> {
+ HiveIcebergStorageHandlerStub.setPreAlterTablePostAction(
+ () -> executeInOtherSession("INSERT INTO customers VALUES (1, 'Bob',
'Green')"));
+ return null;
+ });
+ shell.executeStatement("INSERT INTO customers VALUES (2, 'Trudy',
'Pink')");
+
+ List<Object[]> rows = shell.executeStatement("SELECT customer_id FROM
customers ORDER BY customer_id");
+ Assert.assertEquals(ImmutableList.of(0L, 1L, 2L), rows.stream().map(row ->
row[0]).toList());
+ }
+
+ private List<Object[]> executeInOtherSession(String statement) throws
Exception {
+ try (ExecutorService executor =
Executors.newVirtualThreadPerTaskExecutor()) {
+ return executor.submit(() -> {
+ HiveIcebergStorageHandlerTestUtils.init(shell, testTables, temp);
+ try {
+ return shell.executeStatement(statement);
+ } finally {
+ shell.closeSession();
+ }
+ }).get();
+ }
+ }
+
@Test
public void testBranchWriteLeavesTableStatsUntouched() {
// a branch write leaves the table's snapshot where it is, so the table's
column and basic
diff --git
a/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergStorageHandlerNoScan.java
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergStorageHandlerNoScan.java
index 81d9c9e9f31..12841504522 100644
---
a/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergStorageHandlerNoScan.java
+++
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergStorageHandlerNoScan.java
@@ -39,11 +39,13 @@
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.common.StatsSetupConst;
import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.HiveMetaHook;
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants;
+import org.apache.hadoop.hive.ql.ddl.table.AlterTableType;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAuthorizer;
import
org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObject;
@@ -1520,23 +1522,58 @@ public void testAlterTableChangeColumnTypeAndComment()
throws TException, Interr
}
/**
- * Checks that HiveIcebergMetaHook doesn't run into failures with undefined
alter operation type (e.g. stat updates)
+ * Checks that an alter with undefined alter operation type (e.g. stat
updates) takes no lock, and that an alter
+ * without the lock carries the current metadata_location, guarded by the
CAS.
* @throws Exception - any test failure
*/
@Test
public void testMetaHookWithUndefinedAlterOperationType() throws Exception {
Assume.assumeTrue("Enough to check for one type only",
testTableType.equals(TestTableType.HIVE_CATALOG));
+ assertUnlockedAlterKeepsCurrentMetadataLocation("customers", "true",
ImmutableMap.of());
+ assertUnlockedAlterKeepsCurrentMetadataLocation("unlocked_customers",
"false",
+ ImmutableMap.of(HiveMetaHook.ALTER_TABLE_OPERATION_TYPE,
AlterTableType.ADDPROPS.name()));
+ }
+
+ @Test
+ public void testHadoopCatalogStatsAlterSkipsCas() throws Exception {
+ Assume.assumeTrue("Enough to check for one type only",
+ testTableType.equals(TestTableType.HADOOP_CATALOG));
TableIdentifier identifier = TableIdentifier.of("default", "customers");
testTables.createTable(shell, identifier.name(),
HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, SPEC,
FileFormat.PARQUET, ImmutableList.of());
- org.apache.hadoop.hive.metastore.api.Table hmsTable =
shell.metastore().getTable("default", "customers");
+ // HMS doesn't track the table's metadata location
+ org.apache.hadoop.hive.metastore.api.Table hmsTable =
shell.metastore().getTable(identifier);
HiveIcebergMetaHook metaHook = new
HiveIcebergMetaHook(shell.getHiveConf());
EnvironmentContext environmentContext = new
EnvironmentContext(Maps.newHashMap());
+ metaHook.preAlterTable(hmsTable, environmentContext);
+ metaHook.rollbackAlterTable(hmsTable, environmentContext);
+
Assert.assertFalse(hmsTable.getParameters().containsKey(BaseMetastoreTableOperations.METADATA_LOCATION_PROP));
+
Assert.assertFalse(environmentContext.getProperties().containsKey(hive_metastoreConstants.EXPECTED_PARAMETER_KEY));
+ }
+
+ private void assertUnlockedAlterKeepsCurrentMetadataLocation(String
tableName, String hiveLockEnabled,
+ Map<String, String> contextProperties) throws Exception {
+ TableIdentifier identifier = TableIdentifier.of("default", tableName);
+ Table table = testTables.createTable(shell, tableName,
HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, SPEC,
+ FileFormat.PARQUET, ImmutableList.of(), 2,
ImmutableMap.of(TableProperties.HIVE_LOCK_ENABLED, hiveLockEnabled));
+ org.apache.hadoop.hive.metastore.api.Table hmsTable =
shell.metastore().getTable(identifier);
+ // a commit after the caller read the table
+ testTables.appendIcebergTable(shell.getHiveConf(), table,
FileFormat.PARQUET, null,
+ HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS);
+ String currentMetadataLocation =
TableUtil.metadataFileLocation(testTables.loadTable(identifier));
+
+ HiveIcebergMetaHook metaHook = new
HiveIcebergMetaHook(shell.getHiveConf());
+ EnvironmentContext environmentContext = new
EnvironmentContext(Maps.newHashMap(contextProperties));
metaHook.preAlterTable(hmsTable, environmentContext);
- metaHook.commitAlterTable(hmsTable, environmentContext);
+ metaHook.rollbackAlterTable(hmsTable, environmentContext);
+
+ Assert.assertEquals(currentMetadataLocation,
+
hmsTable.getParameters().get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP));
+ Assert.assertEquals(currentMetadataLocation,
+
environmentContext.getProperties().get(hive_metastoreConstants.EXPECTED_PARAMETER_VALUE));
}
@Test
diff --git
a/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/test/concurrent/HiveIcebergStorageHandlerStub.java
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/test/concurrent/HiveIcebergStorageHandlerStub.java
index 05b70edc1d3..ec735632d64 100644
---
a/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/test/concurrent/HiveIcebergStorageHandlerStub.java
+++
b/iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/test/concurrent/HiveIcebergStorageHandlerStub.java
@@ -21,14 +21,53 @@
import java.io.IOException;
import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.hadoop.hive.metastore.HiveMetaHook;
+import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.ql.Context;
import org.apache.hadoop.hive.ql.plan.TableDesc;
import org.apache.hadoop.mapred.JobContext;
+import org.apache.iceberg.mr.hive.HiveIcebergMetaHook;
import org.apache.iceberg.mr.hive.HiveIcebergOutputCommitter;
import org.apache.iceberg.mr.hive.HiveIcebergStorageHandler;
public class HiveIcebergStorageHandlerStub extends HiveIcebergStorageHandler {
+ private static final AtomicReference<Callable<?>>
PRE_ALTER_TABLE_POST_ACTION = new AtomicReference<>();
+
+ /**
+ * Runs the action once, in the next alter_table call, between the meta hook
and the HMS call.
+ */
+ public static void setPreAlterTablePostAction(Callable<?> action) {
+ PRE_ALTER_TABLE_POST_ACTION.set(action);
+ }
+
+ @Override
+ public HiveMetaHook getMetaHook() {
+ HiveMetaHook metaHook = super.getMetaHook();
+ if (!(metaHook instanceof HiveIcebergMetaHook)) {
+ return metaHook;
+ }
+ return new HiveIcebergMetaHook(conf) {
+
+ @Override
+ public void preAlterTable(Table hmsTable, EnvironmentContext context)
throws MetaException {
+ super.preAlterTable(hmsTable, context);
+ Callable<?> action = PRE_ALTER_TABLE_POST_ACTION.getAndSet(null);
+ if (action != null) {
+ try {
+ action.call();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+ };
+ }
+
@Override
public HiveIcebergOutputCommitter getOutputCommitter() {
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/stats/ColStatsProcessor.java
b/ql/src/java/org/apache/hadoop/hive/ql/stats/ColStatsProcessor.java
index d1bc1ee222b..a6b4413475b 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/stats/ColStatsProcessor.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/stats/ColStatsProcessor.java
@@ -318,7 +318,15 @@ private void setOrRemoveColumnStatsAccurateProperty(Hive
db, Table tbl, List<Str
} else {
StatsSetupConst.removeColumnStatsState(tbl.getParameters(), colNames);
}
- db.alterTable(tbl.getFullyQualifiedName(), tbl, environmentContext, false);
+ try {
+ db.alterTable(tbl.getFullyQualifiedName(), tbl, environmentContext,
false);
+ } catch (HiveException e) {
+ // the statement's data is committed already: a failed alter costs the
statistics state flag, not the statement
+ if (isStatsReliable) {
+ throw e;
+ }
+ LOG.warn("Unable to update the column statistics state of {}",
tbl.getFullyQualifiedName(), e);
+ }
}
/**