Search in sources :

Example 61 with Time

use of org.apache.kafka.common.utils.Time in project connect-utils by jcustenborder.

the class TimeSerializationModuleTest method roundtrip.

@Test
public void roundtrip() throws IOException {
    final Time expected = mock(Time.class);
    when(expected.milliseconds()).thenReturn(1485910473123L);
    when(expected.nanoseconds()).thenReturn(1485910473123123L);
    final String temp = ObjectMapperFactory.INSTANCE.writeValueAsString(expected);
    log.trace(temp);
    final Time actual = ObjectMapperFactory.INSTANCE.readValue(temp, Time.class);
    assertNotNull(actual);
    assertEquals(expected.milliseconds(), actual.milliseconds(), "milliseconds() does not match.");
    assertEquals(expected.nanoseconds(), actual.nanoseconds(), "nanoseconds() does not match.");
}
Also used : Time(org.apache.kafka.common.utils.Time) Test(org.junit.jupiter.api.Test)

Example 62 with Time

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

the class SelfHealingNotifierTest method testOnBrokerFailure.

@Test
public void testOnBrokerFailure() {
    final long failureTime1 = 200L;
    final long failureTime2 = 400L;
    final long startTime = 500L;
    Time mockTime = new MockTime(0, startTime, TimeUnit.NANOSECONDS.convert(startTime, TimeUnit.MILLISECONDS));
    TestingBrokerFailureAutoFixNotifier anomalyNotifier = new TestingBrokerFailureAutoFixNotifier(mockTime);
    anomalyNotifier.configure(Collections.singletonMap(SelfHealingNotifier.SELF_HEALING_ENABLED_CONFIG, "true"));
    Map<Integer, Long> failedBrokers = new HashMap<>();
    failedBrokers.put(1, failureTime1);
    failedBrokers.put(2, failureTime2);
    AnomalyNotificationResult result = anomalyNotifier.onBrokerFailure(new BrokerFailures(failedBrokers));
    assertEquals(AnomalyNotificationResult.Action.CHECK, result.action());
    assertEquals(SelfHealingNotifier.DEFAULT_ALERT_THRESHOLD_MS + failureTime1 - mockTime.milliseconds(), result.delay());
    assertFalse(anomalyNotifier.alertCalled);
    // Sleep to 1 ms before alert.
    mockTime.sleep(result.delay() - 1);
    result = anomalyNotifier.onBrokerFailure(new BrokerFailures(failedBrokers));
    assertEquals(AnomalyNotificationResult.Action.CHECK, result.action());
    assertEquals(1, result.delay());
    assertFalse(anomalyNotifier.alertCalled);
    // Sleep 1 ms
    mockTime.sleep(1);
    anomalyNotifier.resetAlert();
    result = anomalyNotifier.onBrokerFailure(new BrokerFailures(failedBrokers));
    assertEquals(AnomalyNotificationResult.Action.CHECK, result.action());
    assertEquals(SelfHealingNotifier.DEFAULT_AUTO_FIX_THRESHOLD_MS + failureTime1 - mockTime.milliseconds(), result.delay());
    assertTrue(anomalyNotifier.alertCalled);
    // Sleep to 1 ms before alert.
    mockTime.sleep(result.delay() - 1);
    anomalyNotifier.resetAlert();
    result = anomalyNotifier.onBrokerFailure(new BrokerFailures(failedBrokers));
    assertEquals(AnomalyNotificationResult.Action.CHECK, result.action());
    assertEquals(1, result.delay());
    assertTrue(anomalyNotifier.alertCalled);
    assertFalse(anomalyNotifier.autoFixTriggered);
    // Sleep 1 ms
    mockTime.sleep(1);
    anomalyNotifier.resetAlert();
    result = anomalyNotifier.onBrokerFailure(new BrokerFailures(failedBrokers));
    assertEquals(AnomalyNotificationResult.Action.FIX, result.action());
    assertEquals(-1L, result.delay());
    assertTrue(anomalyNotifier.alertCalled);
    assertTrue(anomalyNotifier.autoFixTriggered);
}
Also used : HashMap(java.util.HashMap) BrokerFailures(com.linkedin.kafka.cruisecontrol.detector.BrokerFailures) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 63 with Time

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

