yongkyunlee commented on code in PR #13746: URL: https://github.com/apache/hudi/pull/13746#discussion_r2299416802
########## hudi-client/hudi-client-common/src/test/java/org/apache/hudi/io/TestHoodieCreateHandle.java: ########## @@ -0,0 +1,450 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hudi.io; + +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericRecord; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hudi.client.WriteStatus; +import org.apache.hudi.common.config.HoodieMetadataConfig; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.engine.LocalTaskContextSupplier; +import org.apache.hudi.common.engine.TaskContextSupplier; +import org.apache.hudi.common.fs.FSUtils; +import org.apache.hudi.common.model.HoodiePartitionMetadata; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.model.HoodieWriteStat; +import org.apache.hudi.common.model.IOType; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.testutils.HoodieCommonTestHarness; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.ParquetUtils; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.exception.HoodieInsertException; +import org.apache.hudi.io.storage.HoodieFileWriter; +import org.apache.hudi.storage.StoragePath; +import org.apache.hudi.table.HoodieTable; +import org.apache.hudi.table.TestBaseHoodieTable; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +import static org.apache.hudi.common.testutils.HoodieTestDataGenerator.DEFAULT_FIRST_PARTITION_PATH; +import static org.apache.hudi.common.testutils.HoodieTestDataGenerator.DEFAULT_PARTITION_PATHS; +import static org.apache.hudi.common.testutils.HoodieTestDataGenerator.DEFAULT_SECOND_PARTITION_PATH; +import static org.apache.hudi.common.testutils.HoodieTestDataGenerator.TRIP_EXAMPLE_SCHEMA; +import static org.apache.hudi.common.testutils.HoodieTestDataGenerator.TRIP_FLATTENED_SCHEMA; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.spy; + +@ExtendWith(MockitoExtension.class) +public class TestHoodieCreateHandle extends HoodieCommonTestHarness { + private static final String TEST_INSTANT_TIME = "20231201120000"; + private static final String TEST_FILE_ID = "file-001"; + private static final Schema TEST_SCHEMA = new Schema.Parser().parse(TRIP_EXAMPLE_SCHEMA); + private static final String TEST_PARTITION_PATH = DEFAULT_FIRST_PARTITION_PATH; + + private HoodieWriteConfig writeConfig; + private TaskContextSupplier taskContextSupplier; + private HoodieTable hoodieTable; + + @BeforeEach + void setUp() throws IOException { + initPath(); + initMetaClient(false); + initTestDataGenerator(); + + writeConfig = HoodieWriteConfig.newBuilder() + .withPath(basePath) + .withSchema(TRIP_EXAMPLE_SCHEMA) + .withMarkersType("DIRECT") + .withMetadataConfig(HoodieMetadataConfig.newBuilder().enable(false).build()) + .build(); + hoodieTable = new TestBaseHoodieTable(writeConfig, getEngineContext(), metaClient); + taskContextSupplier = new LocalTaskContextSupplier(); + } + + @AfterEach + void tearDown() throws Exception { + cleanMetaClient(); + cleanupTestDataGenerator(); + } + + @Test + void testConstructorWithBasicParameters() { + HoodieCreateHandle createHandle = new HoodieCreateHandle( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, + TEST_FILE_ID, taskContextSupplier); + + assertNotNull(createHandle); + assertEquals(IOType.CREATE, createHandle.getIOType()); + assertFalse(createHandle.preserveMetadata); + } + + @Test + void testConstructorWithPreserveMetadata() { + HoodieCreateHandle createHandle = new HoodieCreateHandle( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, + TEST_FILE_ID, taskContextSupplier, true); + + assertNotNull(createHandle); + assertTrue(createHandle.preserveMetadata); + } + + @Test + public void testConstructorWithOverriddenSchema() { + Schema overridenSchema = new Schema.Parser().parse(TRIP_FLATTENED_SCHEMA); + + HoodieCreateHandle handleWithOverridenSchema = new HoodieCreateHandle<>( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, + TEST_FILE_ID, Option.of(overridenSchema), taskContextSupplier); + assertEquals(overridenSchema, handleWithOverridenSchema.writeSchema); + } + + @Test + void testConstructorCreatesPartitionMetadataAndMarkerAndBaseFile() throws Exception { + HoodieCreateHandle createHandle = new HoodieCreateHandle( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, + TEST_FILE_ID, taskContextSupplier); + assertNotNull(createHandle); + + FileSystem fs = (FileSystem) metaClient.getStorage().getFileSystem(); + + // Verify partition metadata file exists under the target partition + Path partitionPath = new Path(basePath, TEST_PARTITION_PATH); + validatePartitionMetaPathExistence(fs, true); + + // Verify marker file exists with expected name + String expectedBaseFileName = getExpectedBaseFileName(); + validateMarkerFileExistence(fs, expectedBaseFileName, true); + + // Verify the base file path exists + Path expectedBaseFilePath = new Path(partitionPath, expectedBaseFileName); + assertTrue(fs.exists(expectedBaseFilePath)); + } + + @ParameterizedTest + @MethodSource("generateParametersForCanWrite") + void testCanWriteWhenFileWriterCanWrite(String partitionPath, boolean expectedCanWrite) { + HoodieCreateHandle createHandle = new HoodieCreateHandle<>( + writeConfig, TEST_INSTANT_TIME, hoodieTable, partitionPath, + TEST_FILE_ID, taskContextSupplier); + + boolean canWrite = createHandle.canWrite(dataGen.generateInserts(TEST_INSTANT_TIME, 1).get(0)); + assertEquals(expectedCanWrite, canWrite); + } + + private static Stream<Arguments> generateParametersForCanWrite() { + // partition path, expected canWrite value + return Stream.of( + Arguments.of(DEFAULT_FIRST_PARTITION_PATH, true), + // datagen generates records for each partition in a round-robin fashion so first record is written to first partition path + Arguments.of(DEFAULT_SECOND_PARTITION_PATH, false) + ); + } + + @Test + void testEmptyRecordMapNoWrite() { + HoodieCreateHandle createHandle = new HoodieCreateHandle<>( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, + TEST_FILE_ID, taskContextSupplier); + createHandle.recordMap = new HashMap<>(); + + assertDoesNotThrow(() -> createHandle.write()); + assertEquals(0, createHandle.recordsWritten); + } + + @Test + void testWriteSingleRecordFromRecordMap() { + Map<String, HoodieRecord<Object>> recordMap = new HashMap<>(); + HoodieRecord record = dataGen.generateInserts(TEST_INSTANT_TIME, 1).get(0); + recordMap.put(record.getRecordKey(), record); + HoodieCreateHandle createHandle = new HoodieCreateHandle<>( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, + TEST_FILE_ID, recordMap, taskContextSupplier); + + assertDoesNotThrow(() -> createHandle.write()); + assertEquals(1, createHandle.recordsWritten); + } + + @Test + void testDoWrite() throws Exception { + HoodieCreateHandle createHandle = new HoodieCreateHandle<>( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, + TEST_FILE_ID, taskContextSupplier); + + // Generate and store records for comparison + List<HoodieRecord> sourceRecords = dataGen.generateInserts(TEST_INSTANT_TIME, 4); + for (HoodieRecord record : sourceRecords) { + createHandle.doWrite(record, TEST_SCHEMA, new TypedProperties()); + } + List<WriteStatus> writeStatuses = createHandle.close(); + + // By default, preserveMetadata is false so written records should contain meta fields + validateWrittenRecords(writeStatuses, sourceRecords, true); + } + + @Test + public void testDoWriteWithPreserveMetadata() throws Exception { + HoodieCreateHandle createHandle = new HoodieCreateHandle<>( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, + TEST_FILE_ID, taskContextSupplier, true); + + List<HoodieRecord> sourceRecords = dataGen.generateInserts(TEST_INSTANT_TIME, 4); + for (HoodieRecord record : sourceRecords) { + createHandle.doWrite(record, TEST_SCHEMA, new TypedProperties()); + } + List<WriteStatus> writeStatuses = createHandle.close(); + + // Since preserveMetadata is true, create handle doesn't populate meta fields + validateWrittenRecords(writeStatuses, sourceRecords, false); + } + + @Test + public void testDoWriteWithDeleteRecord() throws Exception { + HoodieCreateHandle createHandle = new HoodieCreateHandle<>( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, TEST_FILE_ID, taskContextSupplier); + + List<HoodieRecord> deleteRecords = dataGen.generateDeletes(TEST_INSTANT_TIME, 4); + for (HoodieRecord deleteRecord : deleteRecords) { + createHandle.doWrite(deleteRecord, TEST_SCHEMA, new TypedProperties()); + } + List<WriteStatus> writeStatuses = createHandle.close(); + assertEquals(0, createHandle.recordsWritten); + assertEquals(4, createHandle.recordsDeleted); + + validateNoRecordWritten(writeStatuses); + } + + @Test + public void testDoWriteHandlesException() throws Exception { + HoodieCreateHandle createHandle = new HoodieCreateHandle<>( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, + TEST_FILE_ID, taskContextSupplier); + + HoodieRecord realRecord = dataGen.generateInserts(TEST_INSTANT_TIME, 1).get(0); + HoodieRecord spyRecord = spy(realRecord); + + // Stub the prependMetaFields method to throw exception + doThrow(new RuntimeException("Test exception")) + .when(spyRecord).prependMetaFields(any(), any(), any(), any()); + + assertDoesNotThrow(() -> + createHandle.doWrite(spyRecord, TEST_SCHEMA, new TypedProperties())); + List<WriteStatus> writeStatuses = createHandle.close(); + + assertEquals(1, createHandle.writeStatus.getErrors().size()); + validateNoRecordWritten(writeStatuses); + } + + @Test + public void testCloseWhenAlreadyClosed() { + HoodieCreateHandle createHandle = new HoodieCreateHandle<>( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, + TEST_FILE_ID, taskContextSupplier); + List<WriteStatus> writeStatuses = createHandle.close(); + assertEquals(1, writeStatuses.size()); + + List<WriteStatus> secondCloseOutput = createHandle.close(); + assertEquals(writeStatuses, secondCloseOutput); + } + + @Test + public void testCloseWithFileWriterIOException() throws IOException { + HoodieCreateHandle<Object, Object, Object, Object> createHandle = new HoodieCreateHandle<>( + writeConfig, TEST_INSTANT_TIME, hoodieTable, TEST_PARTITION_PATH, + TEST_FILE_ID, taskContextSupplier); + + // Spy on the real fileWriter object that was created + HoodieFileWriter spyFileWriter = spy(createHandle.fileWriter); + createHandle.fileWriter = spyFileWriter; + + doThrow(new IOException("Test IO exception")).when(spyFileWriter).close(); + assertThrows(HoodieInsertException.class, () -> createHandle.close()); + } + + @Test + void testNoMarkerFileCreatedWhenFileWriterFails() throws Exception { + // Create a custom HoodieCreateHandle that simulates file writer initialization failure + class FailingFileWriterCreateHandle extends HoodieCreateHandle<Object, Object, Object, Object> { + public FailingFileWriterCreateHandle(HoodieWriteConfig config, String instantTime, + HoodieTable<Object, Object, Object, Object> hoodieTable, + String partitionPath, String fileId, + TaskContextSupplier taskContextSupplier) { + super(config, instantTime, hoodieTable, partitionPath, fileId, taskContextSupplier); + } + + @Override + protected HoodieFileWriter initializeFileWriter() throws IOException { + throw new IOException("Simulated file writer initialization failure"); Review Comment: I created a new `TestFileWriter` class per your suggestion. Let me know if it's the right approach. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
