o
    `j[                     @   sd   d dl mZ d dlZd dlZd dlmZ d dlmZm	Z	 e
eZG dd deZG dd dZdS )    )IntEnumN)FutureRecordMetadataFutureProduceResultc                   @   s   e Zd ZdZdZdZdS )
FinalStater         N)__name__
__module____qualname__ABORTEDFAILED	SUCCEEDED r   r   [/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/producer/producer_batch.pyr      s    r   c                   @   s   e Zd Zd(ddZedd Zedd Zedd	 Zed
d Zedd Z	edd Z
d(ddZdd Zdd Zdd Zd)ddZdd Zd(ddZdd Zd(d d!Zed"d# Zd$d% Zd&d' ZdS )*ProducerBatchNc                 C   sd   |d u rt  n|}d| _|| _d | _d| _|| _|| _|| _|| _	t
|| _g | _d| _d | _d S )Nr   F)time	monotonicmax_record_sizecreateddrainedattemptslast_attemptlast_appendrecordstopic_partitionr   produce_future_record_futures_retry_final_state)selftpr   nowr   r   r   __init__   s   

zProducerBatch.__init__c                 C      | j S N)r   r   r   r   r   final_state"   s   zProducerBatch.final_statec                 C   s
   | j  S r$   )r   next_offsetr%   r   r   r   record_count&   s   
zProducerBatch.record_countc                 C      | j r| j jS d S r$   )r   producer_idr%   r   r   r   r*   *      zProducerBatch.producer_idc                 C   r)   r$   )r   producer_epochr%   r   r   r   r,   .   r+   zProducerBatch.producer_epochc                 C   r)   r$   )r   base_sequencer%   r   r   r   r-   2   r+   zProducerBatch.base_sequencec                 C   s   | j }|d uo
|dkS )N)r-   )r   base_seqr   r   r   has_sequence6   s   zProducerBatch.has_sequencec              
   C   s   | j ||||}|d u rd S |d u rt n|}t| j|j| _|| _t| j	|j
|j|j|d ur5t|nd|d ur>t|nd|rJtdd |D nd}| j| |S )Nr.   c                 s   s*    | ]\}}t |d t | V  qdS )zutf-8N)lenencode).0h_keyh_valr   r   r   	<genexpr>J   s   ( z+ProducerBatch.try_append.<locals>.<genexpr>)r   appendr   r   maxr   sizer   r   r   offset	timestampcrcr1   sumr   )r   timestamp_mskeyvalueheadersr!   metadatafuturer   r   r   
try_append;   s"   zProducerBatch.try_appendc                    sL   | j durtd| j  tj| _ td| j  | dd fdd dS )z6Abort the batch and complete the future and callbacks.Nz3Batch has already been completed in final state: %sz#Aborting batch for partition %s: %sr.   c                    s    S r$   r   )_	exceptionr   r   <lambda>U   s    z%ProducerBatch.abort.<locals>.<lambda>)	r   ErrorsIllegalStateErrorr   r   logdebugr   _complete_future)r   rG   r   rF   r   abortN   s
   
zProducerBatch.abortc                 C   s   | j ||dS )as  Complete the batch successfully.

        Arguments:
            base_offset (int): The base offset of the messages assigned by the server
            log_append_time (int): The log append time or -1 if CreateTime is being used

        Returns: True if the batch was completed as a result of this call, and False
            if it had been completed previously.
        )base_offsetr>   )done)r   rO   log_append_timer   r   r   completeW   s   
zProducerBatch.completec                 C   s0   t |ts	tdt|std| j||dS )a  
        Complete the batch exceptionally. The provided top-level exception will be used
        for each record future contained in the batch.

        Arguments:
            top_level_exception (Exception): top-level partition error.
            record_exceptions_fn (callable int -> Exception): Record exception function mapping
                batch_index to the respective record exception.

        Raises:
            TypeError: if top_level_exception is not Exception,
                or record_exceptions_fn is not callable.

        Returns:
            True if the batch was completed as a result of this call,
            or False if it had been completed previously.
        z*top_level_exception must be type Exceptionz%record_exceptions_fn must be callable)top_level_exceptionrecord_exceptions_fn)
isinstance	Exception	TypeErrorcallablerP   )r   rS   rT   r   r   r   complete_exceptionallyc   s
   
z$ProducerBatch.complete_exceptionallyc                 C   s   |du rt jnt j}| jdu r2|| _|t ju r td| j| n	td| j|| | ||| dS | jt jurW|t ju rJtd|| j|| j dS td| j|| j| dS t	
d| j|f )	a  
        Finalize the state of a batch. Final state, once set, is immutable. This function may be called
        once or twice on a batch. It may be called twice if
            1. An inflight batch expires before a response from the broker is received. The batch's final
            state is set to FAILED. But it could succeed on the broker and second time around batch.done() may
            try to set SUCCEEDED final state.

            2. If a transaction abortion happens or if the producer is closed forcefully, the final state is
            ABORTED but again it could succeed if broker responds with a success.

        Attempted transitions from [FAILED | ABORTED] --> SUCCEEDED are logged.
        Attempted transitions from one failure state to the same or a different failed state are ignored.
        Attempted transitions from SUCCEEDED to the same or a failed state throw an exception.
        Nz8Successfully produced messages to %s with base offset %szHFailed to produce messages to topic-partition %s with base offset %s: %sTzWProduceResponse returned %s for %s after batch with base offset %s had already been %s.zBIgnored state transition %s -> %s for %s batch with base offset %sz6A %s batch must not attempt another state change to %sF)r   r   r   r   rK   rL   r   warningrM   rI   rJ   )r   rO   r>   rS   rT   r&   r   r   r   rP   {   s*   


	zProducerBatch.donec                 C   s(   | j jr	td| j |||f d S )NzBatch is already closed!)r   is_donerI   rJ   success)r   rO   r>   rT   r   r   r   rM      s   
zProducerBatch._complete_futurec                 C   s&   |d u rt  n|}|d || j kS )Ni  )r   r   r   )r   delivery_timeout_msr!   r   r   r   has_reached_delivery_timeout   s   z*ProducerBatch.has_reached_delivery_timeoutc                 C   r#   r$   )r   r%   r   r   r   in_retry   s   zProducerBatch.in_retryc                 C   s8   |d u rt  n|}d| _|  jd7  _|| _|| _d S )NTr   )r   r   r   r   r   r   )r   r!   r   r   r   retry   s
   
zProducerBatch.retryc                 C   s   | j jS r$   )r   r[   r%   r   r   r   r[      s   zProducerBatch.is_donec                 C   s   d| j | j f S )Nz2ProducerBatch(topic_partition=%s, record_count=%d))r   r   r'   r%   r   r   r   __str__   s   zProducerBatch.__str__c                 C   s   | j |j k S r$   )r   )r   otherr   r   r   __lt__   s   zProducerBatch.__lt__r$   )NNNN)r   r	   r
   r"   propertyr&   r(   r*   r,   r-   r0   rD   rN   rR   rY   rP   rM   r^   r_   r`   r[   ra   rc   r   r   r   r   r      s6    







	
(


r   )enumr   loggingr   kafka.errorserrorsrI   kafka.producer.futurer   r   	getLoggerr   rK   r   r   r   r   r   r   <module>   s    
