Search in sources :

Example 66 with ApiMessageAndVersion

use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.

the class ReplicationControlManager method deleteTopics.

ControllerResult<Map<Uuid, ApiError>> deleteTopics(Collection<Uuid> ids) {
    Map<Uuid, ApiError> results = new HashMap<>(ids.size());
    List<ApiMessageAndVersion> records = new ArrayList<>(ids.size());
    for (Uuid id : ids) {
        try {
            deleteTopic(id, records);
            results.put(id, ApiError.NONE);
        } catch (ApiException e) {
            results.put(id, ApiError.fromThrowable(e));
        } catch (Exception e) {
            log.error("Unexpected deleteTopics error for {}", id, e);
            results.put(id, ApiError.fromThrowable(e));
        }
    }
    return ControllerResult.atomicOf(records, results);
}
Also used : Uuid(org.apache.kafka.common.Uuid) TimelineHashMap(org.apache.kafka.timeline.TimelineHashMap) HashMap(java.util.HashMap) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList) ApiError(org.apache.kafka.common.requests.ApiError) InvalidReplicationFactorException(org.apache.kafka.common.errors.InvalidReplicationFactorException) NoReassignmentInProgressException(org.apache.kafka.common.errors.NoReassignmentInProgressException) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) InvalidPartitionsException(org.apache.kafka.common.errors.InvalidPartitionsException) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) UnknownTopicIdException(org.apache.kafka.common.errors.UnknownTopicIdException) BrokerIdNotRegisteredException(org.apache.kafka.common.errors.BrokerIdNotRegisteredException) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) PolicyViolationException(org.apache.kafka.common.errors.PolicyViolationException) NoSuchElementException(java.util.NoSuchElementException) InvalidReplicaAssignmentException(org.apache.kafka.common.errors.InvalidReplicaAssignmentException) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) ApiException(org.apache.kafka.common.errors.ApiException) ApiException(org.apache.kafka.common.errors.ApiException)

Example 67 with ApiMessageAndVersion

use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.

the class ReplicationControlManager method alterPartitionReassignments.

ControllerResult<AlterPartitionReassignmentsResponseData> alterPartitionReassignments(AlterPartitionReassignmentsRequestData request) {
    List<ApiMessageAndVersion> records = new ArrayList<>();
    AlterPartitionReassignmentsResponseData result = new AlterPartitionReassignmentsResponseData().setErrorMessage(null);
    int successfulAlterations = 0, totalAlterations = 0;
    for (ReassignableTopic topic : request.topics()) {
        ReassignableTopicResponse topicResponse = new ReassignableTopicResponse().setName(topic.name());
        for (ReassignablePartition partition : topic.partitions()) {
            ApiError error = ApiError.NONE;
            try {
                alterPartitionReassignment(topic.name(), partition, records);
                successfulAlterations++;
            } catch (Throwable e) {
                log.info("Unable to alter partition reassignment for " + topic.name() + ":" + partition.partitionIndex() + " because " + "of an " + e.getClass().getSimpleName() + " error: " + e.getMessage());
                error = ApiError.fromThrowable(e);
            }
            totalAlterations++;
            topicResponse.partitions().add(new ReassignablePartitionResponse().setPartitionIndex(partition.partitionIndex()).setErrorCode(error.error().code()).setErrorMessage(error.message()));
        }
        result.responses().add(topicResponse);
    }
    log.info("Successfully altered {} out of {} partition reassignment(s).", successfulAlterations, totalAlterations);
    return ControllerResult.atomicOf(records, result);
}
Also used : ReassignableTopicResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse) ReassignablePartition(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignablePartition) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) ReassignablePartitionResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ReassignableTopic(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic) ArrayList(java.util.ArrayList) ApiError(org.apache.kafka.common.requests.ApiError)

Example 68 with ApiMessageAndVersion

use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.

the class ReplicationControlManager method electLeader.

