Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
71fe638
[FXC-5651] feat: add client-side OBB computation via DraftContext.com…
benflexcompute Mar 26, 2026
0c2ad03
[FXC-5651] refactor: move rotation_axis_hint to compute_obb(), make a…
benflexcompute Mar 26, 2026
524b8c5
linter
benflexcompute Mar 27, 2026
5a6e6f0
[FXC-5651] fix: skip caching entries that exceed total cache size limit
benflexcompute Mar 27, 2026
3d70686
[FXC-5651] fix: reject non-Surface selectors in compute_obb() upfront
benflexcompute Mar 27, 2026
c140dab
[FXC-5651] fix: prevent self-eviction and overwrite size overestimati…
benflexcompute Mar 27, 2026
1e35f61
[FXC-5651] fix: face index collision detection + singleton CloudFileC…
benflexcompute Mar 27, 2026
b091ed9
format
benflexcompute Mar 27, 2026
ed456b3
[FXC-5651] fix: handle zero extents in circularity heuristic
benflexcompute Mar 27, 2026
c4c9e83
[FXC-5651] fix: filter out MirroredSurface in compute_obb()
benflexcompute Mar 27, 2026
7eee5ee
format
benflexcompute Mar 27, 2026
9fcfa64
[FXC-5651] refactor: deduplicate MockEntityList into shared _Selector…
benflexcompute Mar 27, 2026
d336e1c
[FXC-5651] fix: use monkeypatch instead of chmod for write-failure test
benflexcompute Mar 27, 2026
78232b3
[FXC-5651] fix: descriptive error for unknown face IDs in tessellatio…
benflexcompute Mar 27, 2026
52f738c
[FXC-5651] fix: clear error when faces yield no vertex data
benflexcompute Mar 27, 2026
aa4f393
[FXC-5651] fix: consistent cache size accounting + EntityRegistryView…
benflexcompute Mar 27, 2026
f5009cc
[FXC-5651] fix: type validation for else branch, path traversal guard…
benflexcompute Mar 27, 2026
e32bf53
[FXC-5651] fix: safe .name access in non-Surface warning log
benflexcompute Mar 27, 2026
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
163 changes: 163 additions & 0 deletions flow360/cloud/file_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
"""General-purpose size-based LRU disk cache for cloud file downloads.

Stores files under ``~/.flow360/cache/<namespace>/<resource_id>/<file_path>``
with a configurable total size limit. Eviction granularity is the resource
directory — all files for a resource are deleted together to avoid partial
state (e.g. manifest present but bin evicted).
"""

from __future__ import annotations

import shutil
from pathlib import Path
from typing import List, Optional, Tuple

from ..log import log

CLOUD_FILE_CACHE_MAX_SIZE_MB: int = 2048 # default 2 GB, user-adjustable

_shared_cache_instance: Optional["CloudFileCache"] = None


def get_shared_cloud_file_cache() -> "CloudFileCache":
"""Return the module-level shared CloudFileCache instance (created on first call)."""
global _shared_cache_instance # pylint: disable=global-statement
if _shared_cache_instance is None:
_shared_cache_instance = CloudFileCache()
return _shared_cache_instance


class CloudFileCache:
"""Size-based LRU disk cache.

Keys are ``(namespace, resource_id, file_path)`` triples.
All namespaces share a single total-size budget.
"""

def __init__(
self,
cache_root: Optional[Path] = None,
max_size_bytes: Optional[int] = None,
) -> None:
self._cache_root = (cache_root or Path("~/.flow360/cache")).expanduser()
self._max_size_bytes = (
CLOUD_FILE_CACHE_MAX_SIZE_MB * 1024 * 1024 if max_size_bytes is None else max_size_bytes
)
self._disabled = False

# ------------------------------------------------------------------
# Public API
# ------------------------------------------------------------------

def get(self, namespace: str, resource_id: str, file_path: str) -> Optional[bytes]:
"""Return cached bytes or ``None``. Touches ``.last_access`` on hit."""
if self._disabled:
return None

target = self._file_path(namespace, resource_id, file_path)
if not target.is_file():
return None

try:
data = target.read_bytes()
except OSError:
return None

self._touch_last_access(namespace, resource_id)
return data

def put(self, namespace: str, resource_id: str, file_path: str, data: bytes) -> None:
"""Write *data* to disk, evicting oldest resources if over size limit."""
if self._disabled:
return

# Skip caching entries that exceed the entire cache budget
if len(data) > self._max_size_bytes:
return

try:
# Account for the file being overwritten (net size delta, not gross)
target = self._file_path(namespace, resource_id, file_path)
existing_size = target.stat().st_size if target.is_file() else 0
net_incoming = len(data) - existing_size

current_resource_dir = self._resource_dir(namespace, resource_id)
self._evict_if_needed(net_incoming, protect=current_resource_dir)

target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(data)
self._touch_last_access(namespace, resource_id)
except OSError as exc:
log.warning(f"CloudFileCache: disk write failed ({exc}), disabling cache")
self._disabled = True

# ------------------------------------------------------------------
# Internals
# ------------------------------------------------------------------

