@ -27,6 +27,7 @@ import org.apache.kafka.metadata.PartitionRegistration;
@@ -27,6 +27,7 @@ import org.apache.kafka.metadata.PartitionRegistration;
import org.apache.kafka.metadata.Replicas ;
import org.apache.kafka.metadata.placement.PartitionAssignment ;
import org.apache.kafka.server.common.ApiMessageAndVersion ;
import org.apache.kafka.server.common.MetadataVersion ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.api.Timeout ;
import org.junit.jupiter.params.ParameterizedTest ;
@ -89,7 +90,7 @@ public class PartitionChangeBuilderTest {
@@ -89,7 +90,7 @@ public class PartitionChangeBuilderTest {
private final static Uuid FOO_ID = Uuid . fromString ( "FbrrdcfiR-KC2CPSTHaJrg" ) ;
private static PartitionChangeBuilder createFooBuilder ( ) {
return new PartitionChangeBuilder ( FOO , FOO_ID , 0 , r - > r ! = 3 , true ) ;
return new PartitionChangeBuilder ( FOO , FOO_ID , 0 , r - > r ! = 3 , MetadataVersion . latest ( ) ) ;
}
private static final PartitionRegistration BAR = new PartitionRegistration . Builder ( ) .
@ -106,7 +107,7 @@ public class PartitionChangeBuilderTest {
@@ -106,7 +107,7 @@ public class PartitionChangeBuilderTest {
private final static Uuid BAR_ID = Uuid . fromString ( "LKfUsCBnQKekvL9O5dY9nw" ) ;
private static PartitionChangeBuilder createBarBuilder ( ) {
return new PartitionChangeBuilder ( BAR , BAR_ID , 0 , r - > r ! = 3 , true ) ;
return new PartitionChangeBuilder ( BAR , BAR_ID , 0 , r - > r ! = 3 , MetadataVersion . latest ( ) ) ;
}
private static final PartitionRegistration BAZ = new PartitionRegistration . Builder ( ) .
@ -121,7 +122,7 @@ public class PartitionChangeBuilderTest {
@@ -121,7 +122,7 @@ public class PartitionChangeBuilderTest {
private final static Uuid BAZ_ID = Uuid . fromString ( "wQzt5gkSTwuQNXZF5gIw7A" ) ;
private static PartitionChangeBuilder createBazBuilder ( ) {
return new PartitionChangeBuilder ( BAZ , BAZ_ID , 0 , __ - > true , true ) ;
return new PartitionChangeBuilder ( BAZ , BAZ_ID , 0 , __ - > true , MetadataVersion . latest ( ) ) ;
}
private static final PartitionRegistration OFFLINE = new PartitionRegistration . Builder ( ) .
@ -136,7 +137,7 @@ public class PartitionChangeBuilderTest {
@@ -136,7 +137,7 @@ public class PartitionChangeBuilderTest {
private final static Uuid OFFLINE_ID = Uuid . fromString ( "LKfUsCBnQKekvL9O5dY9nw" ) ;
private static PartitionChangeBuilder createOfflineBuilder ( ) {
return new PartitionChangeBuilder ( OFFLINE , OFFLINE_ID , 0 , r - > r = = 1 , true ) ;
return new PartitionChangeBuilder ( OFFLINE , OFFLINE_ID , 0 , r - > r = = 1 , MetadataVersion . latest ( ) ) ;
}
private static void assertElectLeaderEquals ( PartitionChangeBuilder builder ,
@ -183,19 +184,39 @@ public class PartitionChangeBuilderTest {
@@ -183,19 +184,39 @@ public class PartitionChangeBuilderTest {
public void testTriggerLeaderEpochBumpIfNeeded ( ) {
testTriggerLeaderEpochBumpIfNeededLeader ( createFooBuilder ( ) ,
new PartitionChangeRecord ( ) , NO_LEADER_CHANGE ) ;
testTriggerLeaderEpochBumpIfNeededLeader ( createFooBuilder ( ) .
setTargetIsrWithBrokerStates ( AlterPartitionRequest . newIsrToSimpleNewIsrWithBrokerEpochs ( Arrays . asList ( 2 , 1 ) ) ) ,
new PartitionChangeRecord ( ) , 1 ) ;
testTriggerLeaderEpochBumpIfNeededLeader ( createFooBuilder ( ) .
setTargetIsrWithBrokerStates ( AlterPartitionRequest . newIsrToSimpleNewIsrWithBrokerEpochs ( Arrays . asList ( 2 , 1 , 3 , 4 ) ) ) ,
// Shrinking the ISR doesn't increase the leader epoch
testTriggerLeaderEpochBumpIfNeededLeader (
createFooBuilder ( ) . setTargetIsrWithBrokerStates (
AlterPartitionRequest . newIsrToSimpleNewIsrWithBrokerEpochs ( Arrays . asList ( 2 , 1 ) )
) ,
new PartitionChangeRecord ( ) ,
NO_LEADER_CHANGE ) ;
NO_LEADER_CHANGE
) ;
// Expanding the ISR doesn't increase the leader epoch
testTriggerLeaderEpochBumpIfNeededLeader (
createFooBuilder ( ) . setTargetIsrWithBrokerStates (
AlterPartitionRequest . newIsrToSimpleNewIsrWithBrokerEpochs ( Arrays . asList ( 2 , 1 , 3 , 4 ) )
) ,
new PartitionChangeRecord ( ) ,
NO_LEADER_CHANGE
) ;
testTriggerLeaderEpochBumpIfNeededLeader ( createFooBuilder ( ) .
setTargetReplicas ( Arrays . asList ( 2 , 1 , 3 , 4 ) ) , new PartitionChangeRecord ( ) ,
NO_LEADER_CHANGE ) ;
testTriggerLeaderEpochBumpIfNeededLeader ( createFooBuilder ( ) .
setTargetReplicas ( Arrays . asList ( 2 , 1 , 3 , 4 ) ) ,
new PartitionChangeRecord ( ) . setLeader ( 2 ) , 2 ) ;
// Check that the leader epoch is bump if the ISR shrinks and isSkipLeaderEpochBumpSupported is not supported.
// See KAFKA-15021 for details.
testTriggerLeaderEpochBumpIfNeededLeader (
new PartitionChangeBuilder ( FOO , FOO_ID , 0 , r - > r ! = 3 , MetadataVersion . IBP_3_5_IV2 )
. setTargetIsrWithBrokerStates (
AlterPartitionRequest . newIsrToSimpleNewIsrWithBrokerEpochs ( Arrays . asList ( 2 , 1 ) )
) ,
new PartitionChangeRecord ( ) ,
1
) ;
}
@Test
@ -208,13 +229,26 @@ public class PartitionChangeBuilderTest {
@@ -208,13 +229,26 @@ public class PartitionChangeBuilderTest {
}
@Test
public void testIsrChangeAndLeaderBump ( ) {
assertEquals ( Optional . of ( new ApiMessageAndVersion ( new PartitionChangeRecord ( ) .
setTopicId ( FOO_ID ) .
setPartitionId ( 0 ) .
setIsr ( Arrays . asList ( 2 , 1 ) ) .
setLeader ( 1 ) , PARTITION_CHANGE_RECORD . highestSupportedVersion ( ) ) ) ,
createFooBuilder ( ) . setTargetIsrWithBrokerStates ( AlterPartitionRequest . newIsrToSimpleNewIsrWithBrokerEpochs ( Arrays . asList ( 2 , 1 ) ) ) . build ( ) ) ;
public void testIsrChangeDoesntBumpLeaderEpoch ( ) {
// Changing the ISR should not cause the leader epoch to increase
assertEquals (
// Expected
Optional . of (
new ApiMessageAndVersion (
new PartitionChangeRecord ( )
. setTopicId ( FOO_ID )
. setPartitionId ( 0 )
. setIsr ( Arrays . asList ( 2 , 1 ) ) ,
PARTITION_CHANGE_RECORD . highestSupportedVersion ( )
)
) ,
// Actual
createFooBuilder ( )
. setTargetIsrWithBrokerStates (
AlterPartitionRequest . newIsrToSimpleNewIsrWithBrokerEpochs ( Arrays . asList ( 2 , 1 ) )
)
. build ( )
) ;
}
@Test
@ -363,13 +397,15 @@ public class PartitionChangeBuilderTest {
@@ -363,13 +397,15 @@ public class PartitionChangeBuilderTest {
setPartitionEpoch ( 200 ) .
build ( ) ;
MetadataVersion metadataVersion = leaderRecoveryMetadataVersion ( isLeaderRecoverySupported ) ;
// Change the partition so that there is no leader
PartitionChangeBuilder offlineBuilder = new PartitionChangeBuilder (
registration ,
FOO_ID ,
0 ,
brokerId - > false ,
isLeaderRecoverySupported
metadataVersion
) ;
// Set the target ISR to empty to indicate that the last leader is offline
offlineBuilder . setTargetIsrWithBrokerStates ( Collections . emptyList ( ) ) ;
@ -394,7 +430,7 @@ public class PartitionChangeBuilderTest {
@@ -394,7 +430,7 @@ public class PartitionChangeBuilderTest {
FOO_ID ,
0 ,
brokerId - > true ,
isLeaderRecoverySupported
metadataVersion
) ;
// The only broker in the ISR is elected leader and stays in the recovering
@ -422,15 +458,16 @@ public class PartitionChangeBuilderTest {
@@ -422,15 +458,16 @@ public class PartitionChangeBuilderTest {
setPartitionEpoch ( 200 ) .
build ( ) ;
MetadataVersion metadataVersion = leaderRecoveryMetadataVersion ( isLeaderRecoverySupported ) ;
// Change the partition using unclean leader election
PartitionChangeBuilder onlineBuilder = new PartitionChangeBuilder (
registration ,
FOO_ID ,
0 ,
brokerId - > brokerId = = leaderId ,
isLeaderRecoverySupported
metadataVersion
) . setElection ( Election . UNCLEAN ) ;
// The partition should stay as recovering
PartitionChangeRecord changeRecord = ( PartitionChangeRecord ) onlineBuilder
@ -493,14 +530,13 @@ public class PartitionChangeBuilderTest {
@@ -493,14 +530,13 @@ public class PartitionChangeBuilderTest {
// being stopped as leader.
IntPredicate isValidLeader = l - > false ;
PartitionChangeBuilder partitionChangeBuilder =
new PartitionChangeBuilder (
part ,
topicId ,
0 ,
isValidLeader ,
false
) ;
PartitionChangeBuilder partitionChangeBuilder = new PartitionChangeBuilder (
part ,
topicId ,
0 ,
isValidLeader ,
leaderRecoveryMetadataVersion ( false )
) ;
// Before we build the new PartitionChangeBuilder, confirm the current leader is 0.
assertEquals ( 0 , part . leader ) ;
@ -518,5 +554,11 @@ public class PartitionChangeBuilderTest {
@@ -518,5 +554,11 @@ public class PartitionChangeBuilderTest {
build ( ) ) ;
}
private MetadataVersion leaderRecoveryMetadataVersion ( boolean isSupported ) {
if ( isSupported ) {
return MetadataVersion . IBP_3_2_IV0 ;
} else {
return MetadataVersion . IBP_3_1_IV0 ;
}
}
}