Search in sources :

Example 1 with AlterIsrRequestData

use of org.apache.kafka.common.message.AlterIsrRequestData 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 2 with AlterIsrRequestData

use of org.apache.kafka.common.message.AlterIsrRequestData in project kafka by apache.

the class QuorumControllerTest method testMissingInMemorySnapshot.

@Test
public void testMissingInMemorySnapshot() throws Exception {
    int numBrokers = 3;
    int numPartitions = 3;
    String topicName = "topic-name";
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(1, Optional.empty());
        QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, b -> b.setConfigDefs(CONFIGS))) {
        QuorumController controller = controlEnv.activeController();
        Map<Integer, Long> brokerEpochs = registerBrokers(controller, numBrokers);
        // Create a lot of partitions
        List<CreatableReplicaAssignment> partitions = IntStream.range(0, numPartitions).mapToObj(partitionIndex -> new CreatableReplicaAssignment().setPartitionIndex(partitionIndex).setBrokerIds(Arrays.asList(0, 1, 2))).collect(Collectors.toList());
        Uuid topicId = controller.createTopics(new CreateTopicsRequestData().setTopics(new CreatableTopicCollection(Collections.singleton(new CreatableTopic().setName(topicName).setNumPartitions(-1).setReplicationFactor((short) -1).setAssignments(new CreatableReplicaAssignmentCollection(partitions.iterator()))).iterator()))).get().topics().find(topicName).topicId();
        // Create a lot of alter isr
        List<AlterIsrRequestData.PartitionData> alterIsrs = IntStream.range(0, numPartitions).mapToObj(partitionIndex -> {
            PartitionRegistration partitionRegistration = controller.replicationControl().getPartition(topicId, partitionIndex);
            return new AlterIsrRequestData.PartitionData().setPartitionIndex(partitionIndex).setLeaderEpoch(partitionRegistration.leaderEpoch).setCurrentIsrVersion(partitionRegistration.partitionEpoch).setNewIsr(Arrays.asList(0, 1));
        }).collect(Collectors.toList());
        AlterIsrRequestData.TopicData topicData = new AlterIsrRequestData.TopicData().setName(topicName);
        topicData.partitions().addAll(alterIsrs);
        int leaderId = 0;
        AlterIsrRequestData alterIsrRequest = new AlterIsrRequestData().setBrokerId(leaderId).setBrokerEpoch(brokerEpochs.get(leaderId));
        alterIsrRequest.topics().add(topicData);
        logEnv.logManagers().get(0).resignAfterNonAtomicCommit();
        int oldClaimEpoch = controller.curClaimEpoch();
        assertThrows(ExecutionException.class, () -> controller.alterIsr(alterIsrRequest).get());
        // Wait for the controller to become active again
        assertSame(controller, controlEnv.activeController());
        assertTrue(oldClaimEpoch < controller.curClaimEpoch(), String.format("oldClaimEpoch = %s, newClaimEpoch = %s", oldClaimEpoch, controller.curClaimEpoch()));
        // Since the alterIsr partially failed we expect to see
        // some partitions to still have 2 in the ISR.
        int partitionsWithReplica2 = Utils.toList(controller.replicationControl().brokersToIsrs().partitionsWithBrokerInIsr(2)).size();
        int partitionsWithReplica0 = Utils.toList(controller.replicationControl().brokersToIsrs().partitionsWithBrokerInIsr(0)).size();
        assertEquals(numPartitions, partitionsWithReplica0);
        assertNotEquals(0, partitionsWithReplica2);
        assertTrue(partitionsWithReplica0 > partitionsWithReplica2, String.format("partitionsWithReplica0 = %s, partitionsWithReplica2 = %s", partitionsWithReplica0, partitionsWithReplica2));
    }
}
Also used : Arrays(java.util.Arrays) BrokerRegistrationRequestData(org.apache.kafka.common.message.BrokerRegistrationRequestData) BrokerIdNotRegisteredException(org.apache.kafka.common.errors.BrokerIdNotRegisteredException) ConfigurationControlManagerTest.entry(org.apache.kafka.controller.ConfigurationControlManagerTest.entry) Spliterators(java.util.Spliterators) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) Listener(org.apache.kafka.common.message.BrokerRegistrationRequestData.Listener) CreatePartitionsTopicResult(org.apache.kafka.common.message.CreatePartitionsResponseData.CreatePartitionsTopicResult) Future(java.util.concurrent.Future) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) CreatePartitionsTopic(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic) BufferSupplier(org.apache.kafka.common.utils.BufferSupplier) Map(java.util.Map) SET(org.apache.kafka.clients.admin.AlterConfigOp.OpType.SET) CreatableReplicaAssignmentCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignmentCollection) ListPartitionReassignmentsRequestData(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData) TopicRecord(org.apache.kafka.common.metadata.TopicRecord) RawSnapshotReader(org.apache.kafka.snapshot.RawSnapshotReader) TestUtils(org.apache.kafka.test.TestUtils) PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) ConfigResourceExistenceChecker(org.apache.kafka.controller.QuorumController.ConfigResourceExistenceChecker) List(java.util.List) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) AllocateProducerIdsRequestData(org.apache.kafka.common.message.AllocateProducerIdsRequestData) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) Optional(java.util.Optional) Errors(org.apache.kafka.common.protocol.Errors) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) Spliterator(java.util.Spliterator) IntStream(java.util.stream.IntStream) Uuid(org.apache.kafka.common.Uuid) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) CONFIGS(org.apache.kafka.controller.ConfigurationControlManagerTest.CONFIGS) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) ApiError(org.apache.kafka.common.requests.ApiError) TOPIC(org.apache.kafka.common.config.ConfigResource.Type.TOPIC) ConfigResource(org.apache.kafka.common.config.ConfigResource) BrokerHeartbeatReply(org.apache.kafka.metadata.BrokerHeartbeatReply) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) StreamSupport(java.util.stream.StreamSupport) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SnapshotReader(org.apache.kafka.snapshot.SnapshotReader) Utils(org.apache.kafka.common.utils.Utils) RecordsSnapshotReader(org.apache.kafka.snapshot.RecordsSnapshotReader) MetadataRecordSerde(org.apache.kafka.metadata.MetadataRecordSerde) BROKER0(org.apache.kafka.controller.ConfigurationControlManagerTest.BROKER0) TimeoutException(org.apache.kafka.common.errors.TimeoutException) CreatableReplicaAssignment(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment) Iterator(java.util.Iterator) BrokerRegistrationReply(org.apache.kafka.metadata.BrokerRegistrationReply) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) BrokerHeartbeatRequestData(org.apache.kafka.common.message.BrokerHeartbeatRequestData) BROKER(org.apache.kafka.common.config.ConfigResource.Type.BROKER) Batch(org.apache.kafka.raft.Batch) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) ExecutionException(java.util.concurrent.ExecutionException) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) 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) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) RegisterBrokerRecord(org.apache.kafka.common.metadata.RegisterBrokerRecord) HOURS(java.util.concurrent.TimeUnit.HOURS) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) ListenerCollection(org.apache.kafka.common.message.BrokerRegistrationRequestData.ListenerCollection) BrokerEndpointCollection(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpointCollection) ProducerIdsRecord(org.apache.kafka.common.metadata.ProducerIdsRecord) PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) BrokerEndpoint(org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) Uuid(org.apache.kafka.common.Uuid) CreatableReplicaAssignment(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignment) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) CreatableReplicaAssignmentCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignmentCollection) Test(org.junit.jupiter.api.Test)

