Search in sources :

Example 26 with Connector

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

the class ConnectorPluginsResourceTest method testValidateConfigWithSingleErrorDueToMissingConnectorClassname.

@Test
public void testValidateConfigWithSingleErrorDueToMissingConnectorClassname() throws Throwable {
    Capture<Callback<ConfigInfos>> configInfosCallback = EasyMock.newCapture();
    herder.validateConnectorConfig(EasyMock.eq(partialProps), EasyMock.capture(configInfosCallback), EasyMock.anyBoolean());
    PowerMock.expectLastCall().andAnswer((IAnswer<Void>) () -> {
        ConfigDef connectorConfigDef = ConnectorConfig.configDef();
        List<ConfigValue> connectorConfigValues = connectorConfigDef.validate(partialProps);
        Connector connector = new ConnectorPluginsResourceTestConnector();
        Config config = connector.validate(partialProps);
        ConfigDef configDef = connector.config();
        Map<String, ConfigDef.ConfigKey> configKeys = configDef.configKeys();
        List<ConfigValue> configValues = config.configValues();
        Map<String, ConfigDef.ConfigKey> resultConfigKeys = new HashMap<>(configKeys);
        resultConfigKeys.putAll(connectorConfigDef.configKeys());
        configValues.addAll(connectorConfigValues);
        ConfigInfos configInfos = AbstractHerder.generateResult(ConnectorPluginsResourceTestConnector.class.getName(), resultConfigKeys, configValues, Collections.singletonList("Test"));
        configInfosCallback.getValue().onCompletion(null, configInfos);
        return null;
    });
    PowerMock.replayAll();
    // This call to validateConfigs does not throw a BadRequestException because we've mocked
    // validateConnectorConfig.
    ConfigInfos configInfos = connectorPluginsResource.validateConfigs(ConnectorPluginsResourceTestConnector.class.getSimpleName(), partialProps);
    assertEquals(PARTIAL_CONFIG_INFOS.name(), configInfos.name());
    assertEquals(PARTIAL_CONFIG_INFOS.errorCount(), configInfos.errorCount());
    assertEquals(PARTIAL_CONFIG_INFOS.groups(), configInfos.groups());
    assertEquals(new HashSet<>(PARTIAL_CONFIG_INFOS.values()), new HashSet<>(configInfos.values()));
    PowerMock.verifyAll();
}
Also used : TestSourceConnector(org.apache.kafka.connect.runtime.TestSourceConnector) SourceConnector(org.apache.kafka.connect.source.SourceConnector) VerifiableSourceConnector(org.apache.kafka.connect.tools.VerifiableSourceConnector) MockSinkConnector(org.apache.kafka.connect.tools.MockSinkConnector) MockSourceConnector(org.apache.kafka.connect.tools.MockSourceConnector) VerifiableSinkConnector(org.apache.kafka.connect.tools.VerifiableSinkConnector) MockConnector(org.apache.kafka.connect.tools.MockConnector) Connector(org.apache.kafka.connect.connector.Connector) SchemaSourceConnector(org.apache.kafka.connect.tools.SchemaSourceConnector) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) TestSinkConnector(org.apache.kafka.connect.runtime.TestSinkConnector) Callback(org.apache.kafka.connect.util.Callback) WorkerConfig(org.apache.kafka.connect.runtime.WorkerConfig) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) Config(org.apache.kafka.common.config.Config) Arrays.asList(java.util.Arrays.asList) List(java.util.List) LinkedList(java.util.LinkedList) ConfigDef(org.apache.kafka.common.config.ConfigDef) Map(java.util.Map) HashMap(java.util.HashMap) ConfigInfos(org.apache.kafka.connect.runtime.rest.entities.ConfigInfos) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 27 with Connector

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

the class StandaloneHerderTest method testCreateConnectorFailedValidation.

