@@ -269,8 +269,12 @@ def _read_msg(
269269 "payload, but instead read {}" .format (length , pos - start_pos ))
270270 self ._pos = pos
271271
272- return DefaultRecord (
273- offset , timestamp , self .timestamp_type , key , value , headers )
272+ if self .is_control_batch :
273+ return ControlRecord (
274+ offset , timestamp , self .timestamp_type , key , value , headers )
275+ else :
276+ return DefaultRecord (
277+ offset , timestamp , self .timestamp_type , key , value , headers )
274278
275279 def __iter__ (self ):
276280 self ._maybe_uncompress ()
@@ -362,6 +366,45 @@ def __repr__(self):
362366 )
363367
364368
369+ class ControlRecord (DefaultRecord ):
370+ __slots__ = ("_offset" , "_timestamp" , "_timestamp_type" , "_key" , "_value" ,
371+ "_headers" , "_version" , "_type" )
372+
373+ KEY_STRUCT = struct .Struct (
374+ ">h" # Current Version => Int16
375+ "h" # Type => Int16 (0 indicates an abort marker, 1 indicates a commit)
376+ )
377+
378+ def __init__ (self , offset , timestamp , timestamp_type , key , value , headers ):
379+ super (ControlRecord , self ).__init__ (offset , timestamp , timestamp_type , key , value , headers )
380+ (self ._version , self ._type ) = self .KEY_STRUCT .unpack (self ._key )
381+
382+ # see https://kafka.apache.org/documentation/#controlbatch
383+ @property
384+ def version (self ):
385+ return self ._version
386+
387+ @property
388+ def type (self ):
389+ return self ._type
390+
391+ @property
392+ def abort (self ):
393+ return self ._type == 0
394+
395+ @property
396+ def commit (self ):
397+ return self ._type == 1
398+
399+ def __repr__ (self ):
400+ return (
401+ "ControlRecord(offset={!r}, timestamp={!r}, timestamp_type={!r},"
402+ " version={!r}, type={!r} <{!s}>)" .format (
403+ self ._offset , self ._timestamp , self ._timestamp_type ,
404+ self ._version , self ._type , "abort" if self .abort else "commit" )
405+ )
406+
407+
365408class DefaultRecordBatchBuilder (DefaultRecordBase , ABCRecordBatchBuilder ):
366409
367410 # excluding key, value and headers:
0 commit comments