Search in sources :

Example 21 with ConnectException

use of org.apache.kafka.connect.errors.ConnectException in project kafka by apache.

the class DistributedHerder method reconfigureConnector.

// Updates configurations for a connector by requesting them from the connector, filling in parameters provided
// by the system, then checks whether any configs have actually changed before submitting the new configs to storage
private void reconfigureConnector(final String connName, final Callback<Void> cb) {
    try {
        if (!worker.isRunning(connName)) {
            log.info("Skipping reconfiguration of connector {} since it is not running", connName);
            return;
        }
        Map<String, String> configs = configState.connectorConfig(connName);
        ConnectorConfig connConfig;
        List<String> sinkTopics = null;
        if (worker.isSinkConnector(connName)) {
            connConfig = new SinkConnectorConfig(configs);
            sinkTopics = connConfig.getList(SinkConnectorConfig.TOPICS_CONFIG);
        } else {
            connConfig = new SourceConnectorConfig(configs);
        }
        final List<Map<String, String>> taskProps = worker.connectorTaskConfigs(connName, connConfig.getInt(ConnectorConfig.TASKS_MAX_CONFIG), sinkTopics);
        boolean changed = false;
        int currentNumTasks = configState.taskCount(connName);
        if (taskProps.size() != currentNumTasks) {
            log.debug("Change in connector task count from {} to {}, writing updated task configurations", currentNumTasks, taskProps.size());
            changed = true;
        } else {
            int index = 0;
            for (Map<String, String> taskConfig : taskProps) {
                if (!taskConfig.equals(configState.taskConfig(new ConnectorTaskId(connName, index)))) {
                    log.debug("Change in task configurations, writing updated task configurations");
                    changed = true;
                    break;
                }
                index++;
            }
        }
        if (changed) {
            if (isLeader()) {
                configBackingStore.putTaskConfigs(connName, taskProps);
                cb.onCompletion(null, null);
            } else {
                // We cannot forward the request on the same thread because this reconfiguration can happen as a result of connector
                // addition or removal. If we blocked waiting for the response from leader, we may be kicked out of the worker group.
                forwardRequestExecutor.submit(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            String reconfigUrl = RestServer.urlJoin(leaderUrl(), "/connectors/" + connName + "/tasks");
                            RestServer.httpRequest(reconfigUrl, "POST", taskProps, null);
                            cb.onCompletion(null, null);
                        } catch (ConnectException e) {
                            log.error("Request to leader to reconfigure connector tasks failed", e);
                            cb.onCompletion(e, null);
                        }
                    }
                });
            }
        }
    } catch (Throwable t) {
        cb.onCompletion(t, null);
    }
}
Also used : 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) SinkConnectorConfig(org.apache.kafka.connect.runtime.SinkConnectorConfig) SourceConnectorConfig(org.apache.kafka.connect.runtime.SourceConnectorConfig) Map(java.util.Map) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 22 with ConnectException

use of org.apache.kafka.connect.errors.ConnectException in project kafka by apache.

the class DistributedHerder method restartTask.

@Override
public void restartTask(final ConnectorTaskId id, final Callback<Void> callback) {
    addRequest(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            if (checkRebalanceNeeded(callback))
                return null;
            if (!configState.connectors().contains(id.connector())) {
                callback.onCompletion(new NotFoundException("Unknown connector: " + id.connector()), null);
                return null;
            }
            if (configState.taskConfig(id) == null) {
                callback.onCompletion(new NotFoundException("Unknown task: " + id), null);
                return null;
            }
            if (assignment.tasks().contains(id)) {
                try {
                    worker.stopAndAwaitTask(id);
                    if (startTask(id))
                        callback.onCompletion(null, null);
                    else
                        callback.onCompletion(new ConnectException("Failed to start task: " + id), null);
                } catch (Throwable t) {
                    callback.onCompletion(t, null);
                }
            } else if (isLeader()) {
                callback.onCompletion(new NotAssignedException("Cannot restart task since it is not assigned to this member", member.ownerUrl(id)), null);
            } else {
                callback.onCompletion(new NotLeaderException("Cannot restart task since it is not assigned to this member", leaderUrl()), null);
            }
            return null;
        }
    }, forwardErrorCallback(callback));
}
Also used : NotFoundException(org.apache.kafka.connect.errors.NotFoundException) TimeoutException(java.util.concurrent.TimeoutException) AlreadyExistsException(org.apache.kafka.connect.errors.AlreadyExistsException) WakeupException(org.apache.kafka.common.errors.WakeupException) BadRequestException(org.apache.kafka.connect.runtime.rest.errors.BadRequestException) NotFoundException(org.apache.kafka.connect.errors.NotFoundException) NoSuchElementException(java.util.NoSuchElementException) ConnectException(org.apache.kafka.connect.errors.ConnectException) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 23 with ConnectException

