Search in sources :

Example 1 with LocalLogManagerTestEnv

use of org.apache.kafka.metalog.LocalLogManagerTestEnv in project kafka by apache.

the class QuorumControllerTest method testCreateAndClose.

/**
 * Test creating a new QuorumController and closing it.
 */
@Test
public void testCreateAndClose() throws Throwable {
    MockControllerMetrics metrics = new MockControllerMetrics();
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(1, Optional.empty());
        QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, builder -> builder.setMetrics(metrics))) {
    }
    assertTrue(metrics.isClosed(), "metrics were not closed");
}
Also used : LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) Test(org.junit.jupiter.api.Test)

Example 2 with LocalLogManagerTestEnv

use of org.apache.kafka.metalog.LocalLogManagerTestEnv in project kafka by apache.

the class QuorumControllerTest method testUnregisterBroker.

@Test
public void testUnregisterBroker() throws Throwable {
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(1, Optional.empty())) {
        try (QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, b -> b.setConfigDefs(CONFIGS))) {
            ListenerCollection listeners = new ListenerCollection();
            listeners.add(new Listener().setName("PLAINTEXT").setHost("localhost").setPort(9092));
            QuorumController active = controlEnv.activeController();
            CompletableFuture<BrokerRegistrationReply> reply = active.registerBroker(new BrokerRegistrationRequestData().setBrokerId(0).setClusterId(active.clusterId()).setIncarnationId(Uuid.fromString("kxAT73dKQsitIedpiPtwBA")).setListeners(listeners));
            assertEquals(0L, reply.get().epoch());
            CreateTopicsRequestData createTopicsRequestData = new CreateTopicsRequestData().setTopics(new CreatableTopicCollection(Collections.singleton(new CreatableTopic().setName("foo").setNumPartitions(1).setReplicationFactor((short) 1)).iterator()));
            assertEquals(Errors.INVALID_REPLICATION_FACTOR.code(), active.createTopics(createTopicsRequestData).get().topics().find("foo").errorCode());
            assertEquals("Unable to replicate the partition 1 time(s): All brokers " + "are currently fenced.", active.createTopics(createTopicsRequestData).get().topics().find("foo").errorMessage());
            assertEquals(new BrokerHeartbeatReply(true, false, false, false), active.processBrokerHeartbeat(new BrokerHeartbeatRequestData().setWantFence(false).setBrokerEpoch(0L).setBrokerId(0).setCurrentMetadataOffset(100000L)).get());
            assertEquals(Errors.NONE.code(), active.createTopics(createTopicsRequestData).get().topics().find("foo").errorCode());
            CompletableFuture<TopicIdPartition> topicPartitionFuture = active.appendReadEvent("debugGetPartition", () -> {
                Iterator<TopicIdPartition> iterator = active.replicationControl().brokersToIsrs().iterator(0, true);
                assertTrue(iterator.hasNext());
                return iterator.next();
            });
            assertEquals(0, topicPartitionFuture.get().partitionId());
            active.unregisterBroker(0).get();
            topicPartitionFuture = active.appendReadEvent("debugGetPartition", () -> {
                Iterator<TopicIdPartition> iterator = active.replicationControl().brokersToIsrs().partitionsWithNoLeader();
                assertTrue(iterator.hasNext());
                return iterator.next();
            });
            assertEquals(0, topicPartitionFuture.get().partitionId());
        }
    }
}
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) BrokerRegistrationRequestData(org.apache.kafka.common.message.BrokerRegistrationRequestData) BrokerRegistrationReply(org.apache.kafka.metadata.BrokerRegistrationReply) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) BrokerHeartbeatRequestData(org.apache.kafka.common.message.BrokerHeartbeatRequestData) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) Iterator(java.util.Iterator) Test(org.junit.jupiter.api.Test)

Example 3 with LocalLogManagerTestEnv

use of org.apache.kafka.metalog.LocalLogManagerTestEnv in project kafka by apache.

the class QuorumControllerTest method testTimeouts.

/**
 * Test that certain controller operations time out if they stay on the controller
 * queue for too long.
 */
