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

Unit test Ceph cache and fix timezone awareness #2723

Merged
merged 7 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
162 changes: 162 additions & 0 deletions tests/data/result/adviser/adviser-220131050913-b7c4ef0fc532ac45
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"metadata": {
"analyzer": "thoth-adviser",
"analyzer_version": "0.50.0",
"arguments": {
"advise": {
"beam_width": 25000,
"constraints": "input/constraints.txt",
"count": 1,
"dev": false,
"labels": "input/labels.json",
"library_usage": "input/library_usage.json",
"limit": 1,
"limit_latest_versions": -1,
"no_pretty": false,
"output": "/mnt/workdir/adviser-220131050913-b7c4ef0fc532ac45",
"pipeline": null,
"plot": null,
"predictor": "AUTO",
"predictor_config": {},
"prescription": [
"/opt/app-root/src/prescriptions.pickle"
],
"recommendation_type": "latest",
"requirements": "input/Pipfile",
"requirements_format": "pipenv",
"requirements_locked": "input/Pipfile.lock",
"runtime_environment": "input/runtime_environment.json",
"seed": 42,
"user_stack_scoring": true
},
"thoth-adviser": {
"metadata": {
"kebechet_metadata": null,
"origin": "https://github.com/thoth-station/ps-cv",
"re_run_adviser_id": null,
"source_type": null,
"thoth.adviser": {
"justification": null,
"stack_info": null
}
},
"verbose": false
}
},
"datetime": "2022-01-31T05:15:49.969966",
"distribution": {
"codename": "Ootpa",
"id": "rhel",
"like": "fedora",
"version": "8.3",
"version_parts": {
"build_number": "",
"major": "8",
"minor": "3"
}
},
"document_id": "adviser-220131050913-b7c4ef0fc532ac45",
"duration": 376,
"hostname": "adviser-220131050913-b7c4ef0fc532ac45-1876565397",
"os_release": {
"id": "rhel",
"name": "Red Hat Enterprise Linux",
"platform_id": "platform:el8",
"redhat_bugzilla_product": "Red Hat Enterprise Linux 8",
"redhat_bugzilla_product_version": "8.3",
"redhat_support_product": "Red Hat Enterprise Linux",
"redhat_support_product_version": "8.3",
"version": "8.3 (Ootpa)",
"version_id": "8.3"
},
"python": {
"api_version": 1013,
"implementation_name": "cpython",
"major": 3,
"micro": 3,
"minor": 8,
"releaselevel": "final",
"serial": 0
},
"thoth_deployment_name": "ocp4-stage",
"timestamp": 1643606149
},
"result": {
"error": true,
"error_msg": "Resolver was killed as allocated memory has been exceeded (OOM) - https://thoth-station.ninja/j/oom",
"parameters": {
"beam_width": 25000,
"constraints": "input/constraints.txt",
"count": 1,
"dev": false,
"labels": {},
"library_usage": null,
"limit": 1,
"limit_latest_versions": -1,
"no_pretty": false,
"output": "/mnt/workdir/adviser-220131050913-b7c4ef0fc532ac45",
"pipeline": null,
"plot": null,
"predictor": "AUTO",
"predictor_config": "{}",
"prescription": [
"/opt/app-root/src/prescriptions.pickle"
],
"project": {
"constraints": [],
"requirements": {
"dev-packages": {},
"packages": {
"jupyter-tensorboard": "*",
"keras": "*",
"opencv-python": "*",
"pillow": "*",
"tensorboard": "*",
"tensorflow": ">=2.0.0"
},
"requires": {
"python_version": "3.8"
},
"source": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"requirements_locked": null,
"runtime_environment": {
"base_image": null,
"cuda_version": null,
"cudnn_version": null,
"hardware": {
"cpu_family": null,
"cpu_model": null,
"gpu_model": null
},
"labels": null,
"mkl_version": null,
"name": "ps-cv-tensorflow",
"openblas_version": null,
"openmpi_version": null,
"operating_system": {
"name": "ubi",
"version": "8"
},
"platform": "linux-x86_64",
"python_version": "3.8",
"recommendation_type": null
}
},
"recommendation_type": "latest",
"requirements": "input/Pipfile",
"requirements_format": "pipenv",
"requirements_locked": "input/Pipfile.lock",
"runtime_environment": "input/runtime_environment.json",
"seed": 42,
"user_stack_scoring": true
},
"report": null
}
}
27 changes: 27 additions & 0 deletions tests/test_ceph.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

