@@ -660,57 +660,70 @@ public ProcessContinuation processElement(
660660
661661 // Visible progress within the consumer polling timeout.
662662 // Partially or fully claim and process records in this batch.
663- for (ConsumerRecord <byte [], byte []> rawRecord : rawRecords ) {
664- if (!tracker .tryClaim (rawRecord .offset ())) {
665- consumer .seek (topicPartition , rawRecord .offset ());
666- consumer .pause (Collections .singleton (topicPartition ));
663+ long rawSizesSum = 0L ;
664+ long rawSizesCount = 0L ;
665+ long rawSizesMin = Long .MAX_VALUE ;
666+ long rawSizesMax = Long .MIN_VALUE ;
667+ try {
668+ for (ConsumerRecord <byte [], byte []> rawRecord : rawRecords ) {
669+ if (!tracker .tryClaim (rawRecord .offset ())) {
670+ consumer .seek (topicPartition , rawRecord .offset ());
671+ consumer .pause (Collections .singleton (topicPartition ));
667672
668- return ProcessContinuation .stop ();
669- }
670- expectedOffset = rawRecord .offset () + 1 ;
671- try {
672- KafkaRecord <K , V > kafkaRecord =
673- new KafkaRecord <>(
674- rawRecord .topic (),
675- rawRecord .partition (),
676- rawRecord .offset (),
677- ConsumerSpEL .getRecordTimestamp (rawRecord ),
678- ConsumerSpEL .getRecordTimestampType (rawRecord ),
679- ConsumerSpEL .hasHeaders () ? rawRecord .headers () : null ,
680- ConsumerSpEL .deserializeKey (keyDeserializerInstance , rawRecord ),
681- ConsumerSpEL .deserializeValue (valueDeserializerInstance , rawRecord ));
682- int recordSize =
683- (rawRecord .key () == null ? 0 : rawRecord .key ().length )
684- + (rawRecord .value () == null ? 0 : rawRecord .value ().length );
685- avgRecordSize .update (recordSize );
686- rawSizes .update (recordSize );
687- Instant outputTimestamp ;
688- // The outputTimestamp and watermark will be computed by timestampPolicy, where the
689- // WatermarkEstimator should be a manual one.
690- if (timestampPolicy != null ) {
691- TimestampPolicyContext context =
692- updateWatermarkManually (timestampPolicy , watermarkEstimator , tracker );
693- outputTimestamp = timestampPolicy .getTimestampForRecord (context , kafkaRecord );
694- } else {
695- Preconditions .checkStateNotNull (this .extractOutputTimestampFn );
696- outputTimestamp = extractOutputTimestampFn .apply (kafkaRecord );
673+ return ProcessContinuation .stop ();
697674 }
698- receiver
699- .get (recordTag )
700- .outputWithTimestamp (KV .of (kafkaSourceDescriptor , kafkaRecord ), outputTimestamp );
701- } catch (SerializationException e ) {
702- // This exception should only occur during the key and value deserialization when
703- // creating the Kafka Record
704- badRecordRouter .route (
705- receiver ,
706- rawRecord ,
707- null ,
708- e ,
709- "Failure deserializing Key or Value of Kakfa record reading from Kafka" );
710- if (timestampPolicy != null ) {
711- updateWatermarkManually (timestampPolicy , watermarkEstimator , tracker );
675+ expectedOffset = rawRecord .offset () + 1 ;
676+ try {
677+ KafkaRecord <K , V > kafkaRecord =
678+ new KafkaRecord <>(
679+ rawRecord .topic (),
680+ rawRecord .partition (),
681+ rawRecord .offset (),
682+ ConsumerSpEL .getRecordTimestamp (rawRecord ),
683+ ConsumerSpEL .getRecordTimestampType (rawRecord ),
684+ ConsumerSpEL .hasHeaders () ? rawRecord .headers () : null ,
685+ ConsumerSpEL .deserializeKey (keyDeserializerInstance , rawRecord ),
686+ ConsumerSpEL .deserializeValue (valueDeserializerInstance , rawRecord ));
687+ int recordSize =
688+ (rawRecord .key () == null ? 0 : rawRecord .key ().length )
689+ + (rawRecord .value () == null ? 0 : rawRecord .value ().length );
690+ rawSizesSum = rawSizesSum + recordSize ;
691+ rawSizesCount = rawSizesCount + 1L ;
692+ rawSizesMin = Math .min (rawSizesMin , recordSize );
693+ rawSizesMax = Math .max (rawSizesMax , recordSize );
694+ Instant outputTimestamp ;
695+ // The outputTimestamp and watermark will be computed by timestampPolicy, where the
696+ // WatermarkEstimator should be a manual one.
697+ if (timestampPolicy != null ) {
698+ TimestampPolicyContext context =
699+ updateWatermarkManually (timestampPolicy , watermarkEstimator , tracker );
700+ outputTimestamp = timestampPolicy .getTimestampForRecord (context , kafkaRecord );
701+ } else {
702+ Preconditions .checkStateNotNull (this .extractOutputTimestampFn );
703+ outputTimestamp = extractOutputTimestampFn .apply (kafkaRecord );
704+ }
705+ receiver
706+ .get (recordTag )
707+ .outputWithTimestamp (KV .of (kafkaSourceDescriptor , kafkaRecord ), outputTimestamp );
708+ } catch (SerializationException e ) {
709+ // This exception should only occur during the key and value deserialization when
710+ // creating the Kafka Record
711+ badRecordRouter .route (
712+ receiver ,
713+ rawRecord ,
714+ null ,
715+ e ,
716+ "Failure deserializing Key or Value of Kakfa record reading from Kafka" );
717+ if (timestampPolicy != null ) {
718+ updateWatermarkManually (timestampPolicy , watermarkEstimator , tracker );
719+ }
712720 }
713721 }
722+ } finally {
723+ if (rawSizesCount > 0L ) {
724+ avgRecordSize .update (rawSizesSum , rawSizesCount );
725+ rawSizes .update (rawSizesSum , rawSizesCount , rawSizesMin , rawSizesMax );
726+ }
714727 }
715728
716729 // Non-visible progress within the consumer polling timeout.
0 commit comments