9Mjg"dZddlZddlZddlZddlmZmZddlmZddlm Z m Z m Z ddl m Z mZmZGddZdS) z[ This module contains FaultDetector class, aimed to track throttling for incoming requests N)datetime timedelta)RLock)OptionalTupleAny) drop_after check_periodthrottling_thresholdc ZeZdZdZdZdZdZdedede e effdZ e defd Z e defd Ze defd Ze de fd Zed edefdZdedefdZddededeeddfdZdeddfdZdedede e effdZddZddZdefdZdS) FaultDetectorz Fault Detector class i'ictjd|_t|_t jt|_t|_ t|_ d|_ dS)Nfault_detectorz$/usr/share/alt-php-xray/flush.latest) logging getLoggerloggerdictthrottling_stat collections defaultdictint_uid_entry_count _entry_uidrlocktime_marker_fileselfs P/opt/cloudlinux/venv/lib64/python3.11/site-packages/xray/agent/fault_detector.py__init__zFaultDetector.__init__%sZ'(899 #vv!, 7 < <&&GG Ft_key cpu_valuereturnc|j5 |j|\}}|||\}}||n#t$rd\}}YnwxYwdddn #1swxYwY||fS)z) Perform fault detection )FrN)rr_was_throttled_since _flush_entryKeyError)rr!r" saved_cpu_hitting_limitsthrottled_times r__call__zFaultDetector.__call__0sY ) ) )#3E: 1261J1Jy2*2*.!!%((((  : : :19. : ) ) ) ) ) ) ) ) ) ) ) ) ) ) )~--s3A*A /A* AA*AA**A.1A.cBttjS)z( Current Unix timestamp )rtimers r timestampzFaultDetector.timestamp?s 49;;r c6|tS)z8 Drop after value as datetime.timedelta )timedelta_in_minutesr rs r _drop_afterzFaultDetector._drop_afterFs ((444r c6|tS)z: Check period value as datetime.timedelta )r1r rs r _check_periodzFaultDetector._check_periodMs ((666r c t|j5}t|}dddn #1swxYwY|||jkS#ttf$r7|j 5| dddn #1swxYwYYdSwxYw)ze If throttling_stat should be flushed right now regarding check period value NF) openrrreadstrip current_deltar4OSError ValueErrorr_update_latest_flush)r latest_infolatest_flush_times r should_flushzFaultDetector.should_flushTsS  d+,, D $' (8(8(:(:(@(@(B(B$C$C! D D D D D D D D D D D D D D D%%&7884;MM M$    , ,))+++ , , , , , , , , , , , , , , ,55 sXA?4A A?AA?A A??CB8, C8B< <C?B< CCminsc"t|S)zB Get timedelta object for given number of minutes )minutes)r)r@s rr1z"FaultDetector.timedelta_in_minutescs &&&&r time_stampc^dtdtfd}||j||z S)zQ Calculate timedelta for given time_stamp from current timestamp tsr#c*tj|S)z> Make a datetime object from ts (int) )r fromtimestamp)rEs r_castz*FaultDetector.current_delta.._castos)"-- -r )rrr/)rrCrHs rr9zFaultDetector.current_deltajsG  .c .h . . . . uT^$$uuZ'8'888r Nkeycpuuidc|j}|j5||jv}|rU|S|j|d|jkr/|jd|j| ddddS|rXt|j|j kr;|jdt|j ddddS|j d|||||f|j|<|r!||j |<||j|xxdz cc<|j dt|jddddS#1swxYwYdS)a Save throttling entry for given key. :param uid: SO_PEERCRED uid of the connecting peer (when known). A per-UID quota is enforced so one uid's request-init flood cannot saturate the shared cap and starve other tenants. NrzEthrottling_stat per-uid quota (%d) reached for uid=%s, dropping entryz0throttling_stat at capacity (%d), dropping entryz(Adding new throttling entry %s: (%i, %i)Throttling entries total %i) r/rrrget_MAX_ENTRIES_PER_UIDrwarninglen _MAX_ENTRIESdebugr)rrIrJrKstampis_news rsavezFaultDetector.savews Y 9 9 44F #/-11#q99T=VVV ##%=>B>WY\^^^ 9 9 9 9 9 9 9 9 #d233t7HHH ##$V$'(<$=$=??? 9 9 9 9 9 9 9 9 K  H!3 / / /),e D  % 4(+$?)#...!3... K  ;!$"677 9 9 9/ 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9s AE0A E A9EEEc|j5|jd|||jv}|j|d|rj|j|d}|M|j|ddkr.|j|xxdzcc<|j|dkr|j|=|jdt|jddddS#1swxYwYdS)z6 Remove throttling entry by given key zAbandon throttling entry %srNrMrN) rrrTrpoprrrOrR)rrIexistedrKs rr&zFaultDetector._flush_entrysXY 9 9 K  ;S A A AT11G  $ $S! , , , 7o))#t44?t'<'@'@a'H'H1'L'L)#...!3...,S1Q66 1#6 K  ;!$"677 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9sCC..C25C2 initial_value current_valuec||z dz}|tk}|jdt||||dkr|ndfS)z Check current throttling time of given lve_id vs given initial_value Return: tuple(fact of throttling, throttling time in ms) i@Bz"Throttling checked with %i: %s, %ir)r rrT)rr[r\ spent_timethrottling_facts rr%z"FaultDetector._was_throttled_sinces\$m3? $';; >.): 7 7 7j1nn !CCr c|jr|j5|jdt |j|D]}||||jdt |jddddS#1swxYwYdSdS)z, Clear throttling stat dict zFlush started, total entries %iz Flush finished, total entries %iN) r?rrrTrRr_expired_entriesr&r<)rrIs rflushzFaultDetector.flushs   = = = !!"C"%d&:";";===0022++C%%c****))+++ !!"D"%d&:";";===  = = = = = = = = = = = = = = = = = = = =sBB77B;>B;c t|jd5}|t|jddddS#1swxYwYdS#t $r|jdYdSwxYw)z? Write current timestamp into time marker file wNz(Failed to save timestamp of latest flush)r6rwritestrr/r:rerror)rr=s rr<z"FaultDetector._update_latest_flushs Jd+S11 7[!!#dn"5"5666 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 J J J K  H I I I I I I Js3A(A  A AAAA$BBc t}|jD][\}}|\}}||}||jkr1|jd||||\|S)ze Collect expired throttling stat entries keys regarding the drop after value z%Expired entry detected %i: %s elapsed)listritemsr9r2rrTappend)rexpiredrIvaluer)r/_deltas rrazFaultDetector._expired_entriess &&.4466 $ $JC LAy'' 22F((( !!"I"%v///s###r )N)r#N)__name__ __module__ __qualname____doc__rSrPrrrrboolr,propertyr/rr2r4r? staticmethodr1r9rrWr&r%rbr<rirar rr r s<L  G G G .c .c .eD#I6F . . . . 3   X 5Y555X5 7y777X7  d   X '3'9'''\' 9 9 9 9 9 9!9!9!9#!9HSM!9T!9!9!9!9F999999$ D,/ D,/ D49$)4D D D D D = = = =JJJJ $      r r )rrrrr.rr threadingrtypingrrrxray.internal.constantsr r r r rvr rrzs ((((((((''''''''''RRRRRRRRRRJJJJJJJJJJr