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);
}
}
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;
}
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"));
}
}
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));
}
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;
}
Aggregations