@Test
public void testTimeouts() throws Throwable {
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(1, Optional.empty())) {
        try (QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, b -> b.setConfigDefs(CONFIGS))) {
            QuorumController controller = controlEnv.activeController();
            CountDownLatch countDownLatch = controller.pause();
            CompletableFuture<CreateTopicsResponseData> createFuture = controller.createTopics(new CreateTopicsRequestData().setTimeoutMs(0).setTopics(new CreatableTopicCollection(Collections.singleton(new CreatableTopic().setName("foo")).iterator())));
            long now = controller.time().nanoseconds();
            CompletableFuture<Map<Uuid, ApiError>> deleteFuture = controller.deleteTopics(now, Collections.singletonList(Uuid.ZERO_UUID));
            CompletableFuture<Map<String, ResultOrError<Uuid>>> findTopicIdsFuture = controller.findTopicIds(now, Collections.singletonList("foo"));
            CompletableFuture<Map<Uuid, ResultOrError<String>>> findTopicNamesFuture = controller.findTopicNames(now, Collections.singletonList(Uuid.ZERO_UUID));
            CompletableFuture<List<CreatePartitionsTopicResult>> createPartitionsFuture = controller.createPartitions(now, Collections.singletonList(new CreatePartitionsTopic()));
            CompletableFuture<ElectLeadersResponseData> electLeadersFuture = controller.electLeaders(new ElectLeadersRequestData().setTimeoutMs(0).setTopicPartitions(null));
            CompletableFuture<AlterPartitionReassignmentsResponseData> alterReassignmentsFuture = controller.alterPartitionReassignments(new AlterPartitionReassignmentsRequestData().setTimeoutMs(0).setTopics(Collections.singletonList(new ReassignableTopic())));
            CompletableFuture<ListPartitionReassignmentsResponseData> listReassignmentsFuture = controller.listPartitionReassignments(new ListPartitionReassignmentsRequestData().setTopics(null).setTimeoutMs(0));
            while (controller.time().nanoseconds() == now) {
                Thread.sleep(0, 10);
            }
            countDownLatch.countDown();
            assertYieldsTimeout(createFuture);
            assertYieldsTimeout(deleteFuture);
            assertYieldsTimeout(findTopicIdsFuture);
            assertYieldsTimeout(findTopicNamesFuture);
            assertYieldsTimeout(createPartitionsFuture);
            assertYieldsTimeout(electLeadersFuture);
            assertYieldsTimeout(alterReassignmentsFuture);
            assertYieldsTimeout(listReassignmentsFuture);
        }
    }
}
Also used : ReassignableTopic(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) List(java.util.List) LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) CountDownLatch(java.util.concurrent.CountDownLatch) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) ListPartitionReassignmentsRequestData(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData) Uuid(org.apache.kafka.common.Uuid) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) Map(java.util.Map) HashMap(java.util.HashMap) CreatePartitionsTopic(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic) Test(org.junit.jupiter.api.Test)

Example 4 with LocalLogManagerTestEnv

use of org.apache.kafka.metalog.LocalLogManagerTestEnv in project kafka by apache.

the class QuorumControllerTest method testMissingInMemorySnapshot.

