Search in sources :

Example 1 with ReplicaElectionResult

use of org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult in project kafka by apache.

the class KafkaAdminClientTest method testElectLeaders.

@Test
public void testElectLeaders() throws Exception {
    TopicPartition topic1 = new TopicPartition("topic", 0);
    TopicPartition topic2 = new TopicPartition("topic", 2);
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        for (ElectionType electionType : ElectionType.values()) {
            env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
            // Test a call where one partition has an error.
            ApiError value = ApiError.fromThrowable(new ClusterAuthorizationException(null));
            List<ReplicaElectionResult> electionResults = new ArrayList<>();
            ReplicaElectionResult electionResult = new ReplicaElectionResult();
            electionResult.setTopic(topic1.topic());
            // Add partition 1 result
            PartitionResult partition1Result = new PartitionResult();
            partition1Result.setPartitionId(topic1.partition());
            partition1Result.setErrorCode(value.error().code());
            partition1Result.setErrorMessage(value.message());
            electionResult.partitionResult().add(partition1Result);
            // Add partition 2 result
            PartitionResult partition2Result = new PartitionResult();
            partition2Result.setPartitionId(topic2.partition());
            partition2Result.setErrorCode(value.error().code());
            partition2Result.setErrorMessage(value.message());
            electionResult.partitionResult().add(partition2Result);
            electionResults.add(electionResult);
            env.kafkaClient().prepareResponse(new ElectLeadersResponse(0, Errors.NONE.code(), electionResults, ApiKeys.ELECT_LEADERS.latestVersion()));
            ElectLeadersResult results = env.adminClient().electLeaders(electionType, new HashSet<>(asList(topic1, topic2)));
            assertEquals(results.partitions().get().get(topic2).get().getClass(), ClusterAuthorizationException.class);
            // Test a call where there are no errors. By mutating the internal of election results
            partition1Result.setErrorCode(ApiError.NONE.error().code());
            partition1Result.setErrorMessage(ApiError.NONE.message());
            partition2Result.setErrorCode(ApiError.NONE.error().code());
            partition2Result.setErrorMessage(ApiError.NONE.message());
            env.kafkaClient().prepareResponse(new ElectLeadersResponse(0, Errors.NONE.code(), electionResults, ApiKeys.ELECT_LEADERS.latestVersion()));
            results = env.adminClient().electLeaders(electionType, new HashSet<>(asList(topic1, topic2)));
            assertFalse(results.partitions().get().get(topic1).isPresent());
            assertFalse(results.partitions().get().get(topic2).isPresent());
            // Now try a timeout
            results = env.adminClient().electLeaders(electionType, new HashSet<>(asList(topic1, topic2)), new ElectLeadersOptions().timeoutMs(100));
            TestUtils.assertFutureError(results.partitions(), TimeoutException.class);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TopicPartition(org.apache.kafka.common.TopicPartition) ElectionType(org.apache.kafka.common.ElectionType) ElectLeadersResponse(org.apache.kafka.common.requests.ElectLeadersResponse) ReplicaElectionResult(org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult) ApiError(org.apache.kafka.common.requests.ApiError) PartitionResult(org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult) AlterReplicaLogDirPartitionResult(org.apache.kafka.common.message.AlterReplicaLogDirsResponseData.AlterReplicaLogDirPartitionResult) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) HashSet(java.util.HashSet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 2 with ReplicaElectionResult

use of org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult in project kafka by apache.

the class ReplicationControlManagerTest method buildElectLeadersResponse.

private ElectLeadersResponseData buildElectLeadersResponse(Errors topLevelError, boolean electAllPartitions, Map<TopicPartition, ApiError> errors) {
    Map<String, List<Map.Entry<TopicPartition, ApiError>>> errorsByTopic = errors.entrySet().stream().collect(Collectors.groupingBy(entry -> entry.getKey().topic()));
    ElectLeadersResponseData response = new ElectLeadersResponseData().setErrorCode(topLevelError.code());
    errorsByTopic.forEach((topic, partitionErrors) -> {
        ReplicaElectionResult electionResult = new ReplicaElectionResult().setTopic(topic);
        electionResult.setPartitionResult(partitionErrors.stream().filter(entry -> !electAllPartitions || entry.getValue().error() != ELECTION_NOT_NEEDED).map(entry -> {
            TopicPartition topicPartition = entry.getKey();
            ApiError error = entry.getValue();
            return new PartitionResult().setPartitionId(topicPartition.partition()).setErrorCode(error.error().code()).setErrorMessage(error.message());
        }).collect(Collectors.toList()));
        response.replicaElectionResults().add(electionResult);
    });
    return response;
}
Also used : MockTime(org.apache.kafka.common.utils.MockTime) TopicPartitionsCollection(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitionsCollection) ListPartitionReassignmentsTopics(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) Collections.singletonList(java.util.Collections.singletonList) StaleBrokerEpochException(org.apache.kafka.common.errors.StaleBrokerEpochException) CreatePartitionsTopic(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic) PartitionResult(org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult) Arrays.asList(java.util.Arrays.asList) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) TopicData(org.apache.kafka.common.message.AlterIsrRequestData.TopicData) AlterIsrResponseData(org.apache.kafka.common.message.AlterIsrResponseData) Set(java.util.Set) SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) NO_LEADER(org.apache.kafka.metadata.LeaderConstants.NO_LEADER) UNKNOWN_TOPIC_ID(org.apache.kafka.common.protocol.Errors.UNKNOWN_TOPIC_ID) PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Errors(org.apache.kafka.common.protocol.Errors) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) NO_REASSIGNMENT_IN_PROGRESS(org.apache.kafka.common.protocol.Errors.NO_REASSIGNMENT_IN_PROGRESS) BrokerRegistration(org.apache.kafka.metadata.BrokerRegistration) ArrayList(java.util.ArrayList) FENCED_LEADER_EPOCH(org.apache.kafka.common.protocol.Errors.FENCED_LEADER_EPOCH) ElectionType(org.apache.kafka.common.ElectionType) NONE(org.apache.kafka.common.protocol.Errors.NONE) BrokerHeartbeatReply(org.apache.kafka.metadata.BrokerHeartbeatReply) ELECTION_NOT_NEEDED(org.apache.kafka.common.protocol.Errors.ELECTION_NOT_NEEDED) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ValueSource(org.junit.jupiter.params.provider.ValueSource) CreatableReplicaAssignment(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) ReassignablePartition(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignablePartition) BrokerHeartbeatRequestData(org.apache.kafka.common.message.BrokerHeartbeatRequestData) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) SEGMENT_BYTES_CONFIG(org.apache.kafka.common.config.TopicConfig.SEGMENT_BYTES_CONFIG) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) 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) PartitionData(org.apache.kafka.common.message.AlterIsrRequestData.PartitionData) CreatePartitionsAssignment(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsAssignment) POLICY_VIOLATION(org.apache.kafka.common.protocol.Errors.POLICY_VIOLATION) ConfigRecord(org.apache.kafka.common.metadata.ConfigRecord) LoggerFactory(org.slf4j.LoggerFactory) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) CreatePartitionsTopicResult(org.apache.kafka.common.message.CreatePartitionsResponseData.CreatePartitionsTopicResult) SecurityProtocol(org.apache.kafka.common.security.auth.SecurityProtocol) ELIGIBLE_LEADERS_NOT_AVAILABLE(org.apache.kafka.common.protocol.Errors.ELIGIBLE_LEADERS_NOT_AVAILABLE) ReassignableTopicResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ReplicaElectionResult(org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult) TopicRecord(org.apache.kafka.common.metadata.TopicRecord) TopicPartition(org.apache.kafka.common.TopicPartition) TopicPartitions(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions) INVALID_PARTITIONS(org.apache.kafka.common.protocol.Errors.INVALID_PARTITIONS) PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) Collectors(java.util.stream.Collectors) Replicas(org.apache.kafka.metadata.Replicas) Test(org.junit.jupiter.api.Test) List(java.util.List) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) Optional(java.util.Optional) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) INVALID_TOPIC_EXCEPTION(org.apache.kafka.common.protocol.Errors.INVALID_TOPIC_EXCEPTION) Uuid(org.apache.kafka.common.Uuid) IntStream(java.util.stream.IntStream) ReassignablePartitionResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) HashMap(java.util.HashMap) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) OptionalInt(java.util.OptionalInt) ApiError(org.apache.kafka.common.requests.ApiError) HashSet(java.util.HashSet) ConfigResource(org.apache.kafka.common.config.ConfigResource) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) PolicyViolationException(org.apache.kafka.common.errors.PolicyViolationException) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) OngoingTopicReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment) Collections.singletonMap(java.util.Collections.singletonMap) UNKNOWN_TOPIC_OR_PARTITION(org.apache.kafka.common.protocol.Errors.UNKNOWN_TOPIC_OR_PARTITION) Utils(org.apache.kafka.common.utils.Utils) Logger(org.slf4j.Logger) InvalidReplicaAssignmentException(org.apache.kafka.common.errors.InvalidReplicaAssignmentException) PREFERRED_LEADER_NOT_AVAILABLE(org.apache.kafka.common.protocol.Errors.PREFERRED_LEADER_NOT_AVAILABLE) INVALID_REPLICA_ASSIGNMENT(org.apache.kafka.common.protocol.Errors.INVALID_REPLICA_ASSIGNMENT) TimeUnit(java.util.concurrent.TimeUnit) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) CreateTopicPolicy(org.apache.kafka.server.policy.CreateTopicPolicy) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) Timeout(org.junit.jupiter.api.Timeout) Collections(java.util.Collections) TopicPartition(org.apache.kafka.common.TopicPartition) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) ReplicaElectionResult(org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) ApiError(org.apache.kafka.common.requests.ApiError) PartitionResult(org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap)

