Search in sources :

Example 41 with Connector

use of org.apache.kafka.connect.connector.Connector in project apache-kafka-on-k8s by banzaicloud.

the class Worker method startConnector.

/**
 * Start a connector managed by this worker.
 *
 * @param connName the connector name.
 * @param connProps the properties of the connector.
 * @param ctx the connector runtime context.
 * @param statusListener a listener for the runtime status transitions of the connector.
 * @param initialState the initial state of the connector.
 * @return true if the connector started successfully.
 */
public boolean startConnector(String connName, Map<String, String> connProps, ConnectorContext ctx, ConnectorStatus.Listener statusListener, TargetState initialState) {
    if (connectors.containsKey(connName))
        throw new ConnectException("Connector with name " + connName + " already exists");
    final WorkerConnector workerConnector;
    ClassLoader savedLoader = plugins.currentThreadLoader();
    try {
        final ConnectorConfig connConfig = new ConnectorConfig(plugins, connProps);
        final String connClass = connConfig.getString(ConnectorConfig.CONNECTOR_CLASS_CONFIG);
        log.info("Creating connector {} of type {}", connName, connClass);
        final Connector connector = plugins.newConnector(connClass);
        workerConnector = new WorkerConnector(connName, connector, ctx, metrics, statusListener);
        log.info("Instantiated connector {} with version {} of type {}", connName, connector.version(), connector.getClass());
        savedLoader = plugins.compareAndSwapLoaders(connector);
        workerConnector.initialize(connConfig);
        workerConnector.transitionTo(initialState);
        Plugins.compareAndSwapLoaders(savedLoader);
    } catch (Throwable t) {
        log.error("Failed to start connector {}", connName, t);
        // Can't be put in a finally block because it needs to be swapped before the call on
        // statusListener
        Plugins.compareAndSwapLoaders(savedLoader);
        workerMetricsGroup.recordConnectorStartupFailure();
        statusListener.onFailure(connName, t);
        return false;
    }
    WorkerConnector existing = connectors.putIfAbsent(connName, workerConnector);
    if (existing != null)
        throw new ConnectException("Connector with name " + connName + " already exists");
    log.info("Finished creating connector {}", connName);
    workerMetricsGroup.recordConnectorStartupSuccess();
    return true;
}
Also used : Connector(org.apache.kafka.connect.connector.Connector) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 42 with Connector

use of org.apache.kafka.connect.connector.Connector in project connect-utils by jcustenborder.

the class PluginLoader method configDef.

