Skip to content

Commit bce5e99

Browse files
GitHKAndrei Neagu
andauthored
🐛 Reverting ITISFoundation#3239 code and fixing it (ITISFoundation#3302)
Co-authored-by: Andrei Neagu <[email protected]>
1 parent 766f81f commit bce5e99

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1441
-340
lines changed

packages/models-library/src/models_library/services.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from datetime import datetime
88
from enum import Enum
99
from typing import Any, Optional, Union
10+
from uuid import UUID
1011

1112
from pydantic import (
1213
BaseModel,
@@ -60,6 +61,8 @@
6061
ServiceKey = constr(regex=KEY_RE)
6162
ServiceVersion = constr(regex=VERSION_RE)
6263

64+
RunID = UUID
65+
6366

6467
class ServiceType(str, Enum):
6568
COMPUTATIONAL = "computational"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from datetime import datetime
2+
3+
DOCKER_TIMESTAMP_LENGTH = len("2020-10-09T12:28:14.771034")
4+
5+
6+
def to_datetime(docker_timestamp: str) -> datetime:
7+
# datetime_str is typically '2020-10-09T12:28:14.771034099Z'
8+
# - The T separates the date portion from the time-of-day portion
9+
# - The Z on the end means UTC, that is, an offset-from-UTC
10+
# The 099 before the Z is not clear, therefore we will truncate the last part
11+
# NOTE: must be in UNIX Timestamp format
12+
return datetime.strptime(
13+
docker_timestamp[:DOCKER_TIMESTAMP_LENGTH], "%Y-%m-%dT%H:%M:%S.%f"
14+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from datetime import datetime
2+
3+
import pytest
4+
from servicelib.docker_utils import to_datetime
5+
6+
7+
@pytest.mark.parametrize(
8+
"docker_time, expected_datetime",
9+
[("2020-10-09T12:28:14.771034099Z", datetime(2020, 10, 9, 12, 28, 14, 771034))],
10+
)
11+
def test_to_datetime(docker_time: str, expected_datetime: datetime):
12+
assert to_datetime(docker_time) == expected_datetime

services/dask-sidecar/src/simcore_service_dask_sidecar/computational_sidecar/docker_utils.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import re
55
import socket
66
from contextlib import asynccontextmanager
7-
from datetime import datetime
87
from pathlib import Path
98
from pprint import pformat
109
from typing import (
@@ -14,8 +13,11 @@
1413
Awaitable,
1514
Callable,
1615
Coroutine,
16+
Dict,
1717
Final,
18+
List,
1819
Optional,
20+
Tuple,
1921
cast,
2022
)
2123

@@ -29,6 +31,7 @@
2931
from packaging import version
3032
from pydantic import ByteSize
3133
from pydantic.networks import AnyUrl
34+
from servicelib.docker_utils import to_datetime
3235
from settings_library.s3 import S3Settings
3336

3437
from ..boot_mode import BootMode
@@ -50,10 +53,10 @@ async def create_container_config(
5053
docker_registry: str,
5154
service_key: str,
5255
service_version: str,
53-
command: list[str],
56+
command: List[str],
5457
comp_volume_mount_point: str,
5558
boot_mode: BootMode,
56-
task_max_resources: dict[str, Any],
59+
task_max_resources: Dict[str, Any],
5760
) -> DockerContainerConfig:
5861

5962
nano_cpus_limit = int(task_max_resources.get("CPU", 1) * 1e9)
@@ -121,20 +124,6 @@ async def managed_container(
121124
raise
122125

123126

124-
DOCKER_TIMESTAMP_LENGTH = len("2020-10-09T12:28:14.771034")
125-
126-
127-
def to_datetime(docker_timestamp: str) -> datetime:
128-
# datetime_str is typically '2020-10-09T12:28:14.771034099Z'
129-
# - The T separates the date portion from the time-of-day portion
130-
# - The Z on the end means UTC, that is, an offset-from-UTC
131-
# The 099 before the Z is not clear, therefore we will truncate the last part
132-
# NOTE: must be in UNIX Timestamp format
133-
return datetime.strptime(
134-
docker_timestamp[:DOCKER_TIMESTAMP_LENGTH], "%Y-%m-%dT%H:%M:%S.%f"
135-
)
136-
137-
138127
DOCKER_LOG_REGEXP = re.compile(
139128
r"^([0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+.[0-9]+.) (.+)$"
140129
)
@@ -144,7 +133,7 @@ def to_datetime(docker_timestamp: str) -> datetime:
144133
DEFAULT_TIME_STAMP = "2000-01-01T00:00:00.000000000Z"
145134

146135

147-
async def parse_line(line: str) -> tuple[LogType, str, str]:
136+
async def parse_line(line: str) -> Tuple[LogType, str, str]:
148137
match = re.search(DOCKER_LOG_REGEXP, line)
149138
if not match:
150139
# default return as log

services/dask-sidecar/tests/unit/test_docker_utils.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
# pylint: disable=no-member
55

66
import asyncio
7-
from datetime import datetime
8-
from typing import Any
7+
from typing import Any, Dict, List, Tuple
98
from unittest.mock import call
109

1110
import aiodocker
@@ -18,7 +17,6 @@
1817
create_container_config,
1918
managed_container,
2019
parse_line,
21-
to_datetime,
2220
)
2321

2422

@@ -38,7 +36,7 @@ def service_version() -> str:
3836

3937

4038
@pytest.fixture()
41-
def command() -> list[str]:
39+
def command() -> List[str]:
4240
return ["sh", "-c", "some_app"]
4341

4442

@@ -55,10 +53,10 @@ async def test_create_container_config(
5553
docker_registry: str,
5654
service_key: str,
5755
service_version: str,
58-
command: list[str],
56+
command: List[str],
5957
comp_volume_mount_point: str,
6058
boot_mode: BootMode,
61-
task_max_resources: dict[str, Any],
59+
task_max_resources: Dict[str, Any],
6260
):
6361

6462
container_config = await create_container_config(
@@ -97,14 +95,6 @@ async def test_create_container_config(
9795
)
9896

9997

100-
@pytest.mark.parametrize(
101-
"docker_time, expected_datetime",
102-
[("2020-10-09T12:28:14.771034099Z", datetime(2020, 10, 9, 12, 28, 14, 771034))],
103-
)
104-
def test_to_datetime(docker_time: str, expected_datetime: datetime):
105-
assert to_datetime(docker_time) == expected_datetime
106-
107-
10898
@pytest.mark.parametrize(
10999
"log_line, expected_parsing",
110100
[
@@ -198,7 +188,7 @@ def test_to_datetime(docker_time: str, expected_datetime: datetime):
198188
),
199189
],
200190
)
201-
async def test_parse_line(log_line: str, expected_parsing: tuple[LogType, str, str]):
191+
async def test_parse_line(log_line: str, expected_parsing: Tuple[LogType, str, str]):
202192
assert await parse_line(log_line) == expected_parsing
203193

204194

@@ -214,7 +204,7 @@ async def test_managed_container_always_removes_container(
214204
docker_registry: str,
215205
service_key: str,
216206
service_version: str,
217-
command: list[str],
207+
command: List[str],
218208
comp_volume_mount_point: str,
219209
mocker: MockerFixture,
220210
exception_type: Exception,

services/director-v2/src/simcore_service_director_v2/cli.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
from .modules.db.repositories.projects import ProjectsRepository
2424
from .modules.director_v0 import DirectorV0Client
2525
from .modules.dynamic_sidecar import api_client
26-
from .modules.dynamic_sidecar.scheduler.events_utils import (
27-
fetch_repo_outside_of_request,
28-
)
26+
from .modules.dynamic_sidecar.scheduler._utils import fetch_repo_outside_of_request
2927
from .modules.projects_networks import requires_dynamic_sidecar
3028

3129
DEFAULT_NODE_SAVE_RETRY: Final[int] = 3

services/director-v2/src/simcore_service_director_v2/core/settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@ class DynamicServicesSchedulerSettings(BaseCustomSettings):
361361
5.0, description="interval at which the scheduler cycle is repeated"
362362
)
363363

364+
DIRECTOR_V2_DYNAMIC_SCHEDULER_PENDING_VOLUME_REMOVAL_INTERVAL_S: PositiveFloat = (
365+
Field(
366+
30 * MINS,
367+
description="interval at which cleaning of unused dy-sidecar "
368+
"docker volume removal services is executed",
369+
)
370+
)
371+
364372

365373
class DynamicServicesSettings(BaseCustomSettings):
366374
# TODO: PC->ANE: refactor dynamic-sidecar settings. One settings per app module

services/director-v2/src/simcore_service_director_v2/models/schemas/constants.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
from typing import Final
2+
13
# dynamic services
24

3-
DYNAMIC_SIDECAR_SERVICE_PREFIX = "dy-sidecar"
4-
DYNAMIC_PROXY_SERVICE_PREFIX = "dy-proxy"
5+
DYNAMIC_SIDECAR_SERVICE_PREFIX: Final[str] = "dy-sidecar"
6+
DYNAMIC_PROXY_SERVICE_PREFIX: Final[str] = "dy-proxy"
7+
DYNAMIC_VOLUME_REMOVER_PREFIX: Final[str] = "dy-volrm"
8+
DY_SIDECAR_NAMED_VOLUME_PREFIX: Final[str] = "dyv"
59

610
# label storing scheduler_data to allow service
711
# monitoring recovery after director-v2 reboots
8-
DYNAMIC_SIDECAR_SCHEDULER_DATA_LABEL = "io.simcore.scheduler-data"
12+
DYNAMIC_SIDECAR_SCHEDULER_DATA_LABEL: Final[str] = "io.simcore.scheduler-data"
913

1014
# This matches registries by:
1115
# - local

services/director-v2/src/simcore_service_director_v2/models/schemas/dynamic_services/scheduler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
PathMappingsLabel,
1111
SimcoreServiceLabels,
1212
)
13+
from models_library.services import RunID
1314
from models_library.services_resources import ServiceResourcesDict
1415
from pydantic import (
1516
AnyHttpUrl,
@@ -142,7 +143,7 @@ def mark_removed(self) -> None:
142143

143144

144145
class DynamicSidecar(BaseModel):
145-
run_id: UUID = Field(
146+
run_id: RunID = Field(
146147
default_factory=uuid4,
147148
description=(
148149
"Used to discriminate between dynamic-sidecar docker resources "
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from ._core import (
2+
are_all_services_present,
3+
constrain_service_to_node,
4+
create_network,
5+
create_service_and_get_id,
6+
get_dynamic_sidecar_state,
7+
get_dynamic_sidecars_to_observe,
8+
get_or_create_networks_ids,
9+
get_projects_networks_containers,
10+
get_service_placement,
11+
get_swarm_network,
12+
inspect_service,
13+
is_dynamic_service_running,
14+
is_dynamic_sidecar_stack_missing,
15+
list_dynamic_sidecar_services,
16+
remove_dynamic_sidecar_network,
17+
remove_dynamic_sidecar_stack,
18+
try_to_remove_network,
19+
update_scheduler_data_label,
20+
)
21+
from ._volume import remove_pending_volume_removal_services, remove_volumes_from_node
22+
23+
__all__: tuple[str, ...] = (
24+
"are_all_services_present",
25+
"constrain_service_to_node",
26+
"create_network",
27+
"create_service_and_get_id",
28+
"get_dynamic_sidecar_state",
29+
"get_dynamic_sidecars_to_observe",
30+
"get_or_create_networks_ids",
31+
"get_projects_networks_containers",
32+
"get_service_placement",
33+
"get_swarm_network",
34+
"inspect_service",
35+
"is_dynamic_service_running",
36+
"is_dynamic_sidecar_stack_missing",
37+
"list_dynamic_sidecar_services",
38+
"remove_dynamic_sidecar_network",
39+
"remove_dynamic_sidecar_stack",
40+
"remove_pending_volume_removal_services",
41+
"remove_volumes_from_node",
42+
"try_to_remove_network",
43+
"update_scheduler_data_label",
44+
)
45+
# nopycln: file

0 commit comments

Comments
 (0)