Search in sources :

Example 36 with ConnectorTaskId

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

the class KafkaConfigBackingStore method putTaskConfigs.

/**
 * Write these task configurations and associated commit messages, unless an inconsistency is found that indicates
 * that we would be leaving one of the referenced connectors with an inconsistent state.
 *
 * @param connector the connector to write task configuration
 * @param configs list of task configurations for the connector
 * @throws ConnectException if the task configurations do not resolve inconsistencies found in the existing root
 *                          and task configurations.
 */
@Override
public void putTaskConfigs(String connector, List<Map<String, String>> configs) {
    // any outstanding lagging data to consume.
    try {
        configLog.readToEnd().get(READ_TO_END_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        log.error("Failed to write root configuration to Kafka: ", e);
        throw new ConnectException("Error writing root configuration to Kafka", e);
    }
    int taskCount = configs.size();
    // Start sending all the individual updates
    int index = 0;
    for (Map<String, String> taskConfig : configs) {
        Struct connectConfig = new Struct(TASK_CONFIGURATION_V0);
        connectConfig.put("properties", taskConfig);
        byte[] serializedConfig = converter.fromConnectData(topic, TASK_CONFIGURATION_V0, connectConfig);
        log.debug("Writing configuration for connector '{}' task {}", connector, index);
        ConnectorTaskId connectorTaskId = new ConnectorTaskId(connector, index);
        configLog.send(TASK_KEY(connectorTaskId), serializedConfig);
        index++;
    }
    // the end of the log
    try {
        // Read to end to ensure all the task configs have been written
        if (taskCount > 0) {
            configLog.readToEnd().get(READ_TO_END_TIMEOUT_MS, TimeUnit.MILLISECONDS);
        }
        // Write the commit message
        Struct connectConfig = new Struct(CONNECTOR_TASKS_COMMIT_V0);
        connectConfig.put("tasks", taskCount);
        byte[] serializedConfig = converter.fromConnectData(topic, CONNECTOR_TASKS_COMMIT_V0, connectConfig);
        log.debug("Writing commit for connector '{}' with {} tasks.", connector, taskCount);
        configLog.send(COMMIT_TASKS_KEY(connector), serializedConfig);
        // Read to end to ensure all the commit messages have been written
        configLog.readToEnd().get(READ_TO_END_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        log.error("Failed to write root configuration to Kafka: ", e);
        throw new ConnectException("Error writing root configuration to Kafka", e);
    }
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ConnectException(org.apache.kafka.connect.errors.ConnectException) Struct(org.apache.kafka.connect.data.Struct)

Example 37 with ConnectorTaskId

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

the class KafkaStatusBackingStore method readTaskStatus.

private void readTaskStatus(String key, byte[] value) {
    ConnectorTaskId id = parseConnectorTaskId(key);
    if (id == null) {
        log.warn("Discarding record with invalid task status key {}", key);
        return;
    }
    if (value == null) {
        log.trace("Removing task status for {}", id);
        remove(id);
        return;
    }
    TaskStatus status = parseTaskStatus(id, value);
    if (status == null) {
        log.warn("Failed to parse task status with key {}", key);
        return;
    }
    synchronized (this) {
        log.trace("Received task {} status update {}", id, status);
        CacheEntry<TaskStatus> entry = getOrAdd(id);
        entry.put(status);
    }
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus)

Example 38 with ConnectorTaskId

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

the class MemoryConfigBackingStore method taskConfigListAsMap.

private static Map<ConnectorTaskId, Map<String, String>> taskConfigListAsMap(String connector, List<Map<String, String>> configs) {
    int index = 0;
    Map<ConnectorTaskId, Map<String, String>> result = new TreeMap<>();
    for (Map<String, String> taskConfigMap : configs) {
        result.put(new ConnectorTaskId(connector, index++), taskConfigMap);
    }
    return result;
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 39 with ConnectorTaskId

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

the class ConnectorsResource method restartTask.

@POST
@Path("/{connector}/tasks/{task}/restart")
public void restartTask(@PathParam("connector") final String connector, @PathParam("task") final Integer task, @Context final HttpHeaders headers, @QueryParam("forward") final Boolean forward) throws Throwable {
    FutureCallback<Void> cb = new FutureCallback<>();
    ConnectorTaskId taskId = new ConnectorTaskId(connector, task);
    herder.restartTask(taskId, cb);
    completeOrForwardRequest(cb, "/connectors/" + connector + "/tasks/" + task + "/restart", "POST", headers, null, forward);
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) FutureCallback(org.apache.kafka.connect.util.FutureCallback) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 40 with ConnectorTaskId

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

the class AbstractHerder method buildTasksConfig.

protected Map<ConnectorTaskId, Map<String, String>> buildTasksConfig(String connector) {
    final ClusterConfigState configState = configBackingStore.snapshot();
    if (!configState.contains(connector))
        return Collections.emptyMap();
    Map<ConnectorTaskId, Map<String, String>> configs = new HashMap<>();
    for (ConnectorTaskId cti : configState.tasks(connector)) {
        configs.put(cti, configState.taskConfig(cti));
    }
    return configs;
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) ClusterConfigState(org.apache.kafka.connect.runtime.distributed.ClusterConfigState)

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