Search in sources :

Example 1 with Watermark

use of org.apache.flink.api.common.eventtime.Watermark in project flink by apache.

the class SourceCoordinator method handleEventFromOperator.

@Override
public void handleEventFromOperator(int subtask, OperatorEvent event) {
    runInEventLoop(() -> {
        if (event instanceof RequestSplitEvent) {
            LOG.info("Source {} received split request from parallel task {}", operatorName, subtask);
            enumerator.handleSplitRequest(subtask, ((RequestSplitEvent) event).hostName());
        } else if (event instanceof SourceEventWrapper) {
            final SourceEvent sourceEvent = ((SourceEventWrapper) event).getSourceEvent();
            LOG.debug("Source {} received custom event from parallel task {}: {}", operatorName, subtask, sourceEvent);
            enumerator.handleSourceEvent(subtask, sourceEvent);
        } else if (event instanceof ReaderRegistrationEvent) {
            final ReaderRegistrationEvent registrationEvent = (ReaderRegistrationEvent) event;
            LOG.info("Source {} registering reader for parallel task {} @ {}", operatorName, subtask, registrationEvent.location());
            handleReaderRegistrationEvent(registrationEvent);
        } else if (event instanceof ReportedWatermarkEvent) {
            handleReportedWatermark(subtask, new Watermark(((ReportedWatermarkEvent) event).getWatermark()));
        } else {
            throw new FlinkException("Unrecognized Operator Event: " + event);
        }
    }, "handling operator event %s from subtask %d", event, subtask);
}
Also used : RequestSplitEvent(org.apache.flink.runtime.source.event.RequestSplitEvent) SourceEventWrapper(org.apache.flink.runtime.source.event.SourceEventWrapper) ReaderRegistrationEvent(org.apache.flink.runtime.source.event.ReaderRegistrationEvent) Watermark(org.apache.flink.api.common.eventtime.Watermark) FlinkException(org.apache.flink.util.FlinkException) SourceEvent(org.apache.flink.api.connector.source.SourceEvent) ReportedWatermarkEvent(org.apache.flink.runtime.source.event.ReportedWatermarkEvent)

Example 2 with Watermark

use of org.apache.flink.api.common.eventtime.Watermark in project flink by apache.

the class SourceCoordinator method announceCombinedWatermark.

@VisibleForTesting
void announceCombinedWatermark() {
    checkState(watermarkAlignmentParams != WatermarkAlignmentParams.WATERMARK_ALIGNMENT_DISABLED);
    Watermark globalCombinedWatermark = coordinatorStore.apply(watermarkAlignmentParams.getWatermarkGroup(), (value) -> {
        WatermarkAggregator aggregator = (WatermarkAggregator) value;
        return new Watermark(aggregator.getAggregatedWatermark().getTimestamp());
    });
    long maxAllowedWatermark = globalCombinedWatermark.getTimestamp() + watermarkAlignmentParams.getMaxAllowedWatermarkDrift();
    Set<Integer> subTaskIds = combinedWatermark.keySet();
    LOG.info("Distributing maxAllowedWatermark={} to subTaskIds={}", maxAllowedWatermark, subTaskIds);
    for (Integer subtaskId : subTaskIds) {
        context.sendEventToSourceOperator(subtaskId, new WatermarkAlignmentEvent(maxAllowedWatermark));
    }
}
Also used : WatermarkAlignmentEvent(org.apache.flink.runtime.source.event.WatermarkAlignmentEvent) Watermark(org.apache.flink.api.common.eventtime.Watermark) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting)

Example 3 with Watermark

use of org.apache.flink.api.common.eventtime.Watermark in project flink by apache.

the class SourceOperatorAlignmentTest method testWatermarkAlignmentWithIdleness.

