Search in sources :

Example 1 with ConnectorCallback

use of io.debezium.embedded.EmbeddedEngine.ConnectorCallback in project debezium by debezium.

the class AbstractConnectorTest method start.

/**
 * Start the connector using the supplied connector configuration.
 *
 * @param connectorClass the connector class; may not be null
 * @param connectorConfig the configuration for the connector; may not be null
 * @param isStopRecord the function that will be called to determine if the connector should be stopped before processing
 *            this record; may be null if not needed
 * @param callback the function that will be called when the engine fails to start the connector or when the connector
 *            stops running after completing successfully or due to an error; may be null
 */
protected void start(Class<? extends SourceConnector> connectorClass, Configuration connectorConfig, CompletionCallback callback, Predicate<SourceRecord> isStopRecord) {
    Configuration config = Configuration.copy(connectorConfig).with(EmbeddedEngine.ENGINE_NAME, "testing-connector").with(EmbeddedEngine.CONNECTOR_CLASS, connectorClass.getName()).with(StandaloneConfig.OFFSET_STORAGE_FILE_FILENAME_CONFIG, OFFSET_STORE_PATH).with(EmbeddedEngine.OFFSET_FLUSH_INTERVAL_MS, 0).build();
    latch = new CountDownLatch(1);
    CompletionCallback wrapperCallback = (success, msg, error) -> {
        try {
            if (callback != null)
                callback.handle(success, msg, error);
        } finally {
            if (!success) {
                // we only unblock if there was an error; in all other cases we're unblocking when a task has been started
                latch.countDown();
            }
        }
        Testing.debug("Stopped connector");
    };
    ConnectorCallback connectorCallback = new ConnectorCallback() {

        @Override
        public void taskStarted() {
            // if this is called, it means a task has been started successfully so we can continue
            latch.countDown();
        }
    };
    // Create the connector ...
    engine = EmbeddedEngine.create().using(config).notifying((record) -> {
        if (isStopRecord != null && isStopRecord.test(record)) {
            logger.error("Stopping connector after record as requested");
            throw new ConnectException("Stopping connector after record as requested");
        }
        try {
            consumedLines.put(record);
        } catch (InterruptedException e) {
            Thread.interrupted();
        }
    }).using(this.getClass().getClassLoader()).using(wrapperCallback).using(connectorCallback).build();
    // Submit the connector for asynchronous execution ...
    assertThat(executor).isNull();
    executor = Executors.newFixedThreadPool(1);
    executor.execute(() -> {
        LoggingContext.forConnector(getClass().getSimpleName(), "", "engine");
        engine.run();
    });
    try {
        if (!latch.await(10, TimeUnit.SECONDS)) {
            // maybe it takes more time to start up, so just log a warning and continue
            logger.warn("The connector did not finish starting its task(s) or complete in the expected amount of time");
        }
    } catch (InterruptedException e) {
        if (Thread.interrupted()) {
            fail("Interrupted while waiting for engine startup");
        }
    }
}
Also used : WorkerConfig(org.apache.kafka.connect.runtime.WorkerConfig) FileOffsetBackingStore(org.apache.kafka.connect.storage.FileOffsetBackingStore) Arrays(java.util.Arrays) BooleanConsumer(io.debezium.function.BooleanConsumer) LoggerFactory(org.slf4j.LoggerFactory) Schema(org.apache.kafka.connect.data.Schema) BigDecimal(java.math.BigDecimal) LoggingContext(io.debezium.util.LoggingContext) JsonDeserializer(org.apache.kafka.connect.json.JsonDeserializer) SourceConnector(org.apache.kafka.connect.source.SourceConnector) Assertions.assertThat(org.fest.assertions.Assertions.assertThat) Map(java.util.Map) Converter(org.apache.kafka.connect.storage.Converter) After(org.junit.After) Assert.fail(org.junit.Assert.fail) SkipTestRule(io.debezium.junit.SkipTestRule) Path(java.nio.file.Path) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) ConfigValue(org.apache.kafka.common.config.ConfigValue) SourceRecord(org.apache.kafka.connect.source.SourceRecord) Executors(java.util.concurrent.Executors) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) HistoryRecord(io.debezium.relational.history.HistoryRecord) List(java.util.List) EmbeddedConfig(io.debezium.embedded.EmbeddedEngine.EmbeddedConfig) JsonConverter(org.apache.kafka.connect.json.JsonConverter) Delta(org.fest.assertions.Delta) SchemaUtil(io.debezium.data.SchemaUtil) OffsetStorageReaderImpl(org.apache.kafka.connect.storage.OffsetStorageReaderImpl) TestRule(org.junit.rules.TestRule) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Config(org.apache.kafka.common.config.Config) StandaloneConfig(org.apache.kafka.connect.runtime.standalone.StandaloneConfig) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Logger(org.slf4j.Logger) Field(org.apache.kafka.connect.data.Field) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Configuration(io.debezium.config.Configuration) CompletionCallback(io.debezium.embedded.EmbeddedEngine.CompletionCallback) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Rule(org.junit.Rule) Testing(io.debezium.util.Testing) Struct(org.apache.kafka.connect.data.Struct) ConnectException(org.apache.kafka.connect.errors.ConnectException) VerifyRecord(io.debezium.data.VerifyRecord) Collections(java.util.Collections) ConnectorCallback(io.debezium.embedded.EmbeddedEngine.ConnectorCallback) Configuration(io.debezium.config.Configuration) ConnectorCallback(io.debezium.embedded.EmbeddedEngine.ConnectorCallback) CompletionCallback(io.debezium.embedded.EmbeddedEngine.CompletionCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Aggregations

Configuration (io.debezium.config.Configuration)1 SchemaUtil (io.debezium.data.SchemaUtil)1 VerifyRecord (io.debezium.data.VerifyRecord)1 CompletionCallback (io.debezium.embedded.EmbeddedEngine.CompletionCallback)1 ConnectorCallback (io.debezium.embedded.EmbeddedEngine.ConnectorCallback)1 EmbeddedConfig (io.debezium.embedded.EmbeddedEngine.EmbeddedConfig)1 BooleanConsumer (io.debezium.function.BooleanConsumer)1 SkipTestRule (io.debezium.junit.SkipTestRule)1 HistoryRecord (io.debezium.relational.history.HistoryRecord)1 LoggingContext (io.debezium.util.LoggingContext)1 Testing (io.debezium.util.Testing)1 BigDecimal (java.math.BigDecimal)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1