Search in sources :

Example 1 with ConnectException

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

the class ConnectorFactory method getConnectorClass.

@SuppressWarnings("unchecked")
private static Class<? extends Connector> getConnectorClass(String connectorClassOrAlias) {
    // Avoid the classpath scan if the full class name was provided
    try {
        Class<?> clazz = Class.forName(connectorClassOrAlias);
        if (!Connector.class.isAssignableFrom(clazz))
            throw new ConnectException("Class " + connectorClassOrAlias + " does not implement Connector");
        return (Class<? extends Connector>) clazz;
    } catch (ClassNotFoundException e) {
    // Fall through to scan for the alias
    }
    // Iterate over our entire classpath to find all the connectors and hopefully one of them matches the alias from the connector configration
    Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forJavaClassPath()));
    Set<Class<? extends Connector>> connectors = reflections.getSubTypesOf(Connector.class);
    List<Class<? extends Connector>> results = new ArrayList<>();
    for (Class<? extends Connector> connector : connectors) {
        // Configuration included the class name but not package
        if (connector.getSimpleName().equals(connectorClassOrAlias))
            results.add(connector);
        // Configuration included a short version of the name (i.e. FileStreamSink instead of FileStreamSinkConnector)
        if (connector.getSimpleName().equals(connectorClassOrAlias + "Connector"))
            results.add(connector);
    }
    if (results.isEmpty())
        throw new ConnectException("Failed to find any class that implements Connector and which name matches " + connectorClassOrAlias + ", available connectors are: " + connectorNames(connectors));
    if (results.size() > 1) {
        throw new ConnectException("More than one connector matches alias " + connectorClassOrAlias + ". Please use full package and class name instead. Classes found: " + connectorNames(results));
    }
    // We just validated that we have exactly one result, so this is safe
    return results.get(0);
}
Also used : Connector(org.apache.kafka.connect.connector.Connector) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ArrayList(java.util.ArrayList) ConnectException(org.apache.kafka.connect.errors.ConnectException) Reflections(org.reflections.Reflections)

Example 2 with ConnectException

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

the class RestServer method start.