@Test
public void testMissingInMemorySnapshot() throws Exception {
    int numBrokers = 3;
    int numPartitions = 3;
    String topicName = "topic-name";
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(1, Optional.empty());
        QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, b -> b.setConfigDefs(CONFIGS))) {
        QuorumController controller = controlEnv.activeController();
        Map<Integer, Long> brokerEpochs = registerBrokers(controller, numBrokers);
        // Create a lot of partitions
        List<CreatableReplicaAssignment> partitions = IntStream.range(0, numPartitions).mapToObj(partitionIndex -> new CreatableReplicaAssignment().setPartitionIndex(partitionIndex).setBrokerIds(Arrays.asList(0, 1, 2))).collect(Collectors.toList());
        Uuid topicId = controller.createTopics(new CreateTopicsRequestData().setTopics(new CreatableTopicCollection(Collections.singleton(new CreatableTopic().setName(topicName).setNumPartitions(-1).setReplicationFactor((short) -1).setAssignments(new CreatableReplicaAssignmentCollection(partitions.iterator()))).iterator()))).get().topics().find(topicName).topicId();
        // Create a lot of alter isr
        List<AlterIsrRequestData.PartitionData> alterIsrs = IntStream.range(0, numPartitions).mapToObj(partitionIndex -> {
            PartitionRegistration partitionRegistration = controller.replicationControl().getPartition(topicId, partitionIndex);
            return new AlterIsrRequestData.PartitionData().setPartitionIndex(partitionIndex).setLeaderEpoch(partitionRegistration.leaderEpoch).setCurrentIsrVersion(partitionRegistration.partitionEpoch).setNewIsr(Arrays.asList(0, 1));
        }).collect(Collectors.toList());
        AlterIsrRequestData.TopicData topicData = new AlterIsrRequestData.TopicData().setName(topicName);
        topicData.partitions().addAll(alterIsrs);
        int leaderId = 0;
        AlterIsrRequestData alterIsrRequest = new AlterIsrRequestData().setBrokerId(leaderId).setBrokerEpoch(brokerEpochs.get(leaderId));
        alterIsrRequest.topics().add(topicData);
        logEnv.logManagers().get(0).resignAfterNonAtomicCommit();
        int oldClaimEpoch = controller.curClaimEpoch();
        assertThrows(ExecutionException.class, () -> controller.alterIsr(alterIsrRequest).get());
        // Wait for the controller to become active again
        assertSame(controller, controlEnv.activeController());
        assertTrue(oldClaimEpoch < controller.curClaimEpoch(), String.format("oldClaimEpoch = %s, newClaimEpoch = %s", oldClaimEpoch, controller.curClaimEpoch()));
        // Since the alterIsr partially failed we expect to see
        // some partitions to still have 2 in the ISR.
        int partitionsWithReplica2 = Utils.toList(controller.replicationControl().brokersToIsrs().partitionsWithBrokerInIsr(2)).size();
        int partitionsWithReplica0 = Utils.toList(controller.replicationControl().brokersToIsrs().partitionsWithBrokerInIsr(0)).size();
        assertEquals(numPartitions, partitionsWithReplica0);
        assertNotEquals(0, partitionsWithReplica2);
        assertTrue(partitionsWithReplica0 > partitionsWithReplica2, String.format("partitionsWithReplica0 = %s, partitionsWithReplica2 = %s", partitionsWithReplica0, partitionsWithReplica2));
    }
}
Also used : Arrays(java.util.Arrays) BrokerRegistrationRequestData(org.apache.kafka.common.message.BrokerRegistrationRequestData) BrokerIdNotRegisteredException(org.apache.kafka.common.errors.BrokerIdNotRegisteredException) ConfigurationControlManagerTest.entry(org.apache.kafka.controller.ConfigurationControlManagerTest.entry) Spliterators(java.util.Spliterators) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) Listener(org.apache.kafka.common.message.BrokerRegistrationRequestData.Listener) CreatePartitionsTopicResult(org.apache.kafka.common.message.CreatePartitionsResponseData.CreatePartitionsTopicResult) Future(java.util.concurrent.Future) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) CreatePartitionsTopic(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic) BufferSupplier(org.apache.kafka.common.utils.BufferSupplier) Map(java.util.Map) SET(org.apache.kafka.clients.admin.AlterConfigOp.OpType.SET) CreatableReplicaAssignmentCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignmentCollection) ListPartitionReassignmentsRequestData(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData) TopicRecord(org.apache.kafka.common.metadata.TopicRecord) RawSnapshotReader(org.apache.kafka.snapshot.RawSnapshotReader) TestUtils(org.apache.kafka.test.TestUtils) PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) ConfigResourceExistenceChecker(org.apache.kafka.controller.QuorumController.ConfigResourceExistenceChecker) List(java.util.List) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) AllocateProducerIdsRequestData(org.apache.kafka.common.message.AllocateProducerIdsRequestData) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) Optional(java.util.Optional) Errors(org.apache.kafka.common.protocol.Errors) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) Spliterator(java.util.Spliterator) IntStream(java.util.stream.IntStream) Uuid(org.apache.kafka.common.Uuid) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) CONFIGS(org.apache.kafka.controller.ConfigurationControlManagerTest.CONFIGS) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) ApiError(org.apache.kafka.common.requests.ApiError) TOPIC(org.apache.kafka.common.config.ConfigResource.Type.TOPIC) ConfigResource(org.apache.kafka.common.config.ConfigResource) BrokerHeartbeatReply(org.apache.kafka.metadata.BrokerHeartbeatReply) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) StreamSupport(java.util.stream.StreamSupport) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SnapshotReader(org.apache.kafka.snapshot.SnapshotReader) Utils(org.apache.kafka.common.utils.Utils) RecordsSnapshotReader(org.apache.kafka.snapshot.RecordsSnapshotReader) MetadataRecordSerde(org.apache.kafka.metadata.MetadataRecordSerde) BROKER0(org.apache.kafka.controller.ConfigurationControlManagerTest.BROKER0) TimeoutException(org.apache.kafka.common.errors.TimeoutException) CreatableReplicaAssignment(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment) Iterator(java.util.Iterator) BrokerRegistrationReply(org.apache.kafka.metadata.BrokerRegistrationReply) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) BrokerHeartbeatRequestData(org.apache.kafka.common.message.BrokerHeartbeatRequestData) BROKER(org.apache.kafka.common.config.ConfigResource.Type.BROKER) Batch(org.apache.kafka.raft.Batch) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) ExecutionException(java.util.concurrent.ExecutionException) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) RecordTestUtils(org.apache.kafka.metadata.RecordTestUtils) ReassignableTopic(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic) PartitionRecord(org.apache.kafka.common.metadata.PartitionRecord) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) HOURS(java.util.concurrent.TimeUnit.HOURS) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) ListenerCollection(org.apache.kafka.common.message.BrokerRegistrationRequestData.ListenerCollection) BrokerEndpointCollection(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpointCollection) ProducerIdsRecord(org.apache.kafka.common.metadata.ProducerIdsRecord) PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) Uuid(org.apache.kafka.common.Uuid) CreatableReplicaAssignment(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) CreatableReplicaAssignmentCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignmentCollection) Test(org.junit.jupiter.api.Test)

