Search in sources :

Example 1 with TopicCreationGroup

use of org.apache.kafka.connect.util.TopicCreationGroup in project kafka by apache.

the class WorkerSourceTask method maybeCreateTopic.

// Due to transformations that may change the destination topic of a record (such as
// RegexRouter) topic creation can not be batched for multiple topics
private void maybeCreateTopic(String topic) {
    if (!topicCreation.isTopicCreationRequired(topic)) {
        log.trace("Topic creation by the connector is disabled or the topic {} was previously created." + "If auto.create.topics.enable is enabled on the broker, " + "the topic will be created with default settings", topic);
        return;
    }
    log.info("The task will send records to topic '{}' for the first time. Checking " + "whether topic exists", topic);
    Map<String, TopicDescription> existing = admin.describeTopics(topic);
    if (!existing.isEmpty()) {
        log.info("Topic '{}' already exists.", topic);
        topicCreation.addTopic(topic);
        return;
    }
    log.info("Creating topic '{}'", topic);
    TopicCreationGroup topicGroup = topicCreation.findFirstGroup(topic);
    log.debug("Topic '{}' matched topic creation group: {}", topic, topicGroup);
    NewTopic newTopic = topicGroup.newTopic(topic);
    TopicAdmin.TopicCreationResponse response = admin.createOrFindTopics(newTopic);
    if (response.isCreated(newTopic.name())) {
        topicCreation.addTopic(topic);
        log.info("Created topic '{}' using creation group {}", newTopic, topicGroup);
    } else if (response.isExisting(newTopic.name())) {
        topicCreation.addTopic(topic);
        log.info("Found existing topic '{}'", newTopic);
    } else {
        // The topic still does not exist and could not be created, so treat it as a task failure
        log.warn("Request to create new topic '{}' failed", topic);
        throw new ConnectException("Task failed to create new topic " + newTopic + ". Ensure " + "that the task is authorized to create topics or that the topic exists and " + "restart the task");
    }
}
Also used : TopicCreationGroup(org.apache.kafka.connect.util.TopicCreationGroup) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) NewTopic(org.apache.kafka.clients.admin.NewTopic) TopicAdmin(org.apache.kafka.connect.util.TopicAdmin) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 2 with TopicCreationGroup

use of org.apache.kafka.connect.util.TopicCreationGroup in project kafka by apache.

the class Worker method buildWorkerTask.

