Skip to content

Commit c1f52df

Browse files
ZihanLi58rzhang10
authored andcommitted
Do not delete metadata location when HMS has been successfully updated (#68)
(cherry picked from commit 766407e)
1 parent c7f97ae commit c1f52df

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

hive-metastore/src/main/java/org/apache/iceberg/hive/HiveMetadataPreservingTableOperations.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,30 @@ protected void doCommit(TableMetadata base, TableMetadata metadata) {
216216
throw new RuntimeException("Interrupted during commit", e);
217217

218218
} finally {
219-
if (threw) {
220-
// if anything went wrong, clean up the uncommitted metadata file
219+
if (threw && !metadataUpdatedSuccessfully(newMetadataLocation)) {
220+
// if anything went wrong and meta store hasn't been updated, clean up the uncommitted metadata file
221221
io().deleteFile(newMetadataLocation);
222222
}
223223
unlock(lockId);
224224
}
225225
}
226+
227+
private boolean metadataUpdatedSuccessfully(String newMetadataLocation) {
228+
boolean metadataUpdatedSuccessfully = false;
229+
try {
230+
Table tbl;
231+
boolean tableExists = metaClients.run(client -> client.tableExists(database, tableName));
232+
if (tableExists) {
233+
tbl = metaClients.run(client -> client.getTable(database, tableName));
234+
if (tbl.getParameters().get(METADATA_LOCATION_PROP).equals(newMetadataLocation)) {
235+
metadataUpdatedSuccessfully = true;
236+
}
237+
}
238+
} catch (Exception e) {
239+
// This indicate the client might be closed and we cannout make sure whether the table has been updated, so
240+
// assume it succeeds to avoid table pointing to non-exist location
241+
metadataUpdatedSuccessfully = true;
242+
}
243+
return metadataUpdatedSuccessfully;
244+
}
226245
}

0 commit comments

Comments
 (0)