Skip to content
Snippets Groups Projects
Commit 3df01c05 authored by Siarhei Khaletski (EPAM)'s avatar Siarhei Khaletski (EPAM) :triangular_flag_on_post:
Browse files

Merge branch 'feature/add_lru_cache_for_schemas' into 'master'

GONRG-2142: Add lru cache

See merge request !46
parents a38a7d4c e59469e5
No related branches found
No related tags found
1 merge request!46GONRG-2142: Add lru cache
Pipeline #38356 passed
...@@ -17,18 +17,20 @@ ...@@ -17,18 +17,20 @@
import copy import copy
import logging import logging
from functools import lru_cache
from typing import Any, List, Union, Tuple, Union from typing import Any, List, Union, Tuple, Union
import jsonschema import jsonschema
import requests import requests
import tenacity import tenacity
from jsonschema import exceptions from jsonschema import exceptions
from libs.constants import DATA_SECTION, DATASETS_SECTION, MASTER_DATA_SECTION, \ from libs.constants import DATA_SECTION, DATASETS_SECTION, MASTER_DATA_SECTION, \
REFERENCE_DATA_SECTION, WORK_PRODUCT_SECTION, WORK_PRODUCT_COMPONENTS_SECTION REFERENCE_DATA_SECTION, WORK_PRODUCT_SECTION, WORK_PRODUCT_COMPONENTS_SECTION
from libs.context import Context from libs.context import Context
from libs.exceptions import GenericManifestSchemaError, EntitySchemaValidationError from libs.exceptions import GenericManifestSchemaError, EntitySchemaValidationError
from libs.traverse_manifest import ManifestEntity
from libs.mixins import HeadersMixin from libs.mixins import HeadersMixin
from libs.traverse_manifest import ManifestEntity
from osdu_api.libs.auth.authorization import TokenRefresher, authorize from osdu_api.libs.auth.authorization import TokenRefresher, authorize
logger = logging.getLogger() logger = logging.getLogger()
...@@ -70,7 +72,8 @@ class OSDURefResolver(jsonschema.RefResolver): ...@@ -70,7 +72,8 @@ class OSDURefResolver(jsonschema.RefResolver):
:rtype: dict :rtype: dict
""" """
document = super().resolve_fragment(document, fragment) document = super().resolve_fragment(document, fragment)
fragment_parts = fragment.split("/") # /definitions/<OsduID> -> [..., <OsduID>] # /definitions/<OsduID> -> [..., <OsduID>]
fragment_parts = fragment.split("/")
if len(fragment_parts) > 1: if len(fragment_parts) > 1:
document.pop("$id", None) document.pop("$id", None)
return document return document
...@@ -146,9 +149,15 @@ class SchemaValidator(HeadersMixin): ...@@ -146,9 +149,15 @@ class SchemaValidator(HeadersMixin):
response["$id"] = uri response["$id"] = uri
return response return response
@lru_cache()
def get_schema(self, kind: str) -> Union[dict, None]: def get_schema(self, kind: str) -> Union[dict, None]:
"""Fetch schema from Schema service. """Fetch schema from Schema service.
Implies that the cache is a one-off.
@TODO lru_cache should be replaced by generic cache mechanism to work with providers
solutions
:param kind: The kind of the scheema to fetch :param kind: The kind of the scheema to fetch
:type kind: str :type kind: str
:raises e: Generic exception :raises e: Generic exception
......
...@@ -29,6 +29,7 @@ sys.path.append(f"{os.getenv('AIRFLOW_SRC_DIR')}/dags") ...@@ -29,6 +29,7 @@ sys.path.append(f"{os.getenv('AIRFLOW_SRC_DIR')}/dags")
from libs.exceptions import PipelineFailedError from libs.exceptions import PipelineFailedError
import pytest import pytest
import requests import requests
from functools import lru_cache
from file_paths import ( from file_paths import (
MANIFEST_WELLBORE_VALID_PATH, MANIFEST_WELLBORE_VALID_PATH,
...@@ -80,6 +81,7 @@ class TestOperators(object): ...@@ -80,6 +81,7 @@ class TestOperators(object):
def test_process_manifest_r3_operator(self, monkeypatch): def test_process_manifest_r3_operator(self, monkeypatch):
@lru_cache()
def _get_common_schema(*args, **kwargs): def _get_common_schema(*args, **kwargs):
with open(MANIFEST_GENERIC_SCHEMA_PATH) as f: with open(MANIFEST_GENERIC_SCHEMA_PATH) as f:
manifest_schema = json.load(f) manifest_schema = json.load(f)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment