File size: 5,664 Bytes
02626a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from __future__ import annotations

import contextlib
import fcntl
import hashlib
import os
import shutil
import time
from pathlib import Path


def _enabled() -> bool:
    value = os.environ.get("PSI_OMNI_SQLITE_LOCAL_CACHE", "0").strip().lower()
    return value not in {"0", "false", "no", "off"}


def _cache_root() -> Path:
    return Path(os.environ.get("PSI_OMNI_SQLITE_LOCAL_CACHE_DIR", "/tmp/psi0_omni_sqlite_index_cache"))


def _fallback_root() -> Path | None:
    value = os.environ.get("PSI_OMNI_SQLITE_FALLBACK_ROOT", "").strip()
    return Path(value) if value else None


def _fallback_path(db_path: Path) -> Path | None:
    root = _fallback_root()
    if root is None:
        return None
    parts = db_path.parts
    try:
        tasks_index = len(parts) - 1 - tuple(reversed(parts)).index("tasks")
        task_name = parts[tasks_index + 1]
    except (ValueError, IndexError):
        return None
    return root / task_name / db_path.name


def _is_valid_sqlite_file(path: Path, *, expected_size: int | None = None) -> bool:
    try:
        stat = path.stat()
        if stat.st_size < 16:
            return False
        if expected_size is not None and stat.st_size != int(expected_size):
            return False
        with path.open("rb") as handle:
            return handle.read(16) == b"SQLite format 3\x00"
    except OSError:
        return False


@contextlib.contextmanager
def _cache_lock(lock_path: Path):
    try:
        timeout = max(float(os.environ.get("PSI_OMNI_SQLITE_LOCAL_CACHE_LOCK_TIMEOUT", "1800")), 1.0)
    except ValueError:
        timeout = 1800.0
    deadline = time.monotonic() + timeout
    lock_file = lock_path.open("a+b")
    try:
        while True:
            try:
                fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
                break
            except BlockingIOError:
                if time.monotonic() >= deadline:
                    raise TimeoutError(f"Timed out waiting for SQLite cache lock: {lock_path}")
                time.sleep(0.1)
        yield
    finally:
        try:
            fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
        finally:
            lock_file.close()


def local_sqlite_path(db_path: Path, *, expected_size: int | None = None) -> Path:
    """Return a verified read-only SQLite source, optionally staged in /tmp.

    The OSS/FUSE source must never be opened in SQLite read-write mode.  A
    missing, empty, truncated, or non-SQLite source is rejected before it can
    replace a valid local cache.  Task-level indexes may fall back to a
    separately preserved CPFS copy via ``PSI_OMNI_SQLITE_FALLBACK_ROOT``.
    """

    db_path = Path(db_path)
    fallback_path = _fallback_path(db_path)
    if _is_valid_sqlite_file(db_path, expected_size=expected_size):
        source_path = db_path
    elif fallback_path is not None and _is_valid_sqlite_file(
        fallback_path, expected_size=expected_size
    ):
        source_path = fallback_path
        if int(os.environ.get("RANK", "0")) == 0:
            print(
                f"[OMNI SQLITE FALLBACK] invalid source={db_path}; using={fallback_path}",
                flush=True,
            )
    else:
        source_path = None

    digest = hashlib.sha1(str(db_path.parent).encode("utf-8")).hexdigest()[:16]
    local_dir = _cache_root() / digest
    local_path = local_dir / db_path.name

    # An already verified local copy is preferable to returning a corrupt OSS
    # source, even when the preserved CPFS fallback is temporarily unavailable.
    if source_path is None:
        if _is_valid_sqlite_file(local_path, expected_size=expected_size):
            return local_path
        raise RuntimeError(
            "No valid SQLite index is available: "
            f"source={db_path}, fallback={fallback_path}, local={local_path}, "
            f"expected_size={expected_size}"
        )

    if not _enabled():
        return source_path

    source_stat = source_path.stat()
    local_dir.mkdir(parents=True, exist_ok=True)

    try:
        local_stat = local_path.stat()
        if (
            local_stat.st_size == source_stat.st_size
            and local_stat.st_mtime >= source_stat.st_mtime
            and _is_valid_sqlite_file(local_path, expected_size=expected_size)
        ):
            return local_path
    except OSError:
        pass

    lock_path = local_path.with_suffix(local_path.suffix + ".lock")
    with _cache_lock(lock_path):
        try:
            local_stat = local_path.stat()
            if (
                local_stat.st_size == source_stat.st_size
                and local_stat.st_mtime >= source_stat.st_mtime
                and _is_valid_sqlite_file(local_path, expected_size=expected_size)
            ):
                return local_path
        except OSError:
            pass

        tmp_path = local_path.with_suffix(local_path.suffix + f".tmp.{os.getpid()}")
        try:
            shutil.copy2(source_path, tmp_path)
            if not _is_valid_sqlite_file(tmp_path, expected_size=expected_size):
                raise RuntimeError(
                    f"SQLite cache copy failed validation: {source_path} -> {tmp_path}"
                )
            if tmp_path.stat().st_size != source_stat.st_size:
                raise RuntimeError(
                    f"SQLite cache copy size mismatch: source={source_stat.st_size}, "
                    f"copy={tmp_path.stat().st_size}, path={source_path}"
                )
            os.replace(tmp_path, local_path)
        finally:
            try:
                tmp_path.unlink()
            except OSError:
                pass
    return local_path