use of org.apache.kafka.common.requests.TxnOffsetCommitRequest 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.common.requests.TxnOffsetCommitRequest in project kafka by apache.
the class TransactionManagerTest method prepareTxnOffsetCommitResponse.
private void prepareTxnOffsetCommitResponse(final String consumerGroupId, final long producerId, final short producerEpoch, final String groupInstanceId, final String memberId, final int generationId, Map<TopicPartition, Errors> txnOffsetCommitResponse) {
client.prepareResponse(request -> {
TxnOffsetCommitRequest txnOffsetCommitRequest = (TxnOffsetCommitRequest) request;
assertEquals(consumerGroupId, txnOffsetCommitRequest.data().groupId());
assertEquals(producerId, txnOffsetCommitRequest.data().producerId());
assertEquals(producerEpoch, txnOffsetCommitRequest.data().producerEpoch());
assertEquals(groupInstanceId, txnOffsetCommitRequest.data().groupInstanceId());
assertEquals(memberId, txnOffsetCommitRequest.data().memberId());
assertEquals(generationId, txnOffsetCommitRequest.data().generationId());
return true;
}, new TxnOffsetCommitResponse(0, txnOffsetCommitResponse));
}
use of org.apache.kafka.common.requests.TxnOffsetCommitRequest in project kafka by apache.
the class TransactionManagerTest method testFencedInstanceIdInTxnOffsetCommitByGroupMetadata.
@Test
public void testFencedInstanceIdInTxnOffsetCommitByGroupMetadata() {
final TopicPartition tp = new TopicPartition("foo", 0);
final String fencedMemberId = "fenced_member";
doInitTransactions();
transactionManager.beginTransaction();
TransactionalRequestResult sendOffsetsResult = transactionManager.sendOffsetsToTransaction(singletonMap(tp, new OffsetAndMetadata(39L)), new ConsumerGroupMetadata(consumerGroupId, 5, fencedMemberId, Optional.of(groupInstanceId)));
prepareAddOffsetsToTxnResponse(Errors.NONE, consumerGroupId, producerId, epoch);
prepareFindCoordinatorResponse(Errors.NONE, false, CoordinatorType.GROUP, consumerGroupId);
runUntil(() -> transactionManager.coordinator(CoordinatorType.GROUP) != null);
client.prepareResponse(request -> {
TxnOffsetCommitRequest txnOffsetCommitRequest = (TxnOffsetCommitRequest) request;
assertEquals(consumerGroupId, txnOffsetCommitRequest.data().groupId());
assertEquals(producerId, txnOffsetCommitRequest.data().producerId());
assertEquals(epoch, txnOffsetCommitRequest.data().producerEpoch());
return txnOffsetCommitRequest.data().groupInstanceId().equals(groupInstanceId) && !txnOffsetCommitRequest.data().memberId().equals(memberId);
}, new TxnOffsetCommitResponse(0, singletonMap(tp, Errors.FENCED_INSTANCE_ID)));
runUntil(transactionManager::hasError);
assertTrue(transactionManager.lastError() instanceof FencedInstanceIdException);
assertTrue(sendOffsetsResult.isCompleted());
assertFalse(sendOffsetsResult.isSuccessful());
assertTrue(sendOffsetsResult.error() instanceof FencedInstanceIdException);
assertAbortableError(FencedInstanceIdException.class);
}
use of org.apache.kafka.common.requests.TxnOffsetCommitRequest in project kafka by apache.
the class TransactionManagerTest method testUnknownMemberIdInTxnOffsetCommitByGroupMetadata.
@Test
public void testUnknownMemberIdInTxnOffsetCommitByGroupMetadata() {
final TopicPartition tp = new TopicPartition("foo", 0);
final String unknownMemberId = "unknownMember";
doInitTransactions();
transactionManager.beginTransaction();
TransactionalRequestResult sendOffsetsResult = transactionManager.sendOffsetsToTransaction(singletonMap(tp, new OffsetAndMetadata(39L)), new ConsumerGroupMetadata(consumerGroupId, 5, unknownMemberId, Optional.empty()));
prepareAddOffsetsToTxnResponse(Errors.NONE, consumerGroupId, producerId, epoch);
prepareFindCoordinatorResponse(Errors.NONE, false, CoordinatorType.GROUP, consumerGroupId);
runUntil(() -> transactionManager.coordinator(CoordinatorType.GROUP) != null);
client.prepareResponse(request -> {
TxnOffsetCommitRequest txnOffsetCommitRequest = (TxnOffsetCommitRequest) request;
assertEquals(consumerGroupId, txnOffsetCommitRequest.data().groupId());
assertEquals(producerId, txnOffsetCommitRequest.data().producerId());
assertEquals(epoch, txnOffsetCommitRequest.data().producerEpoch());
return !txnOffsetCommitRequest.data().memberId().equals(memberId);
}, new TxnOffsetCommitResponse(0, singletonMap(tp, Errors.UNKNOWN_MEMBER_ID)));
runUntil(transactionManager::hasError);
assertTrue(transactionManager.lastError() instanceof CommitFailedException);
assertTrue(sendOffsetsResult.isCompleted());
assertFalse(sendOffsetsResult.isSuccessful());
assertTrue(sendOffsetsResult.error() instanceof CommitFailedException);
assertAbortableError(CommitFailedException.class);
}
use of org.apache.kafka.common.requests.TxnOffsetCommitRequest in project kafka by apache.
the class TransactionManagerTest method testIllegalGenerationInTxnOffsetCommitByGroupMetadata.
@Test
public void testIllegalGenerationInTxnOffsetCommitByGroupMetadata() {
final TopicPartition tp = new TopicPartition("foo", 0);
final int illegalGenerationId = 1;
doInitTransactions();
transactionManager.beginTransaction();
TransactionalRequestResult sendOffsetsResult = transactionManager.sendOffsetsToTransaction(singletonMap(tp, new OffsetAndMetadata(39L)), new ConsumerGroupMetadata(consumerGroupId, illegalGenerationId, JoinGroupRequest.UNKNOWN_MEMBER_ID, Optional.empty()));
prepareAddOffsetsToTxnResponse(Errors.NONE, consumerGroupId, producerId, epoch);
prepareFindCoordinatorResponse(Errors.NONE, false, CoordinatorType.GROUP, consumerGroupId);
runUntil(() -> transactionManager.coordinator(CoordinatorType.GROUP) != null);
prepareTxnOffsetCommitResponse(consumerGroupId, producerId, epoch, singletonMap(tp, Errors.ILLEGAL_GENERATION));
client.prepareResponse(request -> {
TxnOffsetCommitRequest txnOffsetCommitRequest = (TxnOffsetCommitRequest) request;
assertEquals(consumerGroupId, txnOffsetCommitRequest.data().groupId());
assertEquals(producerId, txnOffsetCommitRequest.data().producerId());
assertEquals(epoch, txnOffsetCommitRequest.data().producerEpoch());
return txnOffsetCommitRequest.data().generationId() != generationId;
}, new TxnOffsetCommitResponse(0, singletonMap(tp, Errors.ILLEGAL_GENERATION)));
runUntil(transactionManager::hasError);
assertTrue(transactionManager.lastError() instanceof CommitFailedException);
assertTrue(sendOffsetsResult.isCompleted());
assertFalse(sendOffsetsResult.isSuccessful());
assertTrue(sendOffsetsResult.error() instanceof CommitFailedException);
assertAbortableError(CommitFailedException.class);
}
Aggregations