Skip to content

Commit

Permalink
🐛 fix issue with parsing shared store (#4485)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrei Neagu <[email protected]>
  • Loading branch information
GitHK and Andrei Neagu authored Jul 11, 2023
1 parent 56ea412 commit 241d018
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class _StoreMixin(BaseModel):

async def __aenter__(self) -> None:
await self._persist_lock.acquire()
return None

async def __aexit__(self, *args) -> None:
await self._persist_to_disk()
Expand Down Expand Up @@ -74,21 +73,23 @@ async def _setup_initial_volume_states(self) -> None:
self.volume_states[category] = VolumeState(status=status)

@classmethod
async def init_from_disk(cls, shared_store_dir: Path) -> "SharedStore":
data_file_path = shared_store_dir / STORE_FILE_NAME
async def init_from_disk(
cls, shared_store_dir: Path, *, store_file_name: "str"
) -> "SharedStore":
data_file_path = shared_store_dir / store_file_name

if not data_file_path.exists():
obj = cls()
obj.post_init(shared_store_dir)
await obj._setup_initial_volume_states()
await obj._setup_initial_volume_states() # noqa SLF001
return obj

# if the sidecar is started for a second time (usually the container dies)
# it will load the previous data which was stored
async with aiofiles.open(shared_store_dir / STORE_FILE_NAME) as data_file:
async with aiofiles.open(shared_store_dir / store_file_name) as data_file:
file_content = await data_file.read()

obj = cls.parse_obj(file_content)
obj = cls.parse_raw(file_content)
obj.post_init(shared_store_dir)
return obj

Expand All @@ -98,7 +99,7 @@ async def on_startup() -> None:
settings: ApplicationSettings = app.state.settings

app.state.shared_store = await SharedStore.init_from_disk(
settings.DYNAMIC_SIDECAR_SHARED_STORE_DIR
settings.DYNAMIC_SIDECAR_SHARED_STORE_DIR, store_file_name=STORE_FILE_NAME
)

app.add_event_handler("startup", on_startup)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"compose_spec": null, "container_names": [], "volume_states": {"INPUTS": {"status": "CONTENT_NO_SAVE_REQUIRED", "last_changed": "2023-06-28T07:18:11.157717+00:00"}, "SHARED_STORE": {"status": "CONTENT_NO_SAVE_REQUIRED", "last_changed": "2023-06-28T07:18:11.157741+00:00"}, "OUTPUTS": {"status": "CONTENT_NEEDS_TO_BE_SAVED", "last_changed": "2023-06-28T07:18:11.778735+00:00"}, "STATES": {"status": "CONTENT_NEEDS_TO_BE_SAVED", "last_changed": "2023-06-28T07:18:11.776789+00:00"}}}
22 changes: 22 additions & 0 deletions services/dynamic-sidecar/tests/unit/test_models_shared_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=redefined-outer-name
# pylint: disable=unused-argument

import json
from copy import deepcopy
from pathlib import Path
from typing import Any
Expand Down Expand Up @@ -105,3 +106,24 @@ async def replace_list_in_shared_store(item: str):
*(replace_list_in_shared_store(f"{x}") for x in range(PARALLEL_CHANGES))
)
assert len(shared_store.container_names) == PARALLEL_CHANGES


async def test_init_from_disk_with_legacy_data_format(project_tests_dir: Path):
MOCKS_DIR = project_tests_dir / "mocks"
LEGACY_SHARED_STORE = "legacy_shared_store.json"

results = await SharedStore.init_from_disk(
MOCKS_DIR, store_file_name=LEGACY_SHARED_STORE
)
# if file is missing it correctly loaded the storage_file
assert (MOCKS_DIR / STORE_FILE_NAME).exists() is False

# ensure object content and disk content are the same
assert json.loads(results.json()) == json.loads(
(MOCKS_DIR / LEGACY_SHARED_STORE).read_text()
)


async def test_init_from_disk_no_file_present(tmp_path: Path):
await SharedStore.init_from_disk(tmp_path, store_file_name=STORE_FILE_NAME)
assert (tmp_path / STORE_FILE_NAME).exists() is True

0 comments on commit 241d018

Please sign in to comment.