Search in sources :

Example 41 with Uuid

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

the class TopicsImageTest method testLocalReassignmentChanges.

@Test
public void testLocalReassignmentChanges() {
    int localId = 3;
    Uuid zooId = Uuid.fromString("0hHJ3X5ZQ-CFfQ5xgpj90w");
    List<TopicImage> topics = new ArrayList<>();
    topics.add(newTopicImage("zoo", zooId, newPartition(new int[] { 0, 1, localId }), newPartition(new int[] { localId, 1, 2 }), newPartition(new int[] { 0, 1, localId }), newPartition(new int[] { localId, 1, 2 }), newPartition(new int[] { 0, 1, 2 }), newPartition(new int[] { 0, 1, 2 })));
    TopicsImage image = new TopicsImage(newTopicsByIdMap(topics), newTopicsByNameMap(topics));
    List<ApiMessageAndVersion> topicRecords = new ArrayList<>();
    // zoo-0 - follower to leader
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(0).setLeader(localId), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    // zoo-1 - leader to follower
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(1).setLeader(1), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    // zoo-2 - follower to removed
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(2).setIsr(Arrays.asList(0, 1, 2)).setReplicas(Arrays.asList(0, 1, 2)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    // zoo-3 - leader to removed
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(3).setLeader(0).setIsr(Arrays.asList(0, 1, 2)).setReplicas(Arrays.asList(0, 1, 2)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    // zoo-4 - not replica to leader
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(4).setLeader(localId).setIsr(Arrays.asList(localId, 1, 2)).setReplicas(Arrays.asList(localId, 1, 2)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    // zoo-5 - not replica to follower
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(5).setIsr(Arrays.asList(0, 1, localId)).setReplicas(Arrays.asList(0, 1, localId)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    TopicsDelta delta = new TopicsDelta(image);
    RecordTestUtils.replayAll(delta, topicRecords);
    LocalReplicaChanges changes = delta.localChanges(localId);
    assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("zoo", 2), new TopicPartition("zoo", 3))), changes.deletes());
    assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("zoo", 0), new TopicPartition("zoo", 4))), changes.leaders().keySet());
    assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("zoo", 1), new TopicPartition("zoo", 5))), changes.followers().keySet());
}
Also used : PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) ArrayList(java.util.ArrayList) Uuid(org.apache.kafka.common.Uuid) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.jupiter.api.Test)

Example 42 with Uuid

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

the class TopicsImageTest method testBasicLocalChanges.

@Test
public void testBasicLocalChanges() {
    int localId = 3;
    /* Changes already include in DELTA1_RECORDS and IMAGE1:
         * foo - topic id deleted
         * bar-0 - stay as follower with different partition epoch
         * baz-0 - new topic to leader
         */
    List<ApiMessageAndVersion> topicRecords = new ArrayList<>(DELTA1_RECORDS);
    // Create a new foo topic with a different id
    Uuid newFooId = Uuid.fromString("b66ybsWIQoygs01vdjH07A");
    topicRecords.add(new ApiMessageAndVersion(new TopicRecord().setName("foo").setTopicId(newFooId), TOPIC_RECORD.highestSupportedVersion()));
    topicRecords.add(newPartitionRecord(newFooId, 0, Arrays.asList(0, 1, 2)));
    topicRecords.add(newPartitionRecord(newFooId, 1, Arrays.asList(0, 1, localId)));
    // baz-1 - new partition to follower
    topicRecords.add(new ApiMessageAndVersion(new PartitionRecord().setPartitionId(1).setTopicId(BAZ_UUID).setReplicas(Arrays.asList(4, 2, localId)).setIsr(Arrays.asList(4, 2, localId)).setLeader(4).setLeaderEpoch(2).setPartitionEpoch(1), PARTITION_RECORD.highestSupportedVersion()));
    TopicsDelta delta = new TopicsDelta(IMAGE1);
    RecordTestUtils.replayAll(delta, topicRecords);
    LocalReplicaChanges changes = delta.localChanges(localId);
    assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("foo", 0), new TopicPartition("foo", 1))), changes.deletes());
    assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("baz", 0))), changes.leaders().keySet());
    assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("baz", 1), new TopicPartition("bar", 0), new TopicPartition("foo", 1))), changes.followers().keySet());
}
Also used : RemoveTopicRecord(org.apache.kafka.common.metadata.RemoveTopicRecord) TopicRecord(org.apache.kafka.common.metadata.TopicRecord) Uuid(org.apache.kafka.common.Uuid) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) PartitionRecord(org.apache.kafka.common.metadata.PartitionRecord) Test(org.junit.jupiter.api.Test)

Example 43 with Uuid

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

the class MockController method createTopics.

