Search in sources :

Example 1 with Timer

use of io.debezium.util.Threads.Timer in project debezium by debezium.

the class AbstractReader method poll.

@Override
public List<SourceRecord> poll() throws InterruptedException {
    // Before we do anything else, determine if there was a failure and throw that exception ...
    failureException = this.failure.get();
    if (failureException != null) {
        // Regardless, there may be records on the queue that will never be consumed.
        throw failureException;
    }
    // this reader has been stopped before it reached the success or failed end state, so clean up and abort
    if (!running.get()) {
        cleanupResources();
        throw new InterruptedException("Reader was stopped while polling");
    }
    logger.trace("Polling for next batch of records");
    List<SourceRecord> batch = new ArrayList<>(maxBatchSize);
    final Timer timeout = Threads.timer(Clock.SYSTEM, Temporals.max(pollInterval, ConfigurationDefaults.RETURN_CONTROL_INTERVAL));
    while (running.get() && (records.drainTo(batch, maxBatchSize) == 0) && !success.get()) {
        // No records are available even though the snapshot has not yet completed, so sleep for a bit ...
        metronome.pause();
        // Check for failure after waking up ...
        failureException = this.failure.get();
        if (failureException != null)
            throw failureException;
        if (timeout.expired()) {
            break;
        }
    }
    if (batch.isEmpty() && success.get() && records.isEmpty()) {
        // We found no records but the operation completed successfully, so we're done
        this.running.set(false);
        cleanupResources();
        return null;
    }
    pollComplete(batch);
    logger.trace("Completed batch of {} records", batch.size());
    return batch;
}
Also used : Timer(io.debezium.util.Threads.Timer) ArrayList(java.util.ArrayList) SourceRecord(org.apache.kafka.connect.source.SourceRecord)

Example 2 with Timer

use of io.debezium.util.Threads.Timer 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 3 with Timer

use of io.debezium.util.Threads.Timer in project debezium by debezium.

the class ChainedReaderTest method shouldStartAndStopMultipleReaders.

@Test
public void shouldStartAndStopMultipleReaders() throws InterruptedException {
    reader = new ChainedReader.Builder().addReader(new MockReader("r3", records())).addReader(new MockReader("r4", records())).completionMessage("Stopped the r3+r4 reader").build();
    reader.start();
    assertThat(reader.state()).isEqualTo(State.RUNNING);
    assertThat(reader.poll()).isSameAs(RL1);
    assertThat(reader.poll()).isSameAs(RL2);
    assertThat(reader.poll()).isSameAs(RL3);
    assertThat(reader.poll()).isSameAs(RL4);
    assertThat(reader.poll()).isSameAs(RL5);
    // Wait for 2nd reader to start
    List<SourceRecord> records = reader.poll();
    final Timer timeout = Threads.timer(Clock.SYSTEM, ConfigurationDefaults.RETURN_CONTROL_INTERVAL);
    while (records == null) {
        if (timeout.expired()) {
            Assert.fail("Subsequent reader has not started");
        }
        Thread.sleep(100);
        records = reader.poll();
    }
    assertThat(records).isSameAs(RL1);
    assertThat(reader.poll()).isSameAs(RL2);
    assertThat(reader.poll()).isSameAs(RL3);
    assertThat(reader.poll()).isSameAs(RL4);
    assertThat(reader.poll()).isSameAs(RL5);
    // cause the 2nd mock reader to stop itself
    assertThat(reader.poll()).isNull();
    assertThat(reader.state()).isEqualTo(State.STOPPED);
    assertPollReturnsNoMoreRecords();
}
Also used : Timer(io.debezium.util.Threads.Timer) SourceRecord(org.apache.kafka.connect.source.SourceRecord) Test(org.junit.Test)

Aggregations

Timer (io.debezium.util.Threads.Timer)3 ArrayList (java.util.ArrayList)2 SourceRecord (org.apache.kafka.connect.source.SourceRecord)2 LoggingContext (io.debezium.util.LoggingContext)1 PreviousContext (io.debezium.util.LoggingContext.PreviousContext)1 Test (org.junit.Test)1