Search in sources :

Example 1 with PartitionRegistration

use of org.apache.kafka.metadata.PartitionRegistration 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 PartitionRegistration

use of org.apache.kafka.metadata.PartitionRegistration in project kafka by apache.

the class ReplicationControlManager method alterIsr.

ControllerResult<AlterIsrResponseData> alterIsr(AlterIsrRequestData request) {
    clusterControl.checkBrokerEpoch(request.brokerId(), request.brokerEpoch());
    AlterIsrResponseData response = new AlterIsrResponseData();
    List<ApiMessageAndVersion> records = new ArrayList<>();
    for (AlterIsrRequestData.TopicData topicData : request.topics()) {
        AlterIsrResponseData.TopicData responseTopicData = new AlterIsrResponseData.TopicData().setName(topicData.name());
        response.topics().add(responseTopicData);
        Uuid topicId = topicsByName.get(topicData.name());
        if (topicId == null || !topics.containsKey(topicId)) {
            for (AlterIsrRequestData.PartitionData partitionData : topicData.partitions()) {
                responseTopicData.partitions().add(new AlterIsrResponseData.PartitionData().setPartitionIndex(partitionData.partitionIndex()).setErrorCode(UNKNOWN_TOPIC_OR_PARTITION.code()));
            }
            log.info("Rejecting alterIsr request for unknown topic ID {}.", topicId);
            continue;
        }
        TopicControlInfo topic = topics.get(topicId);
        for (AlterIsrRequestData.PartitionData partitionData : topicData.partitions()) {
            int partitionId = partitionData.partitionIndex();
            PartitionRegistration partition = topic.parts.get(partitionId);
            if (partition == null) {
                responseTopicData.partitions().add(new AlterIsrResponseData.PartitionData().setPartitionIndex(partitionId).setErrorCode(UNKNOWN_TOPIC_OR_PARTITION.code()));
                log.info("Rejecting alterIsr request for unknown partition {}-{}.", topic.name, partitionId);
                continue;
            }
            if (partitionData.leaderEpoch() != partition.leaderEpoch) {
                responseTopicData.partitions().add(new AlterIsrResponseData.PartitionData().setPartitionIndex(partitionId).setErrorCode(FENCED_LEADER_EPOCH.code()));
                log.debug("Rejecting alterIsr request from node {} for {}-{} because " + "the current leader epoch is {}, not {}.", request.brokerId(), topic.name, partitionId, partition.leaderEpoch, partitionData.leaderEpoch());
                continue;
            }
            if (request.brokerId() != partition.leader) {
                responseTopicData.partitions().add(new AlterIsrResponseData.PartitionData().setPartitionIndex(partitionId).setErrorCode(INVALID_REQUEST.code()));
                log.info("Rejecting alterIsr request from node {} for {}-{} because " + "the current leader is {}.", request.brokerId(), topic.name, partitionId, partition.leader);
                continue;
            }
            if (partitionData.currentIsrVersion() != partition.partitionEpoch) {
                responseTopicData.partitions().add(new AlterIsrResponseData.PartitionData().setPartitionIndex(partitionId).setErrorCode(INVALID_UPDATE_VERSION.code()));
                log.info("Rejecting alterIsr request from node {} for {}-{} because " + "the current partition epoch is {}, not {}.", request.brokerId(), topic.name, partitionId, partition.partitionEpoch, partitionData.currentIsrVersion());
                continue;
            }
            int[] newIsr = Replicas.toArray(partitionData.newIsr());
            if (!Replicas.validateIsr(partition.replicas, newIsr)) {
                responseTopicData.partitions().add(new AlterIsrResponseData.PartitionData().setPartitionIndex(partitionId).setErrorCode(INVALID_REQUEST.code()));
                log.error("Rejecting alterIsr request from node {} for {}-{} because " + "it specified an invalid ISR {}.", request.brokerId(), topic.name, partitionId, partitionData.newIsr());
                continue;
            }
            if (!Replicas.contains(newIsr, partition.leader)) {
                // An alterIsr request can't ask for the current leader to be removed.
                responseTopicData.partitions().add(new AlterIsrResponseData.PartitionData().setPartitionIndex(partitionId).setErrorCode(INVALID_REQUEST.code()));
                log.error("Rejecting alterIsr request from node {} for {}-{} because " + "it specified an invalid ISR {} that doesn't include itself.", request.brokerId(), topic.name, partitionId, partitionData.newIsr());
                continue;
            }
            // At this point, we have decided to perform the ISR change. We use
            // PartitionChangeBuilder to find out what its effect will be.
            PartitionChangeBuilder builder = new PartitionChangeBuilder(partition, topic.id, partitionId, r -> clusterControl.unfenced(r), () -> configurationControl.uncleanLeaderElectionEnabledForTopic(topicData.name()));
            builder.setTargetIsr(partitionData.newIsr());
            Optional<ApiMessageAndVersion> record = builder.build();
            Errors result = Errors.NONE;
            if (record.isPresent()) {
                records.add(record.get());
                PartitionChangeRecord change = (PartitionChangeRecord) record.get().message();
                partition = partition.merge(change);
                if (log.isDebugEnabled()) {
                    log.debug("Node {} has altered ISR for {}-{} to {}.", request.brokerId(), topic.name, partitionId, change.isr());
                }
                if (change.leader() != request.brokerId() && change.leader() != NO_LEADER_CHANGE) {
                    // Normally, an alterIsr request, which is made by the partition
                    // leader itself, is not allowed to modify the partition leader.
                    // However, if there is an ongoing partition reassignment and the
                    // ISR change completes it, then the leader may change as part of
                    // the changes made during reassignment cleanup.
                    // 
                    // In this case, we report back FENCED_LEADER_EPOCH to the leader
                    // which made the alterIsr request. This lets it know that it must
                    // fetch new metadata before trying again. This return code is
                    // unusual because we both return an error and generate a new
                    // metadata record. We usually only do one or the other.
                    log.info("AlterIsr request from node {} for {}-{} completed " + "the ongoing partition reassignment and triggered a " + "leadership change. Reutrning FENCED_LEADER_EPOCH.", request.brokerId(), topic.name, partitionId);
                    responseTopicData.partitions().add(new AlterIsrResponseData.PartitionData().setPartitionIndex(partitionId).setErrorCode(FENCED_LEADER_EPOCH.code()));
                    continue;
                } else if (change.removingReplicas() != null || change.addingReplicas() != null) {
                    log.info("AlterIsr request from node {} for {}-{} completed " + "the ongoing partition reassignment.", request.brokerId(), topic.name, partitionId);
                }
            }
            responseTopicData.partitions().add(new AlterIsrResponseData.PartitionData().setPartitionIndex(partitionId).setErrorCode(result.code()).setLeaderId(partition.leader).setLeaderEpoch(partition.leaderEpoch).setCurrentIsrVersion(partition.partitionEpoch).setIsr(Replicas.toList(partition.isr)));
        }
    }
    return ControllerResult.of(records, response);
}
Also used : PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) ArrayList(java.util.ArrayList) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) Errors(org.apache.kafka.common.protocol.Errors) Uuid(org.apache.kafka.common.Uuid) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) AlterIsrResponseData(org.apache.kafka.common.message.AlterIsrResponseData)