Example 3 with AlterIsrRequestData

use of org.apache.kafka.common.message.AlterIsrRequestData in project kafka by apache.

the class ReplicationControlManagerTest method testReassignPartitions.

@Test
public void testReassignPartitions() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replication = ctx.replicationControl;
    ctx.registerBrokers(0, 1, 2, 3);
    ctx.unfenceBrokers(0, 1, 2, 3);
    Uuid fooId = ctx.createTestTopic("foo", new int[][] { new int[] { 1, 2, 3 }, new int[] { 3, 2, 1 } }).topicId();
    ctx.createTestTopic("bar", new int[][] { new int[] { 1, 2, 3 } }).topicId();
    assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null));
    ControllerResult<AlterPartitionReassignmentsResponseData> alterResult = replication.alterPartitionReassignments(new AlterPartitionReassignmentsRequestData().setTopics(asList(new ReassignableTopic().setName("foo").setPartitions(asList(new ReassignablePartition().setPartitionIndex(0).setReplicas(asList(3, 2, 1)), new ReassignablePartition().setPartitionIndex(1).setReplicas(asList(0, 2, 1)), new ReassignablePartition().setPartitionIndex(2).setReplicas(asList(0, 2, 1)))), new ReassignableTopic().setName("bar"))));
    assertEquals(new AlterPartitionReassignmentsResponseData().setErrorMessage(null).setResponses(asList(new ReassignableTopicResponse().setName("foo").setPartitions(asList(new ReassignablePartitionResponse().setPartitionIndex(0).setErrorMessage(null), new ReassignablePartitionResponse().setPartitionIndex(1).setErrorMessage(null), new ReassignablePartitionResponse().setPartitionIndex(2).setErrorCode(UNKNOWN_TOPIC_OR_PARTITION.code()).setErrorMessage("Unable to find partition foo:2."))), new ReassignableTopicResponse().setName("bar"))), alterResult.response());
    ctx.replay(alterResult.records());
    ListPartitionReassignmentsResponseData currentReassigning = new ListPartitionReassignmentsResponseData().setErrorMessage(null).setTopics(asList(new OngoingTopicReassignment().setName("foo").setPartitions(asList(new OngoingPartitionReassignment().setPartitionIndex(1).setRemovingReplicas(asList(3)).setAddingReplicas(asList(0)).setReplicas(asList(0, 2, 1, 3))))));
    assertEquals(currentReassigning, replication.listPartitionReassignments(null));
    assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(asList(new ListPartitionReassignmentsTopics().setName("bar").setPartitionIndexes(asList(0, 1, 2)))));
    assertEquals(currentReassigning, replication.listPartitionReassignments(asList(new ListPartitionReassignmentsTopics().setName("foo").setPartitionIndexes(asList(0, 1, 2)))));
    ControllerResult<AlterPartitionReassignmentsResponseData> cancelResult = replication.alterPartitionReassignments(new AlterPartitionReassignmentsRequestData().setTopics(asList(new ReassignableTopic().setName("foo").setPartitions(asList(new ReassignablePartition().setPartitionIndex(0).setReplicas(null), new ReassignablePartition().setPartitionIndex(1).setReplicas(null), new ReassignablePartition().setPartitionIndex(2).setReplicas(null))), new ReassignableTopic().setName("bar").setPartitions(asList(new ReassignablePartition().setPartitionIndex(0).setReplicas(null))))));
    assertEquals(ControllerResult.atomicOf(Collections.singletonList(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(fooId).setPartitionId(1).setReplicas(asList(2, 1, 3)).setLeader(3).setRemovingReplicas(Collections.emptyList()).setAddingReplicas(Collections.emptyList()), (short) 0)), new AlterPartitionReassignmentsResponseData().setErrorMessage(null).setResponses(asList(new ReassignableTopicResponse().setName("foo").setPartitions(asList(new ReassignablePartitionResponse().setPartitionIndex(0).setErrorCode(NO_REASSIGNMENT_IN_PROGRESS.code()).setErrorMessage(null), new ReassignablePartitionResponse().setPartitionIndex(1).setErrorCode(NONE.code()).setErrorMessage(null), new ReassignablePartitionResponse().setPartitionIndex(2).setErrorCode(UNKNOWN_TOPIC_OR_PARTITION.code()).setErrorMessage("Unable to find partition foo:2."))), new ReassignableTopicResponse().setName("bar").setPartitions(asList(new ReassignablePartitionResponse().setPartitionIndex(0).setErrorCode(NO_REASSIGNMENT_IN_PROGRESS.code()).setErrorMessage(null)))))), cancelResult);
    log.info("running final alterIsr...");
    ControllerResult<AlterIsrResponseData> alterIsrResult = replication.alterIsr(new AlterIsrRequestData().setBrokerId(3).setBrokerEpoch(103).setTopics(asList(new TopicData().setName("foo").setPartitions(asList(new PartitionData().setPartitionIndex(1).setCurrentIsrVersion(1).setLeaderEpoch(0).setNewIsr(asList(3, 0, 2, 1)))))));
    assertEquals(new AlterIsrResponseData().setTopics(asList(new AlterIsrResponseData.TopicData().setName("foo").setPartitions(asList(new AlterIsrResponseData.PartitionData().setPartitionIndex(1).setErrorCode(FENCED_LEADER_EPOCH.code()))))), alterIsrResult.response());
    ctx.replay(alterIsrResult.records());
    assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null));
}
Also used : ReassignablePartition(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignablePartition) PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) ReassignablePartitionResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse) ReassignableTopic(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic) TopicData(org.apache.kafka.common.message.AlterIsrRequestData.TopicData) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) OngoingTopicReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) Uuid(org.apache.kafka.common.Uuid) ReassignableTopicResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) PartitionData(org.apache.kafka.common.message.AlterIsrRequestData.PartitionData) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) AlterIsrResponseData(org.apache.kafka.common.message.AlterIsrResponseData) ListPartitionReassignmentsTopics(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 4 with AlterIsrRequestData

use of org.apache.kafka.common.message.AlterIsrRequestData in project kafka by apache.

the class ReplicationControlManagerTest method sendAlterIsr.

private ControllerResult<AlterIsrResponseData> sendAlterIsr(ReplicationControlManager replicationControl, int brokerId, long brokerEpoch, String topic, AlterIsrRequestData.PartitionData partitionData) throws Exception {
    AlterIsrRequestData request = new AlterIsrRequestData().setBrokerId(brokerId).setBrokerEpoch(brokerEpoch);
    AlterIsrRequestData.TopicData topicData = new AlterIsrRequestData.TopicData().setName(topic);
    request.topics().add(topicData);
    topicData.partitions().add(partitionData);
    ControllerResult<AlterIsrResponseData> result = replicationControl.alterIsr(request);
    RecordTestUtils.replayAll(replicationControl, result.records());
    return result;
}
Also used : TopicData(org.apache.kafka.common.message.AlterIsrRequestData.TopicData) AlterIsrResponseData(org.apache.kafka.common.message.AlterIsrResponseData) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData)