@Test
public void testCreateConnectorFailedValidation() {
    // Basic validation should be performed and return an error, but should still evaluate the connector's config
    connector = PowerMock.createMock(BogusSourceConnector.class);
    Map<String, String> config = connectorConfig(SourceSink.SOURCE);
    config.remove(ConnectorConfig.NAME_CONFIG);
    Connector connectorMock = PowerMock.createMock(SourceConnector.class);
    EasyMock.expect(worker.configTransformer()).andReturn(transformer).times(2);
    final Capture<Map<String, String>> configCapture = EasyMock.newCapture();
    EasyMock.expect(transformer.transform(EasyMock.capture(configCapture))).andAnswer(configCapture::getValue);
    EasyMock.expect(worker.getPlugins()).andReturn(plugins).times(3);
    EasyMock.expect(plugins.compareAndSwapLoaders(connectorMock)).andReturn(delegatingLoader);
    EasyMock.expect(plugins.newConnector(EasyMock.anyString())).andReturn(connectorMock);
    EasyMock.expect(connectorMock.config()).andStubReturn(new ConfigDef());
    ConfigValue validatedValue = new ConfigValue("foo.bar");
    EasyMock.expect(connectorMock.validate(config)).andReturn(new Config(singletonList(validatedValue)));
    EasyMock.expect(Plugins.compareAndSwapLoaders(delegatingLoader)).andReturn(pluginLoader);
    PowerMock.replayAll();
    herder.putConnectorConfig(CONNECTOR_NAME, config, false, createCallback);
    ExecutionException exception = assertThrows(ExecutionException.class, () -> createCallback.get(1000L, TimeUnit.SECONDS));
    assertEquals(BadRequestException.class, exception.getCause().getClass());
    PowerMock.verifyAll();
}
Also used : SourceConnector(org.apache.kafka.connect.source.SourceConnector) WorkerConnector(org.apache.kafka.connect.runtime.WorkerConnector) Connector(org.apache.kafka.connect.connector.Connector) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) ConfigValue(org.apache.kafka.common.config.ConfigValue) SourceConnectorConfig(org.apache.kafka.connect.runtime.SourceConnectorConfig) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) Config(org.apache.kafka.common.config.Config) SinkConnectorConfig(org.apache.kafka.connect.runtime.SinkConnectorConfig) TaskConfig(org.apache.kafka.connect.runtime.TaskConfig) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) ConfigDef(org.apache.kafka.common.config.ConfigDef) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 28 with Connector

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

the class StandaloneHerderTest method testRestartConnectorAndTasksNoStatus.

