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

Implement lock file mechanism when handling storage #8801

Closed
wants to merge 1 commit into from
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
10 changes: 3 additions & 7 deletions src/ert/storage/local_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@
import xtgeo
from pydantic import BaseModel

from ert.config import (
ExtParamConfig,
Field,
GenKwConfig,
SurfaceConfig,
)
from ert.config import ExtParamConfig, Field, GenKwConfig, SurfaceConfig
from ert.config.parsing.context_values import ContextBoolEncoder
from ert.config.response_config import ResponseConfig
from ert.storage.mode import BaseMode, Mode, require_write
from ert.storage.mode import BaseMode, Mode, lock_access, require_write

if TYPE_CHECKING:
from ert.config.parameter_config import ParameterConfig
Expand Down Expand Up @@ -80,7 +75,8 @@
(path / "index.json").read_text(encoding="utf-8")
)

@classmethod

Check failure on line 78 in src/ert/storage/local_experiment.py

View workflow job for this annotation

GitHub Actions / type-checking (3.12)

Value of type variable "C" of "lock_access" cannot be "type[LocalExperiment]"
@lock_access
def create(
cls,
storage: LocalStorage,
Expand Down
26 changes: 21 additions & 5 deletions src/ert/storage/mode.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from __future__ import annotations

import fcntl
import os
from enum import Enum
from functools import wraps
from typing import (
TYPE_CHECKING,
Callable,
Literal,
)
from typing import TYPE_CHECKING, Callable, Literal

if TYPE_CHECKING:
from typing_extensions import Concatenate, ParamSpec, TypeVar
Expand Down Expand Up @@ -105,3 +103,21 @@ def inner(self_: C, /, *args: P.args, **kwargs: P.kwargs) -> T:
return func(self_, *args, **kwargs)

return inner


def lock_access(func: F[C, P, T]) -> F[C, P, T]:
_LOCK_FILE = "storage.lock"

@wraps(func)
def inner(self_: C, /, *args: P.args, **kwargs: P.kwargs) -> T:
lock_fd = os.open(_LOCK_FILE, os.O_CREAT | os.O_RDWR)
try:
fcntl.flock(lock_fd, fcntl.LOCK_EX)
result = func(self_, *args, **kwargs)
finally:
fcntl.flock(lock_fd, fcntl.LOCK_UN)
os.close(lock_fd)

return result

return inner
Loading