File size: 8,347 Bytes
ff141ad
 
7ef8b2c
ff141ad
 
e31916b
ff141ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""Byte-bounded disk cache and HTTP range reader.

Shared verbatim with the OCR half (06-multilingual/ocr): the caching problem is identical once the unit
is "a parquet row group", and a divergent copy would be a second thing to get right.
"""

import fcntl
import hashlib
import io
import os
import threading
import time
import uuid
from collections import Counter
from contextlib import contextmanager
from pathlib import Path

import requests


class DiskCache:
    def __init__(self, directory, max_bytes, max_entry_bytes=None):
        self.directory = Path(directory)
        self.directory.mkdir(parents=True, exist_ok=True)
        self.max_bytes = int(max_bytes)
        self.max_entry_bytes = min(int(max_entry_bytes or max_bytes), self.max_bytes)
        if self.max_entry_bytes < 1:
            raise ValueError("Cache budgets must be positive")
        self.counters = Counter()
        self._stats_lock = threading.Lock()
        # Recover staging space after a process was killed before its finally block.
        # A live writer owns this stripe, including writers in another process.
        for path in self.directory.glob("*.part"):
            stripe = path.name[:3]
            with (self.directory / f"stripe-{stripe}.lock").open("a+b") as lock:
                try:
                    fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
                except BlockingIOError:
                    continue
                path.unlink(missing_ok=True)

    def increment(self, key, value=1):
        with self._stats_lock:
            self.counters[key] += value

    def stats(self):
        with self._stats_lock:
            result = dict(self.counters)
        files = list(self.directory.glob("*.bin"))
        sizes = []
        for path in files:
            try:
                sizes.append(path.stat().st_size)
            except FileNotFoundError:
                pass
        return {
            **result,
            "bytes": sum(sizes),
            "entries": len(sizes),
            "max_bytes": self.max_bytes,
            "max_entry_bytes": self.max_entry_bytes,
        }

    @contextmanager
    def lease(self, key, loader):
        digest = hashlib.sha256(key.encode()).hexdigest()
        path = self.directory / f"{digest}.bin"
        # Fixed lock stripes avoid millions of permanent lock files over full epochs.
        # A stripe covers both loading and reading; collisions only serialize work.
        stripe = digest[:3]
        with (self.directory / f"stripe-{stripe}.lock").open("a+b") as lock:
            fcntl.flock(lock, fcntl.LOCK_EX)
            if path.exists():
                self.increment("hits")
            else:
                self.increment("misses")
                temporary = self.directory / f"{digest}.{uuid.uuid4().hex}.part"
                try:
                    loader(temporary)
                    size = temporary.stat().st_size
                    if size > self.max_entry_bytes:
                        raise ValueError(
                            f"Cache entry {size} exceeds {self.max_entry_bytes} byte limit"
                        )
                    with (self.directory / ".eviction.lock").open("a+b") as eviction:
                        fcntl.flock(eviction, fcntl.LOCK_EX)
                        self._make_room(size, stripe)
                        os.replace(temporary, path)
                    self.increment("loads")
                    self.increment("loaded_bytes", size)
                except BaseException:
                    self.increment("load_failures")
                    raise
                finally:
                    temporary.unlink(missing_ok=True)
            os.utime(path, None)
            yield path

    def _make_room(self, needed, owned_stripe):
        entries = [
            (p.stat().st_mtime_ns, p.stat().st_size, p)
            for p in self.directory.glob("*.bin")
        ]
        used = sum(size for _, size, _ in entries)
        for _, size, path in sorted(entries):
            if used + needed <= self.max_bytes:
                return
            stripe = path.stem[:3]
            if stripe == owned_stripe:
                # Our exclusive stripe already proves no other reader holds this entry.
                path.unlink(missing_ok=True)
                used -= size
                self.increment("evictions")
                continue
            with (self.directory / f"stripe-{stripe}.lock").open("a+b") as lock:
                try:
                    fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
                except BlockingIOError:
                    continue
                path.unlink(missing_ok=True)
                used -= size
                self.increment("evictions")
        if used + needed > self.max_bytes:
            raise RuntimeError(
                "Cache budget is occupied by active readers; reduce prefetch or increase cache bytes"
            )


class RangeReader(io.RawIOBase):
    """Seekable HTTP object: require exact 206 responses and meter actual payload bytes."""

    def __init__(self, url, size, *, headers=None, on_read=None, timeout=120):
        super().__init__()
        self.url, self.size, self.position = url, size, 0
        self.headers = headers or {}
        self.on_read = on_read or (lambda amount: None)
        self.timeout = timeout
        self.session = requests.Session()
        self._lock = threading.RLock()

    def readable(self):
        return True

    def seekable(self):
        return True

    def tell(self):
        return self.position

    def seek(self, offset, whence=0):
        with self._lock:
            position = (
                offset
                if whence == 0
                else self.position + offset
                if whence == 1
                else self.size + offset
            )
            if position < 0 or whence not in (0, 1, 2):
                raise ValueError("Invalid seek")
            self.position = position
            return position

    def read(self, size=-1):
        with self._lock:
            start = self.position
            stop = self.size if size < 0 else min(start + size, self.size)
            if start >= stop:
                return b""
            for attempt in range(3):
                try:
                    with self.session.get(
                        self.url,
                        headers={**self.headers, "Range": f"bytes={start}-{stop - 1}"},
                        timeout=(15, self.timeout),
                        stream=True,
                    ) as response:
                        response.raise_for_status()
                        if response.status_code != 206:
                            raise ValueError(
                                "Storage ignored byte range; refusing a whole-shard download"
                            )
                        if (
                            response.headers.get("Content-Range")
                            != f"bytes {start}-{stop - 1}/{self.size}"
                        ):
                            raise ValueError(
                                "Storage returned an unexpected byte range or object size"
                            )
                        data = bytearray()
                        for chunk in response.iter_content(1024 * 1024):
                            self.on_read(len(chunk))
                            data.extend(chunk)
                            if len(data) > stop - start:
                                raise ValueError(
                                    "Storage returned more bytes than requested"
                                )
                        if len(data) != stop - start:
                            raise requests.ConnectionError(
                                "Incomplete byte-range response"
                            )
                    self.position = stop
                    return bytes(data)
                except (requests.ConnectionError, requests.Timeout):
                    if attempt == 2:
                        raise
                    time.sleep(0.25 * (attempt + 1))

    def readinto(self, buffer):
        value = self.read(len(buffer))
        buffer[: len(value)] = value
        return len(value)

    def close(self):
        self.session.close()
        super().close()