use of org.apache.kafka.common.utils.Time in project apache-kafka-on-k8s by banzaicloud.
the class KafkaConsumerTest method testFetchProgressWithMissingPartitionPosition.
@Test
public void testFetchProgressWithMissingPartitionPosition() {
// Verifies that we can make progress on one partition while we are awaiting
// a reset on another partition.
Time time = new MockTime();
Cluster cluster = TestUtils.singletonCluster(topic, 2);
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);
KafkaConsumer<String, String> consumer = newConsumerNoAutoCommit(time, client, metadata);
consumer.assign(Arrays.asList(tp0, tp1));
consumer.seekToEnd(singleton(tp0));
consumer.seekToBeginning(singleton(tp1));
client.prepareResponse(new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
ListOffsetRequest request = (ListOffsetRequest) body;
Map<TopicPartition, Long> expectedTimestamps = new HashMap<>();
expectedTimestamps.put(tp0, ListOffsetRequest.LATEST_TIMESTAMP);
expectedTimestamps.put(tp1, ListOffsetRequest.EARLIEST_TIMESTAMP);
return expectedTimestamps.equals(request.partitionTimestamps());
}
}, listOffsetsResponse(Collections.singletonMap(tp0, 50L), Collections.singletonMap(tp1, Errors.NOT_LEADER_FOR_PARTITION)));
client.prepareResponse(new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
FetchRequest request = (FetchRequest) body;
return request.fetchData().keySet().equals(singleton(tp0)) && request.fetchData().get(tp0).fetchOffset == 50L;
}
}, fetchResponse(tp0, 50L, 5));
ConsumerRecords<String, String> records = consumer.poll(5);
assertEquals(5, records.count());
assertEquals(singleton(tp0), records.partitions());
}
use of org.apache.kafka.common.utils.Time in project apache-kafka-on-k8s by banzaicloud.
the class KafkaConsumerTest method testWakeupWithFetchDataAvailable.
@Test
public void testWakeupWithFetchDataAvailable() throws Exception {
final 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();
KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor, true);
consumer.subscribe(singleton(topic), getConsumerRebalanceListener(consumer));
prepareRebalance(client, node, assignor, singletonList(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());
consumer.wakeup();
try {
consumer.poll(0);
fail();
} catch (WakeupException e) {
}
// make sure the position hasn't been updated
assertEquals(0, consumer.position(tp0));
// the next poll should return the completed fetch
ConsumerRecords<String, String> records = consumer.poll(0);
assertEquals(5, records.count());
// Increment time asynchronously to clear timeouts in closing the consumer
final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
time.sleep(sessionTimeoutMs);
}
}, 0L, 10L, TimeUnit.MILLISECONDS);
consumer.close();
exec.shutdownNow();
exec.awaitTermination(5L, TimeUnit.SECONDS);
}
use of org.apache.kafka.common.utils.Time in project apache-kafka-on-k8s by banzaicloud.
the class KafkaConsumerTest method verifyHeartbeatSent.
@Test
public void verifyHeartbeatSent() throws Exception {
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();
KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor, true);
consumer.subscribe(singleton(topic), getConsumerRebalanceListener(consumer));
Node coordinator = prepareRebalance(client, node, assignor, singletonList(tp0), null);
// initial fetch
client.prepareResponseFrom(fetchResponse(tp0, 0, 0), node);
consumer.poll(0);
assertEquals(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);
}
use of org.apache.kafka.common.utils.Time in project apache-kafka-on-k8s by banzaicloud.
the class KafkaConsumerTest method fetchResponseWithUnexpectedPartitionIsIgnored.
@Test
public void fetchResponseWithUnexpectedPartitionIsIgnored() {
Time time = new MockTime();
Cluster cluster = TestUtils.singletonCluster(singletonMap(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 RangeAssignor();
KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor, true);
consumer.subscribe(singletonList(topic), getConsumerRebalanceListener(consumer));
prepareRebalance(client, node, assignor, singletonList(tp0), null);
Map<TopicPartition, FetchInfo> fetches1 = new HashMap<>();
fetches1.put(tp0, new FetchInfo(0, 1));
// not assigned and not fetched
fetches1.put(t2p0, new FetchInfo(0, 10));
client.prepareResponseFrom(fetchResponse(fetches1), node);
ConsumerRecords<String, String> records = consumer.poll(0);
assertEquals(0, records.count());
consumer.close(0, TimeUnit.MILLISECONDS);
}
use of org.apache.kafka.common.utils.Time in project apache-kafka-on-k8s by banzaicloud.
the class KafkaConsumerTest method testPollThrowsInterruptExceptionIfInterrupted.
@Test
public void testPollThrowsInterruptExceptionIfInterrupted() throws Exception {
final Time time = new MockTime();
Cluster cluster = TestUtils.singletonCluster(topic, 1);
final Node node = cluster.nodes().get(0);
Metadata metadata = createMetadata();
metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
final MockClient client = new MockClient(time, metadata);
client.setNode(node);
final PartitionAssignor assignor = new RoundRobinAssignor();
KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor, false);
consumer.subscribe(singleton(topic), getConsumerRebalanceListener(consumer));
prepareRebalance(client, node, assignor, singletonList(tp0), null);
consumer.poll(0);
// interrupt the thread and call poll
try {
Thread.currentThread().interrupt();
expectedException.expect(InterruptException.class);
consumer.poll(0);
} finally {
// clear interrupted state again since this thread may be reused by JUnit
Thread.interrupted();
}
consumer.close(0, TimeUnit.MILLISECONDS);
}
Aggregations