
    i3                    t    S SK Jr  S SKJr  S SKJr  S SKrS SKJr  S SK	J
r
  S SKJr  S SKJr   " S	 S
5      rg)    )annotations)Sequence)cycleN)Producer)report_collection_diff)parse_tx_spec_config)WorkerControllerc                  
   \ rS rSrSrSSS jjr\SS j5       r\SS j5       r\SS j5       r	\SS j5       r
SS	 jr      SS
 jr S       SS jjrSS jr      SS jrSSS jjrSS jrS S jrS!S jrSS jrSrg)"LoadScheduling   a  Implement load scheduling across nodes.

This distributes the tests collected across all nodes so each test
is run just once.  All nodes collect and submit the test suite and
when all collections are received it is verified they are
identical collections.  Then the collection gets divided up in
chunks and chunks get submitted to nodes.  Whenever a node finishes
an item, it calls ``.mark_test_complete()`` which will trigger the
scheduler to assign more tests if the number of pending tests for
the node falls below a low-watermark.

When created, ``numnodes`` defines how many nodes are expected to
submit a collection. This is used to know when all nodes have
finished collection or how large the chunks need to be created.

Attributes::

:numnodes: The expected number of nodes taking part.  The actual
   number of nodes will vary during the scheduler's lifetime as
   nodes are added by the DSession as they are brought up and
   removed either because of a dead node or normal shutdown.  This
   number is primarily used to know when the initial collection is
   completed.

:node2collection: Map of nodes and their test collection.  All
   collections should always be identical.

:node2pending: Map of nodes and the indices of their pending
   tests.  The indices are an index into ``.pending`` (which is
   identical to their own collection stored in
   ``.node2collection``).

:collection: The one collection once it is validated to be
   identical between all the nodes.  It is initialised to None
   until ``.schedule()`` is called.

:pending: List of indices of globally pending tests.  These are
   tests which have not yet been allocated to a chunk for a node
   to process.

:log: A py.log.Producer instance.

:config: Config object, used for handling hooks.
Nc                   [        [        U5      5      U l        0 U l        0 U l        / U l        S U l        Uc  [        S5      U l        OUR                  U l        Xl
        U R                  R                  S5      U l        g )N	loadschedmaxschedchunk)lenr   numnodesnode2collectionnode2pendingpending
collectionr   logr   config	getoptionr   )selfr   r   s      S/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/xdist/scheduler/load.py__init__LoadScheduling.__init__<   sj    089BD?A"$,0;,DH}}DH![[22?C    c                H    [        U R                  R                  5       5      $ )z%A list of all nodes in the scheduler.)listr   keysr   s    r   nodesLoadScheduling.nodesI   s     D%%**,--r   c                F    [        U R                  5      U R                  :  $ )zBoolean indication initial test collection is complete.

This is a boolean indicating all initial participating nodes
have finished collection.  The required number of initial
nodes is defined by ``.numnodes``.
)r   r   r   r!   s    r   collection_is_completed&LoadScheduling.collection_is_completedN   s     4''(DMM99r   c                    U R                   (       d  gU R                  (       a  gU R                  R                  5        H  n[	        U5      S:  d  M    g   g)z9Return True if all tests have been executed by the nodes.F   T)r%   r   r   valuesr   r   r   s     r   tests_finishedLoadScheduling.tests_finishedX   sG     ++<<((//1G7|q  2 r   c                |    U R                   (       a  gU R                  R                  5        H  nU(       d  M    g   g)zReturn True if there are pending test items.

This indicates that collection has finished and nodes are
still processing test items, so this can be thought of as
"the scheduler is active".
TF)r   r   r)   r*   s     r   has_pendingLoadScheduling.has_pendingd   s4     <<((//1Gw 2 r   c                D    XR                   ;  d   e/ U R                   U'   g)zAdd a new node to the scheduler.

From now on the node will be allocated chunks of tests to
execute.

Called by the ``DSession.worker_workerready`` hook when it
successfully bootstraps a new node.
N)r   )r   nodes     r   add_nodeLoadScheduling.add_nodes   s&     ,,,,,"$$r   c                   XR                   ;   d   eU R                  (       a  U R                  (       d   eX R                  :w  a}  [        [	        U R
                  R                  5       5      5      n[        U R                  X#R                  R                  UR                  R                  5      nU R                  U5        g[        U5      U R
                  U'   g)zAdd the collected test items from a node.

The collection is stored in the ``.node2collection`` map.
Called by the ``DSession.worker_collectionfinish`` hook.
N)r   r%   r   nextiterr   r    r   gatewayidr   r   )r   r1   r   
other_nodemsgs        r   add_node_collection"LoadScheduling.add_node_collection   s     ((((('' ??"?__,!$t';';'@'@'B"CD
,OOZ1C1C1F1F %)*%5T"r   c                ^    U R                   U   R                  U5        U R                  XS9  g)zMark test item as completed by node.

The duration it took to execute the item is used as a hint to
the scheduler.