// ConfigDef transformationConfig(Class<? extends Transformation> transformation) {
// try {
// return transformation.newInstance().config();
// } catch (InstantiationException | IllegalAccessException e) {
// throw new IllegalStateException(e);
// }
// }
// 
// ConfigDef connectorConfig(Class<? extends Connector> connectorClass) {
// try {
// return connectorClass.newInstance().config();
// } catch (InstantiationException | IllegalAccessException e) {
// throw new IllegalStateException(e);
// }
// }
ConfigDef configDef(Class<?> pluginClass) {
    ConfigDef result;
    try {
        if (Connector.class.isAssignableFrom(pluginClass)) {
            Connector connector = (Connector) pluginClass.newInstance();
            result = connector.config();
        } else if (Transformation.class.isAssignableFrom(pluginClass)) {
            Transformation<?> connector = (Transformation<?>) pluginClass.newInstance();
            result = connector.config();
        } else if (Converter.class.isAssignableFrom(pluginClass) || ConfigProvider.class.isAssignableFrom(pluginClass)) {
            Optional<Method> optionalConfigMethod = Stream.of(pluginClass.getMethods()).filter(method -> Modifier.isPublic(method.getModifiers())).filter(method -> ConfigDef.class.equals(method.getReturnType())).findFirst();
            if (optionalConfigMethod.isPresent()) {
                Method configMethod = optionalConfigMethod.get();
                Object instance;
                if (Modifier.isStatic(configMethod.getModifiers())) {
                    instance = null;
                } else {
                    instance = pluginClass.newInstance();
                }
                result = (ConfigDef) configMethod.invoke(instance);
            } else {
                result = null;
            }
        } else {
            throw new UnsupportedOperationException(String.format("Type %s is not supported", pluginClass.getName()));
        }
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
        throw new IllegalStateException(e);
    }
    return result;
}
Also used : Connector(org.apache.kafka.connect.connector.Connector) Arrays(java.util.Arrays) ConfigProvider(org.apache.kafka.common.config.provider.ConfigProvider) LoggerFactory(org.slf4j.LoggerFactory) Reflections(org.reflections.Reflections) ArrayList(java.util.ArrayList) Transformation(org.apache.kafka.connect.transforms.Transformation) LinkedHashMap(java.util.LinkedHashMap) Strings(com.google.common.base.Strings) SourceConnector(org.apache.kafka.connect.source.SourceConnector) Converter(org.apache.kafka.connect.storage.Converter) Map(java.util.Map) Method(java.lang.reflect.Method) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) LinkedHashSet(java.util.LinkedHashSet) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) ConfigDef(org.apache.kafka.common.config.ConfigDef) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) ConfigKeyComparator(com.github.jcustenborder.kafka.connect.utils.config.ConfigKeyComparator) Set(java.util.Set) Collectors(java.util.stream.Collectors) ClasspathHelper(org.reflections.util.ClasspathHelper) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResourcesScanner(org.reflections.scanners.ResourcesScanner) List(java.util.List) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) Comparator(java.util.Comparator) Connector(org.apache.kafka.connect.connector.Connector) SourceConnector(org.apache.kafka.connect.source.SourceConnector) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) Transformation(org.apache.kafka.connect.transforms.Transformation) ConfigProvider(org.apache.kafka.common.config.provider.ConfigProvider) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Converter(org.apache.kafka.connect.storage.Converter) ConfigDef(org.apache.kafka.common.config.ConfigDef)

Example 43 with Connector

use of org.apache.kafka.connect.connector.Connector in project kafka by apache.

the class Worker method startConnector.

/**
     * Start a connector managed by this worker.
     *
     * @param connName the connector name.
     * @param connProps the properties of the connector.
     * @param ctx the connector runtime context.
     * @param statusListener a listener for the runtime status transitions of the connector.
     * @param initialState the initial state of the connector.
     * @return true if the connector started successfully.
     */
public boolean startConnector(String connName, Map<String, String> connProps, ConnectorContext ctx, ConnectorStatus.Listener statusListener, TargetState initialState) {
    if (connectors.containsKey(connName))
        throw new ConnectException("Connector with name " + connName + " already exists");
    final WorkerConnector workerConnector;
    try {
        final ConnectorConfig connConfig = new ConnectorConfig(connProps);
        final String connClass = connConfig.getString(ConnectorConfig.CONNECTOR_CLASS_CONFIG);
        log.info("Creating connector {} of type {}", connName, connClass);
        final Connector connector = connectorFactory.newConnector(connClass);
        workerConnector = new WorkerConnector(connName, connector, ctx, statusListener);
        log.info("Instantiated connector {} with version {} of type {}", connName, connector.version(), connector.getClass());
        workerConnector.initialize(connConfig);
        workerConnector.transitionTo(initialState);
    } catch (Throwable t) {
        log.error("Failed to start connector {}", connName, t);
        statusListener.onFailure(connName, t);
        return false;
    }
    WorkerConnector existing = connectors.putIfAbsent(connName, workerConnector);
    if (existing != null)
        throw new ConnectException("Connector with name " + connName + " already exists");
    log.info("Finished creating connector {}", connName);
    return true;
}
Also used : Connector(org.apache.kafka.connect.connector.Connector) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 44 with Connector

use of org.apache.kafka.connect.connector.Connector in project kafka by apache.

the class AbstractHerder method validateConnectorConfig.

