@@ -335,8 +335,6 @@ async def _start(self, stream: IGrpcWrapperAsyncIO, init_message: StreamReadMess
335
335
self ._started = True
336
336
self ._stream = stream
337
337
338
- print (init_message )
339
-
340
338
stream .write (StreamReadMessage .FromClient (client_message = init_message ))
341
339
init_response = await stream .receive () # type: StreamReadMessage.FromServer
342
340
if isinstance (init_response .server_message , StreamReadMessage .InitResponse ):
@@ -390,6 +388,14 @@ def _get_first_batch(self) -> typing.Tuple[int, datatypes.PublicBatch]:
390
388
partition_session_id , batch = self ._message_batches .popitem (last = False )
391
389
return partition_session_id , batch
392
390
391
+ def _return_batch_to_queue (self , part_sess_id : int , batch : datatypes .PublicBatch ):
392
+ self ._message_batches [part_sess_id ] = batch
393
+
394
+ # In case of auto-split we should return all parent messages ASAP
395
+ # without queue rotation to prevent child's messages before parent's.
396
+ if self ._partition_sessions [part_sess_id ].ended :
397
+ self ._message_batches .move_to_end (part_sess_id , last = False )
398
+
393
399
def receive_batch_nowait (self , max_messages : Optional [int ] = None ):
394
400
if self ._get_first_error ():
395
401
raise self ._get_first_error ()
@@ -405,7 +411,8 @@ def receive_batch_nowait(self, max_messages: Optional[int] = None):
405
411
406
412
cutted_batch = batch ._pop_batch (message_count = max_messages )
407
413
408
- self ._message_batches [part_sess_id ] = batch
414
+ self ._return_batch_to_queue (part_sess_id , batch )
415
+
409
416
self ._buffer_release_bytes (cutted_batch ._bytes_size )
410
417
411
418
return cutted_batch
@@ -425,7 +432,7 @@ def receive_message_nowait(self):
425
432
self ._buffer_release_bytes (batch ._bytes_size )
426
433
else :
427
434
# TODO: we should somehow release bytes from single message as well
428
- self ._message_batches [ part_sess_id ] = batch
435
+ self ._return_batch_to_queue ( part_sess_id , batch )
429
436
430
437
return message
431
438
@@ -584,13 +591,15 @@ def _on_partition_session_stop(self, message: StreamReadMessage.StopPartitionSes
584
591
)
585
592
586
593
def _on_end_partition_session (self , message : StreamReadMessage .EndPartitionSession ):
587
- logger .info (
588
- f"End partition session with id: { message .partition_session_id } , child partitions: { message .child_partition_ids } "
594
+ logger .debug (
595
+ f"End partition session with id: { message .partition_session_id } , "
596
+ f"child partitions: { message .child_partition_ids } "
589
597
)
590
598
591
- print (
592
- f"End partition session with id: { message .partition_session_id } , child partitions: { message .child_partition_ids } "
593
- )
599
+ if message .partition_session_id in self ._partition_sessions :
600
+ # Mark partition session as ended not to shuffle messages.
601
+ self ._partition_sessions [message .partition_session_id ].end ()
602
+
594
603
595
604
def _on_read_response (self , message : StreamReadMessage .ReadResponse ):
596
605
self ._buffer_consume_bytes (message .bytes_size )
0 commit comments