Search in sources :

Example 16 with ConnectorTaskId

use of org.apache.kafka.connect.util.ConnectorTaskId in project apache-kafka-on-k8s by banzaicloud.

the class WorkerCoordinator method performTaskAssignment.

private Map<String, ByteBuffer> performTaskAssignment(String leaderId, long maxOffset, Map<String, ConnectProtocol.WorkerState> memberConfigs) {
    Map<String, List<String>> connectorAssignments = new HashMap<>();
    Map<String, List<ConnectorTaskId>> taskAssignments = new HashMap<>();
    // Perform round-robin task assignment. Assign all connectors and then all tasks because assigning both the
    // connector and its tasks can lead to very uneven distribution of work in some common cases (e.g. for connectors
    // that generate only 1 task each; in a cluster of 2 or an even # of nodes, only even nodes will be assigned
    // connectors and only odd nodes will be assigned tasks, but tasks are, on average, actually more resource
    // intensive than connectors).
    List<String> connectorsSorted = sorted(configSnapshot.connectors());
    CircularIterator<String> memberIt = new CircularIterator<>(sorted(memberConfigs.keySet()));
    for (String connectorId : connectorsSorted) {
        String connectorAssignedTo = memberIt.next();
        log.trace("Assigning connector {} to {}", connectorId, connectorAssignedTo);
        List<String> memberConnectors = connectorAssignments.get(connectorAssignedTo);
        if (memberConnectors == null) {
            memberConnectors = new ArrayList<>();
            connectorAssignments.put(connectorAssignedTo, memberConnectors);
        }
        memberConnectors.add(connectorId);
    }
    for (String connectorId : connectorsSorted) {
        for (ConnectorTaskId taskId : sorted(configSnapshot.tasks(connectorId))) {
            String taskAssignedTo = memberIt.next();
            log.trace("Assigning task {} to {}", taskId, taskAssignedTo);
            List<ConnectorTaskId> memberTasks = taskAssignments.get(taskAssignedTo);
            if (memberTasks == null) {
                memberTasks = new ArrayList<>();
                taskAssignments.put(taskAssignedTo, memberTasks);
            }
            memberTasks.add(taskId);
        }
    }
    this.leaderState = new LeaderState(memberConfigs, connectorAssignments, taskAssignments);
    return fillAssignmentsAndSerialize(memberConfigs.keySet(), ConnectProtocol.Assignment.NO_ERROR, leaderId, memberConfigs.get(leaderId).url(), maxOffset, connectorAssignments, taskAssignments);
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) HashMap(java.util.HashMap) CircularIterator(org.apache.kafka.common.utils.CircularIterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 17 with ConnectorTaskId

use of org.apache.kafka.connect.util.ConnectorTaskId in project apache-kafka-on-k8s by banzaicloud.

the class MemoryStatusBackingStoreTest method deleteTaskStatus.

@Test
public void deleteTaskStatus() {
    MemoryStatusBackingStore store = new MemoryStatusBackingStore();
    ConnectorTaskId taskId = new ConnectorTaskId("connector", 0);
    store.put(new TaskStatus(taskId, ConnectorStatus.State.RUNNING, "localhost:8083", 0));
    store.put(new TaskStatus(taskId, ConnectorStatus.State.DESTROYED, "localhost:8083", 0));
    assertNull(store.get(taskId));
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus) Test(org.junit.Test)

Example 18 with ConnectorTaskId

use of org.apache.kafka.connect.util.ConnectorTaskId in project apache-kafka-on-k8s by banzaicloud.

the class StandaloneHerderTest method expectStop.

private void expectStop() {
    ConnectorTaskId task = new ConnectorTaskId(CONNECTOR_NAME, 0);
    worker.stopAndAwaitTasks(singletonList(task));
    EasyMock.expectLastCall();
    worker.stopConnector(CONNECTOR_NAME);
    EasyMock.expectLastCall().andReturn(true);
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId)

Example 19 with ConnectorTaskId

use of org.apache.kafka.connect.util.ConnectorTaskId in project apache-kafka-on-k8s by banzaicloud.

the class StandaloneHerderTest method expectAdd.

private void expectAdd(SourceSink sourceSink) throws Exception {
    Map<String, String> connectorProps = connectorConfig(sourceSink);
    ConnectorConfig connConfig = sourceSink == SourceSink.SOURCE ? new SourceConnectorConfig(plugins, connectorProps) : new SinkConnectorConfig(plugins, connectorProps);
    worker.startConnector(EasyMock.eq(CONNECTOR_NAME), EasyMock.eq(connectorProps), EasyMock.anyObject(HerderConnectorContext.class), EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED));
    EasyMock.expectLastCall().andReturn(true);
    EasyMock.expect(worker.isRunning(CONNECTOR_NAME)).andReturn(true);
    ConnectorInfo connInfo = new ConnectorInfo(CONNECTOR_NAME, connectorProps, Arrays.asList(new ConnectorTaskId(CONNECTOR_NAME, 0)), SourceSink.SOURCE == sourceSink ? ConnectorType.SOURCE : ConnectorType.SINK);
    createCallback.onCompletion(null, new Herder.Created<>(true, connInfo));
    EasyMock.expectLastCall();
    // And we should instantiate the tasks. For a sink task, we should see added properties for the input topic partitions
    Map<String, String> generatedTaskProps = taskConfig(sourceSink);
    EasyMock.expect(worker.connectorTaskConfigs(CONNECTOR_NAME, connConfig)).andReturn(singletonList(generatedTaskProps));
    worker.startTask(new ConnectorTaskId(CONNECTOR_NAME, 0), connectorConfig(sourceSink), generatedTaskProps, herder, TargetState.STARTED);
    EasyMock.expectLastCall().andReturn(true);
    EasyMock.expect(herder.connectorTypeForClass(BogusSourceConnector.class.getName())).andReturn(ConnectorType.SOURCE).anyTimes();
    EasyMock.expect(herder.connectorTypeForClass(BogusSinkConnector.class.getName())).andReturn(ConnectorType.SINK).anyTimes();
    worker.isSinkConnector(CONNECTOR_NAME);
    PowerMock.expectLastCall().andReturn(sourceSink == SourceSink.SINK);
}
Also used : SourceConnectorConfig(org.apache.kafka.connect.runtime.SourceConnectorConfig) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) SourceConnectorConfig(org.apache.kafka.connect.runtime.SourceConnectorConfig) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) SinkConnectorConfig(org.apache.kafka.connect.runtime.SinkConnectorConfig) HerderConnectorContext(org.apache.kafka.connect.runtime.HerderConnectorContext) Herder(org.apache.kafka.connect.runtime.Herder) SinkConnectorConfig(org.apache.kafka.connect.runtime.SinkConnectorConfig)

