Search in sources :

Example 6 with PreviousContext

use of io.debezium.util.LoggingContext.PreviousContext in project debezium by debezium.

the class ChangeEventQueue method poll.

/**
 * Returns the next batch of elements from this queue. May be empty in case no
 * elements have arrived in the maximum waiting time.
 *
 * @throws InterruptedException
 *             if this thread has been interrupted while waiting for more
 *             elements to arrive
 */
public List<T> poll() throws InterruptedException {
    LoggingContext.PreviousContext previousContext = loggingContextSupplier.get();
    try {
        LOGGER.debug("polling records...");
        List<T> records = new ArrayList<>();
        final Timer timeout = Threads.timer(Clock.SYSTEM, Temporals.max(pollInterval, ConfigurationDefaults.RETURN_CONTROL_INTERVAL));
        while (!timeout.expired() && queue.drainTo(records, maxBatchSize) == 0) {
            throwProducerFailureIfPresent();
            LOGGER.debug("no records available yet, sleeping a bit...");
            // no records yet, so wait a bit
            metronome.pause();
            LOGGER.debug("checking for more records...");
        }
        return records;
    } finally {
        previousContext.restore();
    }
}
Also used : Timer(io.debezium.util.Threads.Timer) LoggingContext(io.debezium.util.LoggingContext) PreviousContext(io.debezium.util.LoggingContext.PreviousContext) ArrayList(java.util.ArrayList)

Example 7 with PreviousContext

use of io.debezium.util.LoggingContext.PreviousContext in project debezium by debezium.

the class MySqlConnectorTask method poll.

@Override
public List<SourceRecord> poll() throws InterruptedException {
    Reader currentReader = readers;
    if (currentReader == null) {
        return null;
    }
    PreviousContext prevLoggingContext = this.taskContext.configureLoggingContext("task");
    try {
        logger.trace("Polling for events");
        return currentReader.poll();
    } finally {
        prevLoggingContext.restore();
    }
}
Also used : PreviousContext(io.debezium.util.LoggingContext.PreviousContext)

Example 8 with PreviousContext

use of io.debezium.util.LoggingContext.PreviousContext in project debezium by debezium.

the class MySqlConnectorTask method completeReaders.

/**
 * When the task is {@link #stop() stopped}, the readers may have additional work to perform before they actually
 * stop and before all their records have been consumed via the {@link #poll()} method. This method signals that
 * all of this has completed.
 */
protected void completeReaders() {
    PreviousContext prevLoggingContext = this.taskContext.configureLoggingContext("task");
    try {
        // Flush and stop database history, close all JDBC connections ...
        if (this.taskContext != null)
            taskContext.shutdown();
    } catch (Throwable e) {
        logger.error("Unexpected error shutting down the database history and/or closing JDBC connections", e);
    } finally {
        context = null;
        logger.info("Connector task finished all work and is now shutdown");
        prevLoggingContext.restore();
    }
}
Also used : PreviousContext(io.debezium.util.LoggingContext.PreviousContext)

Example 9 with PreviousContext

use of io.debezium.util.LoggingContext.PreviousContext in project debezium by debezium.

the class MongoDbConnector method start.

@Override
public void start(Map<String, String> props) {
    // Validate the configuration ...
    final Configuration config = Configuration.from(props);
    if (!config.validateAndRecord(MongoDbConnectorConfig.ALL_FIELDS, logger::error)) {
        throw new ConnectException("Error configuring an instance of " + getClass().getSimpleName() + "; check the logs for details");
    }
    this.config = config;
    // Set up the replication context ...
    taskContext = new MongoDbTaskContext(config);
    this.connectionContext = taskContext.getConnectionContext();
    PreviousContext previousLogContext = taskContext.configureLoggingContext("conn");
    try {
        logger.info("Starting MongoDB connector and discovering replica set(s) at {}", connectionContext.hosts());
        // Set up and start the thread that monitors the members of all of the replica sets ...
        replicaSetMonitorExecutor = Threads.newSingleThreadExecutor(MongoDbConnector.class, taskContext.serverName(), "replica-set-monitor");
        ReplicaSetDiscovery monitor = new ReplicaSetDiscovery(taskContext);
        monitorThread = new ReplicaSetMonitorThread(monitor::getReplicaSets, connectionContext.pollPeriodInSeconds(), TimeUnit.SECONDS, Clock.SYSTEM, () -> taskContext.configureLoggingContext("disc"), this::replicaSetsChanged);
        replicaSetMonitorExecutor.execute(monitorThread);
        logger.info("Successfully started MongoDB connector, and continuing to discover changes in replica sets", connectionContext.hosts());
    } finally {
        previousLogContext.restore();
    }
}
Also used : PreviousContext(io.debezium.util.LoggingContext.PreviousContext) Configuration(io.debezium.config.Configuration) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 10 with PreviousContext

