Search in sources :

Example 1 with ExceptionUtils

use of org.apache.flink.util.ExceptionUtils in project flink by apache.

the class SplitFetcherTest method testWakeup.

@Test
public void testWakeup() throws InterruptedException {
    final int numSplits = 3;
    final int numRecordsPerSplit = 10_000;
    final int wakeupRecordsInterval = 10;
    final int numTotalRecords = numRecordsPerSplit * numSplits;
    FutureCompletingBlockingQueue<RecordsWithSplitIds<int[]>> elementQueue = new FutureCompletingBlockingQueue<>(1);
    SplitFetcher<int[], MockSourceSplit> fetcher = new SplitFetcher<>(0, elementQueue, MockSplitReader.newBuilder().setNumRecordsPerSplitPerFetch(2).setBlockingFetch(true).build(), ExceptionUtils::rethrow, () -> {
    }, (ignore) -> {
    });
    // Prepare the splits.
    List<MockSourceSplit> splits = new ArrayList<>();
    for (int i = 0; i < numSplits; i++) {
        splits.add(new MockSourceSplit(i, 0, numRecordsPerSplit));
        int base = i * numRecordsPerSplit;
        for (int j = base; j < base + numRecordsPerSplit; j++) {
            splits.get(splits.size() - 1).addRecord(j);
        }
    }
    // Add splits to the fetcher.
    fetcher.addSplits(splits);
    // A thread drives the fetcher.
    Thread fetcherThread = new Thread(fetcher, "FetcherThread");
    SortedSet<Integer> recordsRead = Collections.synchronizedSortedSet(new TreeSet<>());
    // A thread waking up the split fetcher frequently.
    AtomicInteger wakeupTimes = new AtomicInteger(0);
    AtomicBoolean stop = new AtomicBoolean(false);
    Thread wakeUpCaller = new Thread("Wakeup Caller") {

        @Override
        public void run() {
            int lastWakeup = 0;
            while (recordsRead.size() < numTotalRecords && !stop.get()) {
                int numRecordsRead = recordsRead.size();
                if (numRecordsRead >= lastWakeup + wakeupRecordsInterval) {
                    fetcher.wakeUp(false);
                    wakeupTimes.incrementAndGet();
                    lastWakeup = numRecordsRead;
                }
            }
        }
    };
    try {
        fetcherThread.start();
        wakeUpCaller.start();
        while (recordsRead.size() < numSplits * numRecordsPerSplit) {
            final RecordsWithSplitIds<int[]> nextBatch = elementQueue.take();
            while (nextBatch.nextSplit() != null) {
                int[] arr;
                while ((arr = nextBatch.nextRecordFromSplit()) != null) {
                    assertTrue(recordsRead.add(arr[0]));
                }
            }
        }
        assertEquals(numTotalRecords, recordsRead.size());
        assertEquals(0, (int) recordsRead.first());
        assertEquals(numTotalRecords - 1, (int) recordsRead.last());
        assertTrue(wakeupTimes.get() > 0);
    } finally {
        stop.set(true);
        fetcher.shutdown();
        fetcherThread.join();
        wakeUpCaller.join();
    }
}
Also used : ArrayList(java.util.ArrayList) ExceptionUtils(org.apache.flink.util.ExceptionUtils) RecordsWithSplitIds(org.apache.flink.connector.base.source.reader.RecordsWithSplitIds) CheckedThread(org.apache.flink.core.testutils.CheckedThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FutureCompletingBlockingQueue(org.apache.flink.connector.base.source.reader.synchronization.FutureCompletingBlockingQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockSourceSplit(org.apache.flink.api.connector.source.mocks.MockSourceSplit) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 MockSourceSplit (org.apache.flink.api.connector.source.mocks.MockSourceSplit)1 RecordsWithSplitIds (org.apache.flink.connector.base.source.reader.RecordsWithSplitIds)1 FutureCompletingBlockingQueue (org.apache.flink.connector.base.source.reader.synchronization.FutureCompletingBlockingQueue)1 CheckedThread (org.apache.flink.core.testutils.CheckedThread)1 ExceptionUtils (org.apache.flink.util.ExceptionUtils)1 Test (org.junit.Test)1