Search in sources :

Example 1 with BoundedInMemoryQueueConsumer

use of org.apache.hudi.common.util.queue.BoundedInMemoryQueueConsumer in project hudi by apache.

the class TestBoundedInMemoryExecutorInSpark method testExecutor.

@Test
public void testExecutor() {
    final List<HoodieRecord> hoodieRecords = dataGen.generateInserts(instantTime, 100);
    HoodieWriteConfig hoodieWriteConfig = mock(HoodieWriteConfig.class);
    when(hoodieWriteConfig.getWriteBufferLimitBytes()).thenReturn(1024);
    BoundedInMemoryQueueConsumer<HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord>, Integer> consumer = new BoundedInMemoryQueueConsumer<HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord>, Integer>() {

        private int count = 0;

        @Override
        protected void consumeOneRecord(HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord> record) {
            count++;
        }

        @Override
        protected void finish() {
        }

        @Override
        protected Integer getResult() {
            return count;
        }
    };
    BoundedInMemoryExecutor<HoodieRecord, Tuple2<HoodieRecord, Option<IndexedRecord>>, Integer> executor = null;
    try {
        executor = new BoundedInMemoryExecutor(hoodieWriteConfig.getWriteBufferLimitBytes(), hoodieRecords.iterator(), consumer, getTransformFunction(HoodieTestDataGenerator.AVRO_SCHEMA), getPreExecuteRunnable());
        int result = executor.execute();
        // It should buffer and write 100 records
        assertEquals(100, result);
        // There should be no remaining records in the buffer
        assertFalse(executor.isRemaining());
    } finally {
        if (executor != null) {
            executor.shutdownNow();
        }
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HoodieRecord(org.apache.hudi.common.model.HoodieRecord) BoundedInMemoryExecutor(org.apache.hudi.common.util.queue.BoundedInMemoryExecutor) HoodieWriteConfig(org.apache.hudi.config.HoodieWriteConfig) BoundedInMemoryQueueConsumer(org.apache.hudi.common.util.queue.BoundedInMemoryQueueConsumer) Tuple2(scala.Tuple2) Test(org.junit.jupiter.api.Test)

Example 2 with BoundedInMemoryQueueConsumer

use of org.apache.hudi.common.util.queue.BoundedInMemoryQueueConsumer in project hudi by apache.

the class TestBoundedInMemoryExecutorInSpark method testInterruptExecutor.

@Test
public void testInterruptExecutor() {
    final List<HoodieRecord> hoodieRecords = dataGen.generateInserts(instantTime, 100);
    HoodieWriteConfig hoodieWriteConfig = mock(HoodieWriteConfig.class);
    when(hoodieWriteConfig.getWriteBufferLimitBytes()).thenReturn(1024);
    BoundedInMemoryQueueConsumer<HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord>, Integer> consumer = new BoundedInMemoryQueueConsumer<HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord>, Integer>() {

        @Override
        protected void consumeOneRecord(HoodieLazyInsertIterable.HoodieInsertValueGenResult<HoodieRecord> record) {
            try {
                while (true) {
                    Thread.sleep(1000);
                }
            } catch (InterruptedException ie) {
                return;
            }
        }

        @Override
        protected void finish() {
        }

        @Override
        protected Integer getResult() {
            return 0;
        }
    };
    BoundedInMemoryExecutor<HoodieRecord, Tuple2<HoodieRecord, Option<IndexedRecord>>, Integer> executor = null;
    try {
        executor = new BoundedInMemoryExecutor(hoodieWriteConfig.getWriteBufferLimitBytes(), hoodieRecords.iterator(), consumer, getTransformFunction(HoodieTestDataGenerator.AVRO_SCHEMA), getPreExecuteRunnable());
        BoundedInMemoryExecutor<HoodieRecord, Tuple2<HoodieRecord, Option<IndexedRecord>>, Integer> finalExecutor = executor;
        Thread.currentThread().interrupt();
        assertThrows(HoodieException.class, () -> finalExecutor.execute());
        assertTrue(Thread.interrupted());
    } finally {
        if (executor != null) {
            executor.shutdownNow();
        }
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HoodieRecord(org.apache.hudi.common.model.HoodieRecord) BoundedInMemoryExecutor(org.apache.hudi.common.util.queue.BoundedInMemoryExecutor) HoodieWriteConfig(org.apache.hudi.config.HoodieWriteConfig) BoundedInMemoryQueueConsumer(org.apache.hudi.common.util.queue.BoundedInMemoryQueueConsumer) Tuple2(scala.Tuple2) Test(org.junit.jupiter.api.Test)

Aggregations

IndexedRecord (org.apache.avro.generic.IndexedRecord)2 HoodieRecord (org.apache.hudi.common.model.HoodieRecord)2 BoundedInMemoryExecutor (org.apache.hudi.common.util.queue.BoundedInMemoryExecutor)2 BoundedInMemoryQueueConsumer (org.apache.hudi.common.util.queue.BoundedInMemoryQueueConsumer)2 HoodieWriteConfig (org.apache.hudi.config.HoodieWriteConfig)2 Test (org.junit.jupiter.api.Test)2 Tuple2 (scala.Tuple2)2