use of org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState in project kafka by apache.
the class RequestResponseTest method createStopReplicaRequest.
private StopReplicaRequest createStopReplicaRequest(short version, boolean deletePartitions) {
List<StopReplicaTopicState> topicStates = new ArrayList<>();
StopReplicaTopicState topic1 = new StopReplicaTopicState().setTopicName("topic1").setPartitionStates(singletonList(new StopReplicaPartitionState().setPartitionIndex(0).setLeaderEpoch(1).setDeletePartition(deletePartitions)));
topicStates.add(topic1);
StopReplicaTopicState topic2 = new StopReplicaTopicState().setTopicName("topic2").setPartitionStates(singletonList(new StopReplicaPartitionState().setPartitionIndex(1).setLeaderEpoch(2).setDeletePartition(deletePartitions)));
topicStates.add(topic2);
return new StopReplicaRequest.Builder(version, 0, 1, 0, deletePartitions, topicStates).build(version);
}
use of org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState in project kafka by apache.
the class StopReplicaRequestTest method testTopicStatesNormalization.
@Test
public void testTopicStatesNormalization() {
List<StopReplicaTopicState> topicStates = topicStates(true);
for (short version : STOP_REPLICA.allVersions()) {
// Create a request for version to get its serialized form
StopReplicaRequest baseRequest = new StopReplicaRequest.Builder(version, 0, 1, 0, true, topicStates).build(version);
// Construct the request from the buffer
StopReplicaRequest request = StopReplicaRequest.parse(baseRequest.serialize(), version);
Map<TopicPartition, StopReplicaPartitionState> partitionStates = StopReplicaRequestTest.partitionStates(request.topicStates());
assertEquals(6, partitionStates.size());
for (StopReplicaTopicState expectedTopicState : topicStates) {
for (StopReplicaPartitionState expectedPartitionState : expectedTopicState.partitionStates()) {
TopicPartition tp = new TopicPartition(expectedTopicState.topicName(), expectedPartitionState.partitionIndex());
StopReplicaPartitionState partitionState = partitionStates.get(tp);
assertEquals(expectedPartitionState.partitionIndex(), partitionState.partitionIndex());
assertTrue(partitionState.deletePartition());
if (version >= 3) {
assertEquals(expectedPartitionState.leaderEpoch(), partitionState.leaderEpoch());
} else {
assertEquals(-1, partitionState.leaderEpoch());
}
}
}
}
}
use of org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState in project kafka by apache.
the class StopReplicaRequest method topicStates.
/**
* Note that this method has allocation overhead per iterated element, so callers should copy the result into
* another collection if they need to iterate more than once.
*
* Implementation note: we should strive to avoid allocation overhead per element, see
* `UpdateMetadataRequest.partitionStates()` for the preferred approach. That's not possible in this case and
* StopReplicaRequest should be relatively rare in comparison to other request types.
*/
public Iterable<StopReplicaTopicState> topicStates() {
if (version() < 1) {
Map<String, StopReplicaTopicState> topicStates = new HashMap<>();
for (StopReplicaPartitionV0 partition : data.ungroupedPartitions()) {
StopReplicaTopicState topicState = topicStates.computeIfAbsent(partition.topicName(), topic -> new StopReplicaTopicState().setTopicName(topic));
topicState.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(partition.partitionIndex()).setDeletePartition(data.deletePartitions()));
}
return topicStates.values();
} else if (version() < 3) {
return () -> new MappedIterator<>(data.topics().iterator(), topic -> new StopReplicaTopicState().setTopicName(topic.name()).setPartitionStates(topic.partitionIndexes().stream().map(partition -> new StopReplicaPartitionState().setPartitionIndex(partition).setDeletePartition(data.deletePartitions())).collect(Collectors.toList())));
} else {
return data.topicStates();
}
}
use of org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState in project kafka by apache.
the class StopReplicaRequest method getErrorResponse.
@Override
public StopReplicaResponse getErrorResponse(int throttleTimeMs, Throwable e) {
Errors error = Errors.forException(e);
StopReplicaResponseData data = new StopReplicaResponseData();
data.setErrorCode(error.code());
List<StopReplicaPartitionError> partitions = new ArrayList<>();
for (StopReplicaTopicState topic : topicStates()) {
for (StopReplicaPartitionState partition : topic.partitionStates()) {
partitions.add(new StopReplicaPartitionError().setTopicName(topic.topicName()).setPartitionIndex(partition.partitionIndex()).setErrorCode(error.code()));
}
}
data.setPartitionErrors(partitions);
return new StopReplicaResponse(data);
}
use of org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState in project kafka by apache.
the class StopReplicaRequestTest method topicStates.
private List<StopReplicaTopicState> topicStates(boolean deletePartition) {
List<StopReplicaTopicState> topicStates = new ArrayList<>();
StopReplicaTopicState topic0 = new StopReplicaTopicState().setTopicName("topic0");
topic0.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(0).setLeaderEpoch(0).setDeletePartition(deletePartition));
topic0.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(1).setLeaderEpoch(1).setDeletePartition(deletePartition));
topicStates.add(topic0);
StopReplicaTopicState topic1 = new StopReplicaTopicState().setTopicName("topic1");
topic1.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(2).setLeaderEpoch(2).setDeletePartition(deletePartition));
topic1.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(3).setLeaderEpoch(3).setDeletePartition(deletePartition));
topicStates.add(topic1);
StopReplicaTopicState topic3 = new StopReplicaTopicState().setTopicName("topic1");
topic3.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(4).setLeaderEpoch(-2).setDeletePartition(deletePartition));
topic3.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(5).setLeaderEpoch(-2).setDeletePartition(deletePartition));
topicStates.add(topic3);
return topicStates;
}
Aggregations