the class SelfHealingNotifierTest method testSelfHealingDisabled.

@Test
public void testSelfHealingDisabled() {
    final long failureTime1 = 200L;
    final long failureTime2 = 400L;
    final long startTime = 500L;
    Time mockTime = new MockTime(startTime);
    TestingBrokerFailureAutoFixNotifier anomalyNotifier = new TestingBrokerFailureAutoFixNotifier(mockTime);
    anomalyNotifier.configure(Collections.singletonMap(SelfHealingNotifier.SELF_HEALING_ENABLED_CONFIG, "false"));
    Map<Integer, Long> failedBrokers = new HashMap<>();
    failedBrokers.put(1, failureTime1);
    failedBrokers.put(2, failureTime2);
    mockTime.sleep(SelfHealingNotifier.DEFAULT_AUTO_FIX_THRESHOLD_MS + failureTime1);
    anomalyNotifier.resetAlert();
    AnomalyNotificationResult result = anomalyNotifier.onBrokerFailure(new BrokerFailures(failedBrokers));
    assertEquals(AnomalyNotificationResult.Action.IGNORE, result.action());
    assertTrue(anomalyNotifier.alertCalled);
    assertFalse(anomalyNotifier.autoFixTriggered);
}
Also used : HashMap(java.util.HashMap) BrokerFailures(com.linkedin.kafka.cruisecontrol.detector.BrokerFailures) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 64 with Time

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

the class BrokerFailureDetectorTest method testDetectorStartWithFailedBrokers.

@Test
public void testDetectorStartWithFailedBrokers() throws Exception {
    Time mockTime = getMockTime();
    Queue<Anomaly> anomalies = new ConcurrentLinkedQueue<>();
    BrokerFailureDetector detector = createBrokerFailureDetector(anomalies, mockTime);
    try {
        int brokerId = 0;
        killBroker(brokerId);
        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 65 with Time

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

the class BrokerFailureDetectorTest method testFailureDetection.

@Test
public void testFailureDetection() throws Exception {
    Time mockTime = getMockTime();
    Queue<Anomaly> anomalies = new ConcurrentLinkedQueue<>();
    BrokerFailureDetector detector = createBrokerFailureDetector(anomalies, mockTime);
    try {
        // Start detection.
        detector.startDetection();
        assertTrue(anomalies.isEmpty());
        int brokerId = 0;
        killBroker(brokerId);
        long start = System.currentTimeMillis();
        while (anomalies.isEmpty() && System.currentTimeMillis() < start + 30000) {
        // wait for the anomalies to be drained.
        }
        assertEquals("One broker failure should have been detected before timeout.", 1, anomalies.size());
        Anomaly anomaly = anomalies.remove();
        assertTrue("The anomaly should be BrokerFailure", anomaly instanceof BrokerFailures);
        BrokerFailures brokerFailures = (BrokerFailures) anomaly;
        assertEquals("The failed broker should be 0 and time should be 100L", Collections.singletonMap(brokerId, 100L), brokerFailures.failedBrokers());
        // Bring the broker back
        System.out.println("Starting brokers.");
        restartDeadBroker(brokerId);
        detector.detectBrokerFailures();
        assertTrue(detector.failedBrokers().isEmpty());
    } 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)

Aggregations

Time (org.apache.kafka.common.utils.Time)125 MockTime (org.apache.kafka.common.utils.MockTime)107 Test (org.junit.jupiter.api.Test)63 MockClient (org.apache.kafka.clients.MockClient)55 HashMap (java.util.HashMap)53 Cluster (org.apache.kafka.common.Cluster)41 Test (org.junit.Test)40 Node (org.apache.kafka.common.Node)39 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)32 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)31 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)30 Metadata (org.apache.kafka.clients.Metadata)28 ProducerMetadata (org.apache.kafka.clients.producer.internals.ProducerMetadata)25 TopicPartition (org.apache.kafka.common.TopicPartition)22 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)21 LogContext (org.apache.kafka.common.utils.LogContext)17 Map (java.util.Map)14 Properties (java.util.Properties)14 MetricName (org.apache.kafka.common.MetricName)14 ExecutionException (java.util.concurrent.ExecutionException)13