Search in sources :

Example 86 with ConnectException

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

the class EmbeddedConnectCluster method requestHttpMethod.

/**
 * A general method that executes an HTTP request on a given URL.
 *
 * @param url the HTTP endpoint
 * @param body the payload of the request; null if there isn't one
 * @param headers a map that stores the request headers; empty if there are no headers
 * @param httpMethod the name of the HTTP method to execute
 * @return the response to the HTTP request
 * @throws ConnectException if execution of the HTTP method fails
 */
protected Response requestHttpMethod(String url, String body, Map<String, String> headers, String httpMethod) {
    log.debug("Executing {} request to URL={}." + (body != null ? " Payload={}" : ""), httpMethod, url, body);
    try {
        HttpURLConnection httpCon = (HttpURLConnection) new URL(url).openConnection();
        httpCon.setDoOutput(true);
        httpCon.setRequestMethod(httpMethod);
        if (body != null) {
            httpCon.setRequestProperty("Content-Type", "application/json");
            headers.forEach(httpCon::setRequestProperty);
            try (OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream())) {
                out.write(body);
            }
        }
        try (InputStream is = httpCon.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST ? httpCon.getInputStream() : httpCon.getErrorStream()) {
            String responseEntity = responseToString(is);
            log.info("{} response for URL={} is {}", httpMethod, url, responseEntity.isEmpty() ? "empty" : responseEntity);
            return Response.status(Response.Status.fromStatusCode(httpCon.getResponseCode())).entity(responseEntity).build();
        }
    } catch (IOException e) {
        log.error("Could not execute " + httpMethod + " request to " + url, e);
        throw new ConnectException(e);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) URL(java.net.URL) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 87 with ConnectException

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

the class EmbeddedKafkaCluster method createConsumer.

public KafkaConsumer<byte[], byte[]> createConsumer(Map<String, Object> consumerProps) {
    Map<String, Object> props = new HashMap<>(consumerProps);
    putIfAbsent(props, GROUP_ID_CONFIG, UUID.randomUUID().toString());
    putIfAbsent(props, BOOTSTRAP_SERVERS_CONFIG, bootstrapServers());
    putIfAbsent(props, ENABLE_AUTO_COMMIT_CONFIG, "false");
    putIfAbsent(props, AUTO_OFFSET_RESET_CONFIG, "earliest");
    putIfAbsent(props, KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArrayDeserializer");
    putIfAbsent(props, VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArrayDeserializer");
    if (sslEnabled()) {
        putIfAbsent(props, SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, brokerConfig.get(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG));
        putIfAbsent(props, SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, brokerConfig.get(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG));
        putIfAbsent(props, CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SSL");
    }
    KafkaConsumer<byte[], byte[]> consumer;
    try {
        consumer = new KafkaConsumer<>(props);
    } catch (Throwable t) {
        throw new ConnectException("Failed to create consumer", t);
    }
    return consumer;
}
Also used : HashMap(java.util.HashMap) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 88 with ConnectException

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

the class TopicAdminTest method endOffsetsShouldFailWithNonRetriableWhenUnknownErrorOccurs.

@Test
public void endOffsetsShouldFailWithNonRetriableWhenUnknownErrorOccurs() {
    String topicName = "myTopic";
    TopicPartition tp1 = new TopicPartition(topicName, 0);
    Set<TopicPartition> tps = Collections.singleton(tp1);
    // response should use error
    Long offset = null;
    Cluster cluster = createCluster(1, topicName, 1);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(new MockTime(), cluster)) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponse(prepareMetadataResponse(cluster, Errors.NONE));
        env.kafkaClient().prepareResponse(listOffsetsResultWithUnknownError(tp1, offset));
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        ConnectException e = assertThrows(ConnectException.class, () -> admin.endOffsets(tps));
        assertTrue(e.getMessage().contains("Error while getting end offsets for topic"));
    }
}
Also used : AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) TopicPartition(org.apache.kafka.common.TopicPartition) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) ConnectException(org.apache.kafka.connect.errors.ConnectException) Test(org.junit.Test)

Example 89 with ConnectException

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

the class EmbeddedConnectCluster method connectors.

/**
 * Get the connector names of the connectors currently running on this cluster.
 *
 * @return the list of connector names
 * @throws ConnectRestException if the HTTP request to the REST API failed with a valid status code.
 * @throws ConnectException for any other error.
 */
