Search in sources :

Example 66 with Uuid

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

the class LeaderAndIsrRequestTest method testTopicPartitionGroupingSizeReduction.

@Test
public void testTopicPartitionGroupingSizeReduction() {
    Set<TopicPartition> tps = TestUtils.generateRandomTopicPartitions(10, 10);
    List<LeaderAndIsrPartitionState> partitionStates = new ArrayList<>();
    Map<String, Uuid> topicIds = new HashMap<>();
    for (TopicPartition tp : tps) {
        partitionStates.add(new LeaderAndIsrPartitionState().setTopicName(tp.topic()).setPartitionIndex(tp.partition()));
        topicIds.put(tp.topic(), Uuid.randomUuid());
    }
    LeaderAndIsrRequest.Builder builder = new LeaderAndIsrRequest.Builder((short) 2, 0, 0, 0, partitionStates, topicIds, Collections.emptySet());
    LeaderAndIsrRequest v2 = builder.build((short) 2);
    LeaderAndIsrRequest v1 = builder.build((short) 1);
    assertTrue(v2.sizeInBytes() < v1.sizeInBytes(), "Expected v2 < v1: v2=" + v2.sizeInBytes() + ", v1=" + v1.sizeInBytes());
}
Also used : Uuid(org.apache.kafka.common.Uuid) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) LeaderAndIsrPartitionState(org.apache.kafka.common.message.LeaderAndIsrRequestData.LeaderAndIsrPartitionState) Test(org.junit.jupiter.api.Test)

Example 67 with Uuid

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

the class TopicCollectionTest method testTopicCollection.

@Test
public void testTopicCollection() {
    List<Uuid> topicIds = Arrays.asList(Uuid.randomUuid(), Uuid.randomUuid(), Uuid.randomUuid());
    List<String> topicNames = Arrays.asList("foo", "bar");
    TopicCollection idCollection = TopicCollection.ofTopicIds(topicIds);
    TopicCollection nameCollection = TopicCollection.ofTopicNames(topicNames);
    assertTrue(((TopicIdCollection) idCollection).topicIds().containsAll(topicIds));
    assertTrue(((TopicNameCollection) nameCollection).topicNames().containsAll(topicNames));
}
Also used : Uuid(org.apache.kafka.common.Uuid) TopicIdCollection(org.apache.kafka.common.TopicCollection.TopicIdCollection) TopicCollection(org.apache.kafka.common.TopicCollection) TopicNameCollection(org.apache.kafka.common.TopicCollection.TopicNameCollection) Test(org.junit.jupiter.api.Test)

Example 68 with Uuid

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

the class MockAdminClient method handleDescribeTopicsByNames.

private Map<String, KafkaFuture<TopicDescription>> handleDescribeTopicsByNames(Collection<String> topicNames, DescribeTopicsOptions options) {
    Map<String, KafkaFuture<TopicDescription>> topicDescriptions = new HashMap<>();
    if (timeoutNextRequests > 0) {
        for (String requestedTopic : topicNames) {
            KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new TimeoutException());
            topicDescriptions.put(requestedTopic, future);
        }
        --timeoutNextRequests;
        return topicDescriptions;
    }
    for (String requestedTopic : topicNames) {
        for (Map.Entry<String, TopicMetadata> topicDescription : allTopics.entrySet()) {
            String topicName = topicDescription.getKey();
            Uuid topicId = topicIds.getOrDefault(topicName, Uuid.ZERO_UUID);
            if (topicName.equals(requestedTopic) && !topicDescription.getValue().markedForDeletion) {
                if (topicDescription.getValue().fetchesRemainingUntilVisible > 0) {
                    topicDescription.getValue().fetchesRemainingUntilVisible--;
                } else {
                    TopicMetadata topicMetadata = topicDescription.getValue();
                    KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
                    future.complete(new TopicDescription(topicName, topicMetadata.isInternalTopic, topicMetadata.partitions, Collections.emptySet(), topicId));
                    topicDescriptions.put(topicName, future);
                    break;
                }
            }
        }
        if (!topicDescriptions.containsKey(requestedTopic)) {
            KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new UnknownTopicOrPartitionException("Topic " + requestedTopic + " not found."));
            topicDescriptions.put(requestedTopic, future);
        }
    }
    return topicDescriptions;
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) Uuid(org.apache.kafka.common.Uuid) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 69 with Uuid

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

the class QuorumControllerTest method testSnapshotConfiguration.

