@ -39,7 +39,10 @@ import java.util.List;
@@ -39,7 +39,10 @@ import java.util.List;
import static org.apache.kafka.common.utils.Utils.mkEntry ;
import static org.apache.kafka.common.utils.Utils.mkMap ;
import static org.hamcrest.MatcherAssert.assertThat ;
import static org.hamcrest.Matchers.equalTo ;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertNull ;
import static org.junit.Assert.assertThrows ;
public class PartitionGroupTest {
@ -47,8 +50,8 @@ public class PartitionGroupTest {
@@ -47,8 +50,8 @@ public class PartitionGroupTest {
private final Serializer < Integer > intSerializer = new IntegerSerializer ( ) ;
private final Deserializer < Integer > intDeserializer = new IntegerDeserializer ( ) ;
private final TimestampExtractor timestampExtractor = new MockTimestampExtractor ( ) ;
private final TopicPartition random Partition = new TopicPartition ( "random -partition" , 0 ) ;
private final String errMessage = "Partition " + random Partition + " not found." ;
private final TopicPartition unknown Partition = new TopicPartition ( "unknown -partition" , 0 ) ;
private final String errMessage = "Partition " + unknown Partition + " not found." ;
private final String [ ] topics = { "topic" } ;
private final TopicPartition partition1 = new TopicPartition ( topics [ 0 ] , 1 ) ;
private final TopicPartition partition2 = new TopicPartition ( topics [ 0 ] , 2 ) ;
@ -88,6 +91,14 @@ public class PartitionGroupTest {
@@ -88,6 +91,14 @@ public class PartitionGroupTest {
@Test
public void testTimeTracking ( ) {
testFirstBatch ( ) ;
testSecondBatch ( ) ;
}
private void testFirstBatch ( ) {
StampedRecord record ;
final PartitionGroup . RecordInfo info = new PartitionGroup . RecordInfo ( ) ;
assertEquals ( 0 , group . numBuffered ( ) ) ;
// add three 3 records with timestamp 1, 3, 5 to partition-1
final List < ConsumerRecord < byte [ ] , byte [ ] > > list1 = Arrays . asList (
@ -109,12 +120,13 @@ public class PartitionGroupTest {
@@ -109,12 +120,13 @@ public class PartitionGroupTest {
// st: -1 since no records was being processed yet
verifyBuffered ( 6 , 3 , 3 ) ;
assertEquals ( 1L , group . partitionTimestamp ( partition1 ) ) ;
assertEquals ( 2L , group . partitionTimestamp ( partition2 ) ) ;
assertEquals ( 1L , group . headRecordOffset ( partition1 ) . longValue ( ) ) ;
assertEquals ( 2L , group . headRecordOffset ( partition2 ) . longValue ( ) ) ;
assertEquals ( - 1L , group . streamTime ( ) ) ;
assertEquals ( 0 . 0 , metrics . metric ( lastLatenessValue ) . metricValue ( ) ) ;
StampedRecord record ;
final PartitionGroup . RecordInfo info = new PartitionGroup . RecordInfo ( ) ;
// get one record, now the time should be advanced
record = group . nextRecord ( info ) ;
// 1:[3, 5]
@ -123,6 +135,8 @@ public class PartitionGroupTest {
@@ -123,6 +135,8 @@ public class PartitionGroupTest {
assertEquals ( partition1 , info . partition ( ) ) ;
assertEquals ( 3L , group . partitionTimestamp ( partition1 ) ) ;
assertEquals ( 2L , group . partitionTimestamp ( partition2 ) ) ;
assertEquals ( 3L , group . headRecordOffset ( partition1 ) . longValue ( ) ) ;
assertEquals ( 2L , group . headRecordOffset ( partition2 ) . longValue ( ) ) ;
assertEquals ( 1L , group . streamTime ( ) ) ;
verifyTimes ( record , 1L , 1L ) ;
verifyBuffered ( 5 , 2 , 3 ) ;
@ -136,10 +150,17 @@ public class PartitionGroupTest {
@@ -136,10 +150,17 @@ public class PartitionGroupTest {
assertEquals ( partition2 , info . partition ( ) ) ;
assertEquals ( 3L , group . partitionTimestamp ( partition1 ) ) ;
assertEquals ( 4L , group . partitionTimestamp ( partition2 ) ) ;
assertEquals ( 3L , group . headRecordOffset ( partition1 ) . longValue ( ) ) ;
assertEquals ( 4L , group . headRecordOffset ( partition2 ) . longValue ( ) ) ;
assertEquals ( 2L , group . streamTime ( ) ) ;
verifyTimes ( record , 2L , 2L ) ;
verifyBuffered ( 4 , 2 , 2 ) ;
assertEquals ( 0 . 0 , metrics . metric ( lastLatenessValue ) . metricValue ( ) ) ;
}
private void testSecondBatch ( ) {
StampedRecord record ;
final PartitionGroup . RecordInfo info = new PartitionGroup . RecordInfo ( ) ;
// add 2 more records with timestamp 2, 4 to partition-1
final List < ConsumerRecord < byte [ ] , byte [ ] > > list3 = Arrays . asList (
@ -153,6 +174,8 @@ public class PartitionGroupTest {
@@ -153,6 +174,8 @@ public class PartitionGroupTest {
verifyBuffered ( 6 , 4 , 2 ) ;
assertEquals ( 3L , group . partitionTimestamp ( partition1 ) ) ;
assertEquals ( 4L , group . partitionTimestamp ( partition2 ) ) ;
assertEquals ( 3L , group . headRecordOffset ( partition1 ) . longValue ( ) ) ;
assertEquals ( 4L , group . headRecordOffset ( partition2 ) . longValue ( ) ) ;
assertEquals ( 2L , group . streamTime ( ) ) ;
assertEquals ( 0 . 0 , metrics . metric ( lastLatenessValue ) . metricValue ( ) ) ;
@ -164,6 +187,8 @@ public class PartitionGroupTest {
@@ -164,6 +187,8 @@ public class PartitionGroupTest {
assertEquals ( partition1 , info . partition ( ) ) ;
assertEquals ( 5L , group . partitionTimestamp ( partition1 ) ) ;
assertEquals ( 4L , group . partitionTimestamp ( partition2 ) ) ;
assertEquals ( 5L , group . headRecordOffset ( partition1 ) . longValue ( ) ) ;
assertEquals ( 4L , group . headRecordOffset ( partition2 ) . longValue ( ) ) ;
assertEquals ( 3L , group . streamTime ( ) ) ;
verifyTimes ( record , 3L , 3L ) ;
verifyBuffered ( 5 , 3 , 2 ) ;
@ -177,6 +202,8 @@ public class PartitionGroupTest {
@@ -177,6 +202,8 @@ public class PartitionGroupTest {
assertEquals ( partition2 , info . partition ( ) ) ;
assertEquals ( 5L , group . partitionTimestamp ( partition1 ) ) ;
assertEquals ( 6L , group . partitionTimestamp ( partition2 ) ) ;
assertEquals ( 5L , group . headRecordOffset ( partition1 ) . longValue ( ) ) ;
assertEquals ( 6L , group . headRecordOffset ( partition2 ) . longValue ( ) ) ;
assertEquals ( 4L , group . streamTime ( ) ) ;
verifyTimes ( record , 4L , 4L ) ;
verifyBuffered ( 4 , 3 , 1 ) ;
@ -190,6 +217,8 @@ public class PartitionGroupTest {
@@ -190,6 +217,8 @@ public class PartitionGroupTest {
assertEquals ( partition1 , info . partition ( ) ) ;
assertEquals ( 5L , group . partitionTimestamp ( partition1 ) ) ;
assertEquals ( 6L , group . partitionTimestamp ( partition2 ) ) ;
assertEquals ( 2L , group . headRecordOffset ( partition1 ) . longValue ( ) ) ;
assertEquals ( 6L , group . headRecordOffset ( partition2 ) . longValue ( ) ) ;
assertEquals ( 5L , group . streamTime ( ) ) ;
verifyTimes ( record , 5L , 5L ) ;
verifyBuffered ( 3 , 2 , 1 ) ;
@ -203,6 +232,8 @@ public class PartitionGroupTest {
@@ -203,6 +232,8 @@ public class PartitionGroupTest {
assertEquals ( partition1 , info . partition ( ) ) ;
assertEquals ( 5L , group . partitionTimestamp ( partition1 ) ) ;
assertEquals ( 6L , group . partitionTimestamp ( partition2 ) ) ;
assertEquals ( 4L , group . headRecordOffset ( partition1 ) . longValue ( ) ) ;
assertEquals ( 6L , group . headRecordOffset ( partition2 ) . longValue ( ) ) ;
assertEquals ( 5L , group . streamTime ( ) ) ;
verifyTimes ( record , 2L , 5L ) ;
verifyBuffered ( 2 , 1 , 1 ) ;
@ -216,6 +247,8 @@ public class PartitionGroupTest {
@@ -216,6 +247,8 @@ public class PartitionGroupTest {
assertEquals ( partition1 , info . partition ( ) ) ;
assertEquals ( 5L , group . partitionTimestamp ( partition1 ) ) ;
assertEquals ( 6L , group . partitionTimestamp ( partition2 ) ) ;
assertNull ( group . headRecordOffset ( partition1 ) ) ;
assertEquals ( 6L , group . headRecordOffset ( partition2 ) . longValue ( ) ) ;
assertEquals ( 5L , group . streamTime ( ) ) ;
verifyTimes ( record , 4L , 5L ) ;
verifyBuffered ( 1 , 0 , 1 ) ;
@ -229,6 +262,8 @@ public class PartitionGroupTest {
@@ -229,6 +262,8 @@ public class PartitionGroupTest {
assertEquals ( partition2 , info . partition ( ) ) ;
assertEquals ( 5L , group . partitionTimestamp ( partition1 ) ) ;
assertEquals ( 6L , group . partitionTimestamp ( partition2 ) ) ;
assertNull ( group . headRecordOffset ( partition1 ) ) ;
assertNull ( group . headRecordOffset ( partition2 ) ) ;
assertEquals ( 6L , group . streamTime ( ) ) ;
verifyTimes ( record , 6L , 6L ) ;
verifyBuffered ( 0 , 0 , 0 ) ;
@ -305,16 +340,78 @@ public class PartitionGroupTest {
@@ -305,16 +340,78 @@ public class PartitionGroupTest {
}
@Test
public void shouldThrowNullpointerUponSetPartitionTimestampFailure ( ) {
assertThrows ( errMessage , NullPointerException . class , ( ) - > {
group . setPartitionTime ( randomPartition , 0L ) ;
} ) ;
public void shouldThrowIllegalStateExceptionUponAddRecordsIfPartitionUnknown ( ) {
final IllegalStateException exception = assertThrows (
IllegalStateException . class ,
( ) - > group . addRawRecords ( unknownPartition , null ) ) ;
assertThat ( errMessage , equalTo ( exception . getMessage ( ) ) ) ;
}
@Test
public void shouldThrowIllegalStateExceptionUponNumBufferedIfPartitionUnknown ( ) {
final IllegalStateException exception = assertThrows (
IllegalStateException . class ,
( ) - > group . numBuffered ( unknownPartition ) ) ;
assertThat ( errMessage , equalTo ( exception . getMessage ( ) ) ) ;
}
@Test
public void shouldThrowNullpointerUponGetPartitionTimestampFailure ( ) {
assertThrows ( errMessage , NullPointerException . class , ( ) - > {
group . partitionTimestamp ( randomPartition ) ;
} ) ;
public void shouldThrowIllegalStateExceptionUponSetPartitionTimestampIfPartitionUnknown ( ) {
final IllegalStateException exception = assertThrows (
IllegalStateException . class ,
( ) - > group . setPartitionTime ( unknownPartition , 0L ) ) ;
assertThat ( errMessage , equalTo ( exception . getMessage ( ) ) ) ;
}
@Test
public void shouldThrowIllegalStateExceptionUponGetPartitionTimestampIfPartitionUnknown ( ) {
final IllegalStateException exception = assertThrows (
IllegalStateException . class ,
( ) - > group . partitionTimestamp ( unknownPartition ) ) ;
assertThat ( errMessage , equalTo ( exception . getMessage ( ) ) ) ;
}
@Test
public void shouldThrowIllegalStateExceptionUponGetHeadRecordOffsetIfPartitionUnknown ( ) {
final IllegalStateException exception = assertThrows (
IllegalStateException . class ,
( ) - > group . headRecordOffset ( unknownPartition ) ) ;
assertThat ( errMessage , equalTo ( exception . getMessage ( ) ) ) ;
}
@Test
public void shouldEmpyPartitionsOnClean ( ) {
final List < ConsumerRecord < byte [ ] , byte [ ] > > list = Arrays . asList (
new ConsumerRecord < > ( "topic" , 1 , 1L , recordKey , recordValue ) ,
new ConsumerRecord < > ( "topic" , 1 , 3L , recordKey , recordValue ) ,
new ConsumerRecord < > ( "topic" , 1 , 5L , recordKey , recordValue ) ) ;
group . addRawRecords ( partition1 , list ) ;
group . clear ( ) ;
assertThat ( group . numBuffered ( ) , equalTo ( 0 ) ) ;
assertThat ( group . streamTime ( ) , equalTo ( RecordQueue . UNKNOWN ) ) ;
assertThat ( group . nextRecord ( new PartitionGroup . RecordInfo ( ) ) , equalTo ( null ) ) ;
group . addRawRecords ( partition1 , list ) ;
}
public void shouldCleanPartitionsOnClose ( ) {
final List < ConsumerRecord < byte [ ] , byte [ ] > > list = Arrays . asList (
new ConsumerRecord < > ( "topic" , 1 , 1L , recordKey , recordValue ) ,
new ConsumerRecord < > ( "topic" , 1 , 3L , recordKey , recordValue ) ,
new ConsumerRecord < > ( "topic" , 1 , 5L , recordKey , recordValue ) ) ;
group . addRawRecords ( partition1 , list ) ;
group . close ( ) ;
assertThat ( group . numBuffered ( ) , equalTo ( 0 ) ) ;
assertThat ( group . streamTime ( ) , equalTo ( RecordQueue . UNKNOWN ) ) ;
assertThat ( group . nextRecord ( new PartitionGroup . RecordInfo ( ) ) , equalTo ( null ) ) ;
final IllegalStateException exception = assertThrows (
IllegalStateException . class ,
( ) - > group . addRawRecords ( partition1 , list ) ) ;
assertThat ( "Partition topic-1 not found." , equalTo ( exception . getMessage ( ) ) ) ;
}
}