Search in sources :

Example 1 with CONNECTOR_SETUP_DURATION_MS

use of org.apache.kafka.connect.util.clusters.EmbeddedConnectClusterAssertions.CONNECTOR_SETUP_DURATION_MS in project kafka by apache.

the class ConnectorRestartApiIntegrationTest method runningConnectorAndTasksRestart.

private void runningConnectorAndTasksRestart(boolean onlyFailed, boolean includeTasks, int expectedConnectorRestarts, Map<String, Integer> expectedTasksRestarts, boolean noopRequest, int numWorkers) throws Exception {
    startOrReuseConnectWithNumWorkers(numWorkers);
    // setup up props for the source connector
    Map<String, String> props = defaultSourceConnectorProps(TOPIC_NAME);
    // Try to start the connector and its single task.
    connect.configureConnector(connectorName, props);
    connect.assertions().assertConnectorAndAtLeastNumTasksAreRunning(connectorName, NUM_TASKS, "Connector tasks are not all in running state.");
    StartsAndStops beforeSnapshot = connectorHandle.startAndStopCounter().countsSnapshot();
    Map<String, StartsAndStops> beforeTasksSnapshot = connectorHandle.tasks().stream().collect(Collectors.toMap(TaskHandle::taskId, task -> task.startAndStopCounter().countsSnapshot()));
    StartAndStopLatch stopLatch = connectorHandle.expectedStops(expectedConnectorRestarts, expectedTasksRestarts, includeTasks);
    StartAndStopLatch startLatch = connectorHandle.expectedStarts(expectedConnectorRestarts, expectedTasksRestarts, includeTasks);
    ConnectorStateInfo connectorStateInfo;
    // Call the Restart API
    if (numWorkers == 1) {
        connectorStateInfo = connect.restartConnectorAndTasks(connectorName, onlyFailed, includeTasks, false);
    } else {
        connectorStateInfo = connect.restartConnectorAndTasks(connectorName, onlyFailed, includeTasks, true);
    }
    if (noopRequest) {
        assertNoRestartingState(connectorStateInfo);
    }
    // Wait for the connector to be stopped
    assertTrue("Failed to stop connector and tasks within " + CONNECTOR_SETUP_DURATION_MS + "ms", stopLatch.await(CONNECTOR_SETUP_DURATION_MS, TimeUnit.MILLISECONDS));
    connect.assertions().assertConnectorAndAtLeastNumTasksAreRunning(connectorName, NUM_TASKS, "Connector tasks are not all in running state.");
    // Expect that the connector has started again
    assertTrue("Failed to start connector and tasks within " + CONNECTOR_SETUP_DURATION_MS + "ms", startLatch.await(CONNECTOR_SETUP_DURATION_MS, TimeUnit.MILLISECONDS));
    StartsAndStops afterSnapshot = connectorHandle.startAndStopCounter().countsSnapshot();
    assertEquals(beforeSnapshot.starts() + expectedConnectorRestarts, afterSnapshot.starts());
    assertEquals(beforeSnapshot.stops() + expectedConnectorRestarts, afterSnapshot.stops());
    connectorHandle.tasks().forEach(t -> {
        StartsAndStops afterTaskSnapshot = t.startAndStopCounter().countsSnapshot();
        if (numWorkers == 1) {
            assertEquals(beforeTasksSnapshot.get(t.taskId()).starts() + expectedTasksRestarts.get(t.taskId()), afterTaskSnapshot.starts());
            assertEquals(beforeTasksSnapshot.get(t.taskId()).stops() + expectedTasksRestarts.get(t.taskId()), afterTaskSnapshot.stops());
        } else {
            // validate tasks stop/start counts only in single worker test because the multi worker rebalance triggers stop/start on task and this make the exact counts unpredictable
            assertTrue(afterTaskSnapshot.starts() >= beforeTasksSnapshot.get(t.taskId()).starts() + expectedTasksRestarts.get(t.taskId()));
            assertTrue(afterTaskSnapshot.stops() >= beforeTasksSnapshot.get(t.taskId()).stops() + expectedTasksRestarts.get(t.taskId()));
        }
    });
}
Also used : PARTITIONS_CONFIG(org.apache.kafka.connect.runtime.TopicCreationConfig.PARTITIONS_CONFIG) TestRule(org.junit.rules.TestRule) LoggerFactory(org.slf4j.LoggerFactory) IntegrationTest(org.apache.kafka.test.IntegrationTest) HashMap(java.util.HashMap) TOPIC_CONFIG(org.apache.kafka.connect.integration.MonitorableSourceConnector.TOPIC_CONFIG) HashSet(java.util.HashSet) TestName(org.junit.rules.TestName) After(org.junit.After) Map(java.util.Map) OFFSET_COMMIT_INTERVAL_MS_CONFIG(org.apache.kafka.connect.runtime.WorkerConfig.OFFSET_COMMIT_INTERVAL_MS_CONFIG) StringConverter(org.apache.kafka.connect.storage.StringConverter) EmbeddedConnectCluster(org.apache.kafka.connect.util.clusters.EmbeddedConnectCluster) CONNECTOR_CLASS_CONFIG(org.apache.kafka.connect.runtime.ConnectorConfig.CONNECTOR_CLASS_CONFIG) VALUE_CONVERTER_CLASS_CONFIG(org.apache.kafka.connect.runtime.ConnectorConfig.VALUE_CONVERTER_CLASS_CONFIG) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) Properties(java.util.Properties) AbstractStatus(org.apache.kafka.connect.runtime.AbstractStatus) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) DEFAULT_TOPIC_CREATION_PREFIX(org.apache.kafka.connect.runtime.TopicCreationConfig.DEFAULT_TOPIC_CREATION_PREFIX) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) KEY_CONVERTER_CLASS_CONFIG(org.apache.kafka.connect.runtime.ConnectorConfig.KEY_CONVERTER_CLASS_CONFIG) TimeUnit(java.util.concurrent.TimeUnit) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) TASKS_MAX_CONFIG(org.apache.kafka.connect.runtime.ConnectorConfig.TASKS_MAX_CONFIG) Rule(org.junit.Rule) Response(javax.ws.rs.core.Response) REPLICATION_FACTOR_CONFIG(org.apache.kafka.connect.runtime.TopicCreationConfig.REPLICATION_FACTOR_CONFIG) CONNECTOR_SETUP_DURATION_MS(org.apache.kafka.connect.util.clusters.EmbeddedConnectClusterAssertions.CONNECTOR_SETUP_DURATION_MS) CONNECTOR_CLIENT_POLICY_CLASS_CONFIG(org.apache.kafka.connect.runtime.WorkerConfig.CONNECTOR_CLIENT_POLICY_CLASS_CONFIG) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo)