def _file_path(self, namespace: str, resource_id: str, file_path: str) -> Path:
target = (self._cache_root / namespace / resource_id / file_path).resolve()
if not target.is_relative_to(self._cache_root.resolve()):
raise ValueError(f"Path traversal detected in cache key: {file_path!r}")
return target

def _resource_dir(self, namespace: str, resource_id: str) -> Path:
return self._cache_root / namespace / resource_id

def _last_access_path(self, namespace: str, resource_id: str) -> Path:
return self._resource_dir(namespace, resource_id) / ".last_access"

def _touch_last_access(self, namespace: str, resource_id: str) -> None:
"""Create or update the ``.last_access`` sentinel in the resource dir."""
path = self._last_access_path(namespace, resource_id)
path.parent.mkdir(parents=True, exist_ok=True)
path.touch()

def _collect_resource_dirs(self) -> Tuple[int, List[Tuple[float, int, Path]]]:
"""Scan cache and return ``(total_size, [(mtime, size, dir), ...])``.

Single pass: computes both the aggregate size and per-resource metadata
needed for LRU eviction.
"""
entries: List[Tuple[float, int, Path]] = []
total_size = 0
if not self._cache_root.exists():
return total_size, entries

for namespace_dir in self._cache_root.iterdir():
if not namespace_dir.is_dir():
continue
for resource_dir in namespace_dir.iterdir():
if not resource_dir.is_dir():
continue
last_access = resource_dir / ".last_access"
mtime = last_access.stat().st_mtime if last_access.exists() else 0.0
size = sum(
f.stat().st_size
for f in resource_dir.rglob("*")
if f.is_file() and f.name != ".last_access"
)
total_size += size
entries.append((mtime, size, resource_dir))
return total_size, entries

def _evict_if_needed(self, incoming_bytes: int, protect: Optional[Path] = None) -> None:
"""Delete oldest resource dirs until total size + *incoming_bytes* fits the budget.

*protect*, if given, is a resource directory that must not be evicted
(the resource currently being populated by the caller).
"""
current_size, entries = self._collect_resource_dirs()
if current_size + incoming_bytes <= self._max_size_bytes:
return

# Sort by last-access time ascending (oldest first)
entries.sort(key=lambda e: e[0])

for _mtime, size, resource_dir in entries:
if current_size + incoming_bytes <= self._max_size_bytes:
break
if protect is not None and resource_dir == protect:
continue
shutil.rmtree(resource_dir, ignore_errors=True)
current_size -= size
9 changes: 9 additions & 0 deletions flow360/component/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ class Geometry(AssetBase):
def __init__(self, id: Union[str, None]):
super().__init__(id)
self.snappy_body_registry = None
self._project_length_unit = None

@property
def face_group_tag(self):
Expand Down Expand Up @@ -447,6 +448,14 @@ def _get_default_geometry_accuracy(simulation_dict: dict) -> LengthType.Positive
else _get_default_geometry_accuracy(simulation_dict=simulation_dict)
)

# Cache project length unit for OBB (avoids extra API call in create_draft)
asset_cache = simulation_dict.get("private_attribute_asset_cache", {})
length_unit_raw = asset_cache.get("project_length_unit")
# pylint: disable=no-member
self._project_length_unit = (
LengthType.validate(length_unit_raw) if length_unit_raw is not None else None
)

@classmethod
# pylint: disable=redefined-builtin
def from_cloud(cls, id: str, **kwargs) -> Geometry:
Expand Down
22 changes: 22 additions & 0 deletions flow360/component/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import typing_extensions
from pydantic import PositiveInt

from flow360.cloud.file_cache import get_shared_cloud_file_cache
from flow360.cloud.flow360_requests import (
CloneVolumeMeshRequest,
LengthUnitType,
Expand Down Expand Up @@ -49,6 +50,9 @@
CoordinateSystemStatus,
)
from flow360.component.simulation.draft_context.mirror import MirrorStatus
from flow360.component.simulation.draft_context.obb.tessellation_loader import (
TessellationFileLoader,
)
from flow360.component.simulation.entity_info import (
GeometryEntityInfo,
merge_geometry_entity_info,
Expand Down Expand Up @@ -292,12 +296,30 @@ def _merge_geometry_entity_info(
cache_key="coordinate_system_status",
)

# Build tessellation loader for geometry-root drafts (enables compute_obb)
tessellation_loader = None
length_unit = None
if isinstance(new_run_from, Geometry):
# pylint: disable=protected-access
geometry_resources: Dict[str, Flow360Resource] = {new_run_from.id: new_run_from._webapi}
geometry_resources.update(
{geo.id: geo._webapi for geo in active_geometry_dependencies.values()}
)
tessellation_loader = TessellationFileLoader(
geometry_resources, get_shared_cloud_file_cache()
)

# Use length unit cached on Geometry during from_cloud (no extra API call)
length_unit = new_run_from._project_length_unit

return DraftContext(
entity_info=entity_info_copy,
mirror_status=mirror_status,
coordinate_system_status=coordinate_system_status,
imported_surfaces=imported_surfaces,
imported_geometries=list(active_geometry_dependencies.values()),
tessellation_loader=tessellation_loader,
length_unit=length_unit,
)


Expand Down
Loading
Loading