Search in sources :

Example 61 with Uuid

use of org.apache.kafka.common.Uuid in project kafka by apache.

the class LeaderAndIsrResponseTest method testErrorCountsNoTopLevelError.

@Test
public void testErrorCountsNoTopLevelError() {
    for (short version : LEADER_AND_ISR.allVersions()) {
        LeaderAndIsrResponse response;
        if (version < 5) {
            List<LeaderAndIsrPartitionError> partitions = createPartitions("foo", asList(Errors.NONE, Errors.CLUSTER_AUTHORIZATION_FAILED));
            response = new LeaderAndIsrResponse(new LeaderAndIsrResponseData().setErrorCode(Errors.NONE.code()).setPartitionErrors(partitions), version);
        } else {
            Uuid id = Uuid.randomUuid();
            LeaderAndIsrTopicErrorCollection topics = createTopic(id, asList(Errors.NONE, Errors.CLUSTER_AUTHORIZATION_FAILED));
            response = new LeaderAndIsrResponse(new LeaderAndIsrResponseData().setErrorCode(Errors.NONE.code()).setTopics(topics), version);
        }
        Map<Errors, Integer> errorCounts = response.errorCounts();
        assertEquals(2, errorCounts.size());
        assertEquals(2, errorCounts.get(Errors.NONE).intValue());
        assertEquals(1, errorCounts.get(Errors.CLUSTER_AUTHORIZATION_FAILED).intValue());
    }
}
Also used : LeaderAndIsrPartitionError(org.apache.kafka.common.message.LeaderAndIsrResponseData.LeaderAndIsrPartitionError) Errors(org.apache.kafka.common.protocol.Errors) LeaderAndIsrResponseData(org.apache.kafka.common.message.LeaderAndIsrResponseData) Uuid(org.apache.kafka.common.Uuid) LeaderAndIsrTopicErrorCollection(org.apache.kafka.common.message.LeaderAndIsrResponseData.LeaderAndIsrTopicErrorCollection) Test(org.junit.jupiter.api.Test)

Example 62 with Uuid

use of org.apache.kafka.common.Uuid in project kafka by apache.

the class LeaderAndIsrResponseTest method testErrorCountsWithTopLevelError.

@Test
public void testErrorCountsWithTopLevelError() {
    for (short version : LEADER_AND_ISR.allVersions()) {
        LeaderAndIsrResponse response;
        if (version < 5) {
            List<LeaderAndIsrPartitionError> partitions = createPartitions("foo", asList(Errors.NONE, Errors.NOT_LEADER_OR_FOLLOWER));
            response = new LeaderAndIsrResponse(new LeaderAndIsrResponseData().setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code()).setPartitionErrors(partitions), version);
        } else {
            Uuid id = Uuid.randomUuid();
            LeaderAndIsrTopicErrorCollection topics = createTopic(id, asList(Errors.NONE, Errors.NOT_LEADER_OR_FOLLOWER));
            response = new LeaderAndIsrResponse(new LeaderAndIsrResponseData().setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code()).setTopics(topics), version);
        }
        assertEquals(Collections.singletonMap(Errors.UNKNOWN_SERVER_ERROR, 3), response.errorCounts());
    }
}
Also used : LeaderAndIsrPartitionError(org.apache.kafka.common.message.LeaderAndIsrResponseData.LeaderAndIsrPartitionError) LeaderAndIsrResponseData(org.apache.kafka.common.message.LeaderAndIsrResponseData) Uuid(org.apache.kafka.common.Uuid) LeaderAndIsrTopicErrorCollection(org.apache.kafka.common.message.LeaderAndIsrResponseData.LeaderAndIsrTopicErrorCollection) Test(org.junit.jupiter.api.Test)

Example 63 with Uuid

use of org.apache.kafka.common.Uuid in project kafka by apache.

the class DeleteTopicsRequestTest method testTopicIdsField.

@Test
public void testTopicIdsField() {
    for (short version : DELETE_TOPICS.allVersions()) {
        // Check topic IDs are handled correctly. We should only use this field on versions 6+.
        Uuid topicId1 = Uuid.randomUuid();
        Uuid topicId2 = Uuid.randomUuid();
        List<Uuid> topicIds = Arrays.asList(topicId1, topicId2);
        DeleteTopicsRequest requestWithIds = new DeleteTopicsRequest.Builder(new DeleteTopicsRequestData().setTopics(Arrays.asList(new DeleteTopicsRequestData.DeleteTopicState().setTopicId(topicId1), new DeleteTopicsRequestData.DeleteTopicState().setTopicId(topicId2)))).build(version);
        if (version >= 6) {
            DeleteTopicsRequest requestWithIdsSerialized = DeleteTopicsRequest.parse(requestWithIds.serialize(), version);
            assertEquals(topicIds, requestWithIds.topicIds());
            assertEquals(topicIds, requestWithIdsSerialized.topicIds());
            // All topic names should be replaced with null
            requestWithIds.data().topics().forEach(topic -> assertNull(topic.name()));
            requestWithIdsSerialized.data().topics().forEach(topic -> assertNull(topic.name()));
        } else {
            // We should fail if version is less than 6.
            assertThrows(UnsupportedVersionException.class, () -> requestWithIds.serialize());
        }
    }
}
Also used : DeleteTopicState(org.apache.kafka.common.message.DeleteTopicsRequestData.DeleteTopicState) Uuid(org.apache.kafka.common.Uuid) DeleteTopicsRequestData(org.apache.kafka.common.message.DeleteTopicsRequestData) Test(org.junit.jupiter.api.Test)

