9Mjg" d Z ddlZddlZddlZddlmZmZ ddlmZ ddlm Z m
Z
mZ ddlm
Z
mZmZ G d d ZdS ) z[
This module contains FaultDetector class, aimed to track throttling
for incoming requests
N)datetime timedelta)RLock)OptionalTupleAny)
drop_aftercheck_periodthrottling_thresholdc Z e Zd ZdZdZdZd Zdedede e
ef fdZedefd Z
edefd
Zedefd Zede
fd Zed
edefd ZdedefdZddededee ddfdZdeddfdZdedede e
ef fdZddZddZdefdZdS )
FaultDetectorz
Fault Detector class
i' i c t j d | _ t | _ t j t | _ t | _ t | _ d| _ d S )Nfault_detectorz$/usr/share/alt-php-xray/flush.latest)
logging getLoggerloggerdictthrottling_statcollectionsdefaultdictint_uid_entry_count
_entry_uidr locktime_marker_fileselfs P/opt/cloudlinux/venv/lib64/python3.11/site-packages/xray/agent/fault_detector.py__init__zFaultDetector.__init__% sZ '(899#vv !, 7 < <&&GG F t_key cpu_valuereturnc | j 5 | j | \ }}| || \ }}| | n# t $ r d\ }}Y nw xY wddd n# 1 swxY w Y ||fS )z)
Perform fault detection
)Fr N)r r _was_throttled_since_flush_entryKeyError)r r! r" saved_cpu_hitting_limitsthrottled_times r __call__zFaultDetector.__call__0 s Y ) )
)#3E: 1 261J1Jy2* 2*.!!%((((
:
:
:19.
: ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ~--s3 A*A /A* AA*AA**A.1A.c B t t j S )z(
Current Unix timestamp
)r timer s r timestampzFaultDetector.timestamp? s
49;;r c 6 | t S )z8
Drop after value as datetime.timedelta
)timedelta_in_minutesr r s r _drop_afterzFaultDetector._drop_afterF s
((444r c 6 | t S )z:
Check period value as datetime.timedelta
)r1 r
r s r
_check_periodzFaultDetector._check_periodM s
((666r c t | j 5 }t | }ddd n# 1 swxY w Y | | | j k S # t t f$ r7 | j 5 |
ddd n# 1 swxY w Y Y dS w xY w)ze
If throttling_stat should be flushed right now
regarding check period value
NF)openr r readstrip
current_deltar4 OSError
ValueErrorr _update_latest_flush)r latest_infolatest_flush_times r should_flushzFaultDetector.should_flushT sS d+,,
D$'(8(8(:(:(@(@(B(B$C$C!
D
D
D
D
D
D
D
D
D
D
D
D
D
D
D%%&7884;MMM$
,
,))+++
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,55 sX A? 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 r r1 z"FaultDetector.timedelta_in_minutesc s
&&&&r
time_stampc ^ dt dt fd} || j || z
S )zQ
Calculate timedelta for given time_stamp from current timestamp
tsr# c * t j | S )z>
Make a datetime object from ts (int)
)r
fromtimestamp)rE s r _castz*FaultDetector.current_delta.