Search in sources :

Example 1 with ActiveTopicsInfo

use of org.apache.kafka.connect.runtime.rest.entities.ActiveTopicsInfo in project kafka by apache.

the class EmbeddedConnectClusterAssertions method checkConnectorActiveTopics.

/**
 * Check whether a connector's set of active topics matches the given collection of topic names.
 *
 * @param connectorName the connector name
 * @param topics a collection of topics to compare against
 * @return true if the connector's active topics matches the given collection; false otherwise
 */
protected Optional<Boolean> checkConnectorActiveTopics(String connectorName, Collection<String> topics) {
    try {
        ActiveTopicsInfo info = connect.connectorTopics(connectorName);
        boolean result = info != null && topics.size() == info.topics().size() && topics.containsAll(info.topics());
        log.debug("Found connector {} using topics: {}", connectorName, info.topics());
        return Optional.of(result);
    } catch (Exception e) {
        log.error("Could not check connector {} state info.", connectorName, e);
        return Optional.empty();
    }
}
Also used : ActiveTopicsInfo(org.apache.kafka.connect.runtime.rest.entities.ActiveTopicsInfo) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException)

Example 2 with ActiveTopicsInfo

use of org.apache.kafka.connect.runtime.rest.entities.ActiveTopicsInfo in project kafka by apache.

the class EmbeddedConnectCluster method connectorTopics.

/**
 * Get the active topics of 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 ActiveTopicsInfo connectorTopics(String connectorName) {
    ObjectMapper mapper = new ObjectMapper();
    String url = endpointForResource(String.format("connectors/%s/topics", connectorName));
    Response response = requestGet(url);
    try {
        if (response.getStatus() < Response.Status.BAD_REQUEST.getStatusCode()) {
            Map<String, Map<String, List<String>>> activeTopics = mapper.readerFor(new TypeReference<Map<String, Map<String, List<String>>>>() {
            }).readValue(responseToString(response));
            return new ActiveTopicsInfo(connectorName, activeTopics.get(connectorName).getOrDefault("topics", Collections.emptyList()));
        }
    } 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) ActiveTopicsInfo(org.apache.kafka.connect.runtime.rest.entities.ActiveTopicsInfo) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 3 with ActiveTopicsInfo

use of org.apache.kafka.connect.runtime.rest.entities.ActiveTopicsInfo in project kafka by apache.

the class ConnectorsResourceTest method testConnectorActiveTopics.

@Test
public void testConnectorActiveTopics() {
    PowerMock.reset(workerConfig);
    EasyMock.expect(workerConfig.getBoolean(TOPIC_TRACKING_ENABLE_CONFIG)).andReturn(true);
    EasyMock.expect(workerConfig.getBoolean(TOPIC_TRACKING_ALLOW_RESET_CONFIG)).andReturn(true);
    EasyMock.expect(herder.connectorActiveTopics(CONNECTOR_NAME)).andReturn(new ActiveTopicsInfo(CONNECTOR_NAME, CONNECTOR_ACTIVE_TOPICS));
    PowerMock.replay(workerConfig);
    connectorsResource = new ConnectorsResource(herder, workerConfig);
    PowerMock.replayAll();
    Response response = connectorsResource.getConnectorActiveTopics(CONNECTOR_NAME);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    Map<String, Map<String, Object>> body = (Map<String, Map<String, Object>>) response.getEntity();
    assertEquals(CONNECTOR_NAME, ((ActiveTopicsInfo) body.get(CONNECTOR_NAME)).connector());
    assertEquals(new HashSet<>(CONNECTOR_ACTIVE_TOPICS), ((ActiveTopicsInfo) body.get(CONNECTOR_NAME)).topics());
    PowerMock.verifyAll();
}
Also used : Response(javax.ws.rs.core.Response) ActiveTopicsInfo(org.apache.kafka.connect.runtime.rest.entities.ActiveTopicsInfo) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ActiveTopicsInfo (org.apache.kafka.connect.runtime.rest.entities.ActiveTopicsInfo)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Response (javax.ws.rs.core.Response)2 ConnectRestException (org.apache.kafka.connect.runtime.rest.errors.ConnectRestException)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 ConnectException (org.apache.kafka.connect.errors.ConnectException)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1