@@ -822,6 +822,48 @@ public void testEligibleLeaderReplicas_IsrShrinkBelowMinISR(short version) {
822
822
}
823
823
}
824
824
825
+ @ Test
826
+ public void testEligibleLeaderReplicas_lastKnownElrShouldBePopulatedWhenNoLeader () {
827
+ PartitionRegistration partition = new PartitionRegistration .Builder ()
828
+ .setReplicas (new int [] {1 , 2 , 3 })
829
+ .setDirectories (new Uuid [] {
830
+ DirectoryId .UNASSIGNED ,
831
+ DirectoryId .UNASSIGNED ,
832
+ DirectoryId .UNASSIGNED
833
+ })
834
+ .setIsr (new int [] {1 })
835
+ .setElr (new int [] {2 })
836
+ .setLeader (1 )
837
+ .setLeaderRecoveryState (LeaderRecoveryState .RECOVERED )
838
+ .setLeaderEpoch (100 )
839
+ .setPartitionEpoch (200 )
840
+ .build ();
841
+
842
+ short version = 2 ; // ELR supported
843
+ Uuid topicId = Uuid .fromString ("FbrrdcfiR-KC2CPSTHaJrg" );
844
+
845
+ // No replica is acceptable as leader, so election yields NO_LEADER.
846
+ // We intentionally do not change target ISR so record.isr remains null.
847
+ PartitionChangeBuilder builder = new PartitionChangeBuilder (partition , topicId , 0 , r -> false ,
848
+ metadataVersionForPartitionChangeRecordVersion (version ), 3 )
849
+ .setElection (Election .PREFERRED )
850
+ .setEligibleLeaderReplicasEnabled (isElrEnabled (version ))
851
+ .setDefaultDirProvider (DEFAULT_DIR_PROVIDER )
852
+ .setUseLastKnownLeaderInBalancedRecovery (true );
853
+
854
+ ApiMessageAndVersion change = builder .build ().get ();
855
+ PartitionChangeRecord record = (PartitionChangeRecord ) change .message ();
856
+
857
+ assertEquals (NO_LEADER , record .leader ());
858
+ // There is no ISR update if we do not perform the leader verification on the ISR members.
859
+ assertNull (record .isr (), record .toString ());
860
+ assertEquals (1 , record .lastKnownElr ().size (), record .toString ());
861
+ assertEquals (1 , record .lastKnownElr ().get (0 ), record .toString ());
862
+ partition = partition .merge ((PartitionChangeRecord ) builder .build ().get ().message ());
863
+ assertArrayEquals (new int [] {1 }, partition .lastKnownElr );
864
+ }
865
+
866
+
825
867
@ ParameterizedTest
826
868
@ MethodSource ("partitionChangeRecordVersions" )
827
869
public void testEligibleLeaderReplicas_IsrExpandAboveMinISR (short version ) {
0 commit comments