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