Search in sources :

Example 11 with BinlogFileOffset

use of io.eventuate.local.common.BinlogFileOffset in project eventuate-local by eventuate-local.

the class PostgresWalClient method connectAndRun.

private void connectAndRun(Optional<BinlogFileOffset> binlogFileOffset, Consumer<EVENT> eventConsumer) throws SQLException, InterruptedException, IOException {
    countDownLatchForStop = new CountDownLatch(1);
    Properties props = new Properties();
    PGProperty.USER.set(props, user);
    PGProperty.PASSWORD.set(props, password);
    PGProperty.ASSUME_MIN_SERVER_VERSION.set(props, "9.4");
    PGProperty.REPLICATION.set(props, "database");
    PGProperty.PREFER_QUERY_MODE.set(props, "simple");
    connection = DriverManager.getConnection(url, props);
    PGConnection replConnection = connection.unwrap(PGConnection.class);
    LogSequenceNumber lsn = binlogFileOffset.flatMap(offset -> Optional.ofNullable(offset.getOffset()).map(LogSequenceNumber::valueOf)).orElse(LogSequenceNumber.valueOf("0/0"));
    stream = replConnection.getReplicationAPI().replicationStream().logical().withSlotName(replicationSlotName).withSlotOption("include-xids", false).withStatusInterval(replicationStatusIntervalInMilliseconds, TimeUnit.MILLISECONDS).withStartPosition(lsn).start();
    logger.info("connection to postgres wal succeed");
    while (running) {
        ByteBuffer messageBuffer = stream.readPending();
        if (messageBuffer == null) {
            logger.info("Got empty message, sleeping");
            TimeUnit.MILLISECONDS.sleep(walIntervalInMilliseconds);
            continue;
        }
        String messageString = extractStringFromBuffer(messageBuffer);
        logger.info("Got message: " + messageString);
        postgresWalMessageParser.parse(JSonMapper.fromJson(messageString, PostgresWalMessage.class), stream.getLastReceiveLSN().asLong(), replicationSlotName).forEach(eventConsumer);
        stream.setAppliedLSN(stream.getLastReceiveLSN());
        stream.setFlushedLSN(stream.getLastReceiveLSN());
    }
    try {
        stream.close();
        connection.close();
    } catch (SQLException e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
    countDownLatchForStop.countDown();
}
Also used : LogSequenceNumber(org.postgresql.replication.LogSequenceNumber) BinLogEvent(io.eventuate.local.common.BinLogEvent) Logger(org.slf4j.Logger) Connection(java.sql.Connection) Properties(java.util.Properties) BinlogFileOffset(io.eventuate.local.common.BinlogFileOffset) DbLogClient(io.eventuate.local.db.log.common.DbLogClient) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) JSonMapper(io.eventuate.javaclient.commonimpl.JSonMapper) PGProperty(org.postgresql.PGProperty) PGConnection(org.postgresql.PGConnection) ByteBuffer(java.nio.ByteBuffer) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) SQLException(java.sql.SQLException) PGReplicationStream(org.postgresql.replication.PGReplicationStream) Optional(java.util.Optional) DriverManager(java.sql.DriverManager) PGConnection(org.postgresql.PGConnection) SQLException(java.sql.SQLException) LogSequenceNumber(org.postgresql.replication.LogSequenceNumber) CountDownLatch(java.util.concurrent.CountDownLatch) Properties(java.util.Properties) ByteBuffer(java.nio.ByteBuffer)

Example 12 with BinlogFileOffset

use of io.eventuate.local.common.BinlogFileOffset in project eventuate-tram-core by eventuate-tram.

the class WriteRowsEventDataParser method parseEventData.

@Override
public MessageWithDestination parseEventData(WriteRowsEventData eventData, String binlogFilename, long position) throws IOException {
    if (columnOrders.isEmpty()) {
        try {
            getColumnOrders();
        } catch (SQLException e) {
            logger.error("Error getting metadata", e);
            throw new RuntimeException(e);
        }
    }
    String id = (String) getValue(eventData, ID);
    String destination = (String) getValue(eventData, DESTINATION);
    String payload = (String) getValue(eventData, PAYLOAD);
    Map<String, String> headers = JSonMapper.fromJson((String) getValue(eventData, HEADERS), Map.class);
    headers.put(Message.ID, id);
    headers.put("binlogfile", binlogFilename);
    headers.put("binlogposition", Long.toString(position));
    return new MessageWithDestination(destination, new MessageImpl(payload, headers), new BinlogFileOffset(binlogFilename, position));
}
Also used : BinlogFileOffset(io.eventuate.local.common.BinlogFileOffset) SQLException(java.sql.SQLException) MessageImpl(io.eventuate.tram.messaging.common.MessageImpl)

Aggregations

BinlogFileOffset (io.eventuate.local.common.BinlogFileOffset)12 AbstractCdcTest (io.eventuate.local.test.util.AbstractCdcTest)4 Test (org.junit.Test)4 DuplicatePublishingDetector (io.eventuate.local.db.log.common.DuplicatePublishingDetector)3 JSonMapper (io.eventuate.javaclient.commonimpl.JSonMapper)2 PublishedEvent (io.eventuate.local.common.PublishedEvent)2 DatabaseOffsetKafkaStore (io.eventuate.local.db.log.common.DatabaseOffsetKafkaStore)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 BinaryLogClient (com.github.shyiko.mysql.binlog.BinaryLogClient)1 BinLogEvent (io.eventuate.local.common.BinLogEvent)1 DbLogClient (io.eventuate.local.db.log.common.DbLogClient)1 ConsumerPropertiesFactory (io.eventuate.local.java.kafka.consumer.ConsumerPropertiesFactory)1 EventuateKafkaConsumer (io.eventuate.local.java.kafka.consumer.EventuateKafkaConsumer)1 MessageImpl (io.eventuate.tram.messaging.common.MessageImpl)1 ByteBuffer (java.nio.ByteBuffer)1 Connection (java.sql.Connection)1 DriverManager (java.sql.DriverManager)1