fj}tdZddlZddlZddlmZmZddlmZddlmZm Z ddl m Z m Z m Z GddZdS) z[ This module contains FaultDetector class, aimed to track throttling for incoming requests N)datetime timedelta)RLock)TupleAny) drop_after check_periodthrottling_thresholdcDeZdZdZdZdZdededee effdZ e defdZ e de fd Ze de fd Ze de fd Zed ede fd Zdede fdZdededdfdZdeddfdZdededee effdZddZddZdefdZdS) FaultDetectorz Fault Detector class i'ctjd|_t|_t |_d|_dS)Nfault_detectorz$/usr/share/alt-php-xray/flush.latest)logging getLoggerloggerdictthrottling_statrlocktime_marker_fileselfs P/opt/cloudlinux/venv/lib64/python3.11/site-packages/xray/agent/fault_detector.py__init__zFaultDetector.__init__s:'(899 #vvGG Ft_key cpu_valuereturnc|j5 |j|\}}|||\}}||n#t$rd\}}YnwxYwdddn #1swxYwY||fS)z) Perform fault detection )FrN)rr_was_throttled_since _flush_entryKeyError)rrr saved_cpu_hitting_limitsthrottled_times r__call__zFaultDetector.__call__"sY ) ) )#3E: 1261J1Jy2*2*.!!%((((  : : :19. : ) ) ) ) ) ) ) ) ) ) ) ) ) ) )~--s3A*A /A* AA*AA**A.1A.cBttjS)z( Current Unix timestamp )inttimers r timestampzFaultDetector.timestamp1s 49;;rc6|tS)z8 Drop after value as datetime.timedelta )timedelta_in_minutesrrs r _drop_afterzFaultDetector._drop_after8s ((444rc6|tS)z: Check period value as datetime.timedelta )r,r rs r _check_periodzFaultDetector._check_period?s ((666rc 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) openrr(readstrip current_deltar/OSError ValueErrorr_update_latest_flush)r latest_infolatest_flush_times r should_flushzFaultDetector.should_flushFsS  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 rr,z"FaultDetector.timedelta_in_minutesUs &&&&r time_stampc^dtdtfd}||j||z S)zQ Calculate timedelta for given time_stamp from current timestamp tsrc*tj|S)z> Make a datetime object from ts (int) )r fromtimestamp)r@s r_castz*FaultDetector.current_delta.._castas)"-- -r)r(rr*)rr>rCs rr4zFaultDetector.current_delta\sG  .c .h . . . . uT^$$uuZ'8'888rkeycpuNc|j}|j5t|j|jkrD||jvr;|jdt|j ddddS|jd|||||f|j|<|jdt|jddddS#1swxYwYdS)z5 Save throttling entry for given key z0throttling_stat at capacity (%d), dropping entryNz(Adding new throttling entry %s: (%i, %i)Throttling entries total %i)r*rlenr _MAX_ENTRIESrwarningdebug)rrDrEstamps rsavezFaultDetector.saveisU Y 9 94'((D,===#TMaBaBa ##$V$'(<$=$=??? 9 9 9 9 9 9 9 9 K  H!3 / / /),e D  % K  ;!$"677 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9sAC1ACCCc |j5|jd||j|d|jdt |jddddS#1swxYwYdS)z6 Remove throttling entry by given key zAbandon throttling entry %srrGN)rrrKrpoprHrrDs rr zFaultDetector._flush_entryysY 9 9 K  ;S A A A  $ $S! , , , K  ;!$"677 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9sA$A99A=A= 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 rrK)rrQrR spent_timethrottling_facts rrz"FaultDetector._was_throttled_sinces\$m3? $';; >.): 7 7 7j1nn !CCrc|jr|j5|jdt |j|D]}||||jdt |jddddS#1swxYwYdSdS)z, Clear throttling stat dict zFlush started, total entries %iz Flush finished, total entries %iN) r:rrrKrHr_expired_entriesr r7rPs 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)r1rwritestrr*r5rerror)rr8s rr7z"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)listritemsr4r-rrKappend)rexpiredrDvaluer#r*_deltas rrWzFaultDetector._expired_entriess &&.4466 $ $JC LAy'' 22F((( !!"I"%v///s###r)rN)__name__ __module__ __qualname____doc__rIrrr(rboolr&propertyr*rr-r/r: staticmethodr,r4rMr rrXr7r_rWrrr r s#LGGG .c .c .eD#I6F . . . . 3   X 5Y555X5 7y777X7  d   X '3'9'''\' 9 9 9 9 9 999#9$9999 999999 D,/ D,/ D49$)4D D D D D = = = =JJJJ $      rr )rhrr)rr threadingrtypingrrxray.internal.constantsrr r r rlrrrps ((((((((RRRRRRRRRRbbbbbbbbbbr