Search in sources :

Example 76 with Uuid

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

Example 77 with Uuid

use of org.apache.kafka.common.Uuid in project kafka by apache.

the class ReplicationControlManagerTest method testCancelReassignPartitions.

@Test
public void testCancelReassignPartitions() 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, 4 }, new int[] { 0, 1, 2, 3 }, new int[] { 4, 3, 1, 0 }, new int[] { 2, 3, 4, 1 } }).topicId();
    Uuid barId = ctx.createTestTopic("bar", new int[][] { new int[] { 4, 3, 2 } }).topicId();
    assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null));
    List<ApiMessageAndVersion> fenceRecords = new ArrayList<>();
    replication.handleBrokerFenced(3, fenceRecords);
    ctx.replay(fenceRecords);
    assertEquals(new PartitionRegistration(new int[] { 1, 2, 3, 4 }, new int[] { 1, 2, 4 }, new int[] {}, new int[] {}, 1, 1, 1), replication.getPartition(fooId, 0));
    ControllerResult<AlterPartitionReassignmentsResponseData> alterResult = replication.alterPartitionReassignments(new AlterPartitionReassignmentsRequestData().setTopics(asList(new ReassignableTopic().setName("foo").setPartitions(asList(new ReassignablePartition().setPartitionIndex(0).setReplicas(asList(1, 2, 3)), new ReassignablePartition().setPartitionIndex(1).setReplicas(asList(1, 2, 3, 0)), new ReassignablePartition().setPartitionIndex(2).setReplicas(asList(5, 6, 7)), new ReassignablePartition().setPartitionIndex(3).setReplicas(asList()))), new ReassignableTopic().setName("bar").setPartitions(asList(new ReassignablePartition().setPartitionIndex(0).setReplicas(asList(1, 2, 3, 4, 0)))))));
    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(INVALID_REPLICA_ASSIGNMENT.code()).setErrorMessage("The manual partition assignment includes broker 5, " + "but no such broker is registered."), new ReassignablePartitionResponse().setPartitionIndex(3).setErrorCode(INVALID_REPLICA_ASSIGNMENT.code()).setErrorMessage("The manual partition assignment includes an empty " + "replica list."))), new ReassignableTopicResponse().setName("bar").setPartitions(asList(new ReassignablePartitionResponse().setPartitionIndex(0).setErrorMessage(null))))), alterResult.response());
    ctx.replay(alterResult.records());
    assertEquals(new PartitionRegistration(new int[] { 1, 2, 3 }, new int[] { 1, 2 }, new int[] {}, new int[] {}, 1, 2, 2), replication.getPartition(fooId, 0));
    assertEquals(new PartitionRegistration(new int[] { 1, 2, 3, 0 }, new int[] { 0, 1, 2 }, new int[] {}, new int[] {}, 0, 1, 2), replication.getPartition(fooId, 1));
    assertEquals(new PartitionRegistration(new int[] { 1, 2, 3, 4, 0 }, new int[] { 4, 2 }, new int[] {}, new int[] { 0, 1 }, 4, 1, 2), replication.getPartition(barId, 0));
    ListPartitionReassignmentsResponseData currentReassigning = new ListPartitionReassignmentsResponseData().setErrorMessage(null).setTopics(asList(new OngoingTopicReassignment().setName("bar").setPartitions(asList(new OngoingPartitionReassignment().setPartitionIndex(0).setRemovingReplicas(Collections.emptyList()).setAddingReplicas(asList(0, 1)).setReplicas(asList(1, 2, 3, 4, 0))))));
    assertEquals(currentReassigning, replication.listPartitionReassignments(null));
    assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(asList(new ListPartitionReassignmentsTopics().setName("foo").setPartitionIndexes(asList(0, 1, 2)))));
    assertEquals(currentReassigning, replication.listPartitionReassignments(asList(new ListPartitionReassignmentsTopics().setName("bar").setPartitionIndexes(asList(0, 1, 2)))));
    ControllerResult<AlterIsrResponseData> alterIsrResult = replication.alterIsr(new AlterIsrRequestData().setBrokerId(4).setBrokerEpoch(104).setTopics(asList(new TopicData().setName("bar").setPartitions(asList(new PartitionData().setPartitionIndex(0).setCurrentIsrVersion(2).setLeaderEpoch(1).setNewIsr(asList(4, 1, 2, 3, 0)))))));
    assertEquals(new AlterIsrResponseData().setTopics(asList(new AlterIsrResponseData.TopicData().setName("bar").setPartitions(asList(new AlterIsrResponseData.PartitionData().setPartitionIndex(0).setLeaderId(4).setLeaderEpoch(1).setIsr(asList(4, 1, 2, 3, 0)).setCurrentIsrVersion(3).setErrorCode(NONE.code()))))), alterIsrResult.response());
    ControllerResult<AlterPartitionReassignmentsResponseData> cancelResult = replication.alterPartitionReassignments(new AlterPartitionReassignmentsRequestData().setTopics(asList(new ReassignableTopic().setName("foo").setPartitions(asList(new ReassignablePartition().setPartitionIndex(0).setReplicas(null))), new ReassignableTopic().setName("bar").setPartitions(asList(new ReassignablePartition().setPartitionIndex(0).setReplicas(null))))));
    assertEquals(ControllerResult.atomicOf(Collections.singletonList(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(barId).setPartitionId(0).setLeader(4).setReplicas(asList(2, 3, 4)).setRemovingReplicas(null).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 ReassignableTopicResponse().setName("bar").setPartitions(asList(new ReassignablePartitionResponse().setPartitionIndex(0).setErrorMessage(null)))))), cancelResult);
    ctx.replay(cancelResult.records());
    assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null));
    assertEquals(new PartitionRegistration(new int[] { 2, 3, 4 }, new int[] { 4, 2 }, new int[] {}, new int[] {}, 4, 2, 3), replication.getPartition(barId, 0));
}
Also used : PartitionRegistration(org.apache.kafka.metadata.PartitionRegistration) ReassignablePartition(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignablePartition) PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) ReassignableTopic(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic) ArrayList(java.util.ArrayList) TopicData(org.apache.kafka.common.message.AlterIsrRequestData.TopicData) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) ReassignableTopicResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse) PartitionData(org.apache.kafka.common.message.AlterIsrRequestData.PartitionData) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ListPartitionReassignmentsTopics(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics) ReassignablePartitionResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) OngoingTopicReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment) Uuid(org.apache.kafka.common.Uuid) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) AlterIsrResponseData(org.apache.kafka.common.message.AlterIsrResponseData) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 78 with Uuid