Example 5 with AlterIsrRequestData

use of org.apache.kafka.common.message.AlterIsrRequestData in project kafka by apache.

the class ReplicationControlManagerTest method testElectPreferredLeaders.

@Test
public void testElectPreferredLeaders() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replication = ctx.replicationControl;
    ctx.registerBrokers(0, 1, 2, 3, 4);
    ctx.unfenceBrokers(2, 3, 4);
    Uuid fooId = ctx.createTestTopic("foo", new int[][] { new int[] { 1, 2, 3 }, new int[] { 2, 3, 4 }, new int[] { 0, 2, 1 } }).topicId();
    ElectLeadersRequestData request1 = new ElectLeadersRequestData().setElectionType(ElectionType.PREFERRED.value).setTopicPartitions(new TopicPartitionsCollection(asList(new TopicPartitions().setTopic("foo").setPartitions(asList(0, 1)), new TopicPartitions().setTopic("bar").setPartitions(asList(0, 1))).iterator()));
    ControllerResult<ElectLeadersResponseData> election1Result = replication.electLeaders(request1);
    ElectLeadersResponseData expectedResponse1 = buildElectLeadersResponse(NONE, false, Utils.mkMap(Utils.mkEntry(new TopicPartition("foo", 0), new ApiError(PREFERRED_LEADER_NOT_AVAILABLE)), Utils.mkEntry(new TopicPartition("foo", 1), new ApiError(ELECTION_NOT_NEEDED)), Utils.mkEntry(new TopicPartition("bar", 0), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar")), Utils.mkEntry(new TopicPartition("bar", 1), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar"))));
    assertElectLeadersResponse(expectedResponse1, election1Result.response());
    assertEquals(Collections.emptyList(), election1Result.records());
    ctx.unfenceBrokers(0, 1);
    ControllerResult<AlterIsrResponseData> alterIsrResult = replication.alterIsr(new AlterIsrRequestData().setBrokerId(2).setBrokerEpoch(102).setTopics(asList(new AlterIsrRequestData.TopicData().setName("foo").setPartitions(asList(new AlterIsrRequestData.PartitionData().setPartitionIndex(0).setCurrentIsrVersion(0).setLeaderEpoch(0).setNewIsr(asList(1, 2, 3)))))));
    assertEquals(new AlterIsrResponseData().setTopics(asList(new AlterIsrResponseData.TopicData().setName("foo").setPartitions(asList(new AlterIsrResponseData.PartitionData().setPartitionIndex(0).setLeaderId(2).setLeaderEpoch(0).setIsr(asList(1, 2, 3)).setCurrentIsrVersion(1).setErrorCode(NONE.code()))))), alterIsrResult.response());
    ElectLeadersResponseData expectedResponse2 = buildElectLeadersResponse(NONE, false, Utils.mkMap(Utils.mkEntry(new TopicPartition("foo", 0), ApiError.NONE), Utils.mkEntry(new TopicPartition("foo", 1), new ApiError(ELECTION_NOT_NEEDED)), Utils.mkEntry(new TopicPartition("bar", 0), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar")), Utils.mkEntry(new TopicPartition("bar", 1), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar"))));
    ctx.replay(alterIsrResult.records());
    ControllerResult<ElectLeadersResponseData> election2Result = replication.electLeaders(request1);
    assertElectLeadersResponse(expectedResponse2, election2Result.response());
    assertEquals(asList(new ApiMessageAndVersion(new PartitionChangeRecord().setPartitionId(0).setTopicId(fooId).setLeader(1), (short) 0)), election2Result.records());
}
Also used : PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) PartitionData(org.apache.kafka.common.message.AlterIsrRequestData.PartitionData) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) TopicData(org.apache.kafka.common.message.AlterIsrRequestData.TopicData) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) AlterIsrResponseData(org.apache.kafka.common.message.AlterIsrResponseData) ApiError(org.apache.kafka.common.requests.ApiError) TopicPartitionsCollection(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitionsCollection) TopicPartitions(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

AlterIsrRequestData (org.apache.kafka.common.message.AlterIsrRequestData)6 Uuid (org.apache.kafka.common.Uuid)5 AlterIsrResponseData (org.apache.kafka.common.message.AlterIsrResponseData)5 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)5 TopicData (org.apache.kafka.common.message.AlterIsrRequestData.TopicData)4 PartitionChangeRecord (org.apache.kafka.common.metadata.PartitionChangeRecord)4 Test (org.junit.jupiter.api.Test)4 PartitionData (org.apache.kafka.common.message.AlterIsrRequestData.PartitionData)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 ListPartitionReassignmentsResponseData (org.apache.kafka.common.message.ListPartitionReassignmentsResponseData)3 PartitionRegistration (org.apache.kafka.metadata.PartitionRegistration)3 ArrayList (java.util.ArrayList)2 Errors (org.apache.kafka.common.protocol.Errors)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1