Skip to content
Open
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
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,3 @@ werkzeug==3.1.8
# flask
# flask-cors

opengeodeweb-microservice==1.*,>=1.4.0rc1
5 changes: 5 additions & 0 deletions src/opengeodeweb_back/crs/crs_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ def assign_all(
for target_id, _, target_object in targets:
if target_id in assigned:
_assign(target_object, authority, code)
# Local import: utils_functions imports the crs modules
from opengeodeweb_back.utils_functions import wait_native_saved # noqa: PLC0415

# Assigning only relabels the coordinates: the viewables are unchanged
for target_id, target_data, target_object in targets:
if target_id in assigned:
# A background save of the same data, started meanwhile, must not write with it
wait_native_saved(target_id)
target_object.save(geode_functions.data_file_path(target_id, target_data.native_file))
entries: dict[str, dict[str, Any]] = {}
for target_id, _, target_object in targets:
Expand Down
9 changes: 7 additions & 2 deletions src/opengeodeweb_back/crs/crs_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def known_geographic_info(


def geographic_info(
crs_manager: og.CoordinateReferenceSystemManager2D | og.CoordinateReferenceSystemManager3D,
crs_manager: (og.CoordinateReferenceSystemManager2D | og.CoordinateReferenceSystemManager3D),
) -> og_geosciences.GeographicCoordinateSystemInfo | None:
crs = crs_manager.active_coordinate_reference_system()
if isinstance(
Expand Down Expand Up @@ -282,7 +282,8 @@ def frame(
crs: og.CoordinateReferenceSystem2D | og.CoordinateReferenceSystem3D,
) -> og.CoordinateSystem2D:
return og.CoordinateSystem2D(
og.Point2D(_xy(crs, 0)), [og.Point2D(_xy(crs, first)), og.Point2D(_xy(crs, second))]
og.Point2D(_xy(crs, 0)),
[og.Point2D(_xy(crs, first)), og.Point2D(_xy(crs, second))],
)

return frame(active), frame(original)
Expand Down Expand Up @@ -310,7 +311,11 @@ def coordinate_system(points: list[og.Point2D]) -> og.CoordinateSystem2D:


def load_crs_object(data_id: str) -> tuple[Data, CrsGeodeObject]:
# Local import: utils_functions imports this module
from opengeodeweb_back.utils_functions import wait_native_saved # noqa: PLC0415

data = geode_functions.get_data_info(data_id)
wait_native_saved(data_id)
geode_object = geode_functions.geode_object_from_string(data.geode_object_type).load_one(
geode_functions.data_file_path(data_id, data.native_file)
)
Expand Down
4 changes: 4 additions & 0 deletions src/opengeodeweb_back/geode_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ def geode_object_from_string(value: str) -> type[GeodeObject]:


def load_geode_object(data_id: str) -> GeodeObject:
# Local import: utils_functions imports this module
from .utils_functions import wait_native_saved # noqa: PLC0415

data = Data.get(data_id)
if not data:
flask.abort(404, f"Data with id {data_id} not found")

wait_native_saved(data_id)
file_absolute_path = data_file_path(data_id, data.native_file)
logger.debug(
"Loading file: %s (exists: %s)",
Expand Down
5 changes: 2 additions & 3 deletions src/opengeodeweb_back/routes/basemap/blueprint_basemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# Standard library imports
import logging
import shutil
from pathlib import Path
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -84,7 +83,7 @@ def _save_tiles(data_id: str, tiles: basemap_tiles.TileRange, images: Images) ->
)
except OSError:
# The request rolls the data row back: its folder must not stay behind
shutil.rmtree(data_folder, ignore_errors=True)
utils_functions.remove_data_folder(data_id, data_folder, ignore_errors=True)
raise


Expand Down Expand Up @@ -133,5 +132,5 @@ def delete(params: schemas.Delete) -> schemas.DeleteResponse:
if not basemap_tiles.is_basemap(params.id):
flask.abort(400, f"Data {params.id} is not a basemap")
get_session().delete(data)
shutil.rmtree(geode_functions.data_file_path(params.id))
utils_functions.remove_data_folder(params.id)
return schemas.DeleteResponse(id=params.id)
5 changes: 3 additions & 2 deletions src/opengeodeweb_back/routes/blueprint_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import logging
import math
import os
import shutil
import sqlite3
import tempfile
import time
Expand Down Expand Up @@ -297,6 +296,7 @@ def apply_time_series(
_check_time_series_extension(params.filename)
if data.geode_object_type != GeodeBRep.geode_object_type():
flask.abort(400, f"{data.geode_object_type} is not a BRep: time series cannot be applied")
utils_functions.wait_native_saved(params.id)
geode_object = GeodeBRep.load_one(geode_functions.data_file_path(params.id, data.native_file))
# Applied in memory first: if OpenGeode fails, nothing is written.
geode_object.load_time_series(geode_functions.upload_file_path(params.filename))
Expand Down Expand Up @@ -841,6 +841,7 @@ def export_project() -> flask.Response:
flask.abort(400, "Requested filename must end with .vease")
export_vease_path = Path(project_folder) / filename

utils_functions.wait_all_native_saved()
with get_session() as session:
rows = session.query(Data.id, Data.native_file).all()
# Basemaps are rebuilt from the project style: neither their rows nor their tiles are exported
Expand Down Expand Up @@ -889,7 +890,7 @@ def _reset_data_folder(data_folder_path: Path) -> None:
if data_folder_path.exists():
for item in data_folder_path.iterdir():
if item.is_dir() and not item.is_symlink():
shutil.rmtree(item)
utils_functions.remove_data_folder(item.name, item)
else:
item.unlink()
else:
Expand Down
162 changes: 150 additions & 12 deletions src/opengeodeweb_back/utils_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import time
import uuid
import zipfile
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import Future, ThreadPoolExecutor, wait
from dataclasses import dataclass, field
from importlib import metadata
from pathlib import Path
from typing import TYPE_CHECKING, Any
Expand All @@ -33,6 +34,11 @@

logger = logging.getLogger(__name__)

# Native files are saved in background: the viewer only needs the viewables.
# Several files can be imported at once, hence the default pool size.
_native_executor = ThreadPoolExecutor(thread_name_prefix="native_save")
_native_saves_lock = threading.Lock()

if TYPE_CHECKING:
from collections.abc import Callable, Sequence

Expand Down Expand Up @@ -303,18 +309,150 @@ def model_components(data_id: str, model: GeodeModel, viewable_file: str | None)
}


@dataclass
class _NativeSave:
"""A background native save. The object is kept until it is saved, to retry a failure."""

future: Future[list[str]]
geode_object: GeodeObject
native_path: str
# Held by a synchronous retry: a later save of the same data waits for it
retry_lock: threading.Lock = field(default_factory=threading.Lock)
retried: bool = False


_native_saves: dict[str, _NativeSave] = {}


def _save_native_in_background(geode_object: GeodeObject, data_id: str, native_path: str) -> None:
"""Save the native file without blocking the request.

The task only uses the captured geode object: no app context nor DB session.
The Werkzeug debug reloader may kill a save mid-write (dev only).
"""

def save_native(previous: _NativeSave | None) -> list[str]:
if previous is not None:
# A re-save of the same data supersedes the previous one, whatever its outcome,
# but never writes the same file at the same time. No deadlock: FIFO order puts
# the previous save ahead in the queue or running.
wait([previous.future])
with previous.retry_lock:
pass
return geode_object.save(native_path)

# One critical section: two re-saves of the same data cannot chain on the same previous
with _native_saves_lock:
previous = _native_saves.get(data_id)
future = _native_executor.submit(save_native, previous)
_native_saves[data_id] = _NativeSave(future, geode_object, native_path)
# Outside the lock: the callback takes it, and runs here if the save is already done
future.add_done_callback(lambda done: _forget_native_save(data_id, done))


def _forget_native_save(data_id: str, future: Future[list[str]]) -> None:
"""Drop a successful save from the registry; failed ones stay to be retried."""
if future.cancelled() or future.exception() is not None:
return
with _native_saves_lock:
entry = _native_saves.get(data_id)
if entry is not None and entry.future is future:
del _native_saves[data_id]


def _drop_native_save(data_id: str, entry: _NativeSave) -> None:
with _native_saves_lock:
if _native_saves.get(data_id) is entry:
del _native_saves[data_id]


def discard_native_save(data_id: str) -> None:
"""Wait for the native save of data_id, if any, and forget it whatever its outcome."""
with _native_saves_lock:
entry = _native_saves.get(data_id)
if entry is None:
return
try:
entry.future.result()
except Exception:
logger.warning("Discarded failed native save of %s", data_id, exc_info=True)
# A retry running on another thread must not write in a folder being removed,
# and none may start once the entry is dropped
with entry.retry_lock:
_drop_native_save(data_id, entry)


def remove_data_folder(
data_id: str, folder: str | Path | None = None, *, ignore_errors: bool = False
) -> None:
"""Remove the folder of data_id once its native save is done (by default its data folder)."""
discard_native_save(data_id)
if folder is None:
folder = geode_functions.data_file_path(data_id)
shutil.rmtree(folder, ignore_errors=ignore_errors)


def _settle_native_save(data_id: str, entry: _NativeSave) -> Exception | None:
"""Wait for entry; on failure, retry its save once on this thread. None once saved."""
try:
entry.future.result()
except Exception as error:
with entry.retry_lock:
with _native_saves_lock:
superseded = _native_saves.get(data_id) is not entry
if not entry.retried and not superseded:
logger.warning("Native save of %s failed, retrying", data_id, exc_info=error)
try:
entry.geode_object.save(entry.native_path)
except Exception as retry_error:
logger.exception("Native file of %s could not be saved", data_id)
return retry_error
entry.retried = True
_drop_native_save(data_id, entry)
return None


def _ensure_native_saved(data_id: str) -> Exception | None:
"""Wait until data_id has no pending save. The error of a save that failed twice, if any."""
while True:
with _native_saves_lock:
entry = _native_saves.get(data_id)
if entry is None:
return None
# A superseded entry is not dropped: the loop then waits for the newer save
error = _settle_native_save(data_id, entry)
if error is not None:
return error


def wait_native_saved(data_id: str) -> None:
"""Wait for the background native save of data_id, if any, retrying a failure once."""
error = _ensure_native_saved(data_id)
if error is not None:
# The failed save stays registered: a later call retries it again
flask.abort(500, f"Native file of {data_id} could not be saved: {error}")


def wait_all_native_saved() -> None:
"""Wait for every pending background native save, retrying each failure once.

Aborts once, naming every data whose native file could not be saved.
"""
with _native_saves_lock:
pending = list(_native_saves)
errors = {data_id: _ensure_native_saved(data_id) for data_id in pending}
failed = [f"{data_id} ({error})" for data_id, error in errors.items() if error]
if failed:
flask.abort(500, f"Native files could not be saved: {', '.join(failed)}")


def save_all_viewables_and_return_info(
geode_object: GeodeObject,
data: Data,
data_path: str,
) -> dict[str, Any]:
with ThreadPoolExecutor() as executor:
tasks: list[tuple[Callable[[str], Any], str]] = [
(
geode_object.save,
str(Path(data_path) / ("native." + geode_object.native_extension())),
)
]
tasks: list[tuple[Callable[[str], Any], str]] = []
if geode_object.is_viewable():
tasks.extend(
[
Expand All @@ -327,10 +465,9 @@ def save_all_viewables_and_return_info(
)

results = list(executor.map(lambda args: args[0](args[1]), tasks))
native_files = results[0]
if geode_object.is_viewable():
viewable_path = results[1]
light_path = results[2]
viewable_path = results[0]
light_path = results[1]
binary_light_viewable = Path(light_path).read_bytes()
binary_light_viewable_str = base64.b64encode(binary_light_viewable).decode("ascii")
data.viewable_file = Path(viewable_path).name
Expand All @@ -340,7 +477,7 @@ def save_all_viewables_and_return_info(
data.viewable_file = None
data.light_viewable_file = None

data.native_file = Path(native_files[0]).name
data.native_file = f"native.{geode_object.native_extension()}"

name = geode_object.identifier.name()
if not name:
Expand All @@ -365,6 +502,7 @@ def save_all_viewables_and_return_info(
crs = active_crs(geode_object)
if crs is not None:
response["crs"] = crs
_save_native_in_background(geode_object, data.id, str(Path(data_path) / data.native_file))
return response


Expand Down Expand Up @@ -426,7 +564,7 @@ def _remove_created_data(created: list[Data]) -> None:
session = get_session()
for data in created:
session.delete(data)
shutil.rmtree(geode_functions.data_file_path(data.id), ignore_errors=True)
remove_data_folder(data.id, ignore_errors=True)
session.flush()


Expand Down
Loading
Loading