Example 5 with LocalLogManagerTestEnv

use of org.apache.kafka.metalog.LocalLogManagerTestEnv in project kafka by apache.

the class QuorumControllerTest method testDelayedConfigurationOperations.

/**
 * Test that an incrementalAlterConfigs operation doesn't complete until the records
 * can be written to the metadata log.
 */
@Test
public void testDelayedConfigurationOperations() throws Throwable {
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(1, Optional.empty());
        QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, b -> b.setConfigDefs(CONFIGS))) {
        controlEnv.activeController().registerBroker(new BrokerRegistrationRequestData().setBrokerId(0).setClusterId(logEnv.clusterId())).get();
        testDelayedConfigurationOperations(logEnv, controlEnv.activeController());
    }
}
Also used : LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) BrokerRegistrationRequestData(org.apache.kafka.common.message.BrokerRegistrationRequestData) Test(org.junit.jupiter.api.Test)

Aggregations

LocalLogManagerTestEnv (org.apache.kafka.metalog.LocalLogManagerTestEnv)12 Test (org.junit.jupiter.api.Test)12 CreateTopicsRequestData (org.apache.kafka.common.message.CreateTopicsRequestData)9 BrokerRegistrationRequestData (org.apache.kafka.common.message.BrokerRegistrationRequestData)8 CreatableTopic (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic)8 CreatableTopicCollection (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection)8 HashMap (java.util.HashMap)7 Uuid (org.apache.kafka.common.Uuid)6 Listener (org.apache.kafka.common.message.BrokerRegistrationRequestData.Listener)6 ListenerCollection (org.apache.kafka.common.message.BrokerRegistrationRequestData.ListenerCollection)6 CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)6 BrokerRegistrationReply (org.apache.kafka.metadata.BrokerRegistrationReply)6 BrokerHeartbeatRequestData (org.apache.kafka.common.message.BrokerHeartbeatRequestData)5 BrokerHeartbeatReply (org.apache.kafka.metadata.BrokerHeartbeatReply)5 BrokerEndpoint (org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint)4 List (java.util.List)3 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AlterPartitionReassignmentsRequestData (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData)3 AlterPartitionReassignmentsResponseData (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData)3