use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.
the class MockProducer method sendOffsetsToTransaction.
@Deprecated
@Override
public void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets, String consumerGroupId) throws ProducerFencedException {
Objects.requireNonNull(consumerGroupId);
sendOffsetsToTransaction(offsets, new ConsumerGroupMetadata(consumerGroupId));
}
use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.
the class KafkaProducerTest method testSendTxnOffsetsWithGroupMetadata.
@Test
public void testSendTxnOffsetsWithGroupMetadata() {
final short maxVersion = (short) 3;
Map<String, Object> configs = new HashMap<>();
configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "some.id");
configs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 10000);
configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
Time time = new MockTime(1);
MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("topic", 1));
ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
MockClient client = new MockClient(time, metadata);
client.updateMetadata(initialUpdateResponse);
client.setNodeApiVersions(NodeApiVersions.create(ApiKeys.TXN_OFFSET_COMMIT.id, (short) 0, maxVersion));
Node node = metadata.fetch().nodes().get(0);
client.throttle(node, 5000);
client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, "some.id", NODE));
client.prepareResponse(initProducerIdResponse(1L, (short) 5, Errors.NONE));
client.prepareResponse(addOffsetsToTxnResponse(Errors.NONE));
client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, "some.id", NODE));
String groupId = "group";
String memberId = "member";
int generationId = 5;
String groupInstanceId = "instance";
client.prepareResponse(request -> {
TxnOffsetCommitRequestData data = ((TxnOffsetCommitRequest) request).data();
return data.groupId().equals(groupId) && data.memberId().equals(memberId) && data.generationId() == generationId && data.groupInstanceId().equals(groupInstanceId);
}, txnOffsetsCommitResponse(Collections.singletonMap(new TopicPartition("topic", 0), Errors.NONE)));
client.prepareResponse(endTxnResponse(Errors.NONE));
try (Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time)) {
producer.initTransactions();
producer.beginTransaction();
ConsumerGroupMetadata groupMetadata = new ConsumerGroupMetadata(groupId, generationId, memberId, Optional.of(groupInstanceId));
producer.sendOffsetsToTransaction(Collections.emptyMap(), groupMetadata);
producer.commitTransaction();
}
}
use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.
the class MockProducerTest method shouldDropConsumerGroupOffsetsOnAbortIfTransactionsAreEnabled.
@Test
public void shouldDropConsumerGroupOffsetsOnAbortIfTransactionsAreEnabled() {
buildMockProducer(true);
producer.initTransactions();
producer.beginTransaction();
String group = "g";
Map<TopicPartition, OffsetAndMetadata> groupCommit = new HashMap<TopicPartition, OffsetAndMetadata>() {
{
put(new TopicPartition(topic, 0), new OffsetAndMetadata(42L, null));
put(new TopicPartition(topic, 1), new OffsetAndMetadata(73L, null));
}
};
producer.sendOffsetsToTransaction(groupCommit, new ConsumerGroupMetadata(group));
producer.abortTransaction();
producer.beginTransaction();
producer.commitTransaction();
assertTrue(producer.consumerGroupOffsetsHistory().isEmpty());
producer.beginTransaction();
producer.sendOffsetsToTransaction(groupCommit, new ConsumerGroupMetadata(group));
producer.abortTransaction();
producer.beginTransaction();
producer.commitTransaction();
assertTrue(producer.consumerGroupOffsetsHistory().isEmpty());
}
use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.
the class MockProducerTest method shouldPublishConsumerGroupOffsetsOnlyAfterCommitIfTransactionsAreEnabled.
@Test
public void shouldPublishConsumerGroupOffsetsOnlyAfterCommitIfTransactionsAreEnabled() {
buildMockProducer(true);
producer.initTransactions();
producer.beginTransaction();
String group1 = "g1";
Map<TopicPartition, OffsetAndMetadata> group1Commit = new HashMap<TopicPartition, OffsetAndMetadata>() {
{
put(new TopicPartition(topic, 0), new OffsetAndMetadata(42L, null));
put(new TopicPartition(topic, 1), new OffsetAndMetadata(73L, null));
}
};
String group2 = "g2";
Map<TopicPartition, OffsetAndMetadata> group2Commit = new HashMap<TopicPartition, OffsetAndMetadata>() {
{
put(new TopicPartition(topic, 0), new OffsetAndMetadata(101L, null));
put(new TopicPartition(topic, 1), new OffsetAndMetadata(21L, null));
}
};
producer.sendOffsetsToTransaction(group1Commit, new ConsumerGroupMetadata(group1));
producer.sendOffsetsToTransaction(group2Commit, new ConsumerGroupMetadata(group2));
assertTrue(producer.consumerGroupOffsetsHistory().isEmpty());
Map<String, Map<TopicPartition, OffsetAndMetadata>> expectedResult = new HashMap<>();
expectedResult.put(group1, group1Commit);
expectedResult.put(group2, group2Commit);
producer.commitTransaction();
assertEquals(Collections.singletonList(expectedResult), producer.consumerGroupOffsetsHistory());
}
use of org.apache.kafka.clients.consumer.ConsumerGroupMetadata in project kafka by apache.
the class MockProducerTest method shouldAddOffsetsWhenSendOffsetsToTransactionByGroupMetadata.
@Test
public void shouldAddOffsetsWhenSendOffsetsToTransactionByGroupMetadata() {
buildMockProducer(true);
producer.initTransactions();
producer.beginTransaction();
assertFalse(producer.sentOffsets());
Map<TopicPartition, OffsetAndMetadata> groupCommit = new HashMap<TopicPartition, OffsetAndMetadata>() {
{
put(new TopicPartition(topic, 0), new OffsetAndMetadata(42L, null));
}
};
producer.sendOffsetsToTransaction(groupCommit, new ConsumerGroupMetadata("groupId"));
assertTrue(producer.sentOffsets());
}
Aggregations