Search in sources :

Example 1 with CDCException

use of org.apache.nifi.cdc.CDCException in project nifi by apache.

the class CaptureChangeMySQL method stop.

protected void stop(StateManager stateManager) throws CDCException {
    try {
        if (binlogClient != null) {
            binlogClient.disconnect();
        }
        if (eventListener != null) {
            eventListener.stop();
            if (binlogClient != null) {
                binlogClient.unregisterEventListener(eventListener);
            }
        }
        doStop.set(true);
        if (hasRun.getAndSet(false)) {
            updateState(stateManager, currentBinlogFile, currentBinlogPosition, currentSequenceId.get());
        }
        currentBinlogPosition = -1;
    } catch (IOException e) {
        throw new CDCException("Error closing CDC connection", e);
    } finally {
        binlogClient = null;
    }
}
Also used : CDCException(org.apache.nifi.cdc.CDCException) IOException(java.io.IOException)

Example 2 with CDCException

use of org.apache.nifi.cdc.CDCException in project nifi by apache.

the class CaptureChangeMySQL method onTrigger.

@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
    // Indicate that this processor has executed at least once, so we know whether or not the state values are valid and should be updated
    hasRun.set(true);
    ComponentLog log = getLogger();
    StateManager stateManager = context.getStateManager();
    // Create a client if we don't have one
    if (binlogClient == null) {
        setup(context);
    }
    // If the client has been disconnected, try to reconnect
    if (!binlogClient.isConnected()) {
        Exception e = lifecycleListener.getException();
        // If there's no exception, the listener callback might not have been executed yet, so try again later. Otherwise clean up and start over next time
        if (e != null) {
            // Communications failure, disconnect and try next time
            log.error("Binlog connector communications failure: " + e.getMessage(), e);
            try {
                stop(stateManager);
            } catch (CDCException ioe) {
                throw new ProcessException(ioe);
            }
        }
        // Try again later
        context.yield();
        return;
    }
    if (currentSession == null) {
        currentSession = sessionFactory.createSession();
    }
    try {
        outputEvents(currentSession, stateManager, log);
        long now = System.currentTimeMillis();
        long timeSinceLastUpdate = now - lastStateUpdate;
        if (stateUpdateInterval != 0 && timeSinceLastUpdate >= stateUpdateInterval) {
            updateState(stateManager, currentBinlogFile, currentBinlogPosition, currentSequenceId.get());
            lastStateUpdate = now;
        }
    } catch (IOException ioe) {
        try {
            // Perform some processor-level "rollback", then rollback the session
            currentBinlogFile = xactBinlogFile == null ? "" : xactBinlogFile;
            currentBinlogPosition = xactBinlogPosition;
            currentSequenceId.set(xactSequenceId);
            inTransaction = false;
            stop(stateManager);
            queue.clear();
            currentSession.rollback();
        } catch (Exception e) {
            // Not much we can recover from here
            log.warn("Error occurred during rollback", e);
        }
        throw new ProcessException(ioe);
    }
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) StateManager(org.apache.nifi.components.state.StateManager) CDCException(org.apache.nifi.cdc.CDCException) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) InitializationException(org.apache.nifi.reporting.InitializationException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) TimeoutException(java.util.concurrent.TimeoutException) RowEventException(org.apache.nifi.cdc.event.RowEventException) CDCException(org.apache.nifi.cdc.CDCException) ProcessException(org.apache.nifi.processor.exception.ProcessException) SQLException(java.sql.SQLException) ConnectException(java.net.ConnectException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 CDCException (org.apache.nifi.cdc.CDCException)2 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 SQLException (java.sql.SQLException)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 TimeoutException (java.util.concurrent.TimeoutException)1 RowEventException (org.apache.nifi.cdc.event.RowEventException)1 StateManager (org.apache.nifi.components.state.StateManager)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 InitializationException (org.apache.nifi.reporting.InitializationException)1