Search in sources :

Example 91 with ConnectException

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

the class EmbeddedConnectCluster method connectorStatus.

/**
 * Get the status for a connector running in this cluster.
 *
 * @param connectorName name of the connector
 * @return an instance of {@link ConnectorStateInfo} populated with state information of the connector and its tasks.
 * @throws ConnectRestException if the HTTP request to the REST API failed with a valid status code.
 * @throws ConnectException for any other error.
 */
public ConnectorStateInfo connectorStatus(String connectorName) {
    ObjectMapper mapper = new ObjectMapper();
    String url = endpointForResource(String.format("connectors/%s/status", connectorName));
    Response response = requestGet(url);
    try {
        if (response.getStatus() < Response.Status.BAD_REQUEST.getStatusCode()) {
            return mapper.readerFor(ConnectorStateInfo.class).readValue(responseToString(response));
        }
    } catch (IOException e) {
        log.error("Could not read connector state from response: {}", responseToString(response), e);
        throw new ConnectException("Could not not parse connector state", e);
    }
    throw new ConnectRestException(response.getStatus(), "Could not read connector state. Error response: " + responseToString(response));
}
Also used : Response(javax.ws.rs.core.Response) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 92 with ConnectException

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

the class EmbeddedConnectCluster method putConnectorConfig.

/**
 * Execute a PUT request with the given connector configuration on the given URL endpoint.
 *
 * @param url        the full URL of the endpoint that corresponds to the given REST resource
 * @param connConfig the intended configuration
 * @throws ConnectRestException if the REST api returns error status
 * @throws ConnectException if the configuration fails to be serialized or if the request could not be sent
 */
protected String putConnectorConfig(String url, Map<String, String> connConfig) {
    ObjectMapper mapper = new ObjectMapper();
    String content;
    try {
        content = mapper.writeValueAsString(connConfig);
    } catch (IOException e) {
        throw new ConnectException("Could not serialize connector configuration and execute PUT request");
    }
    Response response = requestPut(url, content);
    if (response.getStatus() < Response.Status.BAD_REQUEST.getStatusCode()) {
        return responseToString(response);
    }
    throw new ConnectRestException(response.getStatus(), "Could not execute PUT request. Error response: " + responseToString(response));
}
Also used : Response(javax.ws.rs.core.Response) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 93 with ConnectException

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

the class StandaloneHerderTest method testRestartConnectorFailureOnStart.

