Search in sources :

Example 1 with EventHeader

use of com.github.shyiko.mysql.binlog.event.EventHeader in project debezium by debezium.

the class BinlogReader method handleEvent.

protected void handleEvent(Event event) {
    if (event == null)
        return;
    // Update the source offset info. Note that the client returns the value in *milliseconds*, even though the binlog
    // contains only *seconds* precision ...
    EventHeader eventHeader = event.getHeader();
    // client returns milliseconds, but only second
    source.setBinlogTimestampSeconds(eventHeader.getTimestamp() / 1000L);
    // precision
    source.setBinlogServerId(eventHeader.getServerId());
    EventType eventType = eventHeader.getEventType();
    if (eventType == EventType.ROTATE) {
        EventData eventData = event.getData();
        RotateEventData rotateEventData;
        if (eventData instanceof EventDeserializer.EventDataWrapper) {
            rotateEventData = (RotateEventData) ((EventDeserializer.EventDataWrapper) eventData).getInternal();
        } else {
            rotateEventData = (RotateEventData) eventData;
        }
        source.setBinlogStartPoint(rotateEventData.getBinlogFilename(), rotateEventData.getBinlogPosition());
    } else if (eventHeader instanceof EventHeaderV4) {
        EventHeaderV4 trackableEventHeader = (EventHeaderV4) eventHeader;
        source.setEventPosition(trackableEventHeader.getPosition(), trackableEventHeader.getEventLength());
    }
    // If there is a handler for this event, forward the event to it ...
    try {
        // Forward the event to the handler ...
        eventHandlers.getOrDefault(eventType, this::ignoreEvent).accept(event);
        // Generate heartbeat message if the time is right
        heartbeat.heartbeat((BlockingConsumer<SourceRecord>) this::enqueueRecord);
        // Capture that we've completed another event ...
        source.completeEvent();
        if (skipEvent) {
            // We're in the mode of skipping events and we just skipped this one, so decrement our skip count ...
            --initialEventsToSkip;
            skipEvent = initialEventsToSkip > 0;
        }
    } catch (RuntimeException e) {
        // There was an error in the event handler, so propagate the failure to Kafka Connect ...
        logReaderState();
        failed(e, "Error processing binlog event");
        // Do not stop the client, since Kafka Connect should stop the connector on it's own
        // (and doing it here may cause problems the second time it is stopped).
        // We can clear the listeners though so that we ignore all future events ...
        eventHandlers.clear();
        logger.info("Error processing binlog event, and propagating to Kafka Connect so it stops this connector. Future binlog events read before connector is shutdown will be ignored.");
    } catch (InterruptedException e) {
        // Most likely because this reader was stopped and our thread was interrupted ...
        Thread.interrupted();
        eventHandlers.clear();
        logger.info("Stopped processing binlog events due to thread interruption");
    }
}
Also used : BlockingConsumer(io.debezium.function.BlockingConsumer) EventType(com.github.shyiko.mysql.binlog.event.EventType) EventHeaderV4(com.github.shyiko.mysql.binlog.event.EventHeaderV4) EventHeader(com.github.shyiko.mysql.binlog.event.EventHeader) RotateEventData(com.github.shyiko.mysql.binlog.event.RotateEventData) UpdateRowsEventData(com.github.shyiko.mysql.binlog.event.UpdateRowsEventData) TableMapEventData(com.github.shyiko.mysql.binlog.event.TableMapEventData) WriteRowsEventData(com.github.shyiko.mysql.binlog.event.WriteRowsEventData) QueryEventData(com.github.shyiko.mysql.binlog.event.QueryEventData) RotateEventData(com.github.shyiko.mysql.binlog.event.RotateEventData) EventData(com.github.shyiko.mysql.binlog.event.EventData) GtidEventData(com.github.shyiko.mysql.binlog.event.GtidEventData) DeleteRowsEventData(com.github.shyiko.mysql.binlog.event.DeleteRowsEventData)

Aggregations

DeleteRowsEventData (com.github.shyiko.mysql.binlog.event.DeleteRowsEventData)1 EventData (com.github.shyiko.mysql.binlog.event.EventData)1 EventHeader (com.github.shyiko.mysql.binlog.event.EventHeader)1 EventHeaderV4 (com.github.shyiko.mysql.binlog.event.EventHeaderV4)1 EventType (com.github.shyiko.mysql.binlog.event.EventType)1 GtidEventData (com.github.shyiko.mysql.binlog.event.GtidEventData)1 QueryEventData (com.github.shyiko.mysql.binlog.event.QueryEventData)1 RotateEventData (com.github.shyiko.mysql.binlog.event.RotateEventData)1 TableMapEventData (com.github.shyiko.mysql.binlog.event.TableMapEventData)1 UpdateRowsEventData (com.github.shyiko.mysql.binlog.event.UpdateRowsEventData)1 WriteRowsEventData (com.github.shyiko.mysql.binlog.event.WriteRowsEventData)1 BlockingConsumer (io.debezium.function.BlockingConsumer)1