public Collection<String> connectors() {
    ObjectMapper mapper = new ObjectMapper();
    String url = endpointForResource("connectors");
    Response response = requestGet(url);
    if (response.getStatus() < Response.Status.BAD_REQUEST.getStatusCode()) {
        try {
            return mapper.readerFor(Collection.class).readValue(responseToString(response));
        } catch (IOException e) {
            log.error("Could not parse connector list from response: {}", responseToString(response), e);
            throw new ConnectException("Could not not parse connector list", e);
        }
    }
    throw new ConnectRestException(response.getStatus(), "Could not read connector list. Error response: " + responseToString(response));
}
Also used : Response(javax.ws.rs.core.Response) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) Collection(java.util.Collection) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 90 with ConnectException

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

the class EmbeddedConnectCluster method endpointForResourceNotRunningConnector.

/**
 * Get the full URL of the endpoint that corresponds to the given REST resource using a worker
 * that is not running any tasks or connector instance for the connectorName provided in the arguments
 *
 * @param resource the resource under the worker's admin endpoint
 * @param connectorName the name of the connector
 * @return the admin endpoint URL
 * @throws ConnectException if no REST endpoint is available
 */
public String endpointForResourceNotRunningConnector(String resource, String connectorName) {
    ConnectorStateInfo info = connectorStatus(connectorName);
    Set<String> activeWorkerUrls = new HashSet<>();
    activeWorkerUrls.add(String.format("http://%s/", info.connector().workerId()));
    info.tasks().forEach(t -> activeWorkerUrls.add(String.format("http://%s/", t.workerId())));
    String url = connectCluster.stream().map(WorkerHandle::url).filter(Objects::nonNull).filter(workerUrl -> !activeWorkerUrls.contains(workerUrl.toString())).findFirst().orElseThrow(() -> new ConnectException(String.format("Connect workers have not been provisioned or no free worker found that is not running this connector(%s) or its tasks", connectorName))).toString();
    return url + resource;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Exit(org.apache.kafka.common.utils.Exit) URL(java.net.URL) Plugins(org.apache.kafka.connect.runtime.isolation.Plugins) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) LISTENERS_CONFIG(org.apache.kafka.connect.runtime.WorkerConfig.LISTENERS_CONFIG) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) STATUS_STORAGE_REPLICATION_FACTOR_CONFIG(org.apache.kafka.connect.runtime.distributed.DistributedConfig.STATUS_STORAGE_REPLICATION_FACTOR_CONFIG) Map(java.util.Map) ServerInfo(org.apache.kafka.connect.runtime.rest.entities.ServerInfo) OutputStreamWriter(java.io.OutputStreamWriter) OFFSET_STORAGE_REPLICATION_FACTOR_CONFIG(org.apache.kafka.connect.runtime.distributed.DistributedConfig.OFFSET_STORAGE_REPLICATION_FACTOR_CONFIG) TypeReference(com.fasterxml.jackson.core.type.TypeReference) LinkedHashSet(java.util.LinkedHashSet) VALUE_CONVERTER_CLASS_CONFIG(org.apache.kafka.connect.runtime.ConnectorConfig.VALUE_CONVERTER_CLASS_CONFIG) Logger(org.slf4j.Logger) Properties(java.util.Properties) Iterator(java.util.Iterator) GROUP_ID_CONFIG(org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_ID_CONFIG) Collection(java.util.Collection) CONFIG_STORAGE_REPLICATION_FACTOR_CONFIG(org.apache.kafka.connect.runtime.distributed.DistributedConfig.CONFIG_STORAGE_REPLICATION_FACTOR_CONFIG) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) STATUS_STORAGE_TOPIC_CONFIG(org.apache.kafka.connect.runtime.distributed.DistributedConfig.STATUS_STORAGE_TOPIC_CONFIG) KEY_CONVERTER_CLASS_CONFIG(org.apache.kafka.connect.runtime.ConnectorConfig.KEY_CONVERTER_CLASS_CONFIG) Objects(java.util.Objects) BOOTSTRAP_SERVERS_CONFIG(org.apache.kafka.connect.runtime.WorkerConfig.BOOTSTRAP_SERVERS_CONFIG) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) ActiveTopicsInfo(org.apache.kafka.connect.runtime.rest.entities.ActiveTopicsInfo) List(java.util.List) Response(javax.ws.rs.core.Response) ConfigInfos(org.apache.kafka.connect.runtime.rest.entities.ConfigInfos) ConnectException(org.apache.kafka.connect.errors.ConnectException) OFFSET_STORAGE_TOPIC_CONFIG(org.apache.kafka.connect.runtime.distributed.DistributedConfig.OFFSET_STORAGE_TOPIC_CONFIG) CONFIG_TOPIC_CONFIG(org.apache.kafka.connect.runtime.distributed.DistributedConfig.CONFIG_TOPIC_CONFIG) Collections(java.util.Collections) InputStream(java.io.InputStream) Objects(java.util.Objects) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) 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