Search in sources :

Example 1 with TopicIdPartition

use of org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition in project kafka by apache.

the class ReplicationControlManager method alterPartitionReassignment.

void alterPartitionReassignment(String topicName, ReassignablePartition target, List<ApiMessageAndVersion> records) {
    Uuid topicId = topicsByName.get(topicName);
    if (topicId == null) {
        throw new UnknownTopicOrPartitionException("Unable to find a topic " + "named " + topicName + ".");
    }
    TopicControlInfo topicInfo = topics.get(topicId);
    if (topicInfo == null) {
        throw new UnknownTopicOrPartitionException("Unable to find a topic " + "with ID " + topicId + ".");
    }
    TopicIdPartition tp = new TopicIdPartition(topicId, target.partitionIndex());
    PartitionRegistration part = topicInfo.parts.get(target.partitionIndex());
    if (part == null) {
        throw new UnknownTopicOrPartitionException("Unable to find partition " + topicName + ":" + target.partitionIndex() + ".");
    }
    Optional<ApiMessageAndVersion> record;
    if (target.replicas() == null) {
        record = cancelPartitionReassignment(topicName, tp, part);
    } else {
        record = changePartitionReassignment(tp, part, target);
    }
    record.ifPresent(records::add);
}
Also used : PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) Uuid(org.apache.kafka.common.Uuid) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition)

Example 2 with TopicIdPartition

use of org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition in project kafka by apache.

the class BrokersToIsrsTest method testIterator.

@Test
public void testIterator() {
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    BrokersToIsrs brokersToIsrs = new BrokersToIsrs(snapshotRegistry);
    assertEquals(toSet(), toSet(brokersToIsrs.iterator(1, false)));
    brokersToIsrs.update(UUIDS[0], 0, null, new int[] { 1, 2, 3 }, -1, 1);
    brokersToIsrs.update(UUIDS[1], 1, null, new int[] { 2, 3, 4 }, -1, 4);
    assertEquals(toSet(new TopicIdPartition(UUIDS[0], 0)), toSet(brokersToIsrs.iterator(1, false)));
    assertEquals(toSet(new TopicIdPartition(UUIDS[0], 0), new TopicIdPartition(UUIDS[1], 1)), toSet(brokersToIsrs.iterator(2, false)));
    assertEquals(toSet(new TopicIdPartition(UUIDS[1], 1)), toSet(brokersToIsrs.iterator(4, false)));
    assertEquals(toSet(), toSet(brokersToIsrs.iterator(5, false)));
    brokersToIsrs.update(UUIDS[1], 2, null, new int[] { 3, 2, 1 }, -1, 3);
    assertEquals(toSet(new TopicIdPartition(UUIDS[0], 0), new TopicIdPartition(UUIDS[1], 1), new TopicIdPartition(UUIDS[1], 2)), toSet(brokersToIsrs.iterator(2, false)));
}
Also used : SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) LogContext(org.apache.kafka.common.utils.LogContext) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) Test(org.junit.jupiter.api.Test)

Example 3 with TopicIdPartition

use of org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition in project kafka by apache.

the class BrokersToIsrsTest method testNoLeader.

@Test
public void testNoLeader() {
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    BrokersToIsrs brokersToIsrs = new BrokersToIsrs(snapshotRegistry);
    brokersToIsrs.update(UUIDS[0], 2, null, new int[] { 1, 2, 3 }, -1, 3);
    assertEquals(toSet(new TopicIdPartition(UUIDS[0], 2)), toSet(brokersToIsrs.iterator(3, true)));
    assertEquals(toSet(), toSet(brokersToIsrs.iterator(2, true)));
    assertEquals(toSet(), toSet(brokersToIsrs.partitionsWithNoLeader()));
    brokersToIsrs.update(UUIDS[0], 2, new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, 3, -1);
    assertEquals(toSet(new TopicIdPartition(UUIDS[0], 2)), toSet(brokersToIsrs.partitionsWithNoLeader()));
}
Also used : SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) LogContext(org.apache.kafka.common.utils.LogContext) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) Test(org.junit.jupiter.api.Test)

Example 4 with TopicIdPartition

use of org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition 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 5 with TopicIdPartition

use of org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition in project kafka by apache.

the class ReplicationControlManagerTest method testRemoveLeaderships.

@Test
public void testRemoveLeaderships() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replicationControl = ctx.replicationControl;
    ctx.registerBrokers(0, 1, 2, 3);
    ctx.unfenceBrokers(0, 1, 2, 3);
    CreatableTopicResult result = ctx.createTestTopic("foo", new int[][] { new int[] { 0, 1, 2 }, new int[] { 1, 2, 3 }, new int[] { 2, 3, 0 }, new int[] { 0, 2, 1 } });
    Set<TopicIdPartition> expectedPartitions = new HashSet<>();
    expectedPartitions.add(new TopicIdPartition(result.topicId(), 0));
    expectedPartitions.add(new TopicIdPartition(result.topicId(), 3));
    assertEquals(expectedPartitions, RecordTestUtils.iteratorToSet(replicationControl.brokersToIsrs().iterator(0, true)));
    List<ApiMessageAndVersion> records = new ArrayList<>();
    replicationControl.handleBrokerFenced(0, records);
    ctx.replay(records);
    assertEquals(Collections.emptySet(), RecordTestUtils.iteratorToSet(replicationControl.brokersToIsrs().iterator(0, true)));
}
Also used : ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) HashSet(java.util.HashSet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

TopicIdPartition (org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition)11 Test (org.junit.jupiter.api.Test)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 TopicPartition (org.apache.kafka.common.TopicPartition)4 Uuid (org.apache.kafka.common.Uuid)4 CreatableTopicResult (org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult)4 LogContext (org.apache.kafka.common.utils.LogContext)4 SnapshotRegistry (org.apache.kafka.timeline.SnapshotRegistry)4 AlterIsrResponseData (org.apache.kafka.common.message.AlterIsrResponseData)3 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 PartitionData (org.apache.kafka.common.message.AlterIsrRequestData.PartitionData)2 ElectLeadersRequestData (org.apache.kafka.common.message.ElectLeadersRequestData)2 ElectLeadersResponseData (org.apache.kafka.common.message.ElectLeadersResponseData)2 ApiError (org.apache.kafka.common.requests.ApiError)2 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1