@Test
public void testWatermarkAlignmentWithIdleness() throws Exception {
    // we use a separate context, because we need to enable idleness
    try (SourceOperatorTestContext context = new SourceOperatorTestContext(true, WatermarkStrategy.forGenerator(ctx -> new PunctuatedGenerator(PunctuatedGenerator.GenerationMode.ODD)).withWatermarkAlignment("group1", Duration.ofMillis(100), Duration.ofMillis(1)).withTimestampAssigner((r, t) -> r))) {
        final SourceOperator<Integer, MockSourceSplit> operator = context.getOperator();
        operator.initializeState(context.createStateContext());
        operator.open();
        MockSourceSplit newSplit = new MockSourceSplit(2);
        int record1 = 1;
        newSplit.addRecord(record1);
        operator.handleOperatorEvent(new AddSplitEvent<>(Collections.singletonList(newSplit), new MockSourceSplitSerializer()));
        CollectingDataOutput<Integer> actualOutput = new CollectingDataOutput<>();
        List<Integer> expectedOutput = new ArrayList<>();
        assertThat(operator.emitNext(actualOutput), is(DataInputStatus.MORE_AVAILABLE));
        expectedOutput.add(record1);
        context.getTimeService().advance(1);
        assertLatestReportedWatermarkEvent(context, record1);
        assertOutput(actualOutput, expectedOutput);
        assertTrue(operator.isAvailable());
        // source becomes idle, it should report Long.MAX_VALUE as the watermark
        assertThat(operator.emitNext(actualOutput), is(DataInputStatus.NOTHING_AVAILABLE));
        context.getTimeService().advance(1);
        assertLatestReportedWatermarkEvent(context, Long.MAX_VALUE);
        // it is easier to create a new split than add records the old one. The old one is
        // serialized, when sending the AddSplitEvent, so it is not as easy as
        // newSplit.addRecord
        newSplit = new MockSourceSplit(3);
        // even timestamp -> no watermarks
        int record2 = 2;
        newSplit.addRecord(record2);
        operator.handleOperatorEvent(new AddSplitEvent<>(Collections.singletonList(newSplit), new MockSourceSplitSerializer()));
        assertThat(operator.emitNext(actualOutput), is(DataInputStatus.MORE_AVAILABLE));
        expectedOutput.add(record2);
        context.getTimeService().advance(1);
        // becomes active again, should go back to the previously emitted
        // watermark, as the record2 does not emit watermarks
        assertLatestReportedWatermarkEvent(context, record1);
        assertOutput(actualOutput, expectedOutput);
        assertTrue(operator.isAvailable());
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) BeforeEach(org.junit.jupiter.api.BeforeEach) ReportedWatermarkEvent(org.apache.flink.runtime.source.event.ReportedWatermarkEvent) WatermarkGenerator(org.apache.flink.api.common.eventtime.WatermarkGenerator) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) MockSourceSplit(org.apache.flink.api.connector.source.mocks.MockSourceSplit) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) DataInputStatus(org.apache.flink.streaming.runtime.io.DataInputStatus) Duration(java.time.Duration) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) WatermarkAlignmentEvent(org.apache.flink.runtime.source.event.WatermarkAlignmentEvent) Nullable(javax.annotation.Nullable) WatermarkStrategy(org.apache.flink.api.common.eventtime.WatermarkStrategy) MockSourceSplitSerializer(org.apache.flink.api.connector.source.mocks.MockSourceSplitSerializer) AddSplitEvent(org.apache.flink.runtime.source.event.AddSplitEvent) WatermarkOutput(org.apache.flink.api.common.eventtime.WatermarkOutput) Collectors(java.util.stream.Collectors) StopMode(org.apache.flink.runtime.io.network.api.StopMode) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Watermark(org.apache.flink.api.common.eventtime.Watermark) CollectingDataOutput(org.apache.flink.streaming.api.operators.source.CollectingDataOutput) Collections(java.util.Collections) OperatorEvent(org.apache.flink.runtime.operators.coordination.OperatorEvent) ArrayList(java.util.ArrayList) CollectingDataOutput(org.apache.flink.streaming.api.operators.source.CollectingDataOutput) MockSourceSplitSerializer(org.apache.flink.api.connector.source.mocks.MockSourceSplitSerializer) MockSourceSplit(org.apache.flink.api.connector.source.mocks.MockSourceSplit) Test(org.junit.jupiter.api.Test)

Aggregations

Watermark (org.apache.flink.api.common.eventtime.Watermark)3 ReportedWatermarkEvent (org.apache.flink.runtime.source.event.ReportedWatermarkEvent)2 WatermarkAlignmentEvent (org.apache.flink.runtime.source.event.WatermarkAlignmentEvent)2 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 VisibleForTesting (org.apache.flink.annotation.VisibleForTesting)1 WatermarkGenerator (org.apache.flink.api.common.eventtime.WatermarkGenerator)1 WatermarkOutput (org.apache.flink.api.common.eventtime.WatermarkOutput)1 WatermarkStrategy (org.apache.flink.api.common.eventtime.WatermarkStrategy)1 SourceEvent (org.apache.flink.api.connector.source.SourceEvent)1 MockSourceSplit (org.apache.flink.api.connector.source.mocks.MockSourceSplit)1 MockSourceSplitSerializer (org.apache.flink.api.connector.source.mocks.MockSourceSplitSerializer)1 StopMode (org.apache.flink.runtime.io.network.api.StopMode)1 OperatorEvent (org.apache.flink.runtime.operators.coordination.OperatorEvent)1 AddSplitEvent (org.apache.flink.runtime.source.event.AddSplitEvent)1