@ -313,6 +313,10 @@ public class MetadataTest {
boolean [ ] updateResult = { true , false , false , false , false , true , false , false , false , true } ;
boolean [ ] updateResult = { true , false , false , false , false , true , false , false , false , true } ;
TopicPartition tp = new TopicPartition ( "topic" , 0 ) ;
TopicPartition tp = new TopicPartition ( "topic" , 0 ) ;
MetadataResponse metadataResponse = TestUtils . metadataUpdateWith ( "dummy" , 1 ,
Collections . emptyMap ( ) , Collections . singletonMap ( "topic" , 1 ) , _tp - > 0 ) ;
metadata . updateWithCurrentRequestVersion ( metadataResponse , false , 10L ) ;
for ( int i = 0 ; i < epochs . length ; i + + ) {
for ( int i = 0 ; i < epochs . length ; i + + ) {
metadata . updateLastSeenEpochIfNewer ( tp , epochs [ i ] ) ;
metadata . updateLastSeenEpochIfNewer ( tp , epochs [ i ] ) ;
if ( updateResult [ i ] ) {
if ( updateResult [ i ] ) {
@ -325,6 +329,46 @@ public class MetadataTest {
}
}
}
}
@Test
public void testUpdateLastEpoch ( ) {
TopicPartition tp = new TopicPartition ( "topic-1" , 0 ) ;
MetadataResponse metadataResponse = emptyMetadataResponse ( ) ;
metadata . updateWithCurrentRequestVersion ( metadataResponse , false , 0L ) ;
// if we have no leader epoch, this call shouldn't do anything
assertFalse ( metadata . updateLastSeenEpochIfNewer ( tp , 0 ) ) ;
assertFalse ( metadata . updateLastSeenEpochIfNewer ( tp , 1 ) ) ;
assertFalse ( metadata . updateLastSeenEpochIfNewer ( tp , 2 ) ) ;
assertFalse ( metadata . lastSeenLeaderEpoch ( tp ) . isPresent ( ) ) ;
// Metadata with newer epoch is handled
metadataResponse = TestUtils . metadataUpdateWith ( "dummy" , 1 , Collections . emptyMap ( ) , Collections . singletonMap ( "topic-1" , 1 ) , _tp - > 10 ) ;
metadata . updateWithCurrentRequestVersion ( metadataResponse , false , 1L ) ;
assertOptional ( metadata . lastSeenLeaderEpoch ( tp ) , leaderAndEpoch - > assertEquals ( leaderAndEpoch . intValue ( ) , 10 ) ) ;
// Don't update to an older one
assertFalse ( metadata . updateLastSeenEpochIfNewer ( tp , 1 ) ) ;
assertOptional ( metadata . lastSeenLeaderEpoch ( tp ) , leaderAndEpoch - > assertEquals ( leaderAndEpoch . intValue ( ) , 10 ) ) ;
// Don't cause update if it's the same one
assertFalse ( metadata . updateLastSeenEpochIfNewer ( tp , 10 ) ) ;
assertOptional ( metadata . lastSeenLeaderEpoch ( tp ) , leaderAndEpoch - > assertEquals ( leaderAndEpoch . intValue ( ) , 10 ) ) ;
// Update if we see newer epoch
assertTrue ( metadata . updateLastSeenEpochIfNewer ( tp , 12 ) ) ;
assertOptional ( metadata . lastSeenLeaderEpoch ( tp ) , leaderAndEpoch - > assertEquals ( leaderAndEpoch . intValue ( ) , 12 ) ) ;
metadataResponse = TestUtils . metadataUpdateWith ( "dummy" , 1 , Collections . emptyMap ( ) , Collections . singletonMap ( "topic-1" , 1 ) , _tp - > 12 ) ;
metadata . updateWithCurrentRequestVersion ( metadataResponse , false , 2L ) ;
assertOptional ( metadata . lastSeenLeaderEpoch ( tp ) , leaderAndEpoch - > assertEquals ( leaderAndEpoch . intValue ( ) , 12 ) ) ;
// Don't overwrite metadata with older epoch
metadataResponse = TestUtils . metadataUpdateWith ( "dummy" , 1 , Collections . emptyMap ( ) , Collections . singletonMap ( "topic-1" , 1 ) , _tp - > 11 ) ;
metadata . updateWithCurrentRequestVersion ( metadataResponse , false , 3L ) ;
assertOptional ( metadata . lastSeenLeaderEpoch ( tp ) , leaderAndEpoch - > assertEquals ( leaderAndEpoch . intValue ( ) , 12 ) ) ;
}
@Test
@Test
public void testRejectOldMetadata ( ) {
public void testRejectOldMetadata ( ) {
Map < String , Integer > partitionCounts = new HashMap < > ( ) ;
Map < String , Integer > partitionCounts = new HashMap < > ( ) ;
@ -378,26 +422,6 @@ public class MetadataTest {
}
}
}
}
@Test
public void testMaybeRequestUpdate ( ) {
TopicPartition tp = new TopicPartition ( "topic-1" , 0 ) ;
metadata . updateWithCurrentRequestVersion ( emptyMetadataResponse ( ) , false , 0L ) ;
assertTrue ( metadata . updateLastSeenEpochIfNewer ( tp , 1 ) ) ;
assertEquals ( metadata . lastSeenLeaderEpoch ( tp ) . get ( ) . longValue ( ) , 1 ) ;
metadata . updateWithCurrentRequestVersion ( emptyMetadataResponse ( ) , false , 1L ) ;
assertFalse ( metadata . updateLastSeenEpochIfNewer ( tp , 1 ) ) ;
assertEquals ( metadata . lastSeenLeaderEpoch ( tp ) . get ( ) . longValue ( ) , 1 ) ;
metadata . updateWithCurrentRequestVersion ( emptyMetadataResponse ( ) , false , 2L ) ;
assertFalse ( metadata . updateLastSeenEpochIfNewer ( tp , 0 ) ) ;
assertEquals ( metadata . lastSeenLeaderEpoch ( tp ) . get ( ) . longValue ( ) , 1 ) ;
metadata . updateWithCurrentRequestVersion ( emptyMetadataResponse ( ) , false , 3L ) ;
assertTrue ( metadata . updateLastSeenEpochIfNewer ( tp , 2 ) ) ;
assertEquals ( metadata . lastSeenLeaderEpoch ( tp ) . get ( ) . longValue ( ) , 2 ) ;
}
@Test
@Test
public void testOutOfBandEpochUpdate ( ) {
public void testOutOfBandEpochUpdate ( ) {
Map < String , Integer > partitionCounts = new HashMap < > ( ) ;
Map < String , Integer > partitionCounts = new HashMap < > ( ) ;
@ -406,7 +430,7 @@ public class MetadataTest {
metadata . updateWithCurrentRequestVersion ( emptyMetadataResponse ( ) , false , 0L ) ;
metadata . updateWithCurrentRequestVersion ( emptyMetadataResponse ( ) , false , 0L ) ;
assertTru e ( metadata . updateLastSeenEpochIfNewer ( tp , 99 ) ) ;
assertFals e ( metadata . updateLastSeenEpochIfNewer ( tp , 99 ) ) ;
// Update epoch to 100
// Update epoch to 100
MetadataResponse metadataResponse = TestUtils . metadataUpdateWith ( "dummy" , 1 , Collections . emptyMap ( ) , partitionCounts , _tp - > 100 ) ;
MetadataResponse metadataResponse = TestUtils . metadataUpdateWith ( "dummy" , 1 , Collections . emptyMap ( ) , partitionCounts , _tp - > 100 ) ;
@ -414,7 +438,7 @@ public class MetadataTest {
assertNotNull ( metadata . fetch ( ) . partition ( tp ) ) ;
assertNotNull ( metadata . fetch ( ) . partition ( tp ) ) ;
assertEquals ( metadata . lastSeenLeaderEpoch ( tp ) . get ( ) . longValue ( ) , 100 ) ;
assertEquals ( metadata . lastSeenLeaderEpoch ( tp ) . get ( ) . longValue ( ) , 100 ) ;
// Simulate a leader epoch from another response, like a fetch response (not yet implemented)
// Simulate a leader epoch from another response, like a fetch response or list offsets
assertTrue ( metadata . updateLastSeenEpochIfNewer ( tp , 101 ) ) ;
assertTrue ( metadata . updateLastSeenEpochIfNewer ( tp , 101 ) ) ;
// Cache of partition stays, but current partition info is not available since it's stale
// Cache of partition stays, but current partition info is not available since it's stale
@ -454,6 +478,11 @@ public class MetadataTest {
assertTrue ( metadata . partitionMetadataIfCurrent ( tp ) . isPresent ( ) ) ;
assertTrue ( metadata . partitionMetadataIfCurrent ( tp ) . isPresent ( ) ) ;
assertEquals ( 0 , metadata . partitionMetadataIfCurrent ( tp ) . get ( ) . partition ( ) ) ;
assertEquals ( 0 , metadata . partitionMetadataIfCurrent ( tp ) . get ( ) . partition ( ) ) ;
assertEquals ( Optional . of ( 0 ) , metadata . partitionMetadataIfCurrent ( tp ) . get ( ) . leaderId ) ;
assertEquals ( Optional . of ( 0 ) , metadata . partitionMetadataIfCurrent ( tp ) . get ( ) . leaderId ) ;
// Since epoch was null, this shouldn't update it
metadata . updateLastSeenEpochIfNewer ( tp , 10 ) ;
assertTrue ( metadata . partitionMetadataIfCurrent ( tp ) . isPresent ( ) ) ;
assertFalse ( metadata . partitionMetadataIfCurrent ( tp ) . get ( ) . leaderEpoch . isPresent ( ) ) ;
}
}
@Test
@Test