@Test
public void testRestartConnectorFailureOnStart() throws Exception {
    expectAdd(SourceSink.SOURCE);
    Map<String, String> config = connectorConfig(SourceSink.SOURCE);
    Connector connectorMock = PowerMock.createMock(SourceConnector.class);
    expectConfigValidation(connectorMock, true, config);
    worker.stopAndAwaitConnector(CONNECTOR_NAME);
    EasyMock.expectLastCall();
    Capture<Callback<TargetState>> onStart = EasyMock.newCapture();
    worker.startConnector(EasyMock.eq(CONNECTOR_NAME), EasyMock.eq(config), EasyMock.anyObject(HerderConnectorContext.class), EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED), EasyMock.capture(onStart));
    Exception exception = new ConnectException("Failed to start connector");
    EasyMock.expectLastCall().andAnswer(() -> {
        onStart.getValue().onCompletion(exception, null);
        return true;
    });
    PowerMock.replayAll();
    herder.putConnectorConfig(CONNECTOR_NAME, config, false, createCallback);
    Herder.Created<ConnectorInfo> connectorInfo = createCallback.get(1000L, TimeUnit.SECONDS);
    assertEquals(createdInfo(SourceSink.SOURCE), connectorInfo.result());
    FutureCallback<Void> restartCallback = new FutureCallback<>();
    herder.restartConnector(CONNECTOR_NAME, restartCallback);
    try {
        restartCallback.get(1000L, TimeUnit.MILLISECONDS);
        fail();
    } catch (ExecutionException e) {
        assertEquals(exception, e.getCause());
    }
    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) AlreadyExistsException(org.apache.kafka.connect.errors.AlreadyExistsException) BadRequestException(org.apache.kafka.connect.runtime.rest.errors.BadRequestException) NotFoundException(org.apache.kafka.connect.errors.NotFoundException) ExecutionException(java.util.concurrent.ExecutionException) ConnectException(org.apache.kafka.connect.errors.ConnectException) FutureCallback(org.apache.kafka.connect.util.FutureCallback) Callback(org.apache.kafka.connect.util.Callback) HerderConnectorContext(org.apache.kafka.connect.runtime.HerderConnectorContext) ExecutionException(java.util.concurrent.ExecutionException) Herder(org.apache.kafka.connect.runtime.Herder) FutureCallback(org.apache.kafka.connect.util.FutureCallback) ConnectException(org.apache.kafka.connect.errors.ConnectException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 94 with ConnectException

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

the class WorkerSourceTaskTest method testTopicCreateFails.

@Test
public void testTopicCreateFails() {
    if (!enableTopicCreation)
        // should only test with topic creation enabled
        return;
    createWorkerTask();
    SourceRecord record1 = new SourceRecord(PARTITION, OFFSET, TOPIC, 1, KEY_SCHEMA, KEY, RECORD_SCHEMA, RECORD);
    SourceRecord record2 = new SourceRecord(PARTITION, OFFSET, TOPIC, 2, KEY_SCHEMA, KEY, RECORD_SCHEMA, RECORD);
    expectPreliminaryCalls();
    EasyMock.expect(admin.describeTopics(TOPIC)).andReturn(Collections.emptyMap());
    Capture<NewTopic> newTopicCapture = EasyMock.newCapture();
    EasyMock.expect(admin.createOrFindTopics(EasyMock.capture(newTopicCapture))).andThrow(new ConnectException(new TopicAuthorizationException("unauthorized")));
    PowerMock.replayAll();
    Whitebox.setInternalState(workerTask, "toSend", Arrays.asList(record1, record2));
    assertThrows(ConnectException.class, () -> Whitebox.invokeMethod(workerTask, "sendRecords"));
    assertTrue(newTopicCapture.hasCaptured());
}
Also used : NewTopic(org.apache.kafka.clients.admin.NewTopic) SourceRecord(org.apache.kafka.connect.source.SourceRecord) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) ConnectException(org.apache.kafka.connect.errors.ConnectException) ThreadedTest(org.apache.kafka.connect.util.ThreadedTest) RetryWithToleranceOperatorTest(org.apache.kafka.connect.runtime.errors.RetryWithToleranceOperatorTest) ParameterizedTest(org.apache.kafka.connect.util.ParameterizedTest) Test(org.junit.Test)

Example 95 with ConnectException

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

the class WorkerSourceTaskTest method testTopicDescribeFails.

@Test
public void testTopicDescribeFails() {
    if (!enableTopicCreation)
        // should only test with topic creation enabled
        return;
    createWorkerTask();
    SourceRecord record1 = new SourceRecord(PARTITION, OFFSET, TOPIC, 1, KEY_SCHEMA, KEY, RECORD_SCHEMA, RECORD);
    SourceRecord record2 = new SourceRecord(PARTITION, OFFSET, TOPIC, 2, KEY_SCHEMA, KEY, RECORD_SCHEMA, RECORD);
    expectPreliminaryCalls();
    EasyMock.expect(admin.describeTopics(TOPIC)).andThrow(new ConnectException(new TopicAuthorizationException("unauthorized")));
    PowerMock.replayAll();
    Whitebox.setInternalState(workerTask, "toSend", Arrays.asList(record1, record2));
    assertThrows(ConnectException.class, () -> Whitebox.invokeMethod(workerTask, "sendRecords"));
}
Also used : SourceRecord(org.apache.kafka.connect.source.SourceRecord) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) ConnectException(org.apache.kafka.connect.errors.ConnectException) ThreadedTest(org.apache.kafka.connect.util.ThreadedTest) RetryWithToleranceOperatorTest(org.apache.kafka.connect.runtime.errors.RetryWithToleranceOperatorTest) ParameterizedTest(org.apache.kafka.connect.util.ParameterizedTest) Test(org.junit.Test)

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