33import uuid
44from datetime import datetime , date
55from databricks .sdk import WorkspaceClient
6- from typing import Annotated , Any , Dict , List , cast , IO , Optional
6+ from typing import Annotated , Any , Dict , List , cast , IO , Optional , Tuple
77from pydantic import BaseModel , Field
88from fastapi import APIRouter , Depends , HTTPException , status , Response , Query
99from fastapi .responses import FileResponse
@@ -1025,6 +1025,16 @@ def infer_models_from_filename(file_path: str) -> List[str]:
10251025 return sorted (inferred )
10261026
10271027
1028+ class _ValidationState :
1029+ _ar_re = re .compile (r"(?<![A-Za-z0-9])ar(?![A-Za-z0-9])" , re .IGNORECASE )
1030+ _base_cache : Dict [str , Any ] = {"exp" : 0.0 , "val" : None }
1031+ _ext_cache : Dict [str , Tuple [float , Any ]] = {}
1032+ _pdp_cache : Tuple [float , Optional [dict ]] = (0.0 , None )
1033+
1034+
1035+ STATE = _ValidationState ()
1036+
1037+
10281038def validation_helper (
10291039 source_str : str ,
10301040 inst_id : str ,
@@ -1047,22 +1057,7 @@ def validation_helper(
10471057 local_session .set (sql_session )
10481058 sess = local_session .get ()
10491059
1050- # --- one-time initialization on the function object (kept in-process)
1051- if not hasattr (validation_helper , "_ar_re" ):
1052- validation_helper ._ar_re = re .compile (
1053- r"(?<![A-Za-z0-9])ar(?![A-Za-z0-9])" , re .IGNORECASE
1054- )
1055- if not hasattr (validation_helper , "_base_cache" ):
1056- # {"exp": <monotonic expiry>, "val": (<schema_id>, <json_doc>)}
1057- validation_helper ._base_cache = {"exp" : 0.0 , "val" : None }
1058- if not hasattr (validation_helper , "_ext_cache" ):
1059- # { str(inst_uuid): (exp, extension_json_doc) }
1060- validation_helper ._ext_cache = {}
1061- if not hasattr (validation_helper , "_pdp_cache" ):
1062- # PDP-wide extension (active), cached: (exp, doc)
1063- validation_helper ._pdp_cache = (0.0 , None )
1064-
1065- AR_RE = validation_helper ._ar_re
1060+ AR_RE = STATE ._ar_re
10661061 BASE_TTL = 300 # seconds
10671062 EXT_TTL = 120 # seconds
10681063
@@ -1097,7 +1092,7 @@ def validation_helper(
10971092
10981093 # --- fetch active base schema (cached)
10991094 now = time .monotonic ()
1100- base_cache = validation_helper ._base_cache
1095+ base_cache = STATE ._base_cache
11011096 if now < base_cache ["exp" ] and base_cache ["val" ] is not None :
11021097 base_schema_id , base_schema = base_cache ["val" ]
11031098 else :
@@ -1123,7 +1118,6 @@ def validation_helper(
11231118 raise ValueError (f"Institution { inst_id } not found" )
11241119
11251120 bucket = get_external_bucket_name (inst_id )
1126-
11271121 # --- choose / prepare extension schema (try to avoid heavy path)
11281122 updated_inst_schema : Optional [dict ] = None
11291123
@@ -1148,7 +1142,7 @@ def _ext_models_set(doc: Optional[dict]) -> set[str]:
11481142
11491143 if getattr (inst , "pdp_id" , None ):
11501144 # PDP institutions: use active PDP extension (cached)
1151- pdp_exp , pdp_doc = validation_helper ._pdp_cache
1145+ pdp_exp , pdp_doc = STATE ._pdp_cache
11521146 if now < pdp_exp and pdp_doc is not None :
11531147 inst_schema = pdp_doc
11541148 else :
@@ -1160,11 +1154,11 @@ def _ext_models_set(doc: Optional[dict]) -> set[str]:
11601154 )
11611155 .limit (1 )
11621156 ).scalar_one_or_none ()
1163- validation_helper ._pdp_cache = (now + EXT_TTL , inst_schema )
1157+ STATE ._pdp_cache = (now + EXT_TTL , inst_schema )
11641158 updated_inst_schema = inst_schema
11651159 else :
11661160 # custom institutions: try cached extension first
1167- ext_cache = validation_helper ._ext_cache
1161+ ext_cache = STATE ._ext_cache
11681162 key = str (getattr (inst , "id" , "" ))
11691163 cached = ext_cache .get (key )
11701164 if cached and now < cached [0 ]:
@@ -1212,7 +1206,7 @@ def _ext_models_set(doc: Optional[dict]) -> set[str]:
12121206 sess .flush ()
12131207 logging .info ("Schema record inserted for '%s'" , inst_id )
12141208 # refresh cache
1215- validation_helper ._ext_cache [key ] = (
1209+ STATE ._ext_cache [key ] = (
12161210 time .monotonic () + EXT_TTL ,
12171211 schema_extension ,
12181212 )
0 commit comments