use of org.apache.kafka.clients.Metadata in project kafka by apache.
the class WorkerCoordinatorTest method setup.
@Before
public void setup() {
this.time = new MockTime();
this.client = new MockClient(time);
this.metadata = new Metadata(0, Long.MAX_VALUE);
this.metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
this.consumerClient = new ConsumerNetworkClient(client, metadata, time, 100, 1000);
this.metrics = new Metrics(time);
this.rebalanceListener = new MockRebalanceListener();
this.configStorage = PowerMock.createMock(KafkaConfigBackingStore.class);
client.setNode(node);
this.coordinator = new WorkerCoordinator(consumerClient, groupId, rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, metrics, "consumer" + groupId, time, retryBackoffMs, LEADER_URL, configStorage, rebalanceListener);
configState1 = new ClusterConfigState(1L, Collections.singletonMap(connectorId1, 1), Collections.singletonMap(connectorId1, (Map<String, String>) new HashMap<String, String>()), Collections.singletonMap(connectorId1, TargetState.STARTED), Collections.singletonMap(taskId1x0, (Map<String, String>) new HashMap<String, String>()), Collections.<String>emptySet());
Map<String, Integer> configState2ConnectorTaskCounts = new HashMap<>();
configState2ConnectorTaskCounts.put(connectorId1, 2);
configState2ConnectorTaskCounts.put(connectorId2, 1);
Map<String, Map<String, String>> configState2ConnectorConfigs = new HashMap<>();
configState2ConnectorConfigs.put(connectorId1, new HashMap<String, String>());
configState2ConnectorConfigs.put(connectorId2, new HashMap<String, String>());
Map<String, TargetState> configState2TargetStates = new HashMap<>();
configState2TargetStates.put(connectorId1, TargetState.STARTED);
configState2TargetStates.put(connectorId2, TargetState.STARTED);
Map<ConnectorTaskId, Map<String, String>> configState2TaskConfigs = new HashMap<>();
configState2TaskConfigs.put(taskId1x0, new HashMap<String, String>());
configState2TaskConfigs.put(taskId1x1, new HashMap<String, String>());
configState2TaskConfigs.put(taskId2x0, new HashMap<String, String>());
configState2 = new ClusterConfigState(2L, configState2ConnectorTaskCounts, configState2ConnectorConfigs, configState2TargetStates, configState2TaskConfigs, Collections.<String>emptySet());
Map<String, Integer> configStateSingleTaskConnectorsConnectorTaskCounts = new HashMap<>();
configStateSingleTaskConnectorsConnectorTaskCounts.put(connectorId1, 1);
configStateSingleTaskConnectorsConnectorTaskCounts.put(connectorId2, 1);
configStateSingleTaskConnectorsConnectorTaskCounts.put(connectorId3, 1);
Map<String, Map<String, String>> configStateSingleTaskConnectorsConnectorConfigs = new HashMap<>();
configStateSingleTaskConnectorsConnectorConfigs.put(connectorId1, new HashMap<String, String>());
configStateSingleTaskConnectorsConnectorConfigs.put(connectorId2, new HashMap<String, String>());
configStateSingleTaskConnectorsConnectorConfigs.put(connectorId3, new HashMap<String, String>());
Map<String, TargetState> configStateSingleTaskConnectorsTargetStates = new HashMap<>();
configStateSingleTaskConnectorsTargetStates.put(connectorId1, TargetState.STARTED);
configStateSingleTaskConnectorsTargetStates.put(connectorId2, TargetState.STARTED);
configStateSingleTaskConnectorsTargetStates.put(connectorId3, TargetState.STARTED);
Map<ConnectorTaskId, Map<String, String>> configStateSingleTaskConnectorsTaskConfigs = new HashMap<>();
configStateSingleTaskConnectorsTaskConfigs.put(taskId1x0, new HashMap<String, String>());
configStateSingleTaskConnectorsTaskConfigs.put(taskId2x0, new HashMap<String, String>());
configStateSingleTaskConnectorsTaskConfigs.put(taskId3x0, new HashMap<String, String>());
configStateSingleTaskConnectors = new ClusterConfigState(2L, configStateSingleTaskConnectorsConnectorTaskCounts, configStateSingleTaskConnectorsConnectorConfigs, configStateSingleTaskConnectorsTargetStates, configStateSingleTaskConnectorsTaskConfigs, Collections.<String>emptySet());
}
use of org.apache.kafka.clients.Metadata in project kafka by apache.
the class KafkaConsumerTest method verifyHeartbeatSentWhenFetchedDataReady.
@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 = new Metadata(0, Long.MAX_VALUE);
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());
}
use of org.apache.kafka.clients.Metadata in project kafka by apache.
the class KafkaConsumerTest method testSubscriptionChangesWithAutoCommitDisabled.
/**
* Verify that when a consumer changes its topic subscription its assigned partitions
* do not immediately change, and the consumed offsets of its to-be-revoked partitions
* are not committed (when auto-commit is disabled).
* Upon unsubscribing from subscribed topics, the assigned partitions immediately
* change but if auto-commit is disabled the consumer offsets are not committed.
*/
@Test
public void testSubscriptionChangesWithAutoCommitDisabled() {
int rebalanceTimeoutMs = 60000;
int sessionTimeoutMs = 30000;
int heartbeatIntervalMs = 3000;
int autoCommitIntervalMs = 1000;
Time time = new MockTime();
Map<String, Integer> tpCounts = new HashMap<>();
tpCounts.put(topic, 1);
tpCounts.put(topic2, 1);
Cluster cluster = TestUtils.singletonCluster(tpCounts);
Node node = cluster.nodes().get(0);
Metadata metadata = new Metadata(0, Long.MAX_VALUE);
metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
MockClient client = new MockClient(time, metadata);
client.setNode(node);
PartitionAssignor assignor = new RangeAssignor();
final KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor, rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, false, autoCommitIntervalMs);
// initial subscription
consumer.subscribe(Arrays.asList(topic), getConsumerRebalanceListener(consumer));
// verify that subscription has changed but assignment is still unchanged
assertTrue(consumer.subscription().equals(Collections.singleton(topic)));
assertTrue(consumer.assignment().isEmpty());
// mock rebalance responses
prepareRebalance(client, node, assignor, Arrays.asList(tp0), null);
consumer.poll(0);
// verify that subscription is still the same, and now assignment has caught up
assertTrue(consumer.subscription().equals(Collections.singleton(topic)));
assertTrue(consumer.assignment().equals(Collections.singleton(tp0)));
consumer.poll(0);
// subscription change
consumer.subscribe(Arrays.asList(topic2), getConsumerRebalanceListener(consumer));
// verify that subscription has changed but assignment is still unchanged
assertTrue(consumer.subscription().equals(Collections.singleton(topic2)));
assertTrue(consumer.assignment().equals(Collections.singleton(tp0)));
// the auto commit is disabled, so no offset commit request should be sent
for (ClientRequest req : client.requests()) assertTrue(req.requestBuilder().apiKey() != ApiKeys.OFFSET_COMMIT);
// subscription change
consumer.unsubscribe();
// verify that subscription and assignment are both updated
assertTrue(consumer.subscription().isEmpty());
assertTrue(consumer.assignment().isEmpty());
// the auto commit is disabled, so no offset commit request should be sent
for (ClientRequest req : client.requests()) assertTrue(req.requestBuilder().apiKey() != ApiKeys.OFFSET_COMMIT);
client.requests().clear();
consumer.close();
}
use of org.apache.kafka.clients.Metadata in project kafka by apache.
the class KafkaConsumerTest method testChangingRegexSubscription.
@Test
public void testChangingRegexSubscription() {
int rebalanceTimeoutMs = 60000;
int sessionTimeoutMs = 30000;
int heartbeatIntervalMs = 3000;
int autoCommitIntervalMs = 1000;
PartitionAssignor assignor = new RoundRobinAssignor();
String otherTopic = "other";
TopicPartition otherTopicPartition = new TopicPartition(otherTopic, 0);
Time time = new MockTime();
Map<String, Integer> topicMetadata = new HashMap<>();
topicMetadata.put(topic, 1);
topicMetadata.put(otherTopic, 1);
Cluster cluster = TestUtils.clusterWith(1, topicMetadata);
Metadata metadata = new Metadata(0, Long.MAX_VALUE);
Node node = cluster.nodes().get(0);
MockClient client = new MockClient(time, metadata);
client.setNode(node);
metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
final KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor, rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, false, autoCommitIntervalMs);
Node coordinator = prepareRebalance(client, node, singleton(topic), assignor, singletonList(tp0), null);
consumer.subscribe(Pattern.compile(topic), getConsumerRebalanceListener(consumer));
client.prepareMetadataUpdate(cluster, Collections.<String>emptySet());
consumer.poll(0);
assertEquals(singleton(topic), consumer.subscription());
consumer.subscribe(Pattern.compile(otherTopic), getConsumerRebalanceListener(consumer));
client.prepareMetadataUpdate(cluster, Collections.<String>emptySet());
prepareRebalance(client, node, singleton(otherTopic), assignor, singletonList(otherTopicPartition), coordinator);
consumer.poll(0);
assertEquals(singleton(otherTopic), consumer.subscription());
}
use of org.apache.kafka.clients.Metadata in project kafka by apache.
the class KafkaConsumerTest method testWakeupWithFetchDataAvailable.
@Test
public void testWakeupWithFetchDataAvailable() {
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 = new Metadata(0, Long.MAX_VALUE);
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));
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());
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());
}
Aggregations