@Test
public void testSnapshotConfiguration() throws Throwable {
    final int numBrokers = 4;
    final int maxNewRecordBytes = 4;
    Map<Integer, Long> brokerEpochs = new HashMap<>();
    Uuid fooId;
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(3, Optional.empty())) {
        try (QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, builder -> {
            builder.setConfigDefs(CONFIGS).setSnapshotMaxNewRecordBytes(maxNewRecordBytes);
        })) {
            QuorumController active = controlEnv.activeController();
            for (int i = 0; i < numBrokers; i++) {
                BrokerRegistrationReply reply = active.registerBroker(new BrokerRegistrationRequestData().setBrokerId(i).setRack(null).setClusterId(active.clusterId()).setIncarnationId(Uuid.fromString("kxAT73dKQsitIedpiPtwB" + i)).setListeners(new ListenerCollection(Arrays.asList(new Listener().setName("PLAINTEXT").setHost("localhost").setPort(9092 + i)).iterator()))).get();
                brokerEpochs.put(i, reply.epoch());
            }
            for (int i = 0; i < numBrokers - 1; i++) {
                assertEquals(new BrokerHeartbeatReply(true, false, false, false), active.processBrokerHeartbeat(new BrokerHeartbeatRequestData().setWantFence(false).setBrokerEpoch(brokerEpochs.get(i)).setBrokerId(i).setCurrentMetadataOffset(100000L)).get());
            }
            CreateTopicsResponseData fooData = active.createTopics(new CreateTopicsRequestData().setTopics(new CreatableTopicCollection(Collections.singleton(new CreatableTopic().setName("foo").setNumPartitions(-1).setReplicationFactor((short) -1).setAssignments(new CreatableReplicaAssignmentCollection(Arrays.asList(new CreatableReplicaAssignment().setPartitionIndex(0).setBrokerIds(Arrays.asList(0, 1, 2)), new CreatableReplicaAssignment().setPartitionIndex(1).setBrokerIds(Arrays.asList(1, 2, 0))).iterator()))).iterator()))).get();
            fooId = fooData.topics().find("foo").topicId();
            active.allocateProducerIds(new AllocateProducerIdsRequestData().setBrokerId(0).setBrokerEpoch(brokerEpochs.get(0))).get();
            SnapshotReader<ApiMessageAndVersion> snapshot = createSnapshotReader(logEnv.waitForLatestSnapshot());
            checkSnapshotSubcontent(expectedSnapshotContent(fooId, brokerEpochs), snapshot);
        }
    }
}
Also used : BrokerHeartbeatReply(org.apache.kafka.metadata.BrokerHeartbeatReply) ListenerCollection(org.apache.kafka.common.message.BrokerRegistrationRequestData.ListenerCollection) LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) Listener(org.apache.kafka.common.message.BrokerRegistrationRequestData.Listener) HashMap(java.util.HashMap) BrokerRegistrationRequestData(org.apache.kafka.common.message.BrokerRegistrationRequestData) BrokerRegistrationReply(org.apache.kafka.metadata.BrokerRegistrationReply) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) Uuid(org.apache.kafka.common.Uuid) BrokerHeartbeatRequestData(org.apache.kafka.common.message.BrokerHeartbeatRequestData) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreatableReplicaAssignment(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) AllocateProducerIdsRequestData(org.apache.kafka.common.message.AllocateProducerIdsRequestData) CreatableReplicaAssignmentCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignmentCollection) Test(org.junit.jupiter.api.Test)

Example 70 with Uuid

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

the class ReplicationControlManagerTest method testGlobalTopicAndPartitionMetrics.

@Test
public void testGlobalTopicAndPartitionMetrics() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replicationControl = ctx.replicationControl;
    CreateTopicsRequestData request = new CreateTopicsRequestData();
    request.topics().add(new CreatableTopic().setName("foo").setNumPartitions(1).setReplicationFactor((short) -1));
    ctx.registerBrokers(0, 1, 2);
    ctx.unfenceBrokers(0, 1, 2);
    List<Uuid> topicsToDelete = new ArrayList<>();
    ControllerResult<CreateTopicsResponseData> result = replicationControl.createTopics(request);
    topicsToDelete.add(result.response().topics().find("foo").topicId());
    RecordTestUtils.replayAll(replicationControl, result.records());
    assertEquals(1, ctx.metrics.globalTopicsCount());
    request = new CreateTopicsRequestData();
    request.topics().add(new CreatableTopic().setName("bar").setNumPartitions(1).setReplicationFactor((short) -1));
    request.topics().add(new CreatableTopic().setName("baz").setNumPartitions(2).setReplicationFactor((short) -1));
    result = replicationControl.createTopics(request);
    RecordTestUtils.replayAll(replicationControl, result.records());
    assertEquals(3, ctx.metrics.globalTopicsCount());
    assertEquals(4, ctx.metrics.globalPartitionCount());
    topicsToDelete.add(result.response().topics().find("baz").topicId());
    ControllerResult<Map<Uuid, ApiError>> deleteResult = replicationControl.deleteTopics(topicsToDelete);
    RecordTestUtils.replayAll(replicationControl, deleteResult.records());
    assertEquals(1, ctx.metrics.globalTopicsCount());
    assertEquals(1, ctx.metrics.globalPartitionCount());
    Uuid topicToDelete = result.response().topics().find("bar").topicId();
    deleteResult = replicationControl.deleteTopics(Collections.singletonList(topicToDelete));
    RecordTestUtils.replayAll(replicationControl, deleteResult.records());
    assertEquals(0, ctx.metrics.globalTopicsCount());
    assertEquals(0, ctx.metrics.globalPartitionCount());
}
Also used : Uuid(org.apache.kafka.common.Uuid) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) ArrayList(java.util.ArrayList) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) 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