@Test
public void testRestartConnectorAndTasksNoStatus() throws Exception {
    RestartRequest restartRequest = new RestartRequest(CONNECTOR_NAME, false, true);
    EasyMock.expect(herder.buildRestartPlan(restartRequest)).andReturn(Optional.empty()).anyTimes();
    connector = PowerMock.createMock(BogusSinkConnector.class);
    expectAdd(SourceSink.SINK);
    Map<String, String> connectorConfig = connectorConfig(SourceSink.SINK);
    Connector connectorMock = PowerMock.createMock(SinkConnector.class);
    expectConfigValidation(connectorMock, true, connectorConfig);
    PowerMock.replayAll();
    herder.putConnectorConfig(CONNECTOR_NAME, connectorConfig, false, createCallback);
    Herder.Created<ConnectorInfo> connectorInfo = createCallback.get(1000L, TimeUnit.SECONDS);
    assertEquals(createdInfo(SourceSink.SINK), connectorInfo.result());
    FutureCallback<ConnectorStateInfo> restartCallback = new FutureCallback<>();
    herder.restartConnectorAndTasks(restartRequest, restartCallback);
    ExecutionException ee = assertThrows(ExecutionException.class, () -> restartCallback.get(1000L, TimeUnit.MILLISECONDS));
    assertTrue(ee.getCause() instanceof NotFoundException);
    assertTrue(ee.getMessage().contains("Status for connector"));
    PowerMock.verifyAll();
}
Also used : SourceConnector(org.apache.kafka.connect.source.SourceConnector) WorkerConnector(org.apache.kafka.connect.runtime.WorkerConnector) Connector(org.apache.kafka.connect.connector.Connector) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) RestartRequest(org.apache.kafka.connect.runtime.RestartRequest) NotFoundException(org.apache.kafka.connect.errors.NotFoundException) ExecutionException(java.util.concurrent.ExecutionException) Herder(org.apache.kafka.connect.runtime.Herder) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) FutureCallback(org.apache.kafka.connect.util.FutureCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 29 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 30 with Connector

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

the class DistributedHerderTest method testCreateConnector.

@Test
public void testCreateConnector() throws Exception {
    EasyMock.expect(member.memberId()).andStubReturn("leader");
    expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());
    expectPostRebalanceCatchup(SNAPSHOT);
    member.wakeup();
    PowerMock.expectLastCall();
    // config validation
    Connector connectorMock = PowerMock.createMock(SourceConnector.class);
    EasyMock.expect(worker.getPlugins()).andReturn(plugins).times(3);
    EasyMock.expect(plugins.compareAndSwapLoaders(connectorMock)).andReturn(delegatingLoader);
    EasyMock.expect(plugins.newConnector(EasyMock.anyString())).andReturn(connectorMock);
    EasyMock.expect(connectorMock.config()).andReturn(new ConfigDef());
    EasyMock.expect(connectorMock.validate(CONN2_CONFIG)).andReturn(new Config(Collections.<ConfigValue>emptyList()));
    EasyMock.expect(Plugins.compareAndSwapLoaders(delegatingLoader)).andReturn(pluginLoader);
    // CONN2 is new, should succeed
    configBackingStore.putConnectorConfig(CONN2, CONN2_CONFIG);
    PowerMock.expectLastCall();
    ConnectorInfo info = new ConnectorInfo(CONN2, CONN2_CONFIG, Collections.<ConnectorTaskId>emptyList(), ConnectorType.SOURCE);
    putConnectorCallback.onCompletion(null, new Herder.Created<>(true, info));
    PowerMock.expectLastCall();
    member.poll(EasyMock.anyInt());
    PowerMock.expectLastCall();
    // No immediate action besides this -- change will be picked up via the config log
    PowerMock.replayAll();
    herder.putConnectorConfig(CONN2, CONN2_CONFIG, false, putConnectorCallback);
    herder.tick();
    time.sleep(1000L);
    assertStatistics(3, 1, 100, 1000L);
    PowerMock.verifyAll();
}
Also used : SourceConnector(org.apache.kafka.connect.source.SourceConnector) Connector(org.apache.kafka.connect.connector.Connector) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) ConfigValue(org.apache.kafka.common.config.ConfigValue) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) WorkerConfig(org.apache.kafka.connect.runtime.WorkerConfig) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) Config(org.apache.kafka.common.config.Config) SinkConnectorConfig(org.apache.kafka.connect.runtime.SinkConnectorConfig) TaskConfig(org.apache.kafka.connect.runtime.TaskConfig) ConfigDef(org.apache.kafka.common.config.ConfigDef) Herder(org.apache.kafka.connect.runtime.Herder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Connector (org.apache.kafka.connect.connector.Connector)80 SourceConnector (org.apache.kafka.connect.source.SourceConnector)62 Test (org.junit.Test)58 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)55 SinkConnector (org.apache.kafka.connect.sink.SinkConnector)53 HashMap (java.util.HashMap)38 ConfigDef (org.apache.kafka.common.config.ConfigDef)35 WorkerConnector (org.apache.kafka.connect.runtime.WorkerConnector)31 ConnectorInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo)31 Map (java.util.Map)30 ConfigValue (org.apache.kafka.common.config.ConfigValue)27 Herder (org.apache.kafka.connect.runtime.Herder)27 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 List (java.util.List)18 FutureCallback (org.apache.kafka.connect.util.FutureCallback)18 ConnectException (org.apache.kafka.connect.errors.ConnectException)16