Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ ControllerResult<AlterPartitionResponseData> alterPartition(
partitionId,
new LeaderAcceptor(clusterControl, partition),
featureControl.metadataVersionOrThrow(),
getTopicEffectiveMinIsr(topic.name),
getEffectiveMinIsr(topic.name, partition),
featureControl.isElrFeatureEnabled()
);
if (configurationControl.uncleanLeaderElectionEnabledForTopic(topic.name())) {
Expand Down Expand Up @@ -1699,7 +1699,7 @@ ApiError electLeader(String topic, int partitionId, ElectionType electionType,
partitionId,
new LeaderAcceptor(clusterControl, partition),
featureControl.metadataVersionOrThrow(),
getTopicEffectiveMinIsr(topic),
getEffectiveMinIsr(topic, partition),
featureControl.isElrFeatureEnabled()
)
.setElection(election)
Expand Down Expand Up @@ -1866,7 +1866,7 @@ void maybeTriggerLeaderChangeForPartitionsWithoutPreferredLeader(
topicPartition.partitionId(),
new LeaderAcceptor(clusterControl, partition),
featureControl.metadataVersionOrThrow(),
getTopicEffectiveMinIsr(topic.name),
getEffectiveMinIsr(topic.name, partition),
featureControl.isElrFeatureEnabled()
)
.setElection(PartitionChangeBuilder.Election.PREFERRED)
Expand Down Expand Up @@ -2149,7 +2149,7 @@ void generateLeaderAndIsrUpdates(String context,
topicIdPart.partitionId(),
new LeaderAcceptor(clusterControl, partition, isAcceptableLeader),
featureControl.metadataVersionOrThrow(),
getTopicEffectiveMinIsr(topic.name),
getEffectiveMinIsr(topic.name, partition),
featureControl.isElrFeatureEnabled()
);
if (configurationControl.uncleanLeaderElectionEnabledForTopic(topic.name)) {
Expand Down Expand Up @@ -2269,7 +2269,7 @@ Optional<ApiMessageAndVersion> cancelPartitionReassignment(String topicName,
tp.partitionId(),
new LeaderAcceptor(clusterControl, part),
featureControl.metadataVersionOrThrow(),
getTopicEffectiveMinIsr(topicName),
getEffectiveMinIsr(topicName, part),
featureControl.isElrFeatureEnabled()
);
if (configurationControl.uncleanLeaderElectionEnabledForTopic(topicName)) {
Expand Down Expand Up @@ -2335,7 +2335,7 @@ Optional<ApiMessageAndVersion> changePartitionReassignment(TopicIdPartition tp,
tp.partitionId(),
new LeaderAcceptor(clusterControl, part),
featureControl.metadataVersionOrThrow(),
getTopicEffectiveMinIsr(topics.get(tp.topicId()).name),
getEffectiveMinIsr(topics.get(tp.topicId()).name, part),
featureControl.isElrFeatureEnabled()
);
if (!reassignment.replicas().equals(currentReplicas)) {
Expand Down Expand Up @@ -2418,7 +2418,7 @@ ControllerResult<AssignReplicasToDirsResponseData> handleAssignReplicasToDirs(As
partitionIndex,
new LeaderAcceptor(clusterControl, partitionRegistration),
featureControl.metadataVersionOrThrow(),
getTopicEffectiveMinIsr(topicName),
getEffectiveMinIsr(topicName, partitionRegistration),
featureControl.isElrFeatureEnabled()
)
.setDirectory(brokerId, dirId)
Expand Down Expand Up @@ -2480,12 +2480,11 @@ private void listReassigningTopic(ListPartitionReassignmentsResponseData respons
}

// Visible to test.
int getTopicEffectiveMinIsr(String topicName) {
int getEffectiveMinIsr(String topicName, PartitionRegistration partition) {
String minIsrConfig = configurationControl.getTopicConfig(topicName, MIN_IN_SYNC_REPLICAS_CONFIG).value();
int currentMinIsr = Integer.parseInt(minIsrConfig);
Uuid topicId = topicsByName.get(topicName);
int replicationFactor = topics.get(topicId).parts.get(0).replicas.length;
return Math.min(currentMinIsr, replicationFactor);
// Partitions of the same topic can have different replication factors.
return Math.min(currentMinIsr, partition.replicas.length);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,42 @@ public void testEligibleLeaderReplicas_EffectiveMinIsr() {
TopicIdPartition topicIdPartition = new TopicIdPartition(createTopicResult.topicId(), 0);
assertEquals(OptionalInt.of(0), ctx.currentLeader(topicIdPartition));
ctx.alterTopicConfig("foo", TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "5");
assertEquals(3, replicationControl.getTopicEffectiveMinIsr("foo"));
assertEquals(3, replicationControl.getEffectiveMinIsr("foo",
replicationControl.getPartition(topicIdPartition.topicId(), topicIdPartition.partitionId())));
}

@Test
public void testEligibleLeaderReplicas_EffectiveMinIsrUsesTargetPartitionReplicas() {
ReplicationControlTestContext ctx = new ReplicationControlTestContext.Builder().setIsElrEnabled(true).build();
ReplicationControlManager replicationControl = ctx.replicationControl;
ctx.registerBrokers(0, 1, 2);
ctx.unfenceBrokers(0, 1, 2);
Uuid fooId = ctx.createTestTopic("foo",
new int[][] {new int[] {0, 1, 2}, new int[] {0, 1, 2}}).topicId();
// Decrease the replication factor of partition 0 only.
ctx.replay(List.of(new ApiMessageAndVersion(new PartitionChangeRecord().
setTopicId(fooId).
setPartitionId(0).
setReplicas(List.of(0)).
setIsr(List.of(0)).
setDirectories(List.of(DirectoryId.UNASSIGNED)),
(short) 0)));
ctx.alterTopicConfig("foo", TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "2");
assertEquals(1, replicationControl.getEffectiveMinIsr("foo", replicationControl.getPartition(fooId, 0)));
assertEquals(2, replicationControl.getEffectiveMinIsr("foo", replicationControl.getPartition(fooId, 1)));

// Shrink the ISR of partition 1 to {0}. It is below the min ISR of partition 1,
// so the removed replicas should be kept in the ELR.
TopicIdPartition topicIdPartition = new TopicIdPartition(fooId, 1);
PartitionData shrinkIsrRequest = newAlterPartition(
replicationControl, topicIdPartition, isrWithDefaultEpoch(0), LeaderRecoveryState.RECOVERED);
ControllerResult<AlterPartitionResponseData> shrinkIsrResult = sendAlterPartition(
replicationControl, 0, ctx.currentBrokerEpoch(0), fooId, shrinkIsrRequest);
assertAlterPartitionResponse(shrinkIsrResult, topicIdPartition, NONE);

PartitionRegistration partition = replicationControl.getPartition(fooId, 1);
assertArrayEquals(new int[]{0}, partition.isr, partition.toString());
assertArrayEquals(new int[]{1, 2}, partition.elr, partition.toString());
}

@Test
Expand Down Expand Up @@ -1435,7 +1470,7 @@ private PartitionData newAlterPartition(
PartitionRegistration partitionControl =
replicationControl.getPartition(topicIdPartition.topicId(), topicIdPartition.partitionId());
return new AlterPartitionRequestData.PartitionData()
.setPartitionIndex(0)
.setPartitionIndex(topicIdPartition.partitionId())
.setLeaderEpoch(partitionControl.leaderEpoch)
.setPartitionEpoch(partitionControl.partitionEpoch)
.setNewIsrWithEpochs(newIsrWithEpoch)
Expand Down
Loading