ApiError electLeader(String topic, int partitionId, ElectionType electionType, List<ApiMessageAndVersion> records) {
    Uuid topicId = topicsByName.get(topic);
    if (topicId == null) {
        return new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as " + topic);
    }
    TopicControlInfo topicInfo = topics.get(topicId);
    if (topicInfo == null) {
        return new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic id as " + topicId);
    }
    PartitionRegistration partition = topicInfo.parts.get(partitionId);
    if (partition == null) {
        return new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such partition as " + topic + "-" + partitionId);
    }
    if ((electionType == ElectionType.PREFERRED && partition.hasPreferredLeader()) || (electionType == ElectionType.UNCLEAN && partition.hasLeader())) {
        return new ApiError(Errors.ELECTION_NOT_NEEDED);
    }
    PartitionChangeBuilder builder = new PartitionChangeBuilder(partition, topicId, partitionId, r -> clusterControl.unfenced(r), () -> electionType == ElectionType.UNCLEAN);
    builder.setAlwaysElectPreferredIfPossible(electionType == ElectionType.PREFERRED);
    Optional<ApiMessageAndVersion> record = builder.build();
    if (!record.isPresent()) {
        if (electionType == ElectionType.PREFERRED) {
            return new ApiError(Errors.PREFERRED_LEADER_NOT_AVAILABLE);
        } else {
            return new ApiError(Errors.ELIGIBLE_LEADERS_NOT_AVAILABLE);
        }
    }
    records.add(record.get());
    return ApiError.NONE;
}
Also used : PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) Uuid(org.apache.kafka.common.Uuid) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ApiError(org.apache.kafka.common.requests.ApiError)

Example 69 with ApiMessageAndVersion

use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.

the class ReplicationControlManager method createTopic.

private ApiError createTopic(CreatableTopic topic, List<ApiMessageAndVersion> records, Map<String, CreatableTopicResult> successes) {
    Map<Integer, PartitionRegistration> newParts = new HashMap<>();
    if (!topic.assignments().isEmpty()) {
        if (topic.replicationFactor() != -1) {
            return new ApiError(INVALID_REQUEST, "A manual partition assignment was specified, but replication " + "factor was not set to -1.");
        }
        if (topic.numPartitions() != -1) {
            return new ApiError(INVALID_REQUEST, "A manual partition assignment was specified, but numPartitions " + "was not set to -1.");
        }
        OptionalInt replicationFactor = OptionalInt.empty();
        for (CreatableReplicaAssignment assignment : topic.assignments()) {
            if (newParts.containsKey(assignment.partitionIndex())) {
                return new ApiError(Errors.INVALID_REPLICA_ASSIGNMENT, "Found multiple manual partition assignments for partition " + assignment.partitionIndex());
            }
            validateManualPartitionAssignment(assignment.brokerIds(), replicationFactor);
            replicationFactor = OptionalInt.of(assignment.brokerIds().size());
            List<Integer> isr = assignment.brokerIds().stream().filter(clusterControl::unfenced).collect(Collectors.toList());
            if (isr.isEmpty()) {
                return new ApiError(Errors.INVALID_REPLICA_ASSIGNMENT, "All brokers specified in the manual partition assignment for " + "partition " + assignment.partitionIndex() + " are fenced.");
            }
            newParts.put(assignment.partitionIndex(), new PartitionRegistration(Replicas.toArray(assignment.brokerIds()), Replicas.toArray(isr), Replicas.NONE, Replicas.NONE, isr.get(0), 0, 0));
        }
        ApiError error = maybeCheckCreateTopicPolicy(() -> {
            Map<Integer, List<Integer>> assignments = new HashMap<>();
            newParts.entrySet().forEach(e -> assignments.put(e.getKey(), Replicas.toList(e.getValue().replicas)));
            Map<String, String> configs = new HashMap<>();
            topic.configs().forEach(config -> configs.put(config.name(), config.value()));
            return new CreateTopicPolicy.RequestMetadata(topic.name(), null, null, assignments, configs);
        });
        if (error.isFailure())
            return error;
    } else if (topic.replicationFactor() < -1 || topic.replicationFactor() == 0) {
        return new ApiError(Errors.INVALID_REPLICATION_FACTOR, "Replication factor must be larger than 0, or -1 to use the default value.");
    } else if (topic.numPartitions() < -1 || topic.numPartitions() == 0) {
        return new ApiError(Errors.INVALID_PARTITIONS, "Number of partitions was set to an invalid non-positive value.");
    } else {
        int numPartitions = topic.numPartitions() == -1 ? defaultNumPartitions : topic.numPartitions();
        short replicationFactor = topic.replicationFactor() == -1 ? defaultReplicationFactor : topic.replicationFactor();
        try {
            List<List<Integer>> replicas = clusterControl.placeReplicas(0, numPartitions, replicationFactor);
            for (int partitionId = 0; partitionId < replicas.size(); partitionId++) {
                int[] r = Replicas.toArray(replicas.get(partitionId));
                newParts.put(partitionId, new PartitionRegistration(r, r, Replicas.NONE, Replicas.NONE, r[0], 0, 0));
            }
        } catch (InvalidReplicationFactorException e) {
            return new ApiError(Errors.INVALID_REPLICATION_FACTOR, "Unable to replicate the partition " + replicationFactor + " time(s): " + e.getMessage());
        }
        ApiError error = maybeCheckCreateTopicPolicy(() -> {
            Map<String, String> configs = new HashMap<>();
            topic.configs().forEach(config -> configs.put(config.name(), config.value()));
            return new CreateTopicPolicy.RequestMetadata(topic.name(), numPartitions, replicationFactor, null, configs);
        });
        if (error.isFailure())
            return error;
    }
    Uuid topicId = Uuid.randomUuid();
    successes.put(topic.name(), new CreatableTopicResult().setName(topic.name()).setTopicId(topicId).setErrorCode((short) 0).setErrorMessage(null).setNumPartitions(newParts.size()).setReplicationFactor((short) newParts.get(0).replicas.length));
    records.add(new ApiMessageAndVersion(new TopicRecord().setName(topic.name()).setTopicId(topicId), TOPIC_RECORD.highestSupportedVersion()));
    for (Entry<Integer, PartitionRegistration> partEntry : newParts.entrySet()) {
        int partitionIndex = partEntry.getKey();
        PartitionRegistration info = partEntry.getValue();
        records.add(info.toRecord(topicId, partitionIndex));
    }
    return ApiError.NONE;
}
Also used : PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) RemoveTopicRecord(org.apache.kafka.common.metadata.RemoveTopicRecord) TopicRecord(org.apache.kafka.common.metadata.TopicRecord) TimelineHashMap(org.apache.kafka.timeline.TimelineHashMap) HashMap(java.util.HashMap) OptionalInt(java.util.OptionalInt) TimelineInteger(org.apache.kafka.timeline.TimelineInteger) Uuid(org.apache.kafka.common.Uuid) CreatableReplicaAssignment(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment) InvalidReplicationFactorException(org.apache.kafka.common.errors.InvalidReplicationFactorException) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) ArrayList(java.util.ArrayList) List(java.util.List) ApiError(org.apache.kafka.common.requests.ApiError)