Example 64 with Uuid

use of org.apache.kafka.common.Uuid in project kafka by apache.

the class RequestResponseTest method createLeaderAndIsrRequest.

private LeaderAndIsrRequest createLeaderAndIsrRequest(short version) {
    List<LeaderAndIsrPartitionState> partitionStates = new ArrayList<>();
    List<Integer> isr = asList(1, 2);
    List<Integer> replicas = asList(1, 2, 3, 4);
    partitionStates.add(new LeaderAndIsrPartitionState().setTopicName("topic5").setPartitionIndex(105).setControllerEpoch(0).setLeader(2).setLeaderEpoch(1).setIsr(isr).setZkVersion(2).setReplicas(replicas).setIsNew(false));
    partitionStates.add(new LeaderAndIsrPartitionState().setTopicName("topic5").setPartitionIndex(1).setControllerEpoch(1).setLeader(1).setLeaderEpoch(1).setIsr(isr).setZkVersion(2).setReplicas(replicas).setIsNew(false));
    partitionStates.add(new LeaderAndIsrPartitionState().setTopicName("topic20").setPartitionIndex(1).setControllerEpoch(1).setLeader(0).setLeaderEpoch(1).setIsr(isr).setZkVersion(2).setReplicas(replicas).setIsNew(false));
    Set<Node> leaders = Utils.mkSet(new Node(0, "test0", 1223), new Node(1, "test1", 1223));
    Map<String, Uuid> topicIds = new HashMap<>();
    topicIds.put("topic5", Uuid.randomUuid());
    topicIds.put("topic20", Uuid.randomUuid());
    return new LeaderAndIsrRequest.Builder(version, 1, 10, 0, partitionStates, topicIds, leaders).build();
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) Uuid(org.apache.kafka.common.Uuid) LeaderAndIsrPartitionState(org.apache.kafka.common.message.LeaderAndIsrRequestData.LeaderAndIsrPartitionState)

Example 65 with Uuid

use of org.apache.kafka.common.Uuid in project kafka by apache.

the class FetchRequestTest method testToReplaceWithDifferentVersions.

@ParameterizedTest
@MethodSource("fetchVersions")
public void testToReplaceWithDifferentVersions(short version) {
    boolean fetchRequestUsesTopicIds = version >= 13;
    Uuid topicId = Uuid.randomUuid();
    TopicIdPartition tp = new TopicIdPartition(topicId, 0, "topic");
    Map<TopicPartition, FetchRequest.PartitionData> partitionData = Collections.singletonMap(tp.topicPartition(), new FetchRequest.PartitionData(topicId, 0, 0, 0, Optional.empty()));
    List<TopicIdPartition> toReplace = Collections.singletonList(tp);
    FetchRequest fetchRequest = FetchRequest.Builder.forReplica(version, 0, 1, 1, partitionData).removed(Collections.emptyList()).replaced(toReplace).metadata(FetchMetadata.newIncremental(123)).build(version);
    // If version < 13, we should not see any partitions in forgottenTopics. This is because we can not
    // distinguish different topic IDs on versions earlier than 13.
    assertEquals(fetchRequestUsesTopicIds, fetchRequest.data().forgottenTopicsData().size() > 0);
    fetchRequest.data().forgottenTopicsData().forEach(forgottenTopic -> {
        // Since we didn't serialize, we should see the topic name and ID regardless of the version.
        assertEquals(tp.topic(), forgottenTopic.topic());
        assertEquals(topicId, forgottenTopic.topicId());
    });
    assertEquals(1, fetchRequest.data().topics().size());
    fetchRequest.data().topics().forEach(topic -> {
        // Since we didn't serialize, we should see the topic name and ID regardless of the version.
        assertEquals(tp.topic(), topic.topic());
        assertEquals(topicId, topic.topicId());
    });
}
Also used : Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) TopicIdPartition(org.apache.kafka.common.TopicIdPartition) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Uuid (org.apache.kafka.common.Uuid)95 Test (org.junit.jupiter.api.Test)55 HashMap (java.util.HashMap)42 TopicPartition (org.apache.kafka.common.TopicPartition)40 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)30 ArrayList (java.util.ArrayList)29 Map (java.util.Map)21 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)21 LinkedHashMap (java.util.LinkedHashMap)18 List (java.util.List)15 FetchRequest (org.apache.kafka.common.requests.FetchRequest)14 TopicIdPartition (org.apache.kafka.common.TopicIdPartition)13 Errors (org.apache.kafka.common.protocol.Errors)12 FetchResponse (org.apache.kafka.common.requests.FetchResponse)12 Collections (java.util.Collections)11 ByteBuffer (java.nio.ByteBuffer)10 Node (org.apache.kafka.common.Node)10 CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)10 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)10 PartitionRegistration (org.apache.kafka.metadata.PartitionRegistration)10