Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add TTL of cached documents in response headers #1798

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ paths:
$ref: "#/components/headers/x-user-api-service-version"
x-thoth-search-ui-url:
$ref: "#/components/headers/x-thoth-search-ui-url"
x-thoth-cached-document-ttl:
$ref: "#/components/headers/x-thoth-cached-document-ttl"
content:
application/json:
schema:
Expand Down Expand Up @@ -315,6 +317,8 @@ paths:
$ref: "#/components/headers/x-thoth-version"
x-user-api-service-version:
$ref: "#/components/headers/x-user-api-service-version"
x-thoth-cached-document-ttl:
$ref: "#/components/headers/x-thoth-cached-document-ttl"
content:
application/json:
schema:
Expand Down Expand Up @@ -530,6 +534,8 @@ paths:
$ref: "#/components/headers/x-user-api-service-version"
x-thoth-search-ui-url:
$ref: "#/components/headers/x-thoth-search-ui-url"
x-thoth-cached-document-ttl:
$ref: "#/components/headers/x-thoth-cached-document-ttl"
content:
application/json:
schema:
Expand Down Expand Up @@ -896,6 +902,8 @@ paths:
$ref: "#/components/headers/x-thoth-version"
x-user-api-service-version:
$ref: "#/components/headers/x-user-api-service-version"
x-thoth-cached-document-ttl:
$ref: "#/components/headers/x-thoth-cached-document-ttl"
content:
application/json:
schema:
Expand Down Expand Up @@ -1577,6 +1585,11 @@ components:
schema:
type: string
example: https://thoth-station.ninja/search/
x-thoth-cached-document-ttl:
description: Thoth cached document Time To Live
schema:
type: integer
example: 7200
page:
description: Current page
schema:
Expand Down
16 changes: 12 additions & 4 deletions thoth/user_api/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def post_analyze(
origin: Optional[str] = None,
verify_tls: bool = True,
force: bool = False,
) -> Tuple[Dict[str, Any], int]:
) -> Union[Tuple[Dict[str, Any], int, Dict[str, Any]], Tuple[Dict[str, Any], int]]:
"""Run an analyzer in a restricted namespace."""
parameters = locals()
force = parameters.pop("force", None)
Expand All @@ -251,6 +251,7 @@ def post_analyze(
cache = AnalysesCacheStore()
cache.connect()
cached_document_id = metadata["digest"] + "+" + parameters_digest
cache_ttl = cache.retrieve_document_ttl(cached_document_id)

if not force:
try:
Expand All @@ -261,6 +262,7 @@ def post_analyze(
"parameters": parameters,
},
202,
{"x-thoth-cached-document-ttl": cache_ttl},
)
except CacheMiss:
pass
Expand Down Expand Up @@ -433,7 +435,7 @@ def post_provenance_python(
force: bool = False,
origin: Optional[str] = None,
token: Optional[str] = None,
) -> Tuple[Dict[str, Any], int]:
) -> Union[Tuple[Dict[str, Any], int, Dict[str, Any]], Tuple[Dict[str, Any], int]]:
"""Check provenance for the given application stack."""
parameters = locals()
# Translate request body parameters.
Expand Down Expand Up @@ -486,6 +488,7 @@ def post_provenance_python(
if not force:
try:
cache_record = cache.retrieve_document_record(cached_document_id)
cache_ttl = cache.retrieve_document_ttl(cached_document_id)
if cache_record["timestamp"] + Configuration.THOTH_CACHE_EXPIRATION > timestamp_now:
return (
{
Expand All @@ -495,6 +498,7 @@ def post_provenance_python(
"parameters": parameters,
},
202,
{"x-thoth-cached-document-ttl": cache_ttl},
)
except CacheMiss:
pass
Expand Down Expand Up @@ -558,7 +562,7 @@ def post_advise_python(
dev: bool = False,
origin: Optional[str] = None,
token: Optional[str] = None,
) -> Tuple[Dict[str, Any], int]:
) -> Union[Tuple[Dict[str, Any], int, Dict[str, Any]], Tuple[Dict[str, Any], int]]:
"""Compute results for the given package or package stack using adviser."""
parameters = locals()
# Translate request body parameters.
Expand Down Expand Up @@ -651,6 +655,7 @@ def post_advise_python(
if not force:
try:
cache_record = adviser_cache.retrieve_document_record(cached_document_id)
cache_ttl = adviser_cache.retrieve_document_ttl(cached_document_id)
if cache_record["timestamp"] + Configuration.THOTH_CACHE_EXPIRATION > timestamp_now:
if parameters["callback_info"]:
result, status_code = _get_document(
Expand Down Expand Up @@ -682,6 +687,7 @@ def post_advise_python(
"parameters": parameters,
},
202,
{"x-thoth-cached-document-ttl": cache_ttl},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the third element in the tuple map to headers?

)

except CacheMiss:
Expand Down Expand Up @@ -1002,7 +1008,7 @@ def post_build(
environment_type: Optional[str] = None,
force: bool = False,
origin: Optional[str] = None,
) -> Tuple[Dict[str, Any], int]:
) -> Union[Tuple[Dict[str, Any], int, Dict[str, Any]], Tuple[Dict[str, Any], int]]:
"""Run analysis on a build."""
output_image = build_detail.get("output_image")
base_image = build_detail.get("base_image")
Expand Down Expand Up @@ -1157,6 +1163,7 @@ def post_build(
buildlogs_cache = BuildLogsAnalysesCacheStore()
buildlogs_cache.connect()
cached_document_id = _compute_digest_params(build_log)
cache_ttl = buildlogs_cache.retrieve_document_ttl(cached_document_id)
buildlogs_cache.store_document_record(
cached_document_id, {"analysis_id": message_parameters["buildlog_parser_id"]}
)
Expand All @@ -1180,6 +1187,7 @@ def post_build(
"buildlog_document_id": buildlog_document_id,
},
202,
{"x-thoth-cached-document-ttl": cache_ttl},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing from the schema

headers:

)


Expand Down