Search in sources :

Example 76 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class KafkaAdminClientTest method testDescribeLogDirsDeprecated.

@SuppressWarnings("deprecation")
@Test
public void testDescribeLogDirsDeprecated() throws ExecutionException, InterruptedException {
    Set<Integer> brokers = singleton(0);
    TopicPartition tp = new TopicPartition("topic", 12);
    String logDir = "/var/data/kafka";
    Errors error = Errors.NONE;
    int offsetLag = 24;
    long partitionSize = 1234567890;
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponseFrom(prepareDescribeLogDirsResponse(error, logDir, tp, partitionSize, offsetLag), env.cluster().nodeById(0));
        DescribeLogDirsResult result = env.adminClient().describeLogDirs(brokers);
        Map<Integer, KafkaFuture<Map<String, DescribeLogDirsResponse.LogDirInfo>>> deprecatedValues = result.values();
        assertEquals(brokers, deprecatedValues.keySet());
        assertNotNull(deprecatedValues.get(0));
        assertDescriptionContains(deprecatedValues.get(0).get(), logDir, tp, error, offsetLag, partitionSize);
        Map<Integer, Map<String, DescribeLogDirsResponse.LogDirInfo>> deprecatedAll = result.all().get();
        assertEquals(brokers, deprecatedAll.keySet());
        assertDescriptionContains(deprecatedAll.get(0), logDir, tp, error, offsetLag, partitionSize);
    }
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) DescribeLogDirsResponse(org.apache.kafka.common.requests.DescribeLogDirsResponse) Errors(org.apache.kafka.common.protocol.Errors) TopicPartition(org.apache.kafka.common.TopicPartition) Map(java.util.Map) HashMap(java.util.HashMap) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 77 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class KafkaAdminClientTest method testAlterConsumerGroupOffsets.

@Test
public void testAlterConsumerGroupOffsets() throws Exception {
    // Happy path
    final TopicPartition tp1 = new TopicPartition("foo", 0);
    final TopicPartition tp2 = new TopicPartition("bar", 0);
    final TopicPartition tp3 = new TopicPartition("foobar", 0);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(1, 0))) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
        Map<TopicPartition, Errors> responseData = new HashMap<>();
        responseData.put(tp1, Errors.NONE);
        responseData.put(tp2, Errors.NONE);
        env.kafkaClient().prepareResponse(new OffsetCommitResponse(0, responseData));
        Map<TopicPartition, OffsetAndMetadata> offsets = new HashMap<>();
        offsets.put(tp1, new OffsetAndMetadata(123L));
        offsets.put(tp2, new OffsetAndMetadata(456L));
        final AlterConsumerGroupOffsetsResult result = env.adminClient().alterConsumerGroupOffsets(GROUP_ID, offsets);
        assertNull(result.all().get());
        assertNull(result.partitionResult(tp1).get());
        assertNull(result.partitionResult(tp2).get());
        TestUtils.assertFutureError(result.partitionResult(tp3), IllegalArgumentException.class);
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetCommitResponse(org.apache.kafka.common.requests.OffsetCommitResponse) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 78 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class SaslClientAuthenticator method receiveToken.

private byte[] receiveToken() throws IOException {
    if (saslAuthenticateVersion == DISABLE_KAFKA_SASL_AUTHENTICATE_HEADER) {
        return receiveResponseOrToken();
    } else {
        SaslAuthenticateResponse response = (SaslAuthenticateResponse) receiveKafkaResponse();
        if (response != null) {
            Errors error = response.error();
            if (error != Errors.NONE) {
                setSaslState(SaslState.FAILED);
                String errMsg = response.errorMessage();
                throw errMsg == null ? error.exception() : error.exception(errMsg);
            }
            long sessionLifetimeMs = response.sessionLifetimeMs();
            if (sessionLifetimeMs > 0L)
                reauthInfo.positiveSessionLifetimeMs = sessionLifetimeMs;
            return Utils.copyArray(response.saslAuthBytes());
        } else
            return null;
    }
}
Also used : SaslAuthenticateResponse(org.apache.kafka.common.requests.SaslAuthenticateResponse) Errors(org.apache.kafka.common.protocol.Errors)

Example 79 with Errors

use of org.apache.kafka.common.protocol.Errors in project kafka by apache.

the class KafkaConsumerTest method testLeaveGroupTimeout.

@Test
public void testLeaveGroupTimeout() throws Exception {
    Map<TopicPartition, Errors> response = new HashMap<>();
    response.put(tp0, Errors.NONE);
    OffsetCommitResponse commitResponse = offsetCommitResponse(response);
    consumerCloseTest(5000, singletonList(commitResponse), 5000, false);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetCommitResponse(org.apache.kafka.common.requests.OffsetCommitResponse) Test(org.junit.jupiter.api.Test)

Example 80 with Errors

use of org.apache.kafka.common.protocol.Errors 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)

Aggregations

Errors (org.apache.kafka.common.protocol.Errors)167 HashMap (java.util.HashMap)115 TopicPartition (org.apache.kafka.common.TopicPartition)87 Map (java.util.Map)61 ArrayList (java.util.ArrayList)46 LinkedHashMap (java.util.LinkedHashMap)31 Test (org.junit.jupiter.api.Test)31 List (java.util.List)19 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)19 HashSet (java.util.HashSet)18 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)18 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)17 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)17 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)17 KafkaException (org.apache.kafka.common.KafkaException)16 Node (org.apache.kafka.common.Node)16 Cluster (org.apache.kafka.common.Cluster)15 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)14 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)14 Collections (java.util.Collections)13