Search in sources :

Example 6 with PartitionChangeRecord

use of org.apache.kafka.common.metadata.PartitionChangeRecord in project kafka by apache.

the class PartitionChangeBuilderTest method testTriggerLeaderEpochBumpIfNeeded.

@Test
public void testTriggerLeaderEpochBumpIfNeeded() {
    testTriggerLeaderEpochBumpIfNeededLeader(createFooBuilder(false), new PartitionChangeRecord(), NO_LEADER_CHANGE);
    testTriggerLeaderEpochBumpIfNeededLeader(createFooBuilder(false).setTargetIsr(Arrays.asList(2, 1)), new PartitionChangeRecord(), 1);
    testTriggerLeaderEpochBumpIfNeededLeader(createFooBuilder(false).setTargetIsr(Arrays.asList(2, 1, 3, 4)), new PartitionChangeRecord(), NO_LEADER_CHANGE);
    testTriggerLeaderEpochBumpIfNeededLeader(createFooBuilder(false).setTargetReplicas(Arrays.asList(2, 1, 3, 4)), new PartitionChangeRecord(), NO_LEADER_CHANGE);
    testTriggerLeaderEpochBumpIfNeededLeader(createFooBuilder(false).setTargetReplicas(Arrays.asList(2, 1, 3, 4)), new PartitionChangeRecord().setLeader(2), 2);
}
Also used : PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) Test(org.junit.jupiter.api.Test)

Example 7 with PartitionChangeRecord

use of org.apache.kafka.common.metadata.PartitionChangeRecord 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 8 with PartitionChangeRecord

use of org.apache.kafka.common.metadata.PartitionChangeRecord in project kafka by apache.

the class ReplicationControlManagerTest method testElectUncleanLeaders.

@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testElectUncleanLeaders(boolean electAllPartitions) throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replication = ctx.replicationControl;
    ctx.registerBrokers(0, 1, 2, 3, 4);
    ctx.unfenceBrokers(0, 1, 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();
    TopicIdPartition partition0 = new TopicIdPartition(fooId, 0);
    TopicIdPartition partition1 = new TopicIdPartition(fooId, 1);
    TopicIdPartition partition2 = new TopicIdPartition(fooId, 2);
    ctx.fenceBrokers(Utils.mkSet(2, 3));
    ctx.fenceBrokers(Utils.mkSet(1, 2, 3));
    assertLeaderAndIsr(replication, partition0, NO_LEADER, new int[] { 1 });
    assertLeaderAndIsr(replication, partition1, 4, new int[] { 4 });
    assertLeaderAndIsr(replication, partition2, 0, new int[] { 0 });
    ElectLeadersRequestData request = buildElectLeadersRequest(ElectionType.UNCLEAN, electAllPartitions ? null : singletonMap("foo", asList(0, 1, 2)));
    // No election can be done yet because no replicas are available for partition 0
    ControllerResult<ElectLeadersResponseData> result1 = replication.electLeaders(request);
    assertEquals(Collections.emptyList(), result1.records());
    ElectLeadersResponseData expectedResponse1 = buildElectLeadersResponse(NONE, electAllPartitions, Utils.mkMap(Utils.mkEntry(new TopicPartition("foo", 0), new ApiError(ELIGIBLE_LEADERS_NOT_AVAILABLE)), Utils.mkEntry(new TopicPartition("foo", 1), new ApiError(ELECTION_NOT_NEEDED)), Utils.mkEntry(new TopicPartition("foo", 2), new ApiError(ELECTION_NOT_NEEDED))));
    assertElectLeadersResponse(expectedResponse1, result1.response());
    // Now we bring 2 back online which should allow the unclean election of partition 0
    ctx.unfenceBrokers(Utils.mkSet(2));
    // Bring 2 back into the ISR for partition 1. This allows us to verify that
    // preferred election does not occur as a result of the unclean election request.
    ctx.alterIsr(partition1, 4, asList(2, 4));
    ControllerResult<ElectLeadersResponseData> result = replication.electLeaders(request);
    assertEquals(1, result.records().size());
    ApiMessageAndVersion record = result.records().get(0);
    assertTrue(record.message() instanceof PartitionChangeRecord);
    PartitionChangeRecord partitionChangeRecord = (PartitionChangeRecord) record.message();
    assertEquals(0, partitionChangeRecord.partitionId());
    assertEquals(2, partitionChangeRecord.leader());
    assertEquals(singletonList(2), partitionChangeRecord.isr());
    ctx.replay(result.records());
    assertLeaderAndIsr(replication, partition0, 2, new int[] { 2 });
    assertLeaderAndIsr(replication, partition1, 4, new int[] { 2, 4 });
    assertLeaderAndIsr(replication, partition2, 0, new int[] { 0 });
    ElectLeadersResponseData expectedResponse = buildElectLeadersResponse(NONE, electAllPartitions, 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("foo", 2), new ApiError(ELECTION_NOT_NEEDED))));
    assertElectLeadersResponse(expectedResponse, result.response());
}
Also used : Uuid(org.apache.kafka.common.Uuid) PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) TopicPartition(org.apache.kafka.common.TopicPartition) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) ApiError(org.apache.kafka.common.requests.ApiError) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with PartitionChangeRecord