This is called by the ``DSession.worker_testreport`` hook.
)durationN)r   removecheck_schedule)r   r1   
item_indexr>   s       r   mark_test_complete!LoadScheduling.mark_test_complete   s.     	$&&z2D4r   c                    U R                   c   eU R                  R                  SU R                   R                  U5      5        U R                   H  nU R                  U5        M     g )Nr   )r   r   insertindexr   r@   )r   itemr1   s      r   mark_test_pending LoadScheduling.mark_test_pending   sY    ***OO!!$'	
 %%D% &r   c                    [        5       eN)NotImplementedError)r   r1   indicess      r   remove_pending_tests_from_node-LoadScheduling.remove_pending_tests_from_node   s    
 "##r   c                r   UR                   (       a  gU R                  (       a  [        U R                  5      n[	        S[        U R                  5      U-  S-  5      n[	        S[        U R                  5      U-  S-  5      nU R                  U   n[        U5      U:  aa  US:  a  [        U5      S:  a  gU[        U5      -
  n[	        S[        U5      -
  U R
                  5      nU R                  U[        Xx5      5        OUR                  5         U R                  S[        U R                  5      5        g)a  Maybe schedule new items on the node.

If there are any globally pending nodes left then this will
check if the given node should be given any more tests.  The
``duration`` of the last test is optionally used as a
heuristic to influence how many tests the node is assigned.
Nr(      g?znum items waiting for node:)
shutting_downr   r   r   maxr   _send_testsminshutdownr   )	r   r1   r>   	num_nodesitems_per_node_minitems_per_node_maxnode_pendingnum_sendr   s	            r   r@   LoadScheduling.check_schedule   s    <<D--.I "%QDLL(9Y(F!(K!L!$QDLL(9Y(F!(K!L,,T2L< #55s?s<'8A'= -L0AA #AL(9$94;M;M N  s8'CDMMO.DLL0ABr   c                $   U R                   R                  U5      nU(       d  gU R                  c   eU R                  UR                  S5         nU R                  R	                  U5        U R                    H  nU R                  U5        M     U$ )a  Remove a node from the scheduler.

This should be called either when the node crashed or at
shutdown time.  In the former case any pending items assigned
to the node will be re-scheduled.  Called by the
``DSession.worker_workerfinished`` and
``DSession.worker_errordown`` hooks.

Return the item which was being executing while the node
crashed or None if the node has no more pending items.

Nr   )r   popr   r   extendr@   )r   r1   r   	crashitems       r   remove_nodeLoadScheduling.remove_node   s~     ##''- ***OOGKKN3	G$%%D% &r   c                b   U R                   (       d   eU R                  b%  U R                   H  nU R                  U5        M     gU R	                  5       (       d  U R                  S5        g[        [        U R                  R                  5       5      5      U l        [        [        U R                  5      5      U R                  SS& U R                  (       d  gU R                  c  [        U R                  5      U l        [        U R                  5      S[        U R                  5      -  :  aV  [        U R                  5      n[        [        U R                  5      5       H  nU R                  [        U5      S5        M      Ot[        U R                  5      [        U R                   5      -  n[#        US-  U R                  5      n[%        US5      nU R                   H  nU R                  X5        M     U R                  (       d$  U R                   H  nUR'                  5         M     gg)a`  Initiate distribution of the test collection.

Initiate scheduling of the items across the nodes.  If this
gets called again later it behaves the same as calling
``.check_schedule()`` on all nodes so that newly added nodes
will start to be used.

This is called by the ``DSession.worker_collectionfinish`` hook
if ``.collection_is_completed`` is True.
Nz+**Different tests collected, aborting run**r(      rQ   )r%   r   r"   r@   !_check_nodes_have_same_collectionr   r5   r6   r   r)   ranger   r   r   r   rT   r   rU   rS   rV   )r   r1   r"   _items_per_nodenode_chunksizes         r   scheduleLoadScheduling.schedule   s    ++++ ??&

##D) # 5577HHBC tD$8$8$?$?$ABCDOO 45Q%!$T__!5D
 t||q3tzz?22
 $**%E3t||,-  ea0 . !1S9J9J5KKN 1!4d6H6HIN 3N

  6 # ||

 # r   c                    U R                   S U nU(       a?  U R                   S U2	 U R                  U   R                  U5        UR                  U5        g g rK   )r   r   r_   send_runtest_some)r   r1   numtests_per_nodes       r   rT   LoadScheduling._send_tests.  sQ    ds+TcT"d#**>:"">2 r   c                   [        U R                  R                  5       5      nUS   u  p#SnUSS  H  u  pV[        X6UR                  R
                  UR                  R
                  5      nU(       d  MC  SnU R                  U5        U R                  c  Me  [        R                  " UR                  R
                  SU/ S9nU R                  R                  R                  US9  M     U$ )	zReturn True if all nodes have collected the same items.

If collections differ, this method returns False while logging
the collection differences and posting collection errors to
pytest_collectreport hook.
r   Trd   NFfailed)nodeidoutcomelongreprresult)report)r   r   itemsr   r7   r8   r   r   pytestCollectReporthookpytest_collectreport)	r   node_collection_items
first_nodecolsame_collectionr1   r   r:   reps	            r   re   0LoadScheduling._check_nodes_have_same_collection5  s     !%T%9%9%?%?%A B/2
 5ab 9D(!3!3!6!6C s"';;* ..#|| (!$!	C KK$$999E !:  r   )r   r   r   r   r   r   r   r   rK   )r   zpytest.Configr   zProducer | NonereturnNone)r   zlist[WorkerController])r   bool)r1   r	   r   r   )r1   r	   r   zSequence[str]r   r   )r   )r1   r	   rA   intr>   floatr   r   )rG   strr   r   )r1   r	   rM   zSequence[int]r   r   )r1   r	   r>   r   r   r   )r1   r	   r   z
str | None)r   r   )r1   r	   rn   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   propertyr"   r%   r+   r.   r2   r;   rB   rH   rN   r@   ra   rj   rT   re   __static_attributes__ r   r   r   r      s    +ZD . . : : 	 	  
%6$62?6	60 JK5$5255AF5	5&$$ $ 
	$ CD2> @3r   r   )
__future__r   collections.abcr   	itertoolsr   ry   xdist.remoter   xdist.reportr   xdist.workermanager   r	   r   r   r   r   <module>r      s)    " $   ! / 3 /A Ar   