use of io.debezium.util.LoggingContext.PreviousContext in project debezium by debezium.

the class MongoDbConnectorTask method start.

@Override
public void start(Configuration config) {
    if (!this.running.compareAndSet(false, true)) {
        // Already running ...
        return;
    }
    // Read the configuration and set up the replication context ...
    this.taskName = "task" + config.getInteger(MongoDbConnectorConfig.TASK_ID);
    final MongoDbTaskContext taskContext = new MongoDbTaskContext(config);
    this.taskContext = taskContext;
    PreviousContext previousLogContext = taskContext.configureLoggingContext(taskName);
    try {
        // Read from the configuration the information about the replica sets we are to watch ...
        final String hosts = config.getString(MongoDbConnectorConfig.HOSTS);
        final ReplicaSets replicaSets = ReplicaSets.parse(hosts);
        if (replicaSets.validReplicaSetCount() == 0) {
            throw new ConnectException("Unable to start MongoDB connector task since no replica sets were found at " + hosts);
        }
        MongoDbConnectorConfig connectorConfig = new MongoDbConnectorConfig(config);
        // Set up the task record queue ...
        this.queue = new ChangeEventQueue.Builder<SourceRecord>().pollInterval(connectorConfig.getPollInterval()).maxBatchSize(connectorConfig.getMaxBatchSize()).maxQueueSize(connectorConfig.getMaxQueueSize()).loggingContextSupplier(this::getLoggingContext).build();
        // Get the offsets for each of replica set partition ...
        SourceInfo source = taskContext.source();
        Collection<Map<String, String>> partitions = new ArrayList<>();
        replicaSets.onEachReplicaSet(replicaSet -> {
            // may be null for standalone servers
            String replicaSetName = replicaSet.replicaSetName();
            if (replicaSetName != null) {
                partitions.add(source.partition(replicaSetName));
            }
        });
        context.offsetStorageReader().offsets(partitions).forEach(source::setOffsetFor);
        // Set up a replicator for each replica set ...
        final int numThreads = replicaSets.replicaSetCount();
        final ExecutorService executor = Threads.newFixedThreadPool(MongoDbConnector.class, taskContext.serverName(), "replicator", numThreads);
        AtomicInteger stillRunning = new AtomicInteger(numThreads);
        logger.info("Ignoring unnamed replica sets: {}", replicaSets.unnamedReplicaSets());
        logger.info("Starting {} thread(s) to replicate replica sets: {}", numThreads, replicaSets);
        replicaSets.validReplicaSets().forEach(replicaSet -> {
            // Create a replicator for this replica set ...
            Replicator replicator = new Replicator(taskContext, replicaSet, queue::enqueue, this::failedReplicator);
            replicators.add(replicator);
            // and submit it for execution ...
            executor.submit(() -> {
                try {
                    // Configure the logging to use the replica set name ...
                    taskContext.configureLoggingContext(replicaSet.replicaSetName());
                    // Run the replicator, which should run forever until it is stopped ...
                    replicator.run();
                } finally {
                    try {
                        replicators.remove(replicator);
                    } finally {
                        if (stillRunning.decrementAndGet() == 0) {
                            // we are the last one, so clean up ...
                            try {
                                executor.shutdown();
                            } finally {
                                taskContext.getConnectionContext().shutdown();
                            }
                        }
                    }
                }
            });
        });
        logger.info("Successfully started MongoDB connector task with {} thread(s) for replica sets {}", numThreads, replicaSets);
    } finally {
        previousLogContext.restore();
    }
}
Also used : ArrayList(java.util.ArrayList) PreviousContext(io.debezium.util.LoggingContext.PreviousContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) HashMap(java.util.HashMap) Map(java.util.Map) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Aggregations

PreviousContext (io.debezium.util.LoggingContext.PreviousContext)11 ArrayList (java.util.ArrayList)4 ConnectException (org.apache.kafka.connect.errors.ConnectException)4 Map (java.util.Map)3 Configuration (io.debezium.config.Configuration)2 LoggingContext (io.debezium.util.LoggingContext)2 HashMap (java.util.HashMap)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SchemaUtil (io.debezium.data.SchemaUtil)1 VerifyRecord (io.debezium.data.VerifyRecord)1 RecordValueComparator (io.debezium.data.VerifyRecord.RecordValueComparator)1 Array (io.debezium.document.Array)1 ArrayReader (io.debezium.document.ArrayReader)1 ArrayWriter (io.debezium.document.ArrayWriter)1 Document (io.debezium.document.Document)1 DocumentReader (io.debezium.document.DocumentReader)1 Value (io.debezium.document.Value)1 CompletionCallback (io.debezium.embedded.EmbeddedEngine.CompletionCallback)1 CompletionResult (io.debezium.embedded.EmbeddedEngine.CompletionResult)1