Example 2 with CONNECTOR_SETUP_DURATION_MS

use of org.apache.kafka.connect.util.clusters.EmbeddedConnectClusterAssertions.CONNECTOR_SETUP_DURATION_MS in project kafka by apache.

the class ConnectorRestartApiIntegrationTest method failedTasksRestart.

private void failedTasksRestart(boolean onlyFailed, boolean includeTasks, int expectedConnectorRestarts, Map<String, Integer> expectedTasksRestarts, Set<String> tasksToFail, boolean noopRequest) throws Exception {
    // setup up props for the source connector
    Map<String, String> props = defaultSourceConnectorProps(TOPIC_NAME);
    tasksToFail.forEach(taskId -> props.put("task-" + taskId + ".start.inject.error", "true"));
    // build a Connect cluster backed by Kafka and Zk
    startOrReuseConnectWithNumWorkers(ONE_WORKER);
    // Try to start the connector and its single task.
    connect.configureConnector(connectorName, props);
    connect.assertions().assertConnectorIsRunningAndNumTasksHaveFailed(connectorName, NUM_TASKS, tasksToFail.size(), "Connector tasks are in running state.");
    StartsAndStops beforeSnapshot = connectorHandle.startAndStopCounter().countsSnapshot();
    Map<String, StartsAndStops> beforeTasksSnapshot = connectorHandle.tasks().stream().collect(Collectors.toMap(TaskHandle::taskId, task -> task.startAndStopCounter().countsSnapshot()));
    StartAndStopLatch stopLatch = connectorHandle.expectedStops(expectedConnectorRestarts, expectedTasksRestarts, includeTasks);
    StartAndStopLatch startLatch = connectorHandle.expectedStarts(expectedConnectorRestarts, expectedTasksRestarts, includeTasks);
    // Call the Restart API
    ConnectorStateInfo connectorStateInfo = connect.restartConnectorAndTasks(connectorName, onlyFailed, includeTasks, false);
    if (noopRequest) {
        assertNoRestartingState(connectorStateInfo);
    }
    // Wait for the connector to be stopped
    assertTrue("Failed to stop connector and tasks within " + CONNECTOR_SETUP_DURATION_MS + "ms", stopLatch.await(CONNECTOR_SETUP_DURATION_MS, TimeUnit.MILLISECONDS));
    connect.assertions().assertConnectorIsRunningAndNumTasksHaveFailed(connectorName, NUM_TASKS, tasksToFail.size(), "Connector tasks are not all in running state.");
    // Expect that the connector has started again
    assertTrue("Failed to start connector and tasks within " + CONNECTOR_SETUP_DURATION_MS + "ms", startLatch.await(CONNECTOR_SETUP_DURATION_MS, TimeUnit.MILLISECONDS));
    StartsAndStops afterSnapshot = connectorHandle.startAndStopCounter().countsSnapshot();
    assertEquals(beforeSnapshot.starts() + expectedConnectorRestarts, afterSnapshot.starts());
    assertEquals(beforeSnapshot.stops() + expectedConnectorRestarts, afterSnapshot.stops());
    connectorHandle.tasks().forEach(t -> {
        StartsAndStops afterTaskSnapshot = t.startAndStopCounter().countsSnapshot();
        assertEquals(beforeTasksSnapshot.get(t.taskId()).starts() + expectedTasksRestarts.get(t.taskId()), afterTaskSnapshot.starts());
        assertEquals(beforeTasksSnapshot.get(t.taskId()).stops() + expectedTasksRestarts.get(t.taskId()), afterTaskSnapshot.stops());
    });
}
Also used : PARTITIONS_CONFIG(org.apache.kafka.connect.runtime.TopicCreationConfig.PARTITIONS_CONFIG) TestRule(org.junit.rules.TestRule) LoggerFactory(org.slf4j.LoggerFactory) IntegrationTest(org.apache.kafka.test.IntegrationTest) HashMap(java.util.HashMap) TOPIC_CONFIG(org.apache.kafka.connect.integration.MonitorableSourceConnector.TOPIC_CONFIG) HashSet(java.util.HashSet) TestName(org.junit.rules.TestName) After(org.junit.After) Map(java.util.Map) OFFSET_COMMIT_INTERVAL_MS_CONFIG(org.apache.kafka.connect.runtime.WorkerConfig.OFFSET_COMMIT_INTERVAL_MS_CONFIG) StringConverter(org.apache.kafka.connect.storage.StringConverter) EmbeddedConnectCluster(org.apache.kafka.connect.util.clusters.EmbeddedConnectCluster) CONNECTOR_CLASS_CONFIG(org.apache.kafka.connect.runtime.ConnectorConfig.CONNECTOR_CLASS_CONFIG) VALUE_CONVERTER_CLASS_CONFIG(org.apache.kafka.connect.runtime.ConnectorConfig.VALUE_CONVERTER_CLASS_CONFIG) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) Properties(java.util.Properties) AbstractStatus(org.apache.kafka.connect.runtime.AbstractStatus) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) DEFAULT_TOPIC_CREATION_PREFIX(org.apache.kafka.connect.runtime.TopicCreationConfig.DEFAULT_TOPIC_CREATION_PREFIX) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) KEY_CONVERTER_CLASS_CONFIG(org.apache.kafka.connect.runtime.ConnectorConfig.KEY_CONVERTER_CLASS_CONFIG) TimeUnit(java.util.concurrent.TimeUnit) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) TASKS_MAX_CONFIG(org.apache.kafka.connect.runtime.ConnectorConfig.TASKS_MAX_CONFIG) Rule(org.junit.Rule) Response(javax.ws.rs.core.Response) REPLICATION_FACTOR_CONFIG(org.apache.kafka.connect.runtime.TopicCreationConfig.REPLICATION_FACTOR_CONFIG) CONNECTOR_SETUP_DURATION_MS(org.apache.kafka.connect.util.clusters.EmbeddedConnectClusterAssertions.CONNECTOR_SETUP_DURATION_MS) CONNECTOR_CLIENT_POLICY_CLASS_CONFIG(org.apache.kafka.connect.runtime.WorkerConfig.CONNECTOR_CLIENT_POLICY_CLASS_CONFIG) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo)

Aggregations

Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Properties (java.util.Properties)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 Response (javax.ws.rs.core.Response)2 TOPIC_CONFIG (org.apache.kafka.connect.integration.MonitorableSourceConnector.TOPIC_CONFIG)2 AbstractStatus (org.apache.kafka.connect.runtime.AbstractStatus)2 CONNECTOR_CLASS_CONFIG (org.apache.kafka.connect.runtime.ConnectorConfig.CONNECTOR_CLASS_CONFIG)2 KEY_CONVERTER_CLASS_CONFIG (org.apache.kafka.connect.runtime.ConnectorConfig.KEY_CONVERTER_CLASS_CONFIG)2 TASKS_MAX_CONFIG (org.apache.kafka.connect.runtime.ConnectorConfig.TASKS_MAX_CONFIG)2 VALUE_CONVERTER_CLASS_CONFIG (org.apache.kafka.connect.runtime.ConnectorConfig.VALUE_CONVERTER_CLASS_CONFIG)2 DEFAULT_TOPIC_CREATION_PREFIX (org.apache.kafka.connect.runtime.TopicCreationConfig.DEFAULT_TOPIC_CREATION_PREFIX)2 PARTITIONS_CONFIG (org.apache.kafka.connect.runtime.TopicCreationConfig.PARTITIONS_CONFIG)2 REPLICATION_FACTOR_CONFIG (org.apache.kafka.connect.runtime.TopicCreationConfig.REPLICATION_FACTOR_CONFIG)2 CONNECTOR_CLIENT_POLICY_CLASS_CONFIG (org.apache.kafka.connect.runtime.WorkerConfig.CONNECTOR_CLIENT_POLICY_CLASS_CONFIG)2