Example 70 with ApiMessageAndVersion

use of org.apache.kafka.server.common.ApiMessageAndVersion in project kafka by apache.

the class ReplicationControlManager method unregisterBroker.

public ControllerResult<Void> unregisterBroker(int brokerId) {
    BrokerRegistration registration = clusterControl.brokerRegistrations().get(brokerId);
    if (registration == null) {
        throw new BrokerIdNotRegisteredException("Broker ID " + brokerId + " is not currently registered");
    }
    List<ApiMessageAndVersion> records = new ArrayList<>();
    handleBrokerUnregistered(brokerId, registration.epoch(), records);
    return ControllerResult.of(records, null);
}
Also used : BrokerIdNotRegisteredException(org.apache.kafka.common.errors.BrokerIdNotRegisteredException) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList) BrokerRegistration(org.apache.kafka.metadata.BrokerRegistration)

Aggregations

ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)84 ArrayList (java.util.ArrayList)38 Test (org.junit.jupiter.api.Test)35 Uuid (org.apache.kafka.common.Uuid)23 ApiError (org.apache.kafka.common.requests.ApiError)20 LogContext (org.apache.kafka.common.utils.LogContext)17 HashMap (java.util.HashMap)16 SnapshotRegistry (org.apache.kafka.timeline.SnapshotRegistry)15 List (java.util.List)12 Map (java.util.Map)12 PartitionChangeRecord (org.apache.kafka.common.metadata.PartitionChangeRecord)12 PartitionRegistration (org.apache.kafka.metadata.PartitionRegistration)11 TopicRecord (org.apache.kafka.common.metadata.TopicRecord)8 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)7 AlterIsrRequestData (org.apache.kafka.common.message.AlterIsrRequestData)7 Collections (java.util.Collections)6 Iterator (java.util.Iterator)6 Entry (java.util.Map.Entry)6 NoSuchElementException (java.util.NoSuchElementException)6 Optional (java.util.Optional)6