use of org.apache.kafka.common.metadata.PartitionChangeRecord 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());
}
use of org.apache.kafka.common.metadata.PartitionChangeRecord 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));
}
use of org.apache.kafka.common.metadata.PartitionChangeRecord 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());
}
use of org.apache.kafka.common.metadata.PartitionChangeRecord in project kafka by apache.
the class ReplicationControlManager method generateLeaderAndIsrUpdates.
/**
* Iterate over a sequence of partitions and generate ISR changes and/or leader
* changes if necessary.
*
* @param context A human-readable context string used in log4j logging.
* @param brokerToRemove NO_LEADER if no broker is being removed; the ID of the
* broker to remove from the ISR and leadership, otherwise.
* @param brokerToAdd NO_LEADER if no broker is being added; the ID of the
* broker which is now eligible to be a leader, otherwise.
* @param records A list of records which we will append to.
* @param iterator The iterator containing the partitions to examine.
*/
void generateLeaderAndIsrUpdates(String context, int brokerToRemove, int brokerToAdd, List<ApiMessageAndVersion> records, Iterator<TopicIdPartition> iterator) {
int oldSize = records.size();
// If the caller passed a valid broker ID for brokerToAdd, rather than passing
// NO_LEADER, that node will be considered an acceptable leader even if it is
// currently fenced. This is useful when handling unfencing. The reason is that
// while we're generating the records to handle unfencing, the ClusterControlManager
// still shows the node as fenced.
//
// Similarly, if the caller passed a valid broker ID for brokerToRemove, rather
// than passing NO_LEADER, that node will never be considered an acceptable leader.
// This is useful when handling a newly fenced node. We also exclude brokerToRemove
// from the target ISR, but we need to exclude it here too, to handle the case
// where there is an unclean leader election which chooses a leader from outside
// the ISR.
Function<Integer, Boolean> isAcceptableLeader = r -> (r != brokerToRemove) && (r == brokerToAdd || clusterControl.unfenced(r));
while (iterator.hasNext()) {
TopicIdPartition topicIdPart = iterator.next();
TopicControlInfo topic = topics.get(topicIdPart.topicId());
if (topic == null) {
throw new RuntimeException("Topic ID " + topicIdPart.topicId() + " existed in isrMembers, but not in the topics map.");
}
PartitionRegistration partition = topic.parts.get(topicIdPart.partitionId());
if (partition == null) {
throw new RuntimeException("Partition " + topicIdPart + " existed in isrMembers, but not in the partitions map.");
}
PartitionChangeBuilder builder = new PartitionChangeBuilder(partition, topicIdPart.topicId(), topicIdPart.partitionId(), isAcceptableLeader, () -> configurationControl.uncleanLeaderElectionEnabledForTopic(topic.name));
// Note: if brokerToRemove was passed as NO_LEADER, this is a no-op (the new
// target ISR will be the same as the old one).
builder.setTargetIsr(Replicas.toList(Replicas.copyWithout(partition.isr, brokerToRemove)));
builder.build().ifPresent(records::add);
}
if (records.size() != oldSize) {
if (log.isDebugEnabled()) {
StringBuilder bld = new StringBuilder();
String prefix = "";
for (ListIterator<ApiMessageAndVersion> iter = records.listIterator(oldSize); iter.hasNext(); ) {
ApiMessageAndVersion apiMessageAndVersion = iter.next();
PartitionChangeRecord record = (PartitionChangeRecord) apiMessageAndVersion.message();
bld.append(prefix).append(topics.get(record.topicId()).name).append("-").append(record.partitionId());
prefix = ", ";
}
log.debug("{}: changing partition(s): {}", context, bld.toString());
} else if (log.isInfoEnabled()) {
log.info("{}: changing {} partition(s)", context, records.size() - oldSize);
}
}
}
use of org.apache.kafka.common.metadata.PartitionChangeRecord in project kafka by apache.
the class MetadataNodeManagerTest method testPartitionChangeRecord.
@Test
public void testPartitionChangeRecord() {
PartitionRecord oldPartitionRecord = new PartitionRecord().setTopicId(Uuid.fromString("GcaQDl2UTsCNs1p9s37XkQ")).setPartitionId(0).setPartitionEpoch(0).setLeader(0).setLeaderEpoch(0).setIsr(Arrays.asList(0, 1, 2)).setReplicas(Arrays.asList(0, 1, 2));
PartitionChangeRecord partitionChangeRecord = new PartitionChangeRecord().setTopicId(Uuid.fromString("GcaQDl2UTsCNs1p9s37XkQ")).setPartitionId(0).setLeader(NO_LEADER_CHANGE).setReplicas(Arrays.asList(0, 1, 2));
PartitionRecord newPartitionRecord = new PartitionRecord().setTopicId(Uuid.fromString("GcaQDl2UTsCNs1p9s37XkQ")).setPartitionId(0).setPartitionEpoch(1).setLeader(0).setLeaderEpoch(0).setIsr(Arrays.asList(0, 1, 2)).setReplicas(Arrays.asList(0, 1, 2));
// Change nothing
checkPartitionChangeRecord(oldPartitionRecord, partitionChangeRecord, newPartitionRecord);
// Change isr
checkPartitionChangeRecord(oldPartitionRecord, partitionChangeRecord.duplicate().setIsr(Arrays.asList(0, 2)), newPartitionRecord.duplicate().setIsr(Arrays.asList(0, 2)));
// Change leader
checkPartitionChangeRecord(oldPartitionRecord, partitionChangeRecord.duplicate().setLeader(1), newPartitionRecord.duplicate().setLeader(1).setLeaderEpoch(1));
}
Aggregations