Search in sources :

Example 1 with ReaderRegistrationEvent

use of org.apache.flink.runtime.source.event.ReaderRegistrationEvent 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 ReaderRegistrationEvent

use of org.apache.flink.runtime.source.event.ReaderRegistrationEvent in project flink by apache.

the class SourceOperatorTest method testOpen.

@Test
public void testOpen() throws Exception {
    // Initialize the operator.
    operator.initializeState(context.createStateContext());
    // Open the operator.
    operator.open();
    // The source reader should have been assigned a split.
    assertEquals(Collections.singletonList(SourceOperatorTestContext.MOCK_SPLIT), mockSourceReader.getAssignedSplits());
    // The source reader should have started.
    assertTrue(mockSourceReader.isStarted());
    // A ReaderRegistrationRequest should have been sent.
    assertEquals(1, mockGateway.getEventsSent().size());
    OperatorEvent operatorEvent = mockGateway.getEventsSent().get(0);
    assertTrue(operatorEvent instanceof ReaderRegistrationEvent);
    assertEquals(SourceOperatorTestContext.SUBTASK_INDEX, ((ReaderRegistrationEvent) operatorEvent).subtaskId());
}
Also used : ReaderRegistrationEvent(org.apache.flink.runtime.source.event.ReaderRegistrationEvent) OperatorEvent(org.apache.flink.runtime.operators.coordination.OperatorEvent) Test(org.junit.Test)

Example 3 with ReaderRegistrationEvent

use of org.apache.flink.runtime.source.event.ReaderRegistrationEvent in project flink by apache.

the class SourceCoordinatorContextTest method registerReaders.

// ------------------------
private List<ReaderInfo> registerReaders() {
    final List<ReaderInfo> infos = Arrays.asList(new ReaderInfo(0, "subtask_0_location"), new ReaderInfo(1, "subtask_1_location"), new ReaderInfo(2, "subtask_2_location"));
    for (ReaderInfo info : infos) {
        sourceCoordinator.handleEventFromOperator(info.getSubtaskId(), new ReaderRegistrationEvent(info.getSubtaskId(), info.getLocation()));
    }
    waitForCoordinatorToProcessActions();
    return infos;
}
Also used : ReaderInfo(org.apache.flink.api.connector.source.ReaderInfo) ReaderRegistrationEvent(org.apache.flink.runtime.source.event.ReaderRegistrationEvent)

Example 4 with ReaderRegistrationEvent

use of org.apache.flink.runtime.source.event.ReaderRegistrationEvent in project flink by apache.

the class SourceCoordinatorProviderTest method testCheckpointAndReset.

@Test
public void testCheckpointAndReset() throws Exception {
    final OperatorCoordinator.Context context = new MockOperatorCoordinatorContext(OPERATOR_ID, NUM_SPLITS);
    final RecreateOnResetOperatorCoordinator coordinator = (RecreateOnResetOperatorCoordinator) provider.create(context);
    final SourceCoordinator<?, ?> sourceCoordinator = (SourceCoordinator<?, ?>) coordinator.getInternalCoordinator();
    // Start the coordinator.
    coordinator.start();
    // register reader 0 and take a checkpoint.
    coordinator.handleEventFromOperator(0, new ReaderRegistrationEvent(0, "location"));
    CompletableFuture<byte[]> future = new CompletableFuture<>();
    coordinator.checkpointCoordinator(0L, future);
    byte[] bytes = future.get();
    // Register reader 1.
    coordinator.handleEventFromOperator(1, new ReaderRegistrationEvent(1, "location"));
    // Wait until the coordinator context is updated with registration of reader 1.
    while (sourceCoordinator.getContext().registeredReaders().size() < 2) {
        Thread.sleep(1);
    }
    // reset the coordinator to the checkpoint which only contains reader 0.
    coordinator.resetToCheckpoint(0L, bytes);
    final SourceCoordinator<?, ?> restoredSourceCoordinator = (SourceCoordinator<?, ?>) coordinator.getInternalCoordinator();
    assertNotEquals("The restored source coordinator should be a different instance", restoredSourceCoordinator, sourceCoordinator);
    // FLINK-21452: do not (re)store registered readers
    assertEquals("There should be no registered reader.", 0, restoredSourceCoordinator.getContext().registeredReaders().size());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) MockOperatorCoordinatorContext(org.apache.flink.runtime.operators.coordination.MockOperatorCoordinatorContext) RecreateOnResetOperatorCoordinator(org.apache.flink.runtime.operators.coordination.RecreateOnResetOperatorCoordinator) OperatorCoordinator(org.apache.flink.runtime.operators.coordination.OperatorCoordinator) RecreateOnResetOperatorCoordinator(org.apache.flink.runtime.operators.coordination.RecreateOnResetOperatorCoordinator) ReaderRegistrationEvent(org.apache.flink.runtime.source.event.ReaderRegistrationEvent) Test(org.junit.Test)

Aggregations

ReaderRegistrationEvent (org.apache.flink.runtime.source.event.ReaderRegistrationEvent)4 Test (org.junit.Test)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 Watermark (org.apache.flink.api.common.eventtime.Watermark)1 ReaderInfo (org.apache.flink.api.connector.source.ReaderInfo)1 SourceEvent (org.apache.flink.api.connector.source.SourceEvent)1 MockOperatorCoordinatorContext (org.apache.flink.runtime.operators.coordination.MockOperatorCoordinatorContext)1 OperatorCoordinator (org.apache.flink.runtime.operators.coordination.OperatorCoordinator)1 OperatorEvent (org.apache.flink.runtime.operators.coordination.OperatorEvent)1 RecreateOnResetOperatorCoordinator (org.apache.flink.runtime.operators.coordination.RecreateOnResetOperatorCoordinator)1 ReportedWatermarkEvent (org.apache.flink.runtime.source.event.ReportedWatermarkEvent)1 RequestSplitEvent (org.apache.flink.runtime.source.event.RequestSplitEvent)1 SourceEventWrapper (org.apache.flink.runtime.source.event.SourceEventWrapper)1 FlinkException (org.apache.flink.util.FlinkException)1