private WorkerTask buildWorkerTask(ClusterConfigState configState, ConnectorConfig connConfig, ConnectorTaskId id, Task task, TaskStatus.Listener statusListener, TargetState initialState, Converter keyConverter, Converter valueConverter, HeaderConverter headerConverter, ClassLoader loader) {
    ErrorHandlingMetrics errorHandlingMetrics = errorHandlingMetrics(id);
    final Class<? extends Connector> connectorClass = plugins.connectorClass(connConfig.getString(ConnectorConfig.CONNECTOR_CLASS_CONFIG));
    RetryWithToleranceOperator retryWithToleranceOperator = new RetryWithToleranceOperator(connConfig.errorRetryTimeout(), connConfig.errorMaxDelayInMillis(), connConfig.errorToleranceType(), Time.SYSTEM);
    retryWithToleranceOperator.metrics(errorHandlingMetrics);
    // Decide which type of worker task we need based on the type of task.
    if (task instanceof SourceTask) {
        SourceConnectorConfig sourceConfig = new SourceConnectorConfig(plugins, connConfig.originalsStrings(), config.topicCreationEnable());
        retryWithToleranceOperator.reporters(sourceTaskReporters(id, sourceConfig, errorHandlingMetrics));
        TransformationChain<SourceRecord> transformationChain = new TransformationChain<>(sourceConfig.<SourceRecord>transformations(), retryWithToleranceOperator);
        log.info("Initializing: {}", transformationChain);
        CloseableOffsetStorageReader offsetReader = new OffsetStorageReaderImpl(offsetBackingStore, id.connector(), internalKeyConverter, internalValueConverter);
        OffsetStorageWriter offsetWriter = new OffsetStorageWriter(offsetBackingStore, id.connector(), internalKeyConverter, internalValueConverter);
        Map<String, Object> producerProps = producerConfigs(id, "connector-producer-" + id, config, sourceConfig, connectorClass, connectorClientConfigOverridePolicy, kafkaClusterId);
        KafkaProducer<byte[], byte[]> producer = new KafkaProducer<>(producerProps);
        TopicAdmin admin;
        Map<String, TopicCreationGroup> topicCreationGroups;
        if (config.topicCreationEnable() && sourceConfig.usesTopicCreation()) {
            Map<String, Object> adminProps = adminConfigs(id, "connector-adminclient-" + id, config, sourceConfig, connectorClass, connectorClientConfigOverridePolicy, kafkaClusterId);
            admin = new TopicAdmin(adminProps);
            topicCreationGroups = TopicCreationGroup.configuredGroups(sourceConfig);
        } else {
            admin = null;
            topicCreationGroups = null;
        }
        // Note we pass the configState as it performs dynamic transformations under the covers
        return new WorkerSourceTask(id, (SourceTask) task, statusListener, initialState, keyConverter, valueConverter, headerConverter, transformationChain, producer, admin, topicCreationGroups, offsetReader, offsetWriter, config, configState, metrics, loader, time, retryWithToleranceOperator, herder.statusBackingStore(), executor);
    } else if (task instanceof SinkTask) {
        TransformationChain<SinkRecord> transformationChain = new TransformationChain<>(connConfig.<SinkRecord>transformations(), retryWithToleranceOperator);
        log.info("Initializing: {}", transformationChain);
        SinkConnectorConfig sinkConfig = new SinkConnectorConfig(plugins, connConfig.originalsStrings());
        retryWithToleranceOperator.reporters(sinkTaskReporters(id, sinkConfig, errorHandlingMetrics, connectorClass));
        WorkerErrantRecordReporter workerErrantRecordReporter = createWorkerErrantRecordReporter(sinkConfig, retryWithToleranceOperator, keyConverter, valueConverter, headerConverter);
        Map<String, Object> consumerProps = consumerConfigs(id, config, connConfig, connectorClass, connectorClientConfigOverridePolicy, kafkaClusterId);
        KafkaConsumer<byte[], byte[]> consumer = new KafkaConsumer<>(consumerProps);
        return new WorkerSinkTask(id, (SinkTask) task, statusListener, initialState, config, configState, metrics, keyConverter, valueConverter, headerConverter, transformationChain, consumer, loader, time, retryWithToleranceOperator, workerErrantRecordReporter, herder.statusBackingStore());
    } else {
        log.error("Tasks must be a subclass of either SourceTask or SinkTask and current is {}", task);
        throw new ConnectException("Tasks must be a subclass of either SourceTask or SinkTask");
    }
}
Also used : OffsetStorageWriter(org.apache.kafka.connect.storage.OffsetStorageWriter) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) TopicCreationGroup(org.apache.kafka.connect.util.TopicCreationGroup) SourceRecord(org.apache.kafka.connect.source.SourceRecord) CloseableOffsetStorageReader(org.apache.kafka.connect.storage.CloseableOffsetStorageReader) ErrorHandlingMetrics(org.apache.kafka.connect.runtime.errors.ErrorHandlingMetrics) ConnectException(org.apache.kafka.connect.errors.ConnectException) WorkerErrantRecordReporter(org.apache.kafka.connect.runtime.errors.WorkerErrantRecordReporter) RetryWithToleranceOperator(org.apache.kafka.connect.runtime.errors.RetryWithToleranceOperator) SinkTask(org.apache.kafka.connect.sink.SinkTask) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) TopicAdmin(org.apache.kafka.connect.util.TopicAdmin) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) OffsetStorageReaderImpl(org.apache.kafka.connect.storage.OffsetStorageReaderImpl) SourceTask(org.apache.kafka.connect.source.SourceTask) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

ConnectException (org.apache.kafka.connect.errors.ConnectException)2 TopicAdmin (org.apache.kafka.connect.util.TopicAdmin)2 TopicCreationGroup (org.apache.kafka.connect.util.TopicCreationGroup)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 NewTopic (org.apache.kafka.clients.admin.NewTopic)1 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)1 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 ErrorHandlingMetrics (org.apache.kafka.connect.runtime.errors.ErrorHandlingMetrics)1 RetryWithToleranceOperator (org.apache.kafka.connect.runtime.errors.RetryWithToleranceOperator)1 WorkerErrantRecordReporter (org.apache.kafka.connect.runtime.errors.WorkerErrantRecordReporter)1 SinkRecord (org.apache.kafka.connect.sink.SinkRecord)1 SinkTask (org.apache.kafka.connect.sink.SinkTask)1 SourceRecord (org.apache.kafka.connect.source.SourceRecord)1 SourceTask (org.apache.kafka.connect.source.SourceTask)1 CloseableOffsetStorageReader (org.apache.kafka.connect.storage.CloseableOffsetStorageReader)1 OffsetStorageReaderImpl (org.apache.kafka.connect.storage.OffsetStorageReaderImpl)1