Search in sources :

Example 1 with LeaderAndIsrRequestData

use of org.apache.kafka.common.message.LeaderAndIsrRequestData in project kafka by apache.

the class LeaderAndIsrRequestTest method testVersionLogic.

/**
 * Verifies the logic we have in LeaderAndIsrRequest to present a unified interface across the various versions
 * works correctly. For example, `LeaderAndIsrPartitionState.topicName` is not serialiazed/deserialized in
 * recent versions, but we set it manually so that we can always present the ungrouped partition states
 * independently of the version.
 */
@Test
public void testVersionLogic() {
    for (short version : LEADER_AND_ISR.allVersions()) {
        List<LeaderAndIsrPartitionState> partitionStates = asList(new LeaderAndIsrPartitionState().setTopicName("topic0").setPartitionIndex(0).setControllerEpoch(2).setLeader(0).setLeaderEpoch(10).setIsr(asList(0, 1)).setZkVersion(10).setReplicas(asList(0, 1, 2)).setAddingReplicas(asList(3)).setRemovingReplicas(asList(2)), new LeaderAndIsrPartitionState().setTopicName("topic0").setPartitionIndex(1).setControllerEpoch(2).setLeader(1).setLeaderEpoch(11).setIsr(asList(1, 2, 3)).setZkVersion(11).setReplicas(asList(1, 2, 3)).setAddingReplicas(emptyList()).setRemovingReplicas(emptyList()), new LeaderAndIsrPartitionState().setTopicName("topic1").setPartitionIndex(0).setControllerEpoch(2).setLeader(2).setLeaderEpoch(11).setIsr(asList(2, 3, 4)).setZkVersion(11).setReplicas(asList(2, 3, 4)).setAddingReplicas(emptyList()).setRemovingReplicas(emptyList()));
        List<Node> liveNodes = asList(new Node(0, "host0", 9090), new Node(1, "host1", 9091));
        Map<String, Uuid> topicIds = new HashMap<>();
        topicIds.put("topic0", Uuid.randomUuid());
        topicIds.put("topic1", Uuid.randomUuid());
        LeaderAndIsrRequest request = new LeaderAndIsrRequest.Builder(version, 1, 2, 3, partitionStates, topicIds, liveNodes).build();
        List<LeaderAndIsrLiveLeader> liveLeaders = liveNodes.stream().map(n -> new LeaderAndIsrLiveLeader().setBrokerId(n.id()).setHostName(n.host()).setPort(n.port())).collect(Collectors.toList());
        assertEquals(new HashSet<>(partitionStates), iterableToSet(request.partitionStates()));
        assertEquals(liveLeaders, request.liveLeaders());
        assertEquals(1, request.controllerId());
        assertEquals(2, request.controllerEpoch());
        assertEquals(3, request.brokerEpoch());
        ByteBuffer byteBuffer = request.serialize();
        LeaderAndIsrRequest deserializedRequest = new LeaderAndIsrRequest(new LeaderAndIsrRequestData(new ByteBufferAccessor(byteBuffer), version), version);
        // them for earlier versions.
        if (version < 3) {
            partitionStates.get(0).setAddingReplicas(emptyList()).setRemovingReplicas(emptyList());
        }
        // TopicStates is an empty map.
        if (version < 2) {
            topicIds = new HashMap<>();
        }
        // Zero Uuids in place.
        if (version > 1 && version < 5) {
            topicIds.put("topic0", Uuid.ZERO_UUID);
            topicIds.put("topic1", Uuid.ZERO_UUID);
        }
        assertEquals(new HashSet<>(partitionStates), iterableToSet(deserializedRequest.partitionStates()));
        assertEquals(topicIds, deserializedRequest.topicIds());
        assertEquals(liveLeaders, deserializedRequest.liveLeaders());
        assertEquals(1, request.controllerId());
        assertEquals(2, request.controllerEpoch());
        assertEquals(3, request.brokerEpoch());
    }
}
Also used : Uuid(org.apache.kafka.common.Uuid) LeaderAndIsrPartitionState(org.apache.kafka.common.message.LeaderAndIsrRequestData.LeaderAndIsrPartitionState) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) LeaderAndIsrRequestData(org.apache.kafka.common.message.LeaderAndIsrRequestData) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) LeaderAndIsrTopicError(org.apache.kafka.common.message.LeaderAndIsrResponseData.LeaderAndIsrTopicError) HashMap(java.util.HashMap) LEADER_AND_ISR(org.apache.kafka.common.protocol.ApiKeys.LEADER_AND_ISR) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) LeaderAndIsrPartitionError(org.apache.kafka.common.message.LeaderAndIsrResponseData.LeaderAndIsrPartitionError) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TopicPartition(org.apache.kafka.common.TopicPartition) TestUtils(org.apache.kafka.test.TestUtils) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) LeaderAndIsrLiveLeader(org.apache.kafka.common.message.LeaderAndIsrRequestData.LeaderAndIsrLiveLeader) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) Errors(org.apache.kafka.common.protocol.Errors) Node(org.apache.kafka.common.Node) Collections(java.util.Collections) HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) LeaderAndIsrLiveLeader(org.apache.kafka.common.message.LeaderAndIsrRequestData.LeaderAndIsrLiveLeader) Uuid(org.apache.kafka.common.Uuid) LeaderAndIsrRequestData(org.apache.kafka.common.message.LeaderAndIsrRequestData) LeaderAndIsrPartitionState(org.apache.kafka.common.message.LeaderAndIsrRequestData.LeaderAndIsrPartitionState) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 Node (org.apache.kafka.common.Node)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 Uuid (org.apache.kafka.common.Uuid)1 ClusterAuthorizationException (org.apache.kafka.common.errors.ClusterAuthorizationException)1 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)1 LeaderAndIsrRequestData (org.apache.kafka.common.message.LeaderAndIsrRequestData)1 LeaderAndIsrLiveLeader (org.apache.kafka.common.message.LeaderAndIsrRequestData.LeaderAndIsrLiveLeader)1 LeaderAndIsrPartitionState (org.apache.kafka.common.message.LeaderAndIsrRequestData.LeaderAndIsrPartitionState)1