use of org.apache.kafka.connect.errors.ConnectException in project kafka by apache.

the class StandaloneHerder method restartTask.

@Override
public synchronized void restartTask(ConnectorTaskId taskId, Callback<Void> cb) {
    if (!configState.contains(taskId.connector()))
        cb.onCompletion(new NotFoundException("Connector " + taskId.connector() + " not found", null), null);
    Map<String, String> taskConfigProps = configState.taskConfig(taskId);
    if (taskConfigProps == null)
        cb.onCompletion(new NotFoundException("Task " + taskId + " not found", null), null);
    Map<String, String> connConfigProps = configState.connectorConfig(taskId.connector());
    TargetState targetState = configState.targetState(taskId.connector());
    worker.stopAndAwaitTask(taskId);
    if (worker.startTask(taskId, connConfigProps, taskConfigProps, this, targetState))
        cb.onCompletion(null, null);
    else
        cb.onCompletion(new ConnectException("Failed to start task: " + taskId), null);
}
Also used : TargetState(org.apache.kafka.connect.runtime.TargetState) NotFoundException(org.apache.kafka.connect.errors.NotFoundException) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 24 with ConnectException

use of org.apache.kafka.connect.errors.ConnectException in project kafka by apache.

the class StandaloneHerder method deleteConnectorConfig.

@Override
public synchronized void deleteConnectorConfig(String connName, Callback<Created<ConnectorInfo>> callback) {
    try {
        if (!configState.contains(connName)) {
            // Deletion, must already exist
            callback.onCompletion(new NotFoundException("Connector " + connName + " not found", null), null);
            return;
        }
        removeConnectorTasks(connName);
        worker.stopConnector(connName);
        configBackingStore.removeConnectorConfig(connName);
        onDeletion(connName);
        callback.onCompletion(null, new Created<ConnectorInfo>(false, null));
    } catch (ConnectException e) {
        callback.onCompletion(e, null);
    }
}
Also used : ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) NotFoundException(org.apache.kafka.connect.errors.NotFoundException) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 25 with ConnectException

use of org.apache.kafka.connect.errors.ConnectException in project kafka by apache.

the class StandaloneHerder method putConnectorConfig.

@Override
public synchronized void putConnectorConfig(String connName, final Map<String, String> config, boolean allowReplace, final Callback<Created<ConnectorInfo>> callback) {
    try {
        ConfigInfos validatedConfig = validateConnectorConfig(config);
        if (validatedConfig.errorCount() > 0) {
            callback.onCompletion(new BadRequestException("Connector configuration is invalid " + "(use the endpoint `/{connectorType}/config/validate` to get a full list of errors)"), null);
            return;
        }
        boolean created = false;
        if (configState.contains(connName)) {
            if (!allowReplace) {
                callback.onCompletion(new AlreadyExistsException("Connector " + connName + " already exists"), null);
                return;
            }
            worker.stopConnector(connName);
        } else {
            created = true;
        }
        if (!startConnector(config)) {
            callback.onCompletion(new ConnectException("Failed to start connector: " + connName), null);
            return;
        }
        updateConnectorTasks(connName);
        callback.onCompletion(null, new Created<>(created, createConnectorInfo(connName)));
    } catch (ConnectException e) {
        callback.onCompletion(e, null);
    }
}
Also used : AlreadyExistsException(org.apache.kafka.connect.errors.AlreadyExistsException) BadRequestException(org.apache.kafka.connect.runtime.rest.errors.BadRequestException) ConfigInfos(org.apache.kafka.connect.runtime.rest.entities.ConfigInfos) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Aggregations

ConnectException (org.apache.kafka.connect.errors.ConnectException)42 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ArrayList (java.util.ArrayList)6 TimeoutException (java.util.concurrent.TimeoutException)6 IOException (java.io.IOException)5 Connector (org.apache.kafka.connect.connector.Connector)5 ExecutionException (java.util.concurrent.ExecutionException)4 NotFoundException (org.apache.kafka.connect.errors.NotFoundException)4 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 ByteBuffer (java.nio.ByteBuffer)3 AlreadyExistsException (org.apache.kafka.connect.errors.AlreadyExistsException)3 BadRequestException (org.apache.kafka.connect.runtime.rest.errors.BadRequestException)3 SinkRecord (org.apache.kafka.connect.sink.SinkRecord)3 SourceRecord (org.apache.kafka.connect.source.SourceRecord)3 ThreadedTest (org.apache.kafka.connect.util.ThreadedTest)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2