-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchivist.go
More file actions
407 lines (355 loc) · 12.9 KB
/
archivist.go
File metadata and controls
407 lines (355 loc) · 12.9 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package blobcache
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"github.com/miretskiy/blobcache/compression"
"github.com/miretskiy/blobcache/internal/index"
"github.com/miretskiy/blobcache/internal/record"
"github.com/miretskiy/dio/align"
"github.com/miretskiy/dio/iosched"
"github.com/miretskiy/dio/sys"
)
// Archivist manages read-only access to persisted segments.
// It uses the Index Item contract: Offset points to Magic, PhysicalLen = 42 + KeyLen + PhysSize.
//
// If readCache is non-nil, ReadBlob checks it before going to disk.
// The ReadCache is transparent to callers — they see the same API.
type Archivist struct {
config
index *index.DurableIndex
sched *iosched.BlockingIO
cache sync.Map // segmentID (uint32) -> *os.File
readCache *ReadCache // nil if disabled
flights *inflightGroup
// readSem limits concurrent pread syscalls (nil = unlimited).
// Each slot is a struct{} in the channel; acquire = send, release = receive.
// See DESIGN.md §11.6.
readSem chan struct{}
}
func NewArchivist(cfg config, idx *index.DurableIndex, sched *iosched.BlockingIO) *Archivist {
if sched == nil {
sched = iosched.NewBlockingIO(nil)
}
a := &Archivist{
config: cfg,
index: idx,
sched: sched,
flights: newInflightGroup(),
}
if cfg.MaxReadConcurrency > 0 {
a.readSem = make(chan struct{}, cfg.MaxReadConcurrency)
}
return a
}
// Close closes the read cache (if any), all cached segment file handles,
// and the I/O scheduler.
func (a *Archivist) Close() error {
// Close read cache first (before closing segment FDs it depends on).
if a.readCache != nil {
a.readCache.Close()
}
var errs []error
a.cache.Range(func(key, value any) bool {
if closer, ok := value.(io.Closer); ok {
if err := closer.Close(); err != nil {
errs = append(errs, err)
}
}
a.cache.Delete(key)
return true
})
if err := a.sched.Close(); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}
// ReadBlob returns the value bytes for the specified index entry.
// It handles decompression and checksum verification.
// The caller MUST call the returned Releaser when done with the data.
//
// If a ReadCache is configured, it is checked first. On cache miss, the
// Archivist performs exactly ONE disk read. If the blob is admissible for
// caching, the disk read may be widened to a 64KB chunk and the raw bytes
// are populated into the cache inline (no extra goroutines).
//
// expectedKey is used to verify the stored key matches (detects 128-bit hash collisions).
func (a *Archivist) ReadBlob(e index.Item, expectedKey []byte) ([]byte, Releaser, error) {
if a.readCache != nil {
// Check cache first.
if data, storedKey, rel, found := a.readCache.Acquire(e.Key); found {
a.readCache.hits.Add(1)
if expectedKey != nil && !bytes.Equal(storedKey, expectedKey) {
rel.Release()
return nil, Releaser{}, record.ErrKeyMismatch
}
return data, rel, nil
}
a.readCache.misses.Add(1)
// Cache miss — if admissible, do a single widened read and populate.
if a.readCache.Admissible(int64(e.PhysicalLen)) {
a.acquireReadSem()
defer a.releaseReadSem()
return a.readBlobWithPrefetch(e, expectedKey)
}
}
// No cache or blob not admissible — standard disk read.
a.acquireReadSem()
defer a.releaseReadSem()
return a.readBlobFromDisk(e, expectedKey)
}
// acquireReadSem acquires one pread concurrency permit (no-op if unlimited).
// Callers block on the Go channel scheduler — no OS thread consumed.
func (a *Archivist) acquireReadSem() {
if a.readSem != nil {
a.readSem <- struct{}{}
}
}
// releaseReadSem returns one permit to the semaphore.
func (a *Archivist) releaseReadSem() {
if a.readSem != nil {
<-a.readSem
}
}
// readBlobWithPrefetch coalesces concurrent cache misses for the same disk
// region. The "leader" goroutine performs exactly ONE disk read, populates
// the ReadCache, and parses the target record from the read buffer.
// "Waiter" goroutines block until the leader finishes, then serve from cache.
//
// For small blobs (≤ prefetchChunkSize): reads a 64KB aligned chunk and
// populates all valid records in the chunk (temporal prefetch).
// For large blobs (> prefetchChunkSize): reads the exact blob with page
// alignment and inserts only that record.
func (a *Archivist) readBlobWithPrefetch(e index.Item, expectedKey []byte) ([]byte, Releaser, error) {
blobOff := int64(e.Offset)
blobLen := int(e.PhysicalLen)
// Compute coalescing key. Small blobs in the same 64KB chunk share a key.
var fkey uint64
if blobLen <= prefetchChunkSize {
chunkOff := blobOff &^ (int64(prefetchChunkSize) - 1)
fkey = flightKey(e.SegmentID, chunkOff)
} else {
fkey = flightKey(e.SegmentID, blobOff)
}
// Leader captures its result through the closure.
var leaderData []byte
var leaderRel Releaser
var leaderErr error
isLeader := a.flights.DoOnce(fkey, func() {
leaderData, leaderRel, leaderErr = a.prefetchAndParse(e, expectedKey)
})
if isLeader {
return leaderData, leaderRel, leaderErr
}
// Waiter: leader populated the cache. Re-check.
if data, storedKey, rel, found := a.readCache.Acquire(e.Key); found {
a.readCache.hits.Add(1)
if expectedKey != nil && !bytes.Equal(storedKey, expectedKey) {
rel.Release()
return nil, Releaser{}, record.ErrKeyMismatch
}
return data, rel, nil
}
// Cache miss after leader finished (e.g., chunk at segment boundary,
// or record was at the edge and couldn't be parsed). Fall back.
return a.readBlobFromDisk(e, expectedKey)
}
// prefetchAndParse performs the single disk read, populates the cache, and
// parses the target record. Called only by the flight leader.
func (a *Archivist) prefetchAndParse(e index.Item, expectedKey []byte) ([]byte, Releaser, error) {
shard := a.index.SegmentLockShard(e.SegmentID)
shard.RLock()
defer shard.RUnlock()
sf, err := a.getSegmentFile(e.SegmentID)
if err != nil {
return nil, Releaser{}, fmt.Errorf("storage: segment %d not found: %w", e.SegmentID, err)
}
blobOff := int64(e.Offset)
blobLen := int(e.PhysicalLen)
// Determine read region: 64KB chunk for small blobs, exact for large.
// The read must always cover the entire target blob. When a blob starts
// near the end of a chunk, its tail may extend beyond — extend the read.
var readOff, readLen int64
if blobLen <= prefetchChunkSize {
chunkOff := blobOff &^ (int64(prefetchChunkSize) - 1)
neededLen := int(blobOff-chunkOff) + blobLen
if neededLen < prefetchChunkSize {
neededLen = prefetchChunkSize
}
readOff, readLen = align.AlignRange(chunkOff, neededLen)
} else {
readOff, readLen = align.AlignRange(blobOff, blobLen)
}
handle := AcquireAlignedBuffer(int(readLen), int(readLen))
buf := handle.Bytes()
n, err := a.sched.ReadAt(sf, buf, readOff)
if err != nil {
handle.Release()
return nil, Releaser{}, fmt.Errorf("storage: prefetch read failed: %w", err)
}
// Ensure we read enough for the target record.
targetEnd := int(blobOff-readOff) + blobLen
if n < targetEnd {
handle.Release()
return nil, Releaser{}, fmt.Errorf("storage: short prefetch read: got %d, need %d", n, targetEnd)
}
// Populate cache BEFORE parseRecord (which may release handle on decompression).
if blobLen <= prefetchChunkSize {
a.readCache.PopulateChunk(buf[:n])
} else {
lead := int(blobOff - readOff)
a.readCache.Insert(e.Key, buf[lead:lead+blobLen])
}
// Extract and parse the target record.
lead := int(blobOff - readOff)
rec := buf[lead : lead+blobLen]
return a.parseRecord(rec, e, expectedKey, Releaser{bh: &handle}, func() { handle.Release() })
}
// readBlobFromDisk performs the disk read with segment locking.
func (a *Archivist) readBlobFromDisk(e index.Item, expectedKey []byte) ([]byte, Releaser, error) {
// Acquire read lock: Prevent segment deletion & FD close during I/O
shard := a.index.SegmentLockShard(e.SegmentID)
shard.RLock()
defer shard.RUnlock()
sf, err := a.getSegmentFile(e.SegmentID)
if err != nil {
return nil, Releaser{}, fmt.Errorf("storage: segment %d not found: %w", e.SegmentID, err)
}
if a.IO.DirectIORead {
return a.readBlobDirect(sf, e, expectedKey)
}
return a.readBlobBuffered(sf, e, expectedKey)
}
// readBlobBuffered reads a record using buffered I/O (kernel page cache).
func (a *Archivist) readBlobBuffered(
sf *os.File, e index.Item, expectedKey []byte,
) ([]byte, Releaser, error) {
handle := AcquireBuffer(int(e.PhysicalLen), int(e.PhysicalLen))
buf := handle.Bytes()
n, err := a.sched.ReadAt(sf, buf, int64(e.Offset))
if err != nil {
handle.Release()
return nil, Releaser{}, fmt.Errorf("storage: read failed: %w", err)
}
if n < len(buf) {
handle.Release()
return nil, Releaser{}, fmt.Errorf("storage: short read: got %d of %d bytes", n, len(buf))
}
return a.parseRecord(buf, e, expectedKey, Releaser{bh: &handle}, func() { handle.Release() })
}
// readBlobDirect reads a record using Direct I/O with aligned buffers.
func (a *Archivist) readBlobDirect(
sf *os.File, e index.Item, expectedKey []byte,
) ([]byte, Releaser, error) {
alignedOff, alignedLen := align.AlignRange(int64(e.Offset), int(e.PhysicalLen))
handle := AcquireAlignedBuffer(int(alignedLen), int(alignedLen))
buf := handle.Bytes()
n, err := a.sched.ReadAt(sf, buf, alignedOff)
if err != nil {
handle.Release()
return nil, Releaser{}, fmt.Errorf("storage: direct read failed: %w", err)
}
if n < len(buf) {
handle.Release()
return nil, Releaser{}, fmt.Errorf("storage: short direct read: got %d of %d bytes", n, len(buf))
}
lead := int(int64(e.Offset) - alignedOff)
rec := buf[lead : lead+int(e.PhysicalLen)]
return a.parseRecord(rec, e, expectedKey, Releaser{bh: &handle}, func() { handle.Release() })
}
// parseRecord parses a record buffer, handles decompression and checksum verification.
// owner is the Releaser that owns the buffer backing rec. onError is called to free
// the buffer on failure paths (before decompression replaces it).
func (a *Archivist) parseRecord(
rec []byte, e index.Item, expectedKey []byte, owner Releaser, onError func(),
) ([]byte, Releaser, error) {
hdr, err := record.DecodeHeader(rec[:record.HeaderSize])
if err != nil {
onError()
return nil, Releaser{}, fmt.Errorf("storage: invalid header: %w", err)
}
keyEnd := record.HeaderSize + int(hdr.KeyLen)
if expectedKey != nil && !bytes.Equal(rec[record.HeaderSize:keyEnd], expectedKey) {
onError()
return nil, Releaser{}, record.ErrKeyMismatch
}
valueData := rec[keyEnd:]
releaser := owner
if e.IsCompressed() {
decompressedHandle := AcquireBuffer(int(hdr.LogicalSize), int(hdr.LogicalSize))
dstBuf := decompressedHandle.Bytes()
if err := compression.Decompress(e.Compression(), dstBuf, valueData); err != nil {
log.Error("decompression failed",
"codec", e.Compression(),
"logical_size", hdr.LogicalSize,
"physical_size", hdr.PhysicalSize,
"value_data_len", len(valueData),
"dst_buf_len", len(dstBuf),
"dst_buf_cap", cap(dstBuf),
"error", err)
onError()
decompressedHandle.Release()
return nil, Releaser{}, err
}
owner.Release() // Free the read buffer; decompressed data is independent.
valueData = decompressedHandle.Bytes()
releaser = Releaser{bh: &decompressedHandle}
}
if a.Resilience.VerifyOnRead && hdr.HasValidCRC() && a.Resilience.ChecksumHasher != nil {
if err := verifyChecksum(valueData, a.Resilience.ChecksumHasher, hdr.CRC()); err != nil {
releaser.Release()
return nil, Releaser{}, err
}
}
return valueData, releaser, nil
}
// getSegmentPath returns the path for a segment file
func getSegmentPath(basePath string, numShards int, segmentID uint32) string {
shardNo := segmentID % uint32(max(1, numShards))
return filepath.Join(basePath, "segments",
fmt.Sprintf("%04d", shardNo),
fmt.Sprintf("%d.seg", segmentID),
)
}
// GetFooterPath returns the path for a segment's metadata file (.meta).
func GetFooterPath(basePath string, numShards int, segmentID uint32) string {
return SegmentMetaPath(getSegmentPath(basePath, numShards, segmentID))
}
// getSegmentFile returns cached SegmentFile or opens it
func (a *Archivist) getSegmentFile(segmentID uint32) (*os.File, error) {
if cached, ok := a.cache.Load(segmentID); ok {
return cached.(*os.File), nil
}
path := getSegmentPath(a.Path, a.Shards, segmentID)
var flags sys.OpenFlag
if a.IO.DirectIORead {
flags |= sys.FlDirectIO
}
f, err := sys.OpenDirect(path, flags)
if err != nil {
return nil, err
}
// fadvise is meaningless with Direct I/O (no page cache).
if a.IO.Fadvise && !a.IO.DirectIORead {
if err := sys.Fadvise(f, 0, 0, sys.FadvRandom); err != nil {
log.Warn("fadvise failed", "segID", segmentID, "err", err)
}
}
actual, loaded := a.cache.LoadOrStore(segmentID, f)
if loaded {
_ = f.Close()
return actual.(*os.File), nil
}
return f, nil
}
// DropSegmentCache closes and removes a segment's cached file handle.
// Called before deleting segment files during compaction.
func (a *Archivist) DropSegmentCache(segmentID uint32) {
if val, ok := a.cache.LoadAndDelete(segmentID); ok {
_ = val.(*os.File).Close()
}
}