"""This is the tests."""

import json
import pytest
from moto import mock_s3

from thoth.storages import AdvisersCacheStore
from thoth.storages import CephStore
from thoth.storages.exceptions import NotFoundError

Expand Down Expand Up @@ -59,6 +61,7 @@

_ENV = {**CEPH_INIT_ENV}
_BUCKET_PREFIX = "some-prefix/"
_DEPLOYMENT_NAME = "thoth-test-deployment"


@pytest.fixture(name="adapter")
Expand All @@ -79,6 +82,14 @@ def _fixture_connected_adapter():
yield connected_adapter


@pytest.fixture(name="connected_cache_adapter")
def _fixture_connected_cache_adapter():
"""Retrieve a connected cache adapter to Ceph."""
adapter = AdvisersCacheStore(**CEPH_INIT_KWARGS, prefix=_BUCKET_PREFIX, deployment_name=_DEPLOYMENT_NAME)
with connected_ceph_adapter(adapter, raw_ceph=False) as connected_cache_adapter:
yield connected_cache_adapter


class TestCephStore(ThothStoragesTest):
"""Testing class for Ceph operations."""

Expand Down Expand Up @@ -181,3 +192,19 @@ def connect(self, adapter):
assert not adapter.is_connected()
adapter.connect()
assert adapter.is_connected()


class TestCephCache(ThothStoragesTest):
"""Test retrieving properties from cached documents."""

def test_retrieve_document_ttl(self, connected_cache_adapter):
"""Test retrieving TTL from a cached document."""
assert connected_cache_adapter.document_exists("adviser-220131050913-b7c4ef0fc532ac45") is False
with open("tests/data/result/adviser/adviser-220131050913-b7c4ef0fc532ac45", "r") as advise_result:
advise_file = json.load(advise_result)
connected_cache_adapter.store_document(advise_file)
assert connected_cache_adapter.document_exists("adviser-220131050913-b7c4ef0fc532ac45") is True

document_ttl = connected_cache_adapter.retrieve_document_ttl("adviser-220131050913-b7c4ef0fc532ac45")
eps = 10
assert abs(14400.0 - document_ttl) <= eps
4 changes: 2 additions & 2 deletions thoth/storages/ceph_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def retrieve_document_ttl(self, document_id: str) -> float:

# Uses UTC time to be environment agnostic (no timezone)
time_lived = (
datetime.now()
datetime.now(timezone.utc)
- self.ceph.retrieve_document_attr(object_key=document_id, attr="LastModified").replace(tzinfo=timezone.utc)
).total_seconds()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
).total_seconds()
)


if time_lived <= 14400.0:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if time_lived <= 14400.0:
if time_lived <= timedelta(hours=4):

(this makes it clearer that 14400 seconds == 4 hours)

return time_lived
return 14400.0 - time_lived
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return 14400.0 - time_lived
return timedelta(hours=4) - time_lived

or if we don't want to return a timedelta

Suggested change
return 14400.0 - time_lived
return (timedelta(hours=4) - time_lived).total_seconds()


return 0.0
Copy link
Member

Choose a reason for hiding this comment

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

Could we return a timedelta object instead of a raw number of seconds ?

If not, I think we should at least use it internally to the function.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return 0.0
return timedelta()

Only if we switch to a timedelta return.