Example 3 with ReplicaElectionResult

use of org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult in project kafka by apache.

the class RequestResponseTest method createElectLeadersResponse.

private ElectLeadersResponse createElectLeadersResponse() {
    String topic = "myTopic";
    List<ReplicaElectionResult> electionResults = new ArrayList<>();
    ReplicaElectionResult electionResult = new ReplicaElectionResult();
    electionResults.add(electionResult);
    electionResult.setTopic(topic);
    // Add partition 1 result
    PartitionResult partitionResult = new PartitionResult();
    partitionResult.setPartitionId(0);
    partitionResult.setErrorCode(ApiError.NONE.error().code());
    partitionResult.setErrorMessage(ApiError.NONE.message());
    electionResult.partitionResult().add(partitionResult);
    // Add partition 2 result
    partitionResult = new PartitionResult();
    partitionResult.setPartitionId(1);
    partitionResult.setErrorCode(Errors.UNKNOWN_TOPIC_OR_PARTITION.code());
    partitionResult.setErrorMessage(Errors.UNKNOWN_TOPIC_OR_PARTITION.message());
    electionResult.partitionResult().add(partitionResult);
    return new ElectLeadersResponse(200, Errors.NONE.code(), electionResults, ELECT_LEADERS.latestVersion());
}
Also used : ArrayList(java.util.ArrayList) ReplicaElectionResult(org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult) PartitionResult(org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult)

