Search in sources :

Example 1 with JdbcConnectionException

use of io.debezium.jdbc.JdbcConnectionException in project debezium by debezium.

the class PostgresReplicationConnection method initReplicationSlot.

protected void initReplicationSlot() throws SQLException {
    final String postgresPluginName = plugin.getPostgresPluginName();
    ServerInfo.ReplicationSlot slotInfo;
    try (PostgresConnection connection = new PostgresConnection(originalConfig)) {
        slotInfo = connection.readReplicationSlotInfo(slotName, postgresPluginName);
    }
    boolean shouldCreateSlot = ServerInfo.ReplicationSlot.INVALID == slotInfo;
    try {
        if (shouldCreateSlot) {
            LOGGER.debug("Creating new replication slot '{}' for plugin '{}'", slotName, plugin);
            // there's no info for this plugin and slot so create a new slot
            pgConnection().getReplicationAPI().createReplicationSlot().logical().withSlotName(slotName).withOutputPlugin(postgresPluginName).make();
        } else if (slotInfo.active()) {
            LOGGER.error("A logical replication slot named '{}' for plugin '{}' and database '{}' is already active on the server." + "You cannot have multiple slots with the same name active for the same database", slotName, postgresPluginName, database());
            throw new IllegalStateException();
        }
        AtomicLong xlogStart = new AtomicLong();
        execute(statement -> {
            String identifySystemStatement = "IDENTIFY_SYSTEM";
            LOGGER.debug("running '{}' to validate replication connection", identifySystemStatement);
            try (ResultSet rs = statement.executeQuery(identifySystemStatement)) {
                if (!rs.next()) {
                    throw new IllegalStateException("The DB connection is not a valid replication connection");
                }
                String xlogpos = rs.getString("xlogpos");
                LOGGER.debug("received latest xlogpos '{}'", xlogpos);
                xlogStart.compareAndSet(0, LogSequenceNumber.valueOf(xlogpos).asLong());
            }
        });
        if (shouldCreateSlot || !slotInfo.hasValidFlushedLSN()) {
            // this is a new slot or we weren't able to read a valid flush LSN pos, so we always start from the xlog pos that was reported
            this.defaultStartingPos = xlogStart.get();
        } else {
            Long latestFlushedLSN = slotInfo.latestFlushedLSN();
            this.defaultStartingPos = latestFlushedLSN < xlogStart.get() ? latestFlushedLSN : xlogStart.get();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("found previous flushed LSN '{}'", ReplicationConnection.format(latestFlushedLSN));
            }
        }
    } catch (SQLException e) {
        throw new JdbcConnectionException(e);
    }
}
Also used : JdbcConnectionException(io.debezium.jdbc.JdbcConnectionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) PSQLException(org.postgresql.util.PSQLException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

JdbcConnectionException (io.debezium.jdbc.JdbcConnectionException)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 PSQLException (org.postgresql.util.PSQLException)1