alexeykudinkin commented on code in PR #7174: URL: https://github.com/apache/hudi/pull/7174#discussion_r1047726252
########## hudi-client/hudi-spark-client/src/test/java/org/apache/hudi/execution/TestSimpleExecutionInSpark.java: ########## @@ -0,0 +1,254 @@ +/* + * 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.execution; + +import static org.apache.hudi.execution.HoodieLazyInsertIterable.getTransformFunction; + +import org.apache.avro.generic.IndexedRecord; +import org.apache.hudi.common.model.HoodieAvroRecord; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.table.timeline.HoodieActiveTimeline; +import org.apache.hudi.common.testutils.HoodieTestDataGenerator; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.queue.HoodieConsumer; +import org.apache.hudi.common.util.queue.SimpleHoodieExecutor; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.exception.HoodieException; +import org.apache.hudi.testutils.HoodieClientTestHarness; +import org.apache.spark.TaskContext; +import org.apache.spark.TaskContext$; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import scala.Tuple2; + +import static org.junit.jupiter.api.Assertions.assertEquals; +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.when; + +public class TestSimpleExecutionInSpark extends HoodieClientTestHarness { + + private final String instantTime = HoodieActiveTimeline.createNewInstantTime(); + + @BeforeEach + public void setUp() throws Exception { + initTestDataGenerator(); + initExecutorServiceWithFixedThreadPool(2); + } + + @AfterEach + public void tearDown() throws Exception { + cleanupResources(); + } + + private Runnable getPreExecuteRunnable() { Review Comment: I don't think we need this one ########## hudi-client/hudi-spark-client/src/test/java/org/apache/hudi/execution/TestSimpleExecutionInSpark.java: ########## @@ -0,0 +1,254 @@ +/* + * 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.execution; + +import static org.apache.hudi.execution.HoodieLazyInsertIterable.getTransformFunction; + +import org.apache.avro.generic.IndexedRecord; +import org.apache.hudi.common.model.HoodieAvroRecord; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.table.timeline.HoodieActiveTimeline; +import org.apache.hudi.common.testutils.HoodieTestDataGenerator; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.queue.HoodieConsumer; +import org.apache.hudi.common.util.queue.SimpleHoodieExecutor; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.exception.HoodieException; +import org.apache.hudi.testutils.HoodieClientTestHarness; +import org.apache.spark.TaskContext; +import org.apache.spark.TaskContext$; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import scala.Tuple2; + +import static org.junit.jupiter.api.Assertions.assertEquals; +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.when; + +public class TestSimpleExecutionInSpark extends HoodieClientTestHarness { + + private final String instantTime = HoodieActiveTimeline.createNewInstantTime(); + + @BeforeEach + public void setUp() throws Exception { + initTestDataGenerator(); + initExecutorServiceWithFixedThreadPool(2); + } + + @AfterEach + public void tearDown() throws Exception { + cleanupResources(); + } + + private Runnable getPreExecuteRunnable() { + final TaskContext taskContext = TaskContext.get(); + return () -> TaskContext$.MODULE$.setTaskContext(taskContext); + } + + @Test + public void testExecutor() { + + final List<HoodieRecord> hoodieRecords = dataGen.generateInserts(instantTime, 128); + final List<HoodieRecord> consumedRecords = new ArrayList<>(); + + HoodieWriteConfig hoodieWriteConfig = mock(HoodieWriteConfig.class); + when(hoodieWriteConfig.getDisruptorWriteBufferSize()).thenReturn(Option.of(8)); + HoodieConsumer<HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord>, Integer> consumer = + new HoodieConsumer<HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord>, Integer>() { + private int count = 0; + + @Override + public void consume(HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord> record) throws Exception { + consumedRecords.add(record.record); + count++; + } + + @Override + public Integer finish() { + return count; + } + }; + SimpleHoodieExecutor<HoodieRecord, Tuple2<HoodieRecord, Option<IndexedRecord>>, Integer> exec = null; + + try { + exec = new SimpleHoodieExecutor(hoodieRecords.iterator(), consumer, getTransformFunction(HoodieTestDataGenerator.AVRO_SCHEMA)); + + int result = exec.execute(); + // It should buffer and write 128 records + assertEquals(128, result); + + // collect all records and assert that consumed records are identical to produced ones + // assert there's no tampering, and that the ordering is preserved + assertEquals(hoodieRecords, consumedRecords); + for (int i = 0; i < hoodieRecords.size(); i++) { + assertEquals(hoodieRecords.get(i), consumedRecords.get(i)); + } + + } finally { + if (exec != null) { + exec.shutdownNow(); + } + } + } + + // Test to ensure that we are reading all records from queue iterator in the same order + // without any exceptions. + @SuppressWarnings("unchecked") + @Test + @Timeout(value = 60) + public void testRecordReading() { + + final List<HoodieRecord> hoodieRecords = dataGen.generateInserts(instantTime, 100); + ArrayList<HoodieRecord> beforeRecord = new ArrayList<>(); + ArrayList<IndexedRecord> beforeIndexedRecord = new ArrayList<>(); + ArrayList<HoodieAvroRecord> afterRecord = new ArrayList<>(); + ArrayList<IndexedRecord> afterIndexedRecord = new ArrayList<>(); + + hoodieRecords.forEach(record -> { + final HoodieAvroRecord originalRecord = (HoodieAvroRecord) record; + beforeRecord.add(originalRecord); + try { + final Option<IndexedRecord> originalInsertValue = + originalRecord.getData().getInsertValue(HoodieTestDataGenerator.AVRO_SCHEMA); + beforeIndexedRecord.add(originalInsertValue.get()); + } catch (IOException e) { + // ignore exception here. + } + }); + + HoodieWriteConfig hoodieWriteConfig = mock(HoodieWriteConfig.class); + when(hoodieWriteConfig.getDisruptorWriteBufferSize()).thenReturn(Option.of(16)); Review Comment: We need to clean up these ########## hudi-client/hudi-spark-client/src/test/java/org/apache/hudi/execution/TestSimpleExecutionInSpark.java: ########## @@ -0,0 +1,254 @@ +/* + * 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.execution; + +import static org.apache.hudi.execution.HoodieLazyInsertIterable.getTransformFunction; + +import org.apache.avro.generic.IndexedRecord; +import org.apache.hudi.common.model.HoodieAvroRecord; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.table.timeline.HoodieActiveTimeline; +import org.apache.hudi.common.testutils.HoodieTestDataGenerator; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.queue.HoodieConsumer; +import org.apache.hudi.common.util.queue.SimpleHoodieExecutor; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.exception.HoodieException; +import org.apache.hudi.testutils.HoodieClientTestHarness; +import org.apache.spark.TaskContext; +import org.apache.spark.TaskContext$; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import scala.Tuple2; + +import static org.junit.jupiter.api.Assertions.assertEquals; +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.when; + +public class TestSimpleExecutionInSpark extends HoodieClientTestHarness { + + private final String instantTime = HoodieActiveTimeline.createNewInstantTime(); + + @BeforeEach + public void setUp() throws Exception { + initTestDataGenerator(); + initExecutorServiceWithFixedThreadPool(2); + } + + @AfterEach + public void tearDown() throws Exception { + cleanupResources(); + } + + private Runnable getPreExecuteRunnable() { + final TaskContext taskContext = TaskContext.get(); + return () -> TaskContext$.MODULE$.setTaskContext(taskContext); + } + + @Test + public void testExecutor() { + + final List<HoodieRecord> hoodieRecords = dataGen.generateInserts(instantTime, 128); + final List<HoodieRecord> consumedRecords = new ArrayList<>(); + + HoodieWriteConfig hoodieWriteConfig = mock(HoodieWriteConfig.class); + when(hoodieWriteConfig.getDisruptorWriteBufferSize()).thenReturn(Option.of(8)); + HoodieConsumer<HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord>, Integer> consumer = + new HoodieConsumer<HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord>, Integer>() { + private int count = 0; + + @Override + public void consume(HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord> record) throws Exception { + consumedRecords.add(record.record); + count++; + } + + @Override + public Integer finish() { + return count; + } + }; + SimpleHoodieExecutor<HoodieRecord, Tuple2<HoodieRecord, Option<IndexedRecord>>, Integer> exec = null; + + try { + exec = new SimpleHoodieExecutor(hoodieRecords.iterator(), consumer, getTransformFunction(HoodieTestDataGenerator.AVRO_SCHEMA)); + + int result = exec.execute(); + // It should buffer and write 128 records + assertEquals(128, result); + + // collect all records and assert that consumed records are identical to produced ones + // assert there's no tampering, and that the ordering is preserved + assertEquals(hoodieRecords, consumedRecords); + for (int i = 0; i < hoodieRecords.size(); i++) { Review Comment: We don't need this for-loop, right? -- 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]
