-
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 2 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; | ||||||||||||||||||
|
|
@@ -402,9 +404,27 @@ 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(); | ||||||||||||||||||
| if (currentSnapshot != null) { | ||||||||||||||||||
| parameters.put(TableProperties.CURRENT_SNAPSHOT_ID, String.valueOf(currentSnapshot.snapshotId())); | ||||||||||||||||||
| parameters.put(TableProperties.CURRENT_SNAPSHOT_TIMESTAMP, String.valueOf(currentSnapshot.timestampMillis())); | ||||||||||||||||||
| try { | ||||||||||||||||||
| parameters.put(TableProperties.CURRENT_SNAPSHOT_SUMMARY, | ||||||||||||||||||
| JsonUtil.mapper().writeValueAsString(currentSnapshot.summary())); | ||||||||||||||||||
|
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. [question] should we take size of summary, also into consideration, As if when we should keep it or is it always optimal to keep it ? As per my understanding and I may be wrong here, The scenario I have in mind (not sure how practical it is), If some one want's to track changed partition metrics in summary and snapshot had a lot of changed partition and we set iceberg/core/src/main/java/org/apache/iceberg/SnapshotSummary.java Lines 183 to 190 in 86b42fe
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 question! According to https://issues.apache.org/jira/browse/HIVE-12274 and https://issues.apache.org/jira/browse/HIVE-14145, we can at least store 4000 chars as the values before HMS3.x, and more in HMS3.x depending on the under layer RDMS, at least 32672 bytes. I'm going to cap the summary at 4000 chars. |
||||||||||||||||||
| } catch (JsonProcessingException e) { | ||||||||||||||||||
| LOG.warn("Failed to convert snapshot summary to a json string", e); | ||||||||||||||||||
|
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. [minor] Does it makes sense to capture,
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. Make sense, will print out snapshot id in the warn message. |
||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| parameters.put(TableProperties.SNAPSHOT_COUNT, String.valueOf(metadata.snapshots().size())); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| private StorageDescriptor storageDescriptor(TableMetadata metadata, boolean hiveEngineEnabled) { | ||||||||||||||||||
|
|
||||||||||||||||||
| final StorageDescriptor storageDescriptor = new StorageDescriptor(); | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,9 +21,13 @@ | |
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| 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.SortOrder; | ||
|
|
@@ -468,4 +472,51 @@ 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)); | ||
| Assert.assertNotNull(parameters.get(TableProperties.CURRENT_SNAPSHOT_SUMMARY)); | ||
| Assert.assertNotNull(parameters.get(TableProperties.CURRENT_SNAPSHOT_ID)); | ||
| Assert.assertNotNull(parameters.get(TableProperties.CURRENT_SNAPSHOT_TIMESTAMP)); | ||
| } finally { | ||
| catalog.dropTable(tableIdentifier); | ||
| } | ||
| } | ||
| } | ||
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.
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.
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.
Fixed in the new commit.