本文整理汇总了Java中org.apache.kafka.common.utils.Time.sleep方法的典型用法代码示例。如果您正苦于以下问题:Java Time.sleep方法的具体用法?Java Time.sleep怎么用?Java Time.sleep使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.kafka.common.utils.Time
的用法示例。
在下文中一共展示了Time.sleep方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testLoadFailedBrokersFromZK
import org.apache.kafka.common.utils.Time; //导入方法依赖的package包/类
@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();
}
}
示例2: testSelfHealingDisabled
import org.apache.kafka.common.utils.Time; //导入方法依赖的package包/类
@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);
}
示例3: produceKeyValuesSynchronously
import org.apache.kafka.common.utils.Time; //导入方法依赖的package包/类
/**
* @param topic Kafka topic to write the data records to
* @param records Data records to write to Kafka
* @param producerConfig Kafka producer configuration
* @param <K> Key type of the data records
* @param <V> Value type of the data records
*/
public static <K, V> void produceKeyValuesSynchronously(
final String topic, final Collection<KeyValue<K, V>> records, final Properties producerConfig, final Time time)
throws ExecutionException, InterruptedException {
for (final KeyValue<K, V> record : records) {
produceKeyValuesSynchronouslyWithTimestamp(topic,
Collections.singleton(record),
producerConfig,
time.milliseconds());
time.sleep(1L);
}
}
示例4: testTopicRefreshInMetadata
import org.apache.kafka.common.utils.Time; //导入方法依赖的package包/类
@Test
public void testTopicRefreshInMetadata() throws Exception {
Properties props = new Properties();
props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
props.setProperty(ProducerConfig.MAX_BLOCK_MS_CONFIG, "600000");
KafkaProducer<String, String> producer = new KafkaProducer<>(props, new StringSerializer(), new StringSerializer());
long refreshBackoffMs = 500L;
long metadataExpireMs = 60000L;
final Metadata metadata = new Metadata(refreshBackoffMs, metadataExpireMs, true,
true, new ClusterResourceListeners());
final Time time = new MockTime();
MemberModifier.field(KafkaProducer.class, "metadata").set(producer, metadata);
MemberModifier.field(KafkaProducer.class, "time").set(producer, time);
final String topic = "topic";
Thread t = new Thread() {
@Override
public void run() {
long startTimeMs = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
while (!metadata.updateRequested() && System.currentTimeMillis() - startTimeMs < 1000)
yield();
metadata.update(Cluster.empty(), Collections.singleton(topic), time.milliseconds());
time.sleep(60 * 1000L);
}
}
};
t.start();
try {
producer.partitionsFor(topic);
fail("Expect TimeoutException");
} catch (TimeoutException e) {
// skip
}
Assert.assertTrue("Topic should still exist in metadata", metadata.containsTopic(topic));
}
示例5: testOnBrokerFailure
import org.apache.kafka.common.utils.Time; //导入方法依赖的package包/类
@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);
}
示例6: verifyHeartbeatSent
import org.apache.kafka.common.utils.Time; //导入方法依赖的package包/类
@Test
public void verifyHeartbeatSent() throws Exception {
int rebalanceTimeoutMs = 60000;
int sessionTimeoutMs = 30000;
int heartbeatIntervalMs = 1000;
int autoCommitIntervalMs = 10000;
Time time = new MockTime();
Cluster cluster = TestUtils.singletonCluster(topic, 1);
Node node = cluster.nodes().get(0);
Metadata metadata = createMetadata();
metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
MockClient client = new MockClient(time, metadata);
client.setNode(node);
PartitionAssignor assignor = new RoundRobinAssignor();
final KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor,
rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, true, autoCommitIntervalMs);
consumer.subscribe(Arrays.asList(topic), getConsumerRebalanceListener(consumer));
Node coordinator = prepareRebalance(client, node, assignor, Arrays.asList(tp0), null);
// initial fetch
client.prepareResponseFrom(fetchResponse(tp0, 0, 0), node);
consumer.poll(0);
assertEquals(Collections.singleton(tp0), consumer.assignment());
AtomicBoolean heartbeatReceived = prepareHeartbeatResponse(client, coordinator);
// heartbeat interval is 2 seconds
time.sleep(heartbeatIntervalMs);
Thread.sleep(heartbeatIntervalMs);
consumer.poll(0);
assertTrue(heartbeatReceived.get());
consumer.close(0, TimeUnit.MILLISECONDS);
}
示例7: verifyHeartbeatSentWhenFetchedDataReady
import org.apache.kafka.common.utils.Time; //导入方法依赖的package包/类
@Test
public void verifyHeartbeatSentWhenFetchedDataReady() throws Exception {
int rebalanceTimeoutMs = 60000;
int sessionTimeoutMs = 30000;
int heartbeatIntervalMs = 1000;
int autoCommitIntervalMs = 10000;
Time time = new MockTime();
Cluster cluster = TestUtils.singletonCluster(topic, 1);
Node node = cluster.nodes().get(0);
Metadata metadata = createMetadata();
metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
MockClient client = new MockClient(time, metadata);
client.setNode(node);
PartitionAssignor assignor = new RoundRobinAssignor();
final KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor,
rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, true, autoCommitIntervalMs);
consumer.subscribe(Arrays.asList(topic), getConsumerRebalanceListener(consumer));
Node coordinator = prepareRebalance(client, node, assignor, Arrays.asList(tp0), null);
consumer.poll(0);
// respond to the outstanding fetch so that we have data available on the next poll
client.respondFrom(fetchResponse(tp0, 0, 5), node);
client.poll(0, time.milliseconds());
client.prepareResponseFrom(fetchResponse(tp0, 5, 0), node);
AtomicBoolean heartbeatReceived = prepareHeartbeatResponse(client, coordinator);
time.sleep(heartbeatIntervalMs);
Thread.sleep(heartbeatIntervalMs);
consumer.poll(0);
assertTrue(heartbeatReceived.get());
consumer.close(0, TimeUnit.MILLISECONDS);
}
示例8: testAutoCommitSentBeforePositionUpdate
import org.apache.kafka.common.utils.Time; //导入方法依赖的package包/类
@Test
public void testAutoCommitSentBeforePositionUpdate() {
int rebalanceTimeoutMs = 60000;
int sessionTimeoutMs = 30000;
int heartbeatIntervalMs = 3000;
// adjust auto commit interval lower than heartbeat so we don't need to deal with
// a concurrent heartbeat request
int autoCommitIntervalMs = 1000;
Time time = new MockTime();
Cluster cluster = TestUtils.singletonCluster(topic, 1);
Node node = cluster.nodes().get(0);
Metadata metadata = createMetadata();
metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
MockClient client = new MockClient(time, metadata);
client.setNode(node);
PartitionAssignor assignor = new RoundRobinAssignor();
final KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor,
rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, true, autoCommitIntervalMs);
consumer.subscribe(Arrays.asList(topic), getConsumerRebalanceListener(consumer));
Node coordinator = prepareRebalance(client, node, assignor, Arrays.asList(tp0), null);
consumer.poll(0);
// respond to the outstanding fetch so that we have data available on the next poll
client.respondFrom(fetchResponse(tp0, 0, 5), node);
client.poll(0, time.milliseconds());
time.sleep(autoCommitIntervalMs);
client.prepareResponseFrom(fetchResponse(tp0, 5, 0), node);
// no data has been returned to the user yet, so the committed offset should be 0
AtomicBoolean commitReceived = prepareOffsetCommitResponse(client, coordinator, tp0, 0);
consumer.poll(0);
assertTrue(commitReceived.get());
consumer.close(0, TimeUnit.MILLISECONDS);
}