use of org.apache.kafka.common.Uuid in project kafka by apache.

the class PartitionRegistrationTest method testRecordRoundTrip.

@Test
public void testRecordRoundTrip() {
    PartitionRegistration registrationA = new PartitionRegistration(new int[] { 1, 2, 3 }, new int[] { 1, 2 }, new int[] { 1 }, Replicas.NONE, 1, 0, 0);
    Uuid topicId = Uuid.fromString("OGdAI5nxT_m-ds3rJMqPLA");
    int partitionId = 4;
    ApiMessageAndVersion record = registrationA.toRecord(topicId, partitionId);
    PartitionRegistration registrationB = new PartitionRegistration((PartitionRecord) record.message());
    assertEquals(registrationA, registrationB);
}
Also used : Uuid(org.apache.kafka.common.Uuid) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) Test(org.junit.jupiter.api.Test)

Example 79 with Uuid

use of org.apache.kafka.common.Uuid in project kafka by apache.

the class TopicsImageTest method testDeleteAfterChanges.

@Test
public void testDeleteAfterChanges() {
    int localId = 3;
    Uuid zooId = Uuid.fromString("0hHJ3X5ZQ-CFfQ5xgpj90w");
    List<TopicImage> topics = new ArrayList<>();
    topics.add(newTopicImage("zoo", zooId, newPartition(new int[] { localId, 1, 2 })));
    TopicsImage image = new TopicsImage(newTopicsByIdMap(topics), newTopicsByNameMap(topics));
    List<ApiMessageAndVersion> topicRecords = new ArrayList<>();
    // leader to follower
    topicRecords.add(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(zooId).setPartitionId(0).setLeader(1), PARTITION_CHANGE_RECORD.highestSupportedVersion()));
    // remove zoo topic
    topicRecords.add(new ApiMessageAndVersion(new RemoveTopicRecord().setTopicId(zooId), REMOVE_TOPIC_RECORD.highestSupportedVersion()));
    TopicsDelta delta = new TopicsDelta(image);
    RecordTestUtils.replayAll(delta, topicRecords);
    LocalReplicaChanges changes = delta.localChanges(localId);
    assertEquals(new HashSet<>(Arrays.asList(new TopicPartition("zoo", 0))), changes.deletes());
    assertEquals(Collections.emptyMap(), changes.leaders());
    assertEquals(Collections.emptyMap(), changes.followers());
}
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) RemoveTopicRecord(org.apache.kafka.common.metadata.RemoveTopicRecord) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.jupiter.api.Test)

Example 80 with Uuid

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

Aggregations

Uuid (org.apache.kafka.common.Uuid)95 Test (org.junit.jupiter.api.Test)55 HashMap (java.util.HashMap)42 TopicPartition (org.apache.kafka.common.TopicPartition)40 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)30 ArrayList (java.util.ArrayList)29 Map (java.util.Map)21 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)21 LinkedHashMap (java.util.LinkedHashMap)18 List (java.util.List)15 FetchRequest (org.apache.kafka.common.requests.FetchRequest)14 TopicIdPartition (org.apache.kafka.common.TopicIdPartition)13 Errors (org.apache.kafka.common.protocol.Errors)12 FetchResponse (org.apache.kafka.common.requests.FetchResponse)12 Collections (java.util.Collections)11 ByteBuffer (java.nio.ByteBuffer)10 Node (org.apache.kafka.common.Node)10 CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)10 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)10 PartitionRegistration (org.apache.kafka.metadata.PartitionRegistration)10