@Override
public synchronized CompletableFuture<CreateTopicsResponseData> createTopics(CreateTopicsRequestData request) {
    CreateTopicsResponseData response = new CreateTopicsResponseData();
    for (CreatableTopic topic : request.topics()) {
        if (topicNameToId.containsKey(topic.name())) {
            response.topics().add(new CreatableTopicResult().setName(topic.name()).setErrorCode(Errors.TOPIC_ALREADY_EXISTS.code()));
        } else {
            long topicId = nextTopicId.getAndIncrement();
            Uuid topicUuid = new Uuid(0, topicId);
            topicNameToId.put(topic.name(), topicUuid);
            topics.put(topicUuid, new MockTopic(topic.name(), topicUuid));
            response.topics().add(new CreatableTopicResult().setName(topic.name()).setErrorCode(Errors.NONE.code()).setTopicId(topicUuid));
        // For a better mock, we might want to return configs, replication factor,
        // etc.  Right now, the tests that use MockController don't need these
        // things.
        }
    }
    return CompletableFuture.completedFuture(response);
}
Also used : Uuid(org.apache.kafka.common.Uuid) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData)

Example 44 with Uuid

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

the class MetadataCache method mergeWith.

/**
 * Merges the metadata cache's contents with the provided metadata, returning a new metadata cache. The provided
 * metadata is presumed to be more recent than the cache's metadata, and therefore all overlapping metadata will
 * be overridden.
 *
 * @param newClusterId the new cluster Id
 * @param newNodes the new set of nodes
 * @param addPartitions partitions to add
 * @param addUnauthorizedTopics unauthorized topics to add
 * @param addInternalTopics internal topics to add
 * @param newController the new controller node
 * @param topicIds the mapping from topic name to topic ID from the MetadataResponse
 * @param retainTopic returns whether a topic's metadata should be retained
 * @return the merged metadata cache
 */
MetadataCache mergeWith(String newClusterId, Map<Integer, Node> newNodes, Collection<PartitionMetadata> addPartitions, Set<String> addUnauthorizedTopics, Set<String> addInvalidTopics, Set<String> addInternalTopics, Node newController, Map<String, Uuid> topicIds, BiPredicate<String, Boolean> retainTopic) {
    Predicate<String> shouldRetainTopic = topic -> retainTopic.test(topic, internalTopics.contains(topic));
    Map<TopicPartition, PartitionMetadata> newMetadataByPartition = new HashMap<>(addPartitions.size());
    // We want the most recent topic ID. We start with the previous ID stored for retained topics and then
    // update with newest information from the MetadataResponse. We always take the latest state, removing existing
    // topic IDs if the latest state contains the topic name but not a topic ID.
    Map<String, Uuid> newTopicIds = topicIds.entrySet().stream().filter(entry -> shouldRetainTopic.test(entry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    for (PartitionMetadata partition : addPartitions) {
        newMetadataByPartition.put(partition.topicPartition, partition);
        Uuid id = topicIds.get(partition.topic());
        if (id != null)
            newTopicIds.put(partition.topic(), id);
        else
            // Remove if the latest metadata does not have a topic ID
            newTopicIds.remove(partition.topic());
    }
    for (Map.Entry<TopicPartition, PartitionMetadata> entry : metadataByPartition.entrySet()) {
        if (shouldRetainTopic.test(entry.getKey().topic())) {
            newMetadataByPartition.putIfAbsent(entry.getKey(), entry.getValue());
        }
    }
    Set<String> newUnauthorizedTopics = fillSet(addUnauthorizedTopics, unauthorizedTopics, shouldRetainTopic);
    Set<String> newInvalidTopics = fillSet(addInvalidTopics, invalidTopics, shouldRetainTopic);
    Set<String> newInternalTopics = fillSet(addInternalTopics, internalTopics, shouldRetainTopic);
    return new MetadataCache(newClusterId, newNodes, newMetadataByPartition.values(), newUnauthorizedTopics, newInvalidTopics, newInternalTopics, newController, newTopicIds);
}
Also used : Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) ClusterResource(org.apache.kafka.common.ClusterResource) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) HashMap(java.util.HashMap) PartitionInfo(org.apache.kafka.common.PartitionInfo) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) PartitionMetadata(org.apache.kafka.common.requests.MetadataResponse.PartitionMetadata) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) Cluster(org.apache.kafka.common.Cluster) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) Node(org.apache.kafka.common.Node) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Collections(java.util.Collections) HashMap(java.util.HashMap) Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) PartitionMetadata(org.apache.kafka.common.requests.MetadataResponse.PartitionMetadata) HashMap(java.util.HashMap) Map(java.util.Map)

Example 45 with Uuid

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

the class SimpleExampleMessageTest method shouldStoreField.

@Test
public void shouldStoreField() {
    final Uuid uuid = Uuid.randomUuid();
    final ByteBuffer buf = ByteBuffer.wrap(new byte[] { 1, 2, 3 });
    final SimpleExampleMessageData out = new SimpleExampleMessageData();
    out.setProcessId(uuid);
    out.setZeroCopyByteBuffer(buf);
    assertEquals(uuid, out.processId());
    assertEquals(buf, out.zeroCopyByteBuffer());
    out.setNullableZeroCopyByteBuffer(null);
    assertNull(out.nullableZeroCopyByteBuffer());
    out.setNullableZeroCopyByteBuffer(buf);
    assertEquals(buf, out.nullableZeroCopyByteBuffer());
}
Also used : Uuid(org.apache.kafka.common.Uuid) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

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