Example 3 with PartitionRegistration

use of org.apache.kafka.metadata.PartitionRegistration in project kafka by apache.

the class TopicDelta method apply.

public TopicImage apply() {
    Map<Integer, PartitionRegistration> newPartitions = new HashMap<>();
    for (Entry<Integer, PartitionRegistration> entry : image.partitions().entrySet()) {
        int partitionId = entry.getKey();
        PartitionRegistration changedPartition = partitionChanges.get(partitionId);
        if (changedPartition == null) {
            newPartitions.put(partitionId, entry.getValue());
        } else {
            newPartitions.put(partitionId, changedPartition);
        }
    }
    for (Entry<Integer, PartitionRegistration> entry : partitionChanges.entrySet()) {
        if (!newPartitions.containsKey(entry.getKey())) {
            newPartitions.put(entry.getKey(), entry.getValue());
        }
    }
    return new TopicImage(image.name(), image.id(), newPartitions);
}
Also used : PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) HashMap(java.util.HashMap)

Example 4 with PartitionRegistration

use of org.apache.kafka.metadata.PartitionRegistration in project kafka by apache.

the class TopicImage method write.

public void write(Consumer<List<ApiMessageAndVersion>> out) {
    List<ApiMessageAndVersion> batch = new ArrayList<>();
    batch.add(new ApiMessageAndVersion(new TopicRecord().setName(name).setTopicId(id), TOPIC_RECORD.highestSupportedVersion()));
    for (Entry<Integer, PartitionRegistration> entry : partitions.entrySet()) {
        int partitionId = entry.getKey();
        PartitionRegistration partition = entry.getValue();
        batch.add(partition.toRecord(id, partitionId));
    }
    out.accept(batch);
}
Also used : PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) TopicRecord(org.apache.kafka.common.metadata.TopicRecord) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList)

Example 5 with PartitionRegistration

use of org.apache.kafka.metadata.PartitionRegistration in project kafka by apache.

the class ReplicationControlManagerTest method assertLeaderAndIsr.

private void assertLeaderAndIsr(ReplicationControlManager replication, TopicIdPartition topicIdPartition, int leaderId, int[] isr) {
    PartitionRegistration registration = replication.getPartition(topicIdPartition.topicId(), topicIdPartition.partitionId());
    assertArrayEquals(isr, registration.isr);
    assertEquals(leaderId, registration.leader);
}
Also used : PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration)

Aggregations

PartitionRegistration (org.apache.kafka.metadata.PartitionRegistration)26 Uuid (org.apache.kafka.common.Uuid)11 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)10 Test (org.junit.jupiter.api.Test)10 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 TopicRecord (org.apache.kafka.common.metadata.TopicRecord)5 List (java.util.List)4 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)4 AlterIsrRequestData (org.apache.kafka.common.message.AlterIsrRequestData)4 AlterIsrResponseData (org.apache.kafka.common.message.AlterIsrResponseData)3 AlterPartitionReassignmentsRequestData (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData)3 ReassignableTopic (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic)3 AlterPartitionReassignmentsResponseData (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData)3 CreatableTopicResult (org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult)3 PartitionRecord (org.apache.kafka.common.metadata.PartitionRecord)3 ApiError (org.apache.kafka.common.requests.ApiError)3 TimelineInteger (org.apache.kafka.timeline.TimelineInteger)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 OptionalInt (java.util.OptionalInt)2