BsoBird commented on code in PR #9546: URL: https://github.com/apache/iceberg/pull/9546#discussion_r1528470303
########## core/src/test/java/org/apache/iceberg/hadoop/TestHadoopCommits.java: ########## @@ -206,6 +210,133 @@ public void testFailedCommit() throws Exception { Assertions.assertThat(manifests).as("Should contain 0 Avro manifest files").isEmpty(); } + @Test + public void testCommitFailedBeforeChangeVersionHint() throws IOException { + table.newFastAppend().appendFile(FILE_A).commit(); + BaseTable baseTable = (BaseTable) table; + HadoopTableOperations tableOperations = (HadoopTableOperations) baseTable.operations(); + + HadoopTableOperations spyOps2 = spy(tableOperations); + doReturn(10000).when(spyOps2).findVersionWithOutVersionHint(any()); + TableMetadata metadataV1 = spyOps2.current(); + SortOrder dataSort = SortOrder.builderFor(baseTable.schema()).asc("data").build(); + TableMetadata metadataV2 = metadataV1.replaceSortOrder(dataSort); + assertThatThrownBy(() -> spyOps2.commit(metadataV1, metadataV2)) + .isInstanceOf(CommitFailedException.class) + .hasMessageContaining("as the latest version is currently"); + + HadoopTableOperations spyOps3 = spy(tableOperations); + doReturn(false).when(spyOps3).nextVersionIsLatest(any(), any()); + assertCommitNotChangeVersion( + baseTable, spyOps3, CommitFailedException.class, "as the latest version is currently"); + + HadoopTableOperations spyOps4 = spy(tableOperations); + doThrow(new RuntimeException("FileSystem crash!")) + .when(spyOps4) + .renameMetaDataFileAndCheck(any(), any(), any()); + assertCommitNotChangeVersion( + baseTable, spyOps4, CommitFailedException.class, "FileSystem crash!"); + } + + @Test + public void testCommitFailedAndCheckFailed() throws IOException { + table.newFastAppend().appendFile(FILE_A).commit(); + BaseTable baseTable = (BaseTable) table; + HadoopTableOperations tableOperations = (HadoopTableOperations) baseTable.operations(); + HadoopTableOperations spyOps = spy(tableOperations); + doThrow(new RuntimeException("FileSystem crash!")) + .when(spyOps) + .renameMetaDataFile(any(), any(), any()); + doThrow(new RuntimeException("Can not check new Metadata!")) + .when(spyOps) + .checkMetaDataFileRenameSuccess(any(), any(), any()); + assertCommitNotChangeVersion( + baseTable, spyOps, CommitStateUnknownException.class, "FileSystem crash!"); + } + + @Test + public void testCommitFailedAndRenameNotSuccess() throws IOException { + table.newFastAppend().appendFile(FILE_A).commit(); + BaseTable baseTable = (BaseTable) table; + HadoopTableOperations tableOperations = (HadoopTableOperations) baseTable.operations(); + HadoopTableOperations spyOps = spy(tableOperations); + doThrow(new RuntimeException("FileSystem crash!")) + .when(spyOps) + .renameMetaDataFile(any(), any(), any()); + doReturn(false).when(spyOps).checkMetaDataFileRenameSuccess(any(), any(), any()); + assertCommitNotChangeVersion( + baseTable, spyOps, CommitFailedException.class, "Can not commit newMetaData."); + } + + @Test + public void testCommitFailedButActualSuccess() throws IOException { + table.newFastAppend().appendFile(FILE_A).commit(); + BaseTable baseTable = (BaseTable) table; + HadoopTableOperations tableOperations = (HadoopTableOperations) baseTable.operations(); + HadoopTableOperations spyOps = spy(tableOperations); + doThrow(new RuntimeException("FileSystem crash!")) + .when(spyOps) + .renameMetaDataFile(any(), any(), any()); + doReturn(true).when(spyOps).checkMetaDataFileRenameSuccess(any(), any(), any()); + int versionBefore = spyOps.findVersion(); + TableMetadata metadataV1 = spyOps.current(); + SortOrder dataSort = SortOrder.builderFor(baseTable.schema()).asc("data").build(); + TableMetadata metadataV2 = metadataV1.replaceSortOrder(dataSort); + spyOps.commit(metadataV1, metadataV2); + int versionAfter = spyOps.findVersion(); + assert versionAfter - versionBefore == 1; + } + + private void assertCommitNotChangeVersion( + BaseTable baseTable, + HadoopTableOperations spyOps, + Class<? extends RuntimeException> exceptionClass, + String msg) { + int versionBefore = spyOps.findVersion(); + TableMetadata metadataV1 = spyOps.current(); + SortOrder dataSort = SortOrder.builderFor(baseTable.schema()).asc("data").build(); + TableMetadata metadataV2 = metadataV1.replaceSortOrder(dataSort); + assertThatThrownBy(() -> spyOps.commit(metadataV1, metadataV2)) + .isInstanceOf(exceptionClass) + .hasMessageContaining(msg); + int versionAfter = spyOps.findVersion(); + assert versionBefore == versionAfter; Review Comment: got it -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org