Example 4 with ReplicaElectionResult

use of org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult in project kafka by apache.

the class ReplicationControlManager method electLeaders.

ControllerResult<ElectLeadersResponseData> electLeaders(ElectLeadersRequestData request) {
    ElectionType electionType = electionType(request.electionType());
    List<ApiMessageAndVersion> records = new ArrayList<>();
    ElectLeadersResponseData response = new ElectLeadersResponseData();
    if (request.topicPartitions() == null) {
        // compatibility with the old controller.
        for (Entry<String, Uuid> topicEntry : topicsByName.entrySet()) {
            String topicName = topicEntry.getKey();
            ReplicaElectionResult topicResults = new ReplicaElectionResult().setTopic(topicName);
            response.replicaElectionResults().add(topicResults);
            TopicControlInfo topic = topics.get(topicEntry.getValue());
            if (topic != null) {
                for (int partitionId : topic.parts.keySet()) {
                    ApiError error = electLeader(topicName, partitionId, electionType, records);
                    // partitions which already have the desired leader.
                    if (error.error() != Errors.ELECTION_NOT_NEEDED) {
                        topicResults.partitionResult().add(new PartitionResult().setPartitionId(partitionId).setErrorCode(error.error().code()).setErrorMessage(error.message()));
                    }
                }
            }
        }
    } else {
        for (TopicPartitions topic : request.topicPartitions()) {
            ReplicaElectionResult topicResults = new ReplicaElectionResult().setTopic(topic.topic());
            response.replicaElectionResults().add(topicResults);
            for (int partitionId : topic.partitions()) {
                ApiError error = electLeader(topic.topic(), partitionId, electionType, records);
                topicResults.partitionResult().add(new PartitionResult().setPartitionId(partitionId).setErrorCode(error.error().code()).setErrorMessage(error.message()));
            }
        }
    }
    return ControllerResult.of(records, response);
}
Also used : ArrayList(java.util.ArrayList) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) Uuid(org.apache.kafka.common.Uuid) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ElectionType(org.apache.kafka.common.ElectionType) ReplicaElectionResult(org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult) ApiError(org.apache.kafka.common.requests.ApiError) PartitionResult(org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult) TopicPartitions(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions)

Example 5 with ReplicaElectionResult

use of org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult in project kafka by apache.

the class ElectLeadersRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    ApiError apiError = ApiError.fromThrowable(e);
    List<ReplicaElectionResult> electionResults = new ArrayList<>();
    if (data.topicPartitions() != null) {
        for (TopicPartitions topic : data.topicPartitions()) {
            ReplicaElectionResult electionResult = new ReplicaElectionResult();
            electionResult.setTopic(topic.topic());
            for (Integer partitionId : topic.partitions()) {
                PartitionResult partitionResult = new PartitionResult();
                partitionResult.setPartitionId(partitionId);
                partitionResult.setErrorCode(apiError.error().code());
                partitionResult.setErrorMessage(apiError.message());
                electionResult.partitionResult().add(partitionResult);
            }
            electionResults.add(electionResult);
        }
    }
    return new ElectLeadersResponse(throttleTimeMs, apiError.error().code(), electionResults, version());
}
Also used : ArrayList(java.util.ArrayList) ReplicaElectionResult(org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult) PartitionResult(org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult) TopicPartitions(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions)

Aggregations

ArrayList (java.util.ArrayList)5 PartitionResult (org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult)5 ReplicaElectionResult (org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult)5 ElectionType (org.apache.kafka.common.ElectionType)3 ApiError (org.apache.kafka.common.requests.ApiError)3 HashSet (java.util.HashSet)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 Uuid (org.apache.kafka.common.Uuid)2 TopicPartitions (org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 Arrays.asList (java.util.Arrays.asList)1 Collections (java.util.Collections)1 Collections.singletonList (java.util.Collections.singletonList)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 OptionalInt (java.util.OptionalInt)1