use of org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic in project kafka by apache.
the class MessageTest method testTxnOffsetCommitRequestVersions.
@Test
public void testTxnOffsetCommitRequestVersions() throws Exception {
String groupId = "groupId";
String topicName = "topic";
String metadata = "metadata";
String txnId = "transactionalId";
int producerId = 25;
short producerEpoch = 10;
String instanceId = "instance";
String memberId = "member";
int generationId = 1;
int partition = 2;
int offset = 100;
testAllMessageRoundTrips(new TxnOffsetCommitRequestData().setGroupId(groupId).setTransactionalId(txnId).setProducerId(producerId).setProducerEpoch(producerEpoch).setTopics(Collections.singletonList(new TxnOffsetCommitRequestTopic().setName(topicName).setPartitions(Collections.singletonList(new TxnOffsetCommitRequestPartition().setPartitionIndex(partition).setCommittedMetadata(metadata).setCommittedOffset(offset))))));
Supplier<TxnOffsetCommitRequestData> request = () -> new TxnOffsetCommitRequestData().setGroupId(groupId).setTransactionalId(txnId).setProducerId(producerId).setProducerEpoch(producerEpoch).setGroupInstanceId(instanceId).setMemberId(memberId).setGenerationId(generationId).setTopics(Collections.singletonList(new TxnOffsetCommitRequestTopic().setName(topicName).setPartitions(Collections.singletonList(new TxnOffsetCommitRequestPartition().setPartitionIndex(partition).setCommittedLeaderEpoch(10).setCommittedMetadata(metadata).setCommittedOffset(offset)))));
for (short version : ApiKeys.TXN_OFFSET_COMMIT.allVersions()) {
TxnOffsetCommitRequestData requestData = request.get();
if (version < 2) {
requestData.topics().get(0).partitions().get(0).setCommittedLeaderEpoch(-1);
}
if (version < 3) {
final short finalVersion = version;
assertThrows(UnsupportedVersionException.class, () -> testEquivalentMessageRoundTrip(finalVersion, requestData));
requestData.setGroupInstanceId(null);
assertThrows(UnsupportedVersionException.class, () -> testEquivalentMessageRoundTrip(finalVersion, requestData));
requestData.setMemberId("");
assertThrows(UnsupportedVersionException.class, () -> testEquivalentMessageRoundTrip(finalVersion, requestData));
requestData.setGenerationId(-1);
}
testAllMessageRoundTripsFromVersion(version, requestData);
}
}
use of org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic in project kafka by apache.
the class TxnOffsetCommitRequest method offsets.
public Map<TopicPartition, CommittedOffset> offsets() {
List<TxnOffsetCommitRequestTopic> topics = data.topics();
Map<TopicPartition, CommittedOffset> offsetMap = new HashMap<>();
for (TxnOffsetCommitRequestTopic topic : topics) {
for (TxnOffsetCommitRequestPartition partition : topic.partitions()) {
offsetMap.put(new TopicPartition(topic.name(), partition.partitionIndex()), new CommittedOffset(partition.committedOffset(), partition.committedMetadata(), RequestUtils.getLeaderEpoch(partition.committedLeaderEpoch())));
}
}
return offsetMap;
}
use of org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic in project kafka by apache.
the class TxnOffsetCommitRequest method getErrorResponseTopics.
static List<TxnOffsetCommitResponseTopic> getErrorResponseTopics(List<TxnOffsetCommitRequestTopic> requestTopics, Errors e) {
List<TxnOffsetCommitResponseTopic> responseTopicData = new ArrayList<>();
for (TxnOffsetCommitRequestTopic entry : requestTopics) {
List<TxnOffsetCommitResponsePartition> responsePartitions = new ArrayList<>();
for (TxnOffsetCommitRequestPartition requestPartition : entry.partitions()) {
responsePartitions.add(new TxnOffsetCommitResponsePartition().setPartitionIndex(requestPartition.partitionIndex()).setErrorCode(e.code()));
}
responseTopicData.add(new TxnOffsetCommitResponseTopic().setName(entry.name()).setPartitions(responsePartitions));
}
return responseTopicData;
}
use of org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic in project kafka by apache.
the class TxnOffsetCommitRequest method getTopics.
static List<TxnOffsetCommitRequestTopic> getTopics(Map<TopicPartition, CommittedOffset> pendingTxnOffsetCommits) {
Map<String, List<TxnOffsetCommitRequestPartition>> topicPartitionMap = new HashMap<>();
for (Map.Entry<TopicPartition, CommittedOffset> entry : pendingTxnOffsetCommits.entrySet()) {
TopicPartition topicPartition = entry.getKey();
CommittedOffset offset = entry.getValue();
List<TxnOffsetCommitRequestPartition> partitions = topicPartitionMap.getOrDefault(topicPartition.topic(), new ArrayList<>());
partitions.add(new TxnOffsetCommitRequestPartition().setPartitionIndex(topicPartition.partition()).setCommittedOffset(offset.offset).setCommittedLeaderEpoch(offset.leaderEpoch.orElse(RecordBatch.NO_PARTITION_LEADER_EPOCH)).setCommittedMetadata(offset.metadata));
topicPartitionMap.put(topicPartition.topic(), partitions);
}
return topicPartitionMap.entrySet().stream().map(entry -> new TxnOffsetCommitRequestTopic().setName(entry.getKey()).setPartitions(entry.getValue())).collect(Collectors.toList());
}
use of org.apache.kafka.common.message.TxnOffsetCommitRequestData.TxnOffsetCommitRequestTopic in project kafka by apache.
the class TxnOffsetCommitRequestTest method testConstructor.
@Test
@Override
public void testConstructor() {
Map<TopicPartition, Errors> errorsMap = new HashMap<>();
errorsMap.put(new TopicPartition(topicOne, partitionOne), Errors.NOT_COORDINATOR);
errorsMap.put(new TopicPartition(topicTwo, partitionTwo), Errors.NOT_COORDINATOR);
List<TxnOffsetCommitRequestTopic> expectedTopics = Arrays.asList(new TxnOffsetCommitRequestTopic().setName(topicOne).setPartitions(Collections.singletonList(new TxnOffsetCommitRequestPartition().setPartitionIndex(partitionOne).setCommittedOffset(offset).setCommittedLeaderEpoch(leaderEpoch).setCommittedMetadata(metadata))), new TxnOffsetCommitRequestTopic().setName(topicTwo).setPartitions(Collections.singletonList(new TxnOffsetCommitRequestPartition().setPartitionIndex(partitionTwo).setCommittedOffset(offset).setCommittedLeaderEpoch(leaderEpoch).setCommittedMetadata(metadata))));
for (short version : ApiKeys.TXN_OFFSET_COMMIT.allVersions()) {
final TxnOffsetCommitRequest request;
if (version < 3) {
request = builder.build(version);
} else {
request = builderWithGroupMetadata.build(version);
}
assertEquals(offsets, request.offsets());
assertEquals(expectedTopics, TxnOffsetCommitRequest.getTopics(request.offsets()));
TxnOffsetCommitResponse response = request.getErrorResponse(throttleTimeMs, Errors.NOT_COORDINATOR.exception());
assertEquals(errorsMap, response.errors());
assertEquals(Collections.singletonMap(Errors.NOT_COORDINATOR, 2), response.errorCounts());
assertEquals(throttleTimeMs, response.throttleTimeMs());
}
}
Aggregations