-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Hive: Expose Snapshot Stats in HMS. #4456
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
Changes from 8 commits
98a9b73
5fe283c
c7b0e62
1d96a1b
8eac228
3a11d9b
72bd3be
4daa05a
8f5edaa
cd726fe
1e0d23f
a3bea2f
617d665
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| package org.apache.iceberg.hive; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.github.benmanes.caffeine.cache.Cache; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import java.net.InetAddress; | ||
|
|
@@ -71,6 +72,7 @@ | |
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.apache.iceberg.util.JsonUtil; | ||
| import org.apache.iceberg.util.Tasks; | ||
| import org.apache.thrift.TException; | ||
| import org.slf4j.Logger; | ||
|
|
@@ -90,6 +92,8 @@ public class HiveTableOperations extends BaseMetastoreTableOperations { | |
| private static final String HIVE_LOCK_CHECK_MAX_WAIT_MS = "iceberg.hive.lock-check-max-wait-ms"; | ||
| private static final String HIVE_ICEBERG_METADATA_REFRESH_MAX_RETRIES = "iceberg.hive.metadata-refresh-max-retries"; | ||
| private static final String HIVE_TABLE_LEVEL_LOCK_EVICT_MS = "iceberg.hive.table-level-lock-evict-ms"; | ||
| private static final String HIVE_TABLE_PARAMETER_SIZE_MAX = "iceberg.hive.table.parameter.size.max"; | ||
| private static final long HIVE_TABLE_PARAMETER_SIZE_MAX_DEFAULT = 32672; | ||
| private static final long HIVE_ACQUIRE_LOCK_TIMEOUT_MS_DEFAULT = 3 * 60 * 1000; // 3 minutes | ||
| private static final long HIVE_LOCK_CHECK_MIN_WAIT_MS_DEFAULT = 50; // 50 milliseconds | ||
| private static final long HIVE_LOCK_CHECK_MAX_WAIT_MS_DEFAULT = 5 * 1000; // 5 seconds | ||
|
|
@@ -148,6 +152,7 @@ private static class WaitingForLockException extends RuntimeException { | |
| private final long lockAcquireTimeout; | ||
| private final long lockCheckMinWaitTime; | ||
| private final long lockCheckMaxWaitTime; | ||
| private final long maxHiveTableParameterSize; | ||
| private final int metadataRefreshMaxRetries; | ||
| private final FileIO fileIO; | ||
| private final ClientPool<IMetaStoreClient, TException> metaClients; | ||
|
|
@@ -170,6 +175,7 @@ protected HiveTableOperations(Configuration conf, ClientPool metaClients, FileIO | |
| conf.getInt(HIVE_ICEBERG_METADATA_REFRESH_MAX_RETRIES, HIVE_ICEBERG_METADATA_REFRESH_MAX_RETRIES_DEFAULT); | ||
| long tableLevelLockCacheEvictionTimeout = | ||
| conf.getLong(HIVE_TABLE_LEVEL_LOCK_EVICT_MS, HIVE_TABLE_LEVEL_LOCK_EVICT_MS_DEFAULT); | ||
| this.maxHiveTableParameterSize = conf.getLong(HIVE_TABLE_PARAMETER_SIZE_MAX, HIVE_TABLE_PARAMETER_SIZE_MAX_DEFAULT); | ||
|
Member
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. Nit: can you move it up one line so not to break flow of the tableLevelLockCacheEvictionTimeout statement? |
||
| initTableLevelLockCache(tableLevelLockCacheEvictionTimeout); | ||
| } | ||
|
|
||
|
|
@@ -402,9 +408,43 @@ private void setHmsTableParameters(String newMetadataLocation, Table tbl, TableM | |
| parameters.put(StatsSetupConst.TOTAL_SIZE, summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP)); | ||
| } | ||
|
|
||
| setSnapshotStats(metadata, parameters); | ||
|
|
||
| tbl.setParameters(parameters); | ||
| } | ||
|
|
||
| private void setSnapshotStats(TableMetadata metadata, Map<String, String> parameters) { | ||
| Snapshot currentSnapshot = metadata.currentSnapshot(); | ||
|
Member
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 think we need a way to clear the properties if they are not there anymore in current snapshot (like original code setHmsProperties). For example if the summary becomes suddenly over the length in new snapshot, then it will not override the old summary and would be wrong.
Contributor
Author
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. Fixed in the new commit. |
||
| if (currentSnapshot != null) { | ||
| parameters.put(TableProperties.CURRENT_SNAPSHOT_ID, String.valueOf(currentSnapshot.snapshotId())); | ||
| parameters.put(TableProperties.CURRENT_SNAPSHOT_TIMESTAMP, String.valueOf(currentSnapshot.timestampMillis())); | ||
| setSnapshotSummary(parameters, currentSnapshot); | ||
| } else { | ||
| parameters.remove(TableProperties.CURRENT_SNAPSHOT_ID); | ||
| parameters.remove(TableProperties.CURRENT_SNAPSHOT_TIMESTAMP); | ||
| parameters.remove(TableProperties.CURRENT_SNAPSHOT_SUMMARY); | ||
| } | ||
|
|
||
| parameters.put(TableProperties.SNAPSHOT_COUNT, String.valueOf(metadata.snapshots().size())); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| void setSnapshotSummary(Map<String, String> parameters, Snapshot currentSnapshot) { | ||
| try { | ||
| String summary = JsonUtil.mapper().writeValueAsString(currentSnapshot.summary()); | ||
| if (summary.length() <= maxHiveTableParameterSize) { | ||
| parameters.put(TableProperties.CURRENT_SNAPSHOT_SUMMARY, summary); | ||
| } else { | ||
| parameters.remove(TableProperties.CURRENT_SNAPSHOT_SUMMARY); | ||
|
Member
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. Optional: we can actually put the parameters.remove() for all the properties above the parameters.put(), here we are doing the remove three times.
Contributor
Author
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. Good idea. Made the change. |
||
| LOG.warn("Not exposing the current snapshot({}) summary in HMS since it exceeds {} characters", | ||
| currentSnapshot.snapshotId(), maxHiveTableParameterSize); | ||
| } | ||
| } catch (JsonProcessingException e) { | ||
| parameters.remove(TableProperties.CURRENT_SNAPSHOT_SUMMARY); | ||
| LOG.warn("Failed to convert current snapshot({}) summary to a json string", currentSnapshot.snapshotId(), e); | ||
| } | ||
| } | ||
|
|
||
| private StorageDescriptor storageDescriptor(TableMetadata metadata, boolean hiveEngineEnabled) { | ||
|
|
||
| final StorageDescriptor storageDescriptor = new StorageDescriptor(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,11 +21,17 @@ | |
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.hive.metastore.api.Database; | ||
| import org.apache.iceberg.AssertHelpers; | ||
| import org.apache.iceberg.CachingCatalog; | ||
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.DataFiles; | ||
| import org.apache.iceberg.FileFormat; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.Schema; | ||
| import org.apache.iceberg.Snapshot; | ||
| import org.apache.iceberg.SortOrder; | ||
| import org.apache.iceberg.Table; | ||
| import org.apache.iceberg.TableProperties; | ||
|
|
@@ -42,6 +48,7 @@ | |
| import org.apache.iceberg.transforms.Transform; | ||
| import org.apache.iceberg.transforms.Transforms; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.apache.iceberg.util.JsonUtil; | ||
| import org.apache.thrift.TException; | ||
| import org.junit.Assert; | ||
| import org.junit.Rule; | ||
|
|
@@ -52,6 +59,9 @@ | |
| import static org.apache.iceberg.NullOrder.NULLS_FIRST; | ||
| import static org.apache.iceberg.SortDirection.ASC; | ||
| import static org.apache.iceberg.types.Types.NestedField.required; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| public class TestHiveCatalog extends HiveMetastoreTest { | ||
| private static ImmutableMap meta = ImmutableMap.of( | ||
|
|
@@ -468,4 +478,83 @@ public void testUUIDinTableProperties() throws Exception { | |
| catalog.dropTable(tableIdentifier); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testSnapshotStatsTableProperties() throws Exception { | ||
|
Member
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. We should probably have a test with the string exceeding the property size threshold, checking to make sure the behavior is as we expect.
Contributor
Author
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. Added a test case for that |
||
| Schema schema = new Schema( | ||
| required(1, "id", Types.IntegerType.get(), "unique ID"), | ||
| required(2, "data", Types.StringType.get()) | ||
| ); | ||
| TableIdentifier tableIdentifier = TableIdentifier.of(DB_NAME, "tbl"); | ||
| String location = temp.newFolder("tbl").toString(); | ||
|
|
||
| try { | ||
| catalog.buildTable(tableIdentifier, schema) | ||
| .withLocation(location) | ||
| .create(); | ||
|
|
||
| String tableName = tableIdentifier.name(); | ||
| org.apache.hadoop.hive.metastore.api.Table hmsTable = | ||
| metastoreClient.getTable(tableIdentifier.namespace().level(0), tableName); | ||
|
|
||
| // check whether parameters are in expected state | ||
| Map<String, String> parameters = hmsTable.getParameters(); | ||
| Assert.assertEquals("0", parameters.get(TableProperties.SNAPSHOT_COUNT)); | ||
|
Member
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. Can we put some error messages in these asserts, like in other tests?
Contributor
Author
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 sure it is necessary. The code here can explain itself quite well. |
||
| Assert.assertNull(parameters.get(TableProperties.CURRENT_SNAPSHOT_SUMMARY)); | ||
| Assert.assertNull(parameters.get(TableProperties.CURRENT_SNAPSHOT_ID)); | ||
| Assert.assertNull(parameters.get(TableProperties.CURRENT_SNAPSHOT_TIMESTAMP)); | ||
|
|
||
| // create a snapshot | ||
| Table icebergTable = catalog.loadTable(tableIdentifier); | ||
| String fileName = UUID.randomUUID().toString(); | ||
| DataFile file = DataFiles.builder(icebergTable.spec()) | ||
| .withPath(FileFormat.PARQUET.addExtension(fileName)) | ||
| .withRecordCount(2) | ||
| .withFileSizeInBytes(0) | ||
| .build(); | ||
| icebergTable.newFastAppend().appendFile(file).commit(); | ||
|
|
||
| // check whether parameters are in expected state | ||
| hmsTable = metastoreClient.getTable(tableIdentifier.namespace().level(0), tableName); | ||
| parameters = hmsTable.getParameters(); | ||
| Assert.assertEquals("1", parameters.get(TableProperties.SNAPSHOT_COUNT)); | ||
| String summary = JsonUtil.mapper().writeValueAsString(icebergTable.currentSnapshot().summary()); | ||
| Assert.assertEquals(summary, parameters.get(TableProperties.CURRENT_SNAPSHOT_SUMMARY)); | ||
| long snapshotId = icebergTable.currentSnapshot().snapshotId(); | ||
| Assert.assertEquals(String.valueOf(snapshotId), parameters.get(TableProperties.CURRENT_SNAPSHOT_ID)); | ||
| Assert.assertEquals(String.valueOf(icebergTable.currentSnapshot().timestampMillis()), | ||
| parameters.get(TableProperties.CURRENT_SNAPSHOT_TIMESTAMP)); | ||
|
|
||
| } finally { | ||
| catalog.dropTable(tableIdentifier); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testSetSnapshotSummary() throws Exception { | ||
| Configuration conf = new Configuration(); | ||
| conf.set("iceberg.hive.table.parameter.size.max", "4000"); | ||
| HiveTableOperations spyOps = spy(new HiveTableOperations(conf, null, null, catalog.name(), DB_NAME, "tbl")); | ||
| Snapshot snapshot = mock(Snapshot.class); | ||
| Map<String, String> summary = Maps.newHashMap(); | ||
| when(snapshot.summary()).thenReturn(summary); | ||
|
|
||
| // create a snapshot summary whose json string size is less than the limit | ||
| for (int i = 0; i < 100; i++) { | ||
| summary.put(String.valueOf(i), "value"); | ||
| } | ||
| Assert.assertTrue(JsonUtil.mapper().writeValueAsString(summary).length() < 4000); | ||
| Map<String, String> parameter = Maps.newHashMap(); | ||
| spyOps.setSnapshotSummary(parameter, snapshot); | ||
| Assert.assertEquals("The snapshot summary must be in parameters", 1, parameter.size()); | ||
|
|
||
| // create a snapshot summary whose json string size exceeds the limit | ||
| for (int i = 0; i < 1000; i++) { | ||
| summary.put(String.valueOf(i), "value"); | ||
| } | ||
| long summarySize = JsonUtil.mapper().writeValueAsString(summary).length(); | ||
| Assert.assertTrue(summarySize > 4000 && summarySize < 32672); | ||
|
Member
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. Nit: i think no need for the upper limit check, right? We are not really saving in Hive.
Contributor
Author
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. This is to make sure the new limit takes affect.
Contributor
Author
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. Add a comment in the code
Member
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. Hm, I meant, can't we just do
To me the test is checking whether you can persist something beyond 4000 chars right? The fact that it's below or above 32627 chars should not trigger now that we changed limit to 4000 right? Then, no need for the additional comment, if this is the case. Let me know if I am mistaken though.
Contributor
Author
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. The main purpose is to test whether we save the summary in HMS parameters when the size exceeds the limit. Besides, I also want to test if the limit has changed from 32627 to 4000. That's why I check both.
Member
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. OK got it. |
||
| spyOps.setSnapshotSummary(parameter, snapshot); | ||
| Assert.assertEquals("The snapshot summary must not be in parameters due to the size limit", 0, parameter.size()); | ||
| } | ||
| } | ||
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.
Should we add a comment, giving some reference to when it was changed in Hive?
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks, nit: how about "iceberg.hive.max.table.parameter.size".
Also, regarding the comment, it's kind of based on backend but Hive had a global limit of 4000 regardless of database to allow it to support across different database, so maybe "For Hive versions below 2.3, max table parameter size is 4000 characters, see https://issues.apache.org/jira/browse/HIVE-12274"?
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.
Made the change
Uh oh!
There was an error while loading. Please reload this page.
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.
one more nit: can the variable match the changed string
Uh oh!
There was an error while loading. Please reload this page.
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.
Actually sorry about this , I just noticed other flags, weird how they do dash after "iceberg.hive". How about HIVE_TABLE_PROPERTY_MAX_SIZE / 'iceberg.hive.table-property-max-size" (And fixes the correct term to be table-property).
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.
Made the change.