public void start(Herder herder) {
    log.info("Starting REST server");
    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.register(new JacksonJsonProvider());
    resourceConfig.register(RootResource.class);
    resourceConfig.register(new ConnectorsResource(herder));
    resourceConfig.register(new ConnectorPluginsResource(herder));
    resourceConfig.register(ConnectExceptionMapper.class);
    ServletContainer servletContainer = new ServletContainer(resourceConfig);
    ServletHolder servletHolder = new ServletHolder(servletContainer);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.addServlet(servletHolder, "/*");
    String allowedOrigins = config.getString(WorkerConfig.ACCESS_CONTROL_ALLOW_ORIGIN_CONFIG);
    if (allowedOrigins != null && !allowedOrigins.trim().isEmpty()) {
        FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
        filterHolder.setName("cross-origin");
        filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, allowedOrigins);
        String allowedMethods = config.getString(WorkerConfig.ACCESS_CONTROL_ALLOW_METHODS_CONFIG);
        if (allowedMethods != null && !allowedOrigins.trim().isEmpty()) {
            filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, allowedMethods);
        }
        context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    }
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    Slf4jRequestLog requestLog = new Slf4jRequestLog();
    requestLog.setLoggerName(RestServer.class.getCanonicalName());
    requestLog.setLogLatency(true);
    requestLogHandler.setRequestLog(requestLog);
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] { context, new DefaultHandler(), requestLogHandler });
    /* Needed for graceful shutdown as per `setStopTimeout` documentation */
    StatisticsHandler statsHandler = new StatisticsHandler();
    statsHandler.setHandler(handlers);
    jettyServer.setHandler(statsHandler);
    jettyServer.setStopTimeout(GRACEFUL_SHUTDOWN_TIMEOUT_MS);
    jettyServer.setStopAtShutdown(true);
    try {
        jettyServer.start();
    } catch (Exception e) {
        throw new ConnectException("Unable to start REST server", e);
    }
    log.info("REST server listening at " + jettyServer.getURI() + ", advertising URL " + advertisedUrl());
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) ConnectorsResource(org.apache.kafka.connect.runtime.rest.resources.ConnectorsResource) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ConnectorPluginsResource(org.apache.kafka.connect.runtime.rest.resources.ConnectorPluginsResource) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) IOException(java.io.IOException) ConnectException(org.apache.kafka.connect.errors.ConnectException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 3 with ConnectException

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

the class Worker method connectorTaskConfigs.

/**
     * Get a list of updated task properties for the tasks of this connector.
     *
     * @param connName the connector name.
     * @param maxTasks the maxinum number of tasks.
     * @param sinkTopics a list of sink topics.
     * @return a list of updated tasks properties.
     */
public List<Map<String, String>> connectorTaskConfigs(String connName, int maxTasks, List<String> sinkTopics) {
    log.trace("Reconfiguring connector tasks for {}", connName);
    WorkerConnector workerConnector = connectors.get(connName);
    if (workerConnector == null)
        throw new ConnectException("Connector " + connName + " not found in this worker.");
    Connector connector = workerConnector.connector();
    List<Map<String, String>> result = new ArrayList<>();
    String taskClassName = connector.taskClass().getName();
    for (Map<String, String> taskProps : connector.taskConfigs(maxTasks)) {
        // Ensure we don't modify the connector's copy of the config
        Map<String, String> taskConfig = new HashMap<>(taskProps);
        taskConfig.put(TaskConfig.TASK_CLASS_CONFIG, taskClassName);
        if (sinkTopics != null)
            taskConfig.put(SinkTask.TOPICS_CONFIG, Utils.join(sinkTopics, ","));
        result.add(taskConfig);
    }
    return result;
}
Also used : Connector(org.apache.kafka.connect.connector.Connector) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 4 with ConnectException

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

the class IgniteSinkTask method put.

/**
 * Buffers records.
 *
 * @param records Records to inject into grid.
 */
@SuppressWarnings("unchecked")
@Override
public void put(Collection<SinkRecord> records) {
    try {
        for (SinkRecord record : records) {
            // Data is flushed asynchronously when CACHE_PER_NODE_DATA_SIZE is reached.
            if (extractor != null) {
                Map.Entry<Object, Object> entry = extractor.extract(record);
                StreamerContext.getStreamer().addData(entry.getKey(), entry.getValue());
            } else {
                if (record.key() != null) {
                    StreamerContext.getStreamer().addData(record.key(), record.value());
                } else {
                    log.error("Failed to stream a record with null key!");
                }
            }
        }
    } catch (ConnectException e) {
        log.error("Failed adding record", e);
        throw new ConnectException(e);
    }
}
Also used : SinkRecord(org.apache.kafka.connect.sink.SinkRecord) Map(java.util.Map) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 5 with ConnectException

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

the class IgniteSourceConnector method start.

/**
 * {@inheritDoc}
 */
@Override
public void start(Map<String, String> props) {
    try {
        A.notNullOrEmpty(props.get(IgniteSourceConstants.CACHE_NAME), "cache name");
        A.notNullOrEmpty(props.get(IgniteSourceConstants.CACHE_CFG_PATH), "path to cache config file");
        A.notNullOrEmpty(props.get(IgniteSourceConstants.CACHE_EVENTS), "Registered cache events");
        A.notNullOrEmpty(props.get(IgniteSourceConstants.TOPIC_NAMES), "Kafka topics");
    } catch (IllegalArgumentException e) {
        throw new ConnectException("Cannot start IgniteSourceConnector due to configuration error", e);
    }
    configProps = props;
}
Also used : ConnectException(org.apache.kafka.connect.errors.ConnectException)

Aggregations

ConnectException (org.apache.kafka.connect.errors.ConnectException)184 HashMap (java.util.HashMap)38 IOException (java.io.IOException)28 Map (java.util.Map)28 ArrayList (java.util.ArrayList)23 Test (org.junit.Test)23 ExecutionException (java.util.concurrent.ExecutionException)22 TimeoutException (java.util.concurrent.TimeoutException)17 SQLException (java.sql.SQLException)16 SourceRecord (org.apache.kafka.connect.source.SourceRecord)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 Connector (org.apache.kafka.connect.connector.Connector)12 ConfigException (org.apache.kafka.common.config.ConfigException)11 TopicPartition (org.apache.kafka.common.TopicPartition)10 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)10 Collection (java.util.Collection)8 HashSet (java.util.HashSet)8 Set (java.util.Set)8 NotFoundException (org.apache.kafka.connect.errors.NotFoundException)8 SinkRecord (org.apache.kafka.connect.sink.SinkRecord)8