cshuo commented on code in PR #19402: URL: https://github.com/apache/hudi/pull/19402#discussion_r3671738049
########## hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/table/format/cdc/TestCdcIterators.java: ########## @@ -0,0 +1,230 @@ +/* + * 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.table.format.cdc; + +import org.apache.hudi.common.model.FileSlice; +import org.apache.hudi.common.model.HoodieBaseFile; +import org.apache.hudi.common.model.HoodieLogFile; +import org.apache.hudi.common.table.cdc.HoodieCDCFileSplit; +import org.apache.hudi.common.table.cdc.HoodieCDCInferenceCase; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.collection.ClosableIterator; +import org.apache.hudi.storage.StoragePath; +import org.apache.hudi.table.format.mor.MergeOnReadInputSplit; + +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.data.StringData; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.table.types.logical.VarCharType; +import org.apache.flink.types.RowKind; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Tests the lightweight iterator composition in {@link CdcIterators}. + */ +class TestCdcIterators { + + @Test + void testAddBaseFileIteratorSetsInsertKindAndClosesNestedIterator() { + GenericRowData row = GenericRowData.of(StringData.fromString("value")); + ClosableIterator<RowData> nested = mockIterator(); + when(nested.hasNext()).thenReturn(true, false); + when(nested.next()).thenReturn(row); + + CdcIterators.AddBaseFileIterator iterator = + new CdcIterators.AddBaseFileIterator(nested); + assertTrue(iterator.hasNext()); + assertSame(row, iterator.next()); + assertEquals(RowKind.INSERT, row.getRowKind()); + assertFalse(iterator.hasNext()); + + iterator.close(); + verify(nested).close(); + } + + @Test + void testRemoveBaseFileIteratorProjectsAndSetsDeleteKind() { + GenericRowData row = GenericRowData.of( + StringData.fromString("ignored"), StringData.fromString("selected")); + ClosableIterator<RowData> nested = mockIterator(); + when(nested.hasNext()).thenReturn(true); + when(nested.next()).thenReturn(row); + + CdcIterators.RemoveBaseFileIterator iterator = + new CdcIterators.RemoveBaseFileIterator( + rowType("selected"), + new int[] {1}, + nested); + assertTrue(iterator.hasNext()); + RowData projected = iterator.next(); + assertEquals(RowKind.DELETE, projected.getRowKind()); + assertEquals("selected", projected.getString(0).toString()); + + iterator.close(); + verify(nested).close(); + } + + @Test + void testCdcFileSplitsIteratorMovesAcrossEmptySplitsAndClosesResources() { + HoodieCDCFileSplit first = new HoodieCDCFileSplit( + "001", HoodieCDCInferenceCase.BASE_FILE_INSERT, "first.parquet"); + HoodieCDCFileSplit second = new HoodieCDCFileSplit( + "002", HoodieCDCInferenceCase.BASE_FILE_INSERT, "second.parquet"); + ClosableIterator<RowData> firstIterator = mockIterator(); + ClosableIterator<RowData> secondIterator = mockIterator(); + GenericRowData row = GenericRowData.of(StringData.fromString("row")); + when(firstIterator.hasNext()).thenReturn(false); + when(secondIterator.hasNext()).thenReturn(true, false); + when(secondIterator.next()).thenReturn(row); + CdcImageManager imageManager = mock(CdcImageManager.class); + + CdcIterators.CdcFileSplitsIterator iterator = + new CdcIterators.CdcFileSplitsIterator( + new HoodieCDCFileSplit[] {first, second}, + imageManager, + split -> split == first ? firstIterator : secondIterator); + + assertFalse(iterator.hasNext()); Review Comment: Fixed in c3ab8ffc960. `CdcFileSplitsIterator.hasNext()` now closes and advances through empty or exhausted child iterators until it finds a row or exhausts all file splits, and the regression test expects the first call to return `true`. ########## hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/table/format/cdc/TestCdcImageManager.java: ########## @@ -0,0 +1,175 @@ +/* + * 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.table.format.cdc; + +import org.apache.hudi.common.model.FileSlice; +import org.apache.hudi.common.model.HoodieFileGroupId; +import org.apache.hudi.common.util.collection.ClosableIterator; +import org.apache.hudi.common.util.collection.ExternalSpillableMap; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.table.format.FormatUtils; + +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.data.StringData; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.table.types.logical.VarCharType; +import org.apache.flink.types.RowKind; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Tests {@link CdcImageManager}. + */ +class TestCdcImageManager { + + @Test + void testImageRecordLifecycle() { + HoodieWriteConfig writeConfig = mock(HoodieWriteConfig.class); + CdcImageManager imageManager = new CdcImageManager( + rowType("value"), + writeConfig, + split -> { + throw new AssertionError("No split should be loaded by this test"); + }); + ExternalSpillableMap<String, byte[]> imageCache = mockImageCache(); + + GenericRowData original = GenericRowData.of(StringData.fromString("before")); + imageManager.updateImageRecord("key-1", imageCache, original); + + RowData image = imageManager.getImageRecord("key-1", imageCache, RowKind.UPDATE_BEFORE); + assertEquals(RowKind.UPDATE_BEFORE, image.getRowKind()); + assertEquals("before", image.getString(0).toString()); + + RowData removed = imageManager.removeImageRecord("key-1", imageCache); + assertEquals("before", removed.getString(0).toString()); + assertNull(imageManager.removeImageRecord("key-1", imageCache)); + assertThrows( + IllegalStateException.class, + () -> imageManager.getImageRecord("missing", imageCache, RowKind.DELETE)); + assertSame(writeConfig, imageManager.getWriteConfig()); + + imageManager.close(); + } + + @Test + void testDataViewAdapters() throws IOException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + CdcImageManager.BytesArrayOutputView outputView = + new CdcImageManager.BytesArrayOutputView(outputStream); + outputView.writeByte(7); + outputView.skipBytesToWrite(2); + outputView.write(new CdcImageManager.BytesArrayInputView(new byte[] {8, 9}), 2); + outputView.flush(); + + assertArrayEquals(new byte[] {7, 0, 0, 8, 9}, outputStream.toByteArray()); + + CdcImageManager.BytesArrayInputView inputView = + new CdcImageManager.BytesArrayInputView(outputStream.toByteArray()); + inputView.skipBytesToRead(3); Review Comment: Fixed in c3ab8ffc960. `BytesArrayInputView.skipBytesToRead()` now throws `EOFException` when `skipBytes()` makes no progress, with an out-of-bounds regression test covering truncated input. -- 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]