use of org.apache.kafka.common.metadata.PartitionChangeRecord in project kafka by apache.

the class PartitionRegistrationTest method testMergePartitionChangeRecordWithReassignmentData.

@Test
public void testMergePartitionChangeRecordWithReassignmentData() {
    PartitionRegistration partition0 = new PartitionRegistration(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, Replicas.NONE, Replicas.NONE, 1, 100, 200);
    PartitionRegistration partition1 = partition0.merge(new PartitionChangeRecord().setRemovingReplicas(Collections.singletonList(3)).setAddingReplicas(Collections.singletonList(4)).setReplicas(Arrays.asList(1, 2, 3, 4)));
    assertEquals(new PartitionRegistration(new int[] { 1, 2, 3, 4 }, new int[] { 1, 2, 3 }, new int[] { 3 }, new int[] { 4 }, 1, 100, 201), partition1);
    PartitionRegistration partition2 = partition1.merge(new PartitionChangeRecord().setIsr(Arrays.asList(1, 2, 4)).setRemovingReplicas(Collections.emptyList()).setAddingReplicas(Collections.emptyList()).setReplicas(Arrays.asList(1, 2, 4)));
    assertEquals(new PartitionRegistration(new int[] { 1, 2, 4 }, new int[] { 1, 2, 4 }, Replicas.NONE, Replicas.NONE, 1, 100, 202), partition2);
    assertFalse(partition2.isReassigning());
}
Also used : PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) Test(org.junit.jupiter.api.Test)

Example 10 with PartitionChangeRecord

use of org.apache.kafka.common.metadata.PartitionChangeRecord in project kafka by apache.

the class TopicsImageTest method testLocalReassignmentChanges.

@Test
public void testLocalReassignmentChanges() {
    int localId = 3;
    Uuid zooId = Uuid.fromString("0hHJ3X5ZQ-CFfQ5xgpj90w");
    List<TopicImage> topics = new ArrayList<>();
    topics.add(newTopicImage("zoo", zooId, newPartition(new int[] { 0, 1, localId }), newPartition(new int[] { localId, 1, 2 }), newPartition(new int[] { 0, 1, localId }), newPartition(new int[] { localId, 1, 2 }), newPartition(new int[] { 0, 1, 2 }), newPartition(new int[] { 0, 1, 2 })));
    TopicsImage image = new TopicsImage(newTopicsByIdMap(topics), newTopicsByNameMap(topics));
    List<ApiMessageAndVersion> topicRecords = new ArrayList<>();
    // zoo-0 - follower to leader
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(0).setLeader(localId), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    // zoo-1 - leader to follower
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(1).setLeader(1), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    // zoo-2 - follower to removed
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(2).setIsr(Arrays.asList(0, 1, 2)).setReplicas(Arrays.asList(0, 1, 2)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    // zoo-3 - leader to removed
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(3).setLeader(0).setIsr(Arrays.asList(0, 1, 2)).setReplicas(Arrays.asList(0, 1, 2)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    // zoo-4 - not replica to leader
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(4).setLeader(localId).setIsr(Arrays.asList(localId, 1, 2)).setReplicas(Arrays.asList(localId, 1, 2)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    // zoo-5 - not replica to follower
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(5).setIsr(Arrays.asList(0, 1, localId)).setReplicas(Arrays.asList(0, 1, localId)), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    TopicsDelta delta = new TopicsDelta(image);
    RecordTestUtils.replayAll(delta, topicRecords);
    LocalReplicaChanges changes = delta.localChanges(localId);
    assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("zoo", 2), new TopicPartition("zoo", 3))), changes.deletes());
    assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("zoo", 0), new TopicPartition("zoo", 4))), changes.leaders().keySet());
    assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("zoo", 1), new TopicPartition("zoo", 5))), changes.followers().keySet());
}
Also used : PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) ArrayList(java.util.ArrayList) Uuid(org.apache.kafka.common.Uuid) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.jupiter.api.Test)

Aggregations

PartitionChangeRecord (org.apache.kafka.common.metadata.PartitionChangeRecord)15 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)11 Test (org.junit.jupiter.api.Test)11 Uuid (org.apache.kafka.common.Uuid)8 ArrayList (java.util.ArrayList)5 AlterIsrRequestData (org.apache.kafka.common.message.AlterIsrRequestData)5 AlterIsrResponseData (org.apache.kafka.common.message.AlterIsrResponseData)5 TopicPartition (org.apache.kafka.common.TopicPartition)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 PartitionData (org.apache.kafka.common.message.AlterIsrRequestData.PartitionData)3 TopicData (org.apache.kafka.common.message.AlterIsrRequestData.TopicData)3 AlterPartitionReassignmentsRequestData (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData)3 ReassignablePartition (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignablePartition)3 ReassignableTopic (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic)3 AlterPartitionReassignmentsResponseData (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData)3 ReassignablePartitionResponse (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse)3 ReassignableTopicResponse (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse)3 ElectLeadersRequestData (org.apache.kafka.common.message.ElectLeadersRequestData)3 ElectLeadersResponseData (org.apache.kafka.common.message.ElectLeadersResponseData)3 ListPartitionReassignmentsTopics (org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics)3