@ -19,9 +19,10 @@ package kafka.log
@@ -19,9 +19,10 @@ package kafka.log
import java.nio.ByteBuffer
import java.util.concurrent.TimeUnit
import kafka.api. { ApiVersion , KAFKA_2_0_IV1 }
import kafka.common.LongRef
import kafka.message. { CompressionCodec , DefaultCompressionCodec , GZIPCompressionCodec , NoCompressionCodec , SnappyCompressionCodec }
import org.apache.kafka.common.errors. { InvalidTimestampException , UnsupportedForMessageFormatException }
import kafka.message._
import org.apache.kafka.common.errors. { InvalidTimestampException , UnsupportedCompressionTypeException , Unsupported ForMessageFormatException }
import org.apache.kafka.common.record._
import org.apache.kafka.common.utils.Time
import org.apache.kafka.test.TestUtils
@ -55,7 +56,8 @@ class LogValidatorTest {
@@ -55,7 +56,8 @@ class LogValidatorTest {
timestampType = TimestampType . LOG_APPEND_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
val validatedRecords = validatedResults . validatedRecords
assertEquals ( "message set size should not change" , records . records . asScala . size , validatedRecords . records . asScala . size )
validatedRecords . batches . asScala . foreach ( batch => validateLogAppendTime ( now , 1234L , batch ) )
@ -92,7 +94,8 @@ class LogValidatorTest {
@@ -92,7 +94,8 @@ class LogValidatorTest {
timestampType = TimestampType . LOG_APPEND_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
val validatedRecords = validatedResults . validatedRecords
assertEquals ( "message set size should not change" , records . records . asScala . size , validatedRecords . records . asScala . size )
@ -133,7 +136,8 @@ class LogValidatorTest {
@@ -133,7 +136,8 @@ class LogValidatorTest {
timestampType = TimestampType . LOG_APPEND_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
val validatedRecords = validatedResults . validatedRecords
assertEquals ( "message set size should not change" , records . records . asScala . size ,
@ -190,7 +194,8 @@ class LogValidatorTest {
@@ -190,7 +194,8 @@ class LogValidatorTest {
timestampType = TimestampType . LOG_APPEND_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
}
@Test
@ -231,7 +236,8 @@ class LogValidatorTest {
@@ -231,7 +236,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = partitionLeaderEpoch ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
val validatedRecords = validatingResults . validatedRecords
var i = 0
@ -297,7 +303,8 @@ class LogValidatorTest {
@@ -297,7 +303,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = partitionLeaderEpoch ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
val validatedRecords = validatingResults . validatedRecords
var i = 0
@ -347,7 +354,8 @@ class LogValidatorTest {
@@ -347,7 +354,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
val validatedRecords = validatedResults . validatedRecords
for ( batch <- validatedRecords . batches . asScala ) {
@ -388,7 +396,8 @@ class LogValidatorTest {
@@ -388,7 +396,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
val validatedRecords = validatedResults . validatedRecords
for ( batch <- validatedRecords . batches . asScala ) {
@ -442,7 +451,8 @@ class LogValidatorTest {
@@ -442,7 +451,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = partitionLeaderEpoch ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
val validatedRecords = validatedResults . validatedRecords
var i = 0
@ -492,7 +502,8 @@ class LogValidatorTest {
@@ -492,7 +502,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
}
@Test ( expected = classOf [ InvalidTimestampException ] )
@ -512,7 +523,8 @@ class LogValidatorTest {
@@ -512,7 +523,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
}
@Test ( expected = classOf [ InvalidTimestampException ] )
@ -532,7 +544,8 @@ class LogValidatorTest {
@@ -532,7 +544,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
}
@Test ( expected = classOf [ InvalidTimestampException ] )
@ -552,7 +565,8 @@ class LogValidatorTest {
@@ -552,7 +565,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
}
@Test
@ -571,7 +585,8 @@ class LogValidatorTest {
@@ -571,7 +585,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test
@ -590,7 +605,8 @@ class LogValidatorTest {
@@ -590,7 +605,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test
@ -610,7 +626,8 @@ class LogValidatorTest {
@@ -610,7 +626,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords
checkOffsets ( messageWithOffset , offset )
}
@ -631,7 +648,8 @@ class LogValidatorTest {
@@ -631,7 +648,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords
checkOffsets ( messageWithOffset , offset )
}
@ -653,7 +671,8 @@ class LogValidatorTest {
@@ -653,7 +671,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords
checkOffsets ( compressedMessagesWithOffset , offset )
}
@ -675,7 +694,8 @@ class LogValidatorTest {
@@ -675,7 +694,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords
checkOffsets ( compressedMessagesWithOffset , offset )
}
@ -695,7 +715,8 @@ class LogValidatorTest {
@@ -695,7 +715,8 @@ class LogValidatorTest {
timestampType = TimestampType . LOG_APPEND_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
checkOffsets ( validatedResults . validatedRecords , offset )
verifyRecordConversionStats ( validatedResults . recordConversionStats , numConvertedRecords = 3 , records ,
compressed = false )
@ -717,7 +738,8 @@ class LogValidatorTest {
@@ -717,7 +738,8 @@ class LogValidatorTest {
timestampType = TimestampType . LOG_APPEND_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
checkOffsets ( validatedResults . validatedRecords , offset )
verifyRecordConversionStats ( validatedResults . recordConversionStats , numConvertedRecords = 3 , records ,
compressed = false )
@ -739,7 +761,8 @@ class LogValidatorTest {
@@ -739,7 +761,8 @@ class LogValidatorTest {
timestampType = TimestampType . LOG_APPEND_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
checkOffsets ( validatedResults . validatedRecords , offset )
verifyRecordConversionStats ( validatedResults . recordConversionStats , numConvertedRecords = 3 , records ,
compressed = true )
@ -761,7 +784,8 @@ class LogValidatorTest {
@@ -761,7 +784,8 @@ class LogValidatorTest {
timestampType = TimestampType . LOG_APPEND_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
checkOffsets ( validatedResults . validatedRecords , offset )
verifyRecordConversionStats ( validatedResults . recordConversionStats , numConvertedRecords = 3 , records ,
compressed = true )
@ -783,7 +807,8 @@ class LogValidatorTest {
@@ -783,7 +807,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
}
@Test
@ -802,7 +827,8 @@ class LogValidatorTest {
@@ -802,7 +827,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = false )
isFromClient = false ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
val batches = TestUtils . toList ( result . validatedRecords . batches )
assertEquals ( 1 , batches . size )
val batch = batches . get ( 0 )
@ -826,7 +852,8 @@ class LogValidatorTest {
@@ -826,7 +852,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test
@ -846,7 +873,8 @@ class LogValidatorTest {
@@ -846,7 +873,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test
@ -865,7 +893,8 @@ class LogValidatorTest {
@@ -865,7 +893,8 @@ class LogValidatorTest {
timestampType = TimestampType . LOG_APPEND_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test
@ -884,7 +913,8 @@ class LogValidatorTest {
@@ -884,7 +913,8 @@ class LogValidatorTest {
timestampType = TimestampType . LOG_APPEND_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test
@ -904,7 +934,8 @@ class LogValidatorTest {
@@ -904,7 +934,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test
@ -924,7 +955,8 @@ class LogValidatorTest {
@@ -924,7 +955,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test ( expected = classOf [ UnsupportedForMessageFormatException ] )
@ -946,7 +978,8 @@ class LogValidatorTest {
@@ -946,7 +978,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test ( expected = classOf [ UnsupportedForMessageFormatException ] )
@ -968,7 +1001,8 @@ class LogValidatorTest {
@@ -968,7 +1001,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test
@ -988,7 +1022,8 @@ class LogValidatorTest {
@@ -988,7 +1022,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test
@ -1008,7 +1043,8 @@ class LogValidatorTest {
@@ -1008,7 +1043,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ) . validatedRecords , offset )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion ) . validatedRecords , offset )
}
@Test ( expected = classOf [ InvalidRecordException ] )
@ -1026,7 +1062,8 @@ class LogValidatorTest {
@@ -1026,7 +1062,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
}
@Test ( expected = classOf [ InvalidRecordException ] )
@ -1034,6 +1071,26 @@ class LogValidatorTest {
@@ -1034,6 +1071,26 @@ class LogValidatorTest {
testBatchWithoutRecordsNotAllowed ( DefaultCompressionCodec , DefaultCompressionCodec )
}
@Test ( expected = classOf [ UnsupportedCompressionTypeException ] )
def testZStdCompressedWithUnavailableIBPVersion ( ) : Unit = {
val now = System . currentTimeMillis ( )
// The timestamps should be overwritten
val records = createRecords ( magicValue = RecordBatch . MAGIC_VALUE_V2 , timestamp = 1234L , codec = CompressionType . NONE )
LogValidator . validateMessagesAndAssignOffsets ( records ,
offsetCounter = new LongRef ( 0 ) ,
time = time ,
now = now ,
sourceCodec = NoCompressionCodec ,
targetCodec = ZStdCompressionCodec ,
compactedTopic = false ,
magic = RecordBatch . MAGIC_VALUE_V2 ,
timestampType = TimestampType . LOG_APPEND_TIME ,
timestampDiffMaxMs = 1000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true ,
interBrokerProtocolVersion = KAFKA_2_0_IV1 )
}
@Test ( expected = classOf [ InvalidRecordException ] )
def testUncompressedBatchWithoutRecordsNotAllowed ( ) : Unit = {
testBatchWithoutRecordsNotAllowed ( NoCompressionCodec , NoCompressionCodec )
@ -1065,7 +1122,8 @@ class LogValidatorTest {
@@ -1065,7 +1122,8 @@ class LogValidatorTest {
timestampType = TimestampType . CREATE_TIME ,
timestampDiffMaxMs = 5000L ,
partitionLeaderEpoch = RecordBatch . NO_PARTITION_LEADER_EPOCH ,
isFromClient = true )
isFromClient = true ,
interBrokerProtocolVersion = ApiVersion . latestVersion )
}
private def createRecords ( magicValue : Byte ,