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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations