Search in sources :

Example 11 with MockTime

use of org.apache.kafka.common.utils.MockTime in project cruise-control by linkedin.

the class BrokerFailureDetectorTest method testLoadFailedBrokersFromZK.

@Test
public void testLoadFailedBrokersFromZK() throws Exception {
    Time mockTime = getMockTime();
    Queue<Anomaly> anomalies = new ConcurrentLinkedQueue<>();
    BrokerFailureDetector detector = createBrokerFailureDetector(anomalies, mockTime);
    try {
        detector.startDetection();
        int brokerId = 0;
        killBroker(brokerId);
        long start = System.currentTimeMillis();
        while (anomalies.isEmpty() && System.currentTimeMillis() < start + 30000) {
        // Wait for the anomalies to be drained.
        }
        assertEquals(Collections.singletonMap(brokerId, 100L), detector.failedBrokers());
        // shutdown, advance the clock and create a new detector.
        detector.shutdown();
        mockTime.sleep(100L);
        detector = createBrokerFailureDetector(anomalies, mockTime);
        // start the newly created detector and the broker down time should remain previous time.
        detector.startDetection();
        assertEquals(Collections.singletonMap(brokerId, 100L), detector.failedBrokers());
    } finally {
        detector.shutdown();
    }
}
Also used : MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Example 12 with MockTime

use of org.apache.kafka.common.utils.MockTime in project apache-kafka-on-k8s by banzaicloud.

the class WorkerSinkTaskThreadedTest method setup.

@Override
public void setup() {
    super.setup();
    time = new MockTime();
    metrics = new MockConnectMetrics();
    Map<String, String> workerProps = new HashMap<>();
    workerProps.put("key.converter", "org.apache.kafka.connect.json.JsonConverter");
    workerProps.put("value.converter", "org.apache.kafka.connect.json.JsonConverter");
    workerProps.put("internal.key.converter", "org.apache.kafka.connect.json.JsonConverter");
    workerProps.put("internal.value.converter", "org.apache.kafka.connect.json.JsonConverter");
    workerProps.put("internal.key.converter.schemas.enable", "false");
    workerProps.put("internal.value.converter.schemas.enable", "false");
    workerProps.put("offset.storage.file.filename", "/tmp/connect.offsets");
    pluginLoader = PowerMock.createMock(PluginClassLoader.class);
    workerConfig = new StandaloneConfig(workerProps);
    workerTask = PowerMock.createPartialMock(WorkerSinkTask.class, new String[] { "createConsumer" }, taskId, sinkTask, statusListener, initialState, workerConfig, metrics, keyConverter, valueConverter, headerConverter, TransformationChain.noOp(), pluginLoader, time);
    recordsReturned = 0;
}
Also used : HashMap(java.util.HashMap) StandaloneConfig(org.apache.kafka.connect.runtime.standalone.StandaloneConfig) MockTime(org.apache.kafka.common.utils.MockTime) PluginClassLoader(org.apache.kafka.connect.runtime.isolation.PluginClassLoader)

Example 13 with MockTime

use of org.apache.kafka.common.utils.MockTime in project apache-kafka-on-k8s by banzaicloud.

the class WorkerTaskTest method updateMetricsOnListenerEventsForStartupPauseResumeAndFailure.

@Test
public void updateMetricsOnListenerEventsForStartupPauseResumeAndFailure() {
    ConnectorTaskId taskId = new ConnectorTaskId("foo", 0);
    MockConnectMetrics metrics = new MockConnectMetrics();
    MockTime time = metrics.time();
    ConnectException error = new ConnectException("error");
    TaskMetricsGroup group = new TaskMetricsGroup(taskId, metrics, statusListener);
    statusListener.onStartup(taskId);
    expectLastCall();
    statusListener.onPause(taskId);
    expectLastCall();
    statusListener.onResume(taskId);
    expectLastCall();
    statusListener.onPause(taskId);
    expectLastCall();
    statusListener.onResume(taskId);
    expectLastCall();
    statusListener.onFailure(taskId, error);
    expectLastCall();
    statusListener.onShutdown(taskId);
    expectLastCall();
    replay(statusListener);
    time.sleep(1000L);
    group.onStartup(taskId);
    assertRunningMetric(group);
    time.sleep(2000L);
    group.onPause(taskId);
    assertPausedMetric(group);
    time.sleep(3000L);
    group.onResume(taskId);
    assertRunningMetric(group);
    time.sleep(4000L);
    group.onPause(taskId);
    assertPausedMetric(group);
    time.sleep(5000L);
    group.onResume(taskId);
    assertRunningMetric(group);
    time.sleep(6000L);
    group.onFailure(taskId, error);
    assertFailedMetric(group);
    time.sleep(7000L);
    group.onShutdown(taskId);
    assertStoppedMetric(group);
    verify(statusListener);
    long totalTime = 27000L;
    double pauseTimeRatio = (double) (3000L + 5000L) / totalTime;
    double runningTimeRatio = (double) (2000L + 4000L + 6000L) / totalTime;
    assertEquals(pauseTimeRatio, metrics.currentMetricValueAsDouble(group.metricGroup(), "pause-ratio"), 0.000001d);
    assertEquals(runningTimeRatio, metrics.currentMetricValueAsDouble(group.metricGroup(), "running-ratio"), 0.000001d);
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) MockTime(org.apache.kafka.common.utils.MockTime) ConnectException(org.apache.kafka.connect.errors.ConnectException) TaskMetricsGroup(org.apache.kafka.connect.runtime.WorkerTask.TaskMetricsGroup) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 14 with MockTime

use of org.apache.kafka.common.utils.MockTime in project apache-kafka-on-k8s by banzaicloud.

the class WorkerTest method testStartConnectorFailure.

@Test
public void testStartConnectorFailure() throws Exception {
    expectConverters();
    expectStartStorage();
    Map<String, String> props = new HashMap<>();
    props.put(SinkConnectorConfig.TOPICS_CONFIG, "foo,bar");
    props.put(ConnectorConfig.TASKS_MAX_CONFIG, "1");
    props.put(ConnectorConfig.NAME_CONFIG, CONNECTOR_ID);
    // Bad connector class name
    props.put(ConnectorConfig.CONNECTOR_CLASS_CONFIG, "java.util.HashMap");
    EasyMock.expect(plugins.currentThreadLoader()).andReturn(delegatingLoader);
    EasyMock.expect(plugins.newConnector(EasyMock.anyString())).andThrow(new ConnectException("Failed to find Connector"));
    EasyMock.expect(Plugins.compareAndSwapLoaders(delegatingLoader)).andReturn(pluginLoader);
    connectorStatusListener.onFailure(EasyMock.eq(CONNECTOR_ID), EasyMock.<ConnectException>anyObject());
    EasyMock.expectLastCall();
    PowerMock.replayAll();
    worker = new Worker(WORKER_ID, new MockTime(), plugins, config, offsetBackingStore);
    worker.start();
    assertStatistics(worker, 0, 0);
    assertFalse(worker.startConnector(CONNECTOR_ID, props, ctx, connectorStatusListener, TargetState.STARTED));
    assertStartupStatistics(worker, 1, 1, 0, 0);
    assertEquals(Collections.emptySet(), worker.connectorNames());
    assertStatistics(worker, 0, 0);
    assertStartupStatistics(worker, 1, 1, 0, 0);
    assertFalse(worker.stopConnector(CONNECTOR_ID));
    assertStatistics(worker, 0, 0);
    assertStartupStatistics(worker, 1, 1, 0, 0);
    PowerMock.verifyAll();
}
Also used : HashMap(java.util.HashMap) MockTime(org.apache.kafka.common.utils.MockTime) ConnectException(org.apache.kafka.connect.errors.ConnectException) ThreadedTest(org.apache.kafka.connect.util.ThreadedTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with MockTime

use of org.apache.kafka.common.utils.MockTime in project apache-kafka-on-k8s by banzaicloud.

the class CoordinatorTest method testCreateTask.

@Test
public void testCreateTask() throws Exception {
    MockTime time = new MockTime(0, 0, 0);
    Scheduler scheduler = new MockScheduler(time);
    try (MiniTrogdorCluster cluster = new MiniTrogdorCluster.Builder().addCoordinator("node01").addAgent("node02").scheduler(scheduler).build()) {
        new ExpectedTasks().waitFor(cluster.coordinatorClient());
        NoOpTaskSpec fooSpec = new NoOpTaskSpec(1, 2);
        cluster.coordinatorClient().createTask(new CreateTaskRequest("foo", fooSpec));
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskPending(fooSpec)).build()).waitFor(cluster.coordinatorClient());
        time.sleep(2);
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskRunning(fooSpec, 2)).workerState(new WorkerRunning(fooSpec, 2, "")).build()).waitFor(cluster.coordinatorClient()).waitFor(cluster.agentClient("node02"));
        time.sleep(3);
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskDone(fooSpec, 2, 5, "", false)).build()).waitFor(cluster.coordinatorClient());
    }
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) WorkerRunning(org.apache.kafka.trogdor.rest.WorkerRunning) MockScheduler(org.apache.kafka.common.utils.MockScheduler) Scheduler(org.apache.kafka.common.utils.Scheduler) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) TaskDone(org.apache.kafka.trogdor.rest.TaskDone) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) NoOpTaskSpec(org.apache.kafka.trogdor.task.NoOpTaskSpec) TaskPending(org.apache.kafka.trogdor.rest.TaskPending) ExpectedTasks(org.apache.kafka.trogdor.common.ExpectedTasks) CreateTaskRequest(org.apache.kafka.trogdor.rest.CreateTaskRequest) MiniTrogdorCluster(org.apache.kafka.trogdor.common.MiniTrogdorCluster) TaskRunning(org.apache.kafka.trogdor.rest.TaskRunning) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Aggregations

MockTime (org.apache.kafka.common.utils.MockTime)363 Test (org.junit.Test)156 Test (org.junit.jupiter.api.Test)134 HashMap (java.util.HashMap)104 Time (org.apache.kafka.common.utils.Time)98 MockClient (org.apache.kafka.clients.MockClient)73 Cluster (org.apache.kafka.common.Cluster)73 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)65 Node (org.apache.kafka.common.Node)56 Metrics (org.apache.kafka.common.metrics.Metrics)56 TopicPartition (org.apache.kafka.common.TopicPartition)55 LogContext (org.apache.kafka.common.utils.LogContext)50 StreamsConfig (org.apache.kafka.streams.StreamsConfig)49 Before (org.junit.Before)38 Metadata (org.apache.kafka.clients.Metadata)32 MockScheduler (org.apache.kafka.common.utils.MockScheduler)31 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)30 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)30 ProducerMetadata (org.apache.kafka.clients.producer.internals.ProducerMetadata)29 Properties (java.util.Properties)27