use of org.apache.flink.connector.base.source.reader.synchronization.FutureCompletingBlockingQueue in project flink by apache.
the class PulsarSourceReaderFactory method create.
@SuppressWarnings("java:S2095")
public static <OUT> SourceReader<OUT, PulsarPartitionSplit> create(SourceReaderContext readerContext, PulsarDeserializationSchema<OUT> deserializationSchema, SourceConfiguration sourceConfiguration) {
PulsarClient pulsarClient = createClient(sourceConfiguration);
PulsarAdmin pulsarAdmin = createAdmin(sourceConfiguration);
// Create a message queue with the predefined source option.
int queueCapacity = sourceConfiguration.getMessageQueueCapacity();
FutureCompletingBlockingQueue<RecordsWithSplitIds<PulsarMessage<OUT>>> elementsQueue = new FutureCompletingBlockingQueue<>(queueCapacity);
// Create different pulsar source reader by subscription type.
SubscriptionType subscriptionType = sourceConfiguration.getSubscriptionType();
if (subscriptionType == SubscriptionType.Failover || subscriptionType == SubscriptionType.Exclusive) {
// Create a ordered split reader supplier.
Supplier<PulsarOrderedPartitionSplitReader<OUT>> splitReaderSupplier = () -> new PulsarOrderedPartitionSplitReader<>(pulsarClient, pulsarAdmin, sourceConfiguration, deserializationSchema);
return new PulsarOrderedSourceReader<>(elementsQueue, splitReaderSupplier, readerContext, sourceConfiguration, pulsarClient, pulsarAdmin);
} else if (subscriptionType == SubscriptionType.Shared || subscriptionType == SubscriptionType.Key_Shared) {
TransactionCoordinatorClient coordinatorClient = ((PulsarClientImpl) pulsarClient).getTcClient();
if (coordinatorClient == null && !sourceConfiguration.isEnableAutoAcknowledgeMessage()) {
throw new IllegalStateException("Transaction is required but didn't enabled");
}
Supplier<PulsarUnorderedPartitionSplitReader<OUT>> splitReaderSupplier = () -> new PulsarUnorderedPartitionSplitReader<>(pulsarClient, pulsarAdmin, sourceConfiguration, deserializationSchema, coordinatorClient);
return new PulsarUnorderedSourceReader<>(elementsQueue, splitReaderSupplier, readerContext, sourceConfiguration, pulsarClient, pulsarAdmin, coordinatorClient);
} else {
throw new UnsupportedOperationException("This subscription type is not " + subscriptionType + " supported currently.");
}
}
use of org.apache.flink.connector.base.source.reader.synchronization.FutureCompletingBlockingQueue in project flink by apache.
the class SourceReaderBaseTest method createReaderAndAwaitAvailable.
// ------------------------------------------------------------------------
// Testing Setup Helpers
// ------------------------------------------------------------------------
private static <E> SourceReader<E, ?> createReaderAndAwaitAvailable(final String splitId, final RecordsWithSplitIds<E> records) throws Exception {
final FutureCompletingBlockingQueue<RecordsWithSplitIds<E>> elementsQueue = new FutureCompletingBlockingQueue<>();
final SourceReader<E, TestingSourceSplit> reader = new SingleThreadMultiplexSourceReaderBase<E, E, TestingSourceSplit, TestingSourceSplit>(elementsQueue, () -> new TestingSplitReader<>(records), new PassThroughRecordEmitter<>(), new Configuration(), new TestingReaderContext()) {
@Override
public void notifyCheckpointComplete(long checkpointId) {
}
@Override
protected void onSplitFinished(Map<String, TestingSourceSplit> finishedSplitIds) {
}
@Override
protected TestingSourceSplit initializedState(TestingSourceSplit split) {
return split;
}
@Override
protected TestingSourceSplit toSplitType(String splitId, TestingSourceSplit splitState) {
return splitState;
}
};
reader.start();
final List<TestingSourceSplit> splits = Collections.singletonList(new TestingSourceSplit(splitId));
reader.addSplits(splits);
reader.isAvailable().get();
return reader;
}
use of org.apache.flink.connector.base.source.reader.synchronization.FutureCompletingBlockingQueue in project flink by apache.
the class SourceReaderBaseTest method createReader.
// ---------------- helper methods -----------------
@Override
protected MockSourceReader createReader() {
FutureCompletingBlockingQueue<RecordsWithSplitIds<int[]>> elementsQueue = new FutureCompletingBlockingQueue<>();
MockSplitReader mockSplitReader = MockSplitReader.newBuilder().setNumRecordsPerSplitPerFetch(2).setBlockingFetch(true).build();
return new MockSourceReader(elementsQueue, () -> mockSplitReader, getConfig(), new TestingReaderContext());
}
use of org.apache.flink.connector.base.source.reader.synchronization.FutureCompletingBlockingQueue in project flink by apache.
the class SourceReaderBaseTest method testMultipleSplitsWithSeparatedFinishedRecord.
@Test
void testMultipleSplitsWithSeparatedFinishedRecord() throws Exception {
FutureCompletingBlockingQueue<RecordsWithSplitIds<int[]>> elementsQueue = new FutureCompletingBlockingQueue<>();
MockSplitReader mockSplitReader = MockSplitReader.newBuilder().setNumRecordsPerSplitPerFetch(2).setSeparatedFinishedRecord(true).setBlockingFetch(false).build();
MockSourceReader reader = new MockSourceReader(elementsQueue, () -> mockSplitReader, getConfig(), new TestingReaderContext());
reader.start();
List<MockSourceSplit> splits = Arrays.asList(getSplit(0, 10, Boundedness.BOUNDED), getSplit(1, 10, Boundedness.BOUNDED));
reader.addSplits(splits);
reader.notifyNoMoreSplits();
while (true) {
InputStatus status = reader.pollNext(new TestingReaderOutput<>());
if (status == InputStatus.END_OF_INPUT) {
break;
}
if (status == InputStatus.NOTHING_AVAILABLE) {
reader.isAvailable().get();
}
}
}
use of org.apache.flink.connector.base.source.reader.synchronization.FutureCompletingBlockingQueue 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();
}
}
Aggregations