Example 20 with ConnectorTaskId

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

the class Worker method connectorClientConfigOverrides.

private static Map<String, Object> connectorClientConfigOverrides(ConnectorTaskId id, ConnectorConfig connConfig, Class<? extends Connector> connectorClass, String clientConfigPrefix, ConnectorType connectorType, ConnectorClientConfigRequest.ClientType clientType, ConnectorClientConfigOverridePolicy connectorClientConfigOverridePolicy) {
    Map<String, Object> clientOverrides = connConfig.originalsWithPrefix(clientConfigPrefix);
    ConnectorClientConfigRequest connectorClientConfigRequest = new ConnectorClientConfigRequest(id.connector(), connectorType, connectorClass, clientOverrides, clientType);
    List<ConfigValue> configValues = connectorClientConfigOverridePolicy.validate(connectorClientConfigRequest);
    List<ConfigValue> errorConfigs = configValues.stream().filter(configValue -> configValue.errorMessages().size() > 0).collect(Collectors.toList());
    // These should be caught when the herder validates the connector configuration, but just in case
    if (errorConfigs.size() > 0) {
        throw new ConnectException("Client Config Overrides not allowed " + errorConfigs);
    }
    return clientOverrides;
}
Also used : LoggingContext(org.apache.kafka.connect.util.LoggingContext) SinkUtils(org.apache.kafka.connect.util.SinkUtils) JsonConverterConfig(org.apache.kafka.connect.json.JsonConverterConfig) Plugins(org.apache.kafka.connect.runtime.isolation.Plugins) LoggerFactory(org.slf4j.LoggerFactory) ConnectorType(org.apache.kafka.connect.health.ConnectorType) OffsetStorageWriter(org.apache.kafka.connect.storage.OffsetStorageWriter) ErrorHandlingMetrics(org.apache.kafka.connect.runtime.errors.ErrorHandlingMetrics) CloseableOffsetStorageReader(org.apache.kafka.connect.storage.CloseableOffsetStorageReader) ErrorReporter(org.apache.kafka.connect.runtime.errors.ErrorReporter) Converter(org.apache.kafka.connect.storage.Converter) Map(java.util.Map) DeadLetterQueueReporter(org.apache.kafka.connect.runtime.errors.DeadLetterQueueReporter) OffsetBackingStore(org.apache.kafka.connect.storage.OffsetBackingStore) OffsetStorageReader(org.apache.kafka.connect.storage.OffsetStorageReader) ClassLoaderUsage(org.apache.kafka.connect.runtime.isolation.Plugins.ClassLoaderUsage) ConnectUtils(org.apache.kafka.connect.util.ConnectUtils) TopicCreationGroup(org.apache.kafka.connect.util.TopicCreationGroup) Time(org.apache.kafka.common.utils.Time) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) ConfigValue(org.apache.kafka.common.config.ConfigValue) SourceRecord(org.apache.kafka.connect.source.SourceRecord) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) Task(org.apache.kafka.connect.connector.Task) JsonConverter(org.apache.kafka.connect.json.JsonConverter) ConnectorClientConfigOverridePolicy(org.apache.kafka.connect.connector.policy.ConnectorClientConfigOverridePolicy) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) SourceTask(org.apache.kafka.connect.source.SourceTask) OffsetStorageReaderImpl(org.apache.kafka.connect.storage.OffsetStorageReaderImpl) Connector(org.apache.kafka.connect.connector.Connector) ConfigProvider(org.apache.kafka.common.config.provider.ConfigProvider) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) TopicAdmin(org.apache.kafka.connect.util.TopicAdmin) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) LogReporter(org.apache.kafka.connect.runtime.errors.LogReporter) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) HeaderConverter(org.apache.kafka.connect.storage.HeaderConverter) WorkerErrantRecordReporter(org.apache.kafka.connect.runtime.errors.WorkerErrantRecordReporter) MetricGroup(org.apache.kafka.connect.runtime.ConnectMetrics.MetricGroup) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) ExecutorService(java.util.concurrent.ExecutorService) SinkTask(org.apache.kafka.connect.sink.SinkTask) Utils(org.apache.kafka.common.utils.Utils) Callback(org.apache.kafka.connect.util.Callback) Logger(org.slf4j.Logger) ConnectorClientConfigRequest(org.apache.kafka.connect.connector.policy.ConnectorClientConfigRequest) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) ClusterConfigState(org.apache.kafka.connect.runtime.distributed.ClusterConfigState) MetricNameTemplate(org.apache.kafka.common.MetricNameTemplate) TimeUnit(java.util.concurrent.TimeUnit) ConnectException(org.apache.kafka.connect.errors.ConnectException) RetryWithToleranceOperator(org.apache.kafka.connect.runtime.errors.RetryWithToleranceOperator) Collections(java.util.Collections) ConnectorClientConfigRequest(org.apache.kafka.connect.connector.policy.ConnectorClientConfigRequest) ConfigValue(org.apache.kafka.common.config.ConfigValue) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Aggregations

ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)111 Test (org.junit.Test)59 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)45 HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)25 Map (java.util.Map)18 FutureCallback (org.apache.kafka.connect.util.FutureCallback)16 ConnectException (org.apache.kafka.connect.errors.ConnectException)15 Callback (org.apache.kafka.connect.util.Callback)15 Connector (org.apache.kafka.connect.connector.Connector)13 NotFoundException (org.apache.kafka.connect.errors.NotFoundException)12 ConnectorInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo)12 SinkConnector (org.apache.kafka.connect.sink.SinkConnector)12 TaskStatus (org.apache.kafka.connect.runtime.TaskStatus)11 WorkerConnector (org.apache.kafka.connect.runtime.WorkerConnector)11 SourceConnector (org.apache.kafka.connect.source.SourceConnector)11 Herder (org.apache.kafka.connect.runtime.Herder)10 List (java.util.List)9 StatusBackingStore (org.apache.kafka.connect.storage.StatusBackingStore)9 ConnectorStateInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo)8