@Override
public ConfigInfos validateConnectorConfig(Map<String, String> connectorConfig) {
    String connType = connectorConfig.get(ConnectorConfig.CONNECTOR_CLASS_CONFIG);
    if (connType == null)
        throw new BadRequestException("Connector config " + connectorConfig + " contains no connector type");
    Connector connector = getConnector(connType);
    final ConfigDef connectorConfigDef = ConnectorConfig.enrich((connector instanceof SourceConnector) ? SourceConnectorConfig.configDef() : SinkConnectorConfig.configDef(), connectorConfig, false);
    List<ConfigValue> configValues = new ArrayList<>();
    Map<String, ConfigKey> configKeys = new HashMap<>();
    List<String> allGroups = new ArrayList<>();
    // do basic connector validation (name, connector type, etc.)
    Map<String, ConfigValue> validatedConnectorConfig = validateBasicConnectorConfig(connector, connectorConfigDef, connectorConfig);
    configValues.addAll(validatedConnectorConfig.values());
    configKeys.putAll(connectorConfigDef.configKeys());
    allGroups.addAll(connectorConfigDef.groups());
    // do custom connector-specific validation
    Config config = connector.validate(connectorConfig);
    ConfigDef configDef = connector.config();
    configKeys.putAll(configDef.configKeys());
    allGroups.addAll(configDef.groups());
    configValues.addAll(config.configValues());
    return generateResult(connType, configKeys, configValues, allGroups);
}
Also used : Connector(org.apache.kafka.connect.connector.Connector) SourceConnector(org.apache.kafka.connect.source.SourceConnector) ConfigValue(org.apache.kafka.common.config.ConfigValue) ConfigKey(org.apache.kafka.common.config.ConfigDef.ConfigKey) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Config(org.apache.kafka.common.config.Config) ArrayList(java.util.ArrayList) SourceConnector(org.apache.kafka.connect.source.SourceConnector) BadRequestException(org.apache.kafka.connect.runtime.rest.errors.BadRequestException) ConfigDef(org.apache.kafka.common.config.ConfigDef)

Example 45 with Connector

use of org.apache.kafka.connect.connector.Connector in project kafka by apache.

the class StandaloneHerderTest method expectConfigValidation.

private void expectConfigValidation(Map<String, String>... configs) {
    // config validation
    ConnectorFactory connectorFactoryMock = PowerMock.createMock(ConnectorFactory.class);
    EasyMock.expect(worker.getConnectorFactory()).andStubReturn(connectorFactoryMock);
    Connector connectorMock = PowerMock.createMock(Connector.class);
    EasyMock.expect(connectorFactoryMock.newConnector(EasyMock.anyString())).andReturn(connectorMock);
    EasyMock.expect(connectorMock.config()).andStubReturn(new ConfigDef());
    for (Map<String, String> config : configs) EasyMock.expect(connectorMock.validate(config)).andReturn(new Config(Collections.<ConfigValue>emptyList()));
}
Also used : SourceConnector(org.apache.kafka.connect.source.SourceConnector) Connector(org.apache.kafka.connect.connector.Connector) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) ConnectorFactory(org.apache.kafka.connect.runtime.ConnectorFactory) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) Config(org.apache.kafka.common.config.Config) TaskConfig(org.apache.kafka.connect.runtime.TaskConfig) ConfigDef(org.apache.kafka.common.config.ConfigDef)

Aggregations

Connector (org.apache.kafka.connect.connector.Connector)76 SourceConnector (org.apache.kafka.connect.source.SourceConnector)62 Test (org.junit.Test)54 SinkConnector (org.apache.kafka.connect.sink.SinkConnector)50 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)48 ConfigDef (org.apache.kafka.common.config.ConfigDef)35 HashMap (java.util.HashMap)34 WorkerConnector (org.apache.kafka.connect.runtime.WorkerConnector)31 ConnectorInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo)31 ConfigValue (org.apache.kafka.common.config.ConfigValue)30 Herder (org.apache.kafka.connect.runtime.Herder)27 Map (java.util.Map)26 Config (org.apache.kafka.common.config.Config)26 ConnectorConfig (org.apache.kafka.connect.runtime.ConnectorConfig)24 ArrayList (java.util.ArrayList)21 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)20 BadRequestException (org.apache.kafka.connect.runtime.rest.errors.BadRequestException)19 FutureCallback (org.apache.kafka.connect.util.FutureCallback)18 List (java.util.List)15 TaskConfig (org.apache.kafka.connect.runtime.TaskConfig)15