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