-
Notifications
You must be signed in to change notification settings - Fork 351
/
tier_fs.go
465 lines (407 loc) · 13.8 KB
/
tier_fs.go
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package pyramid
import (
"context"
"fmt"
"io"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/google/uuid"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/cache"
"github.com/treeverse/lakefs/pkg/logging"
"github.com/treeverse/lakefs/pkg/pyramid/params"
)
// TierFS is a filesystem where written files are never edited.
// All files are stored in the block storage. Local paths are treated as a
// cache layer that will be evicted according to the eviction control.
type TierFS struct {
logger logging.Logger
adapter block.Adapter
eviction params.Eviction
keyLock cache.OnlyOne
syncDir *directory
fileTracker *fileTracker
fsName string
fsLocalBaseDir string
remotePrefix string
}
const workspaceDir = "workspace"
// NewFS creates a new TierFS.
// It will traverse the existing local folders and will update
// the local disk cache to reflect existing files.
func NewFS(c *params.InstanceParams) (FS, error) {
fsLocalBaseDir := filepath.Clean(path.Join(c.Local.BaseDir, c.FSName))
if err := os.MkdirAll(fsLocalBaseDir, os.ModePerm); err != nil {
return nil, fmt.Errorf("creating base dir: %s - %w", fsLocalBaseDir, err)
}
tfs := &TierFS{
adapter: c.Adapter,
fsName: c.FSName,
logger: c.Logger,
fsLocalBaseDir: fsLocalBaseDir,
syncDir: &directory{ceilingDir: fsLocalBaseDir},
keyLock: cache.NewChanOnlyOne(),
remotePrefix: c.BlockStoragePrefix,
}
tfs.fileTracker = NewFileTracker(tfs.removeFromLocalInternal)
if c.Eviction == nil {
var err error
c.Eviction, err = newRistrettoEviction(c.AllocatedBytes(), tfs.removeFromLocal)
if err != nil {
return nil, fmt.Errorf("creating eviction control: %w", err)
}
}
tfs.eviction = c.Eviction
if err := tfs.handleExistingFiles(); err != nil {
return nil, fmt.Errorf("handling existing files: %w", err)
}
return tfs, nil
}
// log returns a logger with added fields from ctx.
func (tfs *TierFS) log(ctx context.Context) logging.Logger {
// TODO(ariels): Does this add the fields? (Uses a different logging path...)
return tfs.logger.WithContext(ctx)
}
// handleExistingFiles should only be called during init of the TierFS.
// It does 2 things:
// 1. Adds stored files to the eviction control
// 2. Remove workspace directories and all its content if it
// exists under the namespace dir.
func (tfs *TierFS) handleExistingFiles() error {
if err := filepath.Walk(tfs.fsLocalBaseDir, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if info.Name() == workspaceDir {
// skipping workspaces and saving them for later delete
if err := os.RemoveAll(p); err != nil {
return fmt.Errorf("removing dir: %w", err)
}
return filepath.SkipDir
}
return nil
}
rPath := strings.TrimPrefix(p, tfs.fsLocalBaseDir)
tfs.storeLocalFile(params.RelativePath(rPath), info.Size())
return nil
}); err != nil {
return fmt.Errorf("walking the fs dir: %w", err)
}
return nil
}
func (tfs *TierFS) removeFromLocal(rPath params.RelativePath, filesize int64) {
// This will be called by the cache eviction mechanism during entry insert.
// We don't want to wait while the file is being removed from the local disk.
evictionHistograms.WithLabelValues(tfs.fsName).Observe(float64(filesize))
// Notify tracker on delete
tfs.fileTracker.Delete(rPath)
}
func (tfs *TierFS) removeFromLocalInternal(rPath params.RelativePath) {
p := path.Join(tfs.fsLocalBaseDir, string(rPath))
if tfs.logger.IsTracing() {
tfs.logger.WithField("path", p).Trace("remove from local")
}
if err := os.Remove(p); err != nil {
tfs.logger.WithError(err).WithField("path", p).Error("Removing file failed")
errorsTotal.WithLabelValues(tfs.fsName, "FileRemoval")
return
}
// Delete Dir async
go func() {
if err := tfs.syncDir.deleteDirRecIfEmpty(path.Dir(string(rPath))); err != nil {
tfs.logger.WithError(err).Error("Failed deleting empty dir")
errorsTotal.WithLabelValues(tfs.fsName, "DirRemoval")
}
}()
}
func (tfs *TierFS) store(ctx context.Context, namespace, originalPath, nsPath, filename string) error {
if tfs.logger.IsTracing() {
tfs.log(ctx).WithFields(logging.Fields{
"namespace": namespace,
"original_path": originalPath,
"ns_path": nsPath,
"filename": filename,
}).Trace("store")
}
f, err := os.Open(originalPath)
if err != nil {
return fmt.Errorf("open file %s: %w", originalPath, err)
}
stat, err := f.Stat()
if err != nil {
return fmt.Errorf("file stat %s: %w", originalPath, err)
}
if err := tfs.adapter.Put(ctx, tfs.objPointer(namespace, filename), stat.Size(), f, block.PutOpts{}); err != nil {
return fmt.Errorf("adapter put %s %s: %w", namespace, filename, err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("closing file %s: %w", filename, err)
}
fileRef := tfs.newLocalFileRef(namespace, nsPath, filename)
if tfs.eviction.Store(fileRef.fsRelativePath, stat.Size()) {
// file was stored by the policy
return tfs.syncDir.renameFile(originalPath, fileRef.fullPath)
} else {
return os.Remove(originalPath)
}
}
func (tfs *TierFS) GetRemoteURI(_ context.Context, _, filename string) (string, error) {
return tfs.blockStoragePath(filename), nil
}
// Create creates a new file in TierFS. File isn't stored in TierFS until a successful close
// operation. Open(namespace, filename) calls will return an error before the close was
// called. Create only performs local operations so it ignores the context.
func (tfs *TierFS) Create(_ context.Context, namespace string) (StoredFile, error) {
nsPath, err := parseNamespacePath(namespace)
if err != nil {
return nil, err
}
if err := tfs.createNSWorkspaceDir(nsPath); err != nil {
return nil, fmt.Errorf("create namespace dir: %w", err)
}
tempPath := tfs.workspaceTempFilePath(nsPath)
fh, err := os.Create(tempPath)
if err != nil {
return nil, fmt.Errorf("creating file: %w", err)
}
return &WRFile{
File: fh,
logger: tfs.logger,
store: func(ctx context.Context, filename string) error {
return tfs.store(ctx, namespace, tempPath, nsPath, filename)
},
abort: func(context.Context) error {
return os.Remove(tempPath)
},
}, nil
}
// Open returns a file descriptor to the local file.
// If the file is missing from the local disk, it will try to fetch it from the block storage.
func (tfs *TierFS) Open(ctx context.Context, namespace, filename string) (File, error) {
nsPath, err := parseNamespacePath(namespace)
if err != nil {
return nil, err
}
if err := validateFilename(filename); err != nil {
return nil, err
}
// check if file is there - without taking the lock
fileRef := tfs.newLocalFileRef(namespace, nsPath, filename)
fh, err := os.Open(fileRef.fullPath)
if err == nil {
if tfs.logger.IsTracing() {
tfs.log(ctx).WithFields(logging.Fields{
"namespace": namespace,
"ns_path": nsPath,
"filename": filename,
}).Trace("opened locally")
}
cacheAccess.WithLabelValues(tfs.fsName, "Hit").Inc()
return tfs.openFile(ctx, fileRef, fh)
}
if !os.IsNotExist(err) {
return nil, fmt.Errorf("open file: %w", err)
}
cacheAccess.WithLabelValues(tfs.fsName, "Miss").Inc()
fh, err = tfs.openWithLock(ctx, fileRef)
if err != nil {
return nil, err
}
return tfs.openFile(ctx, fileRef, fh)
}
func (tfs *TierFS) Exists(ctx context.Context, namespace, filename string) (bool, error) {
cacheAccess.WithLabelValues(tfs.fsName, "Exists").Inc()
return tfs.adapter.Exists(ctx, tfs.objPointer(namespace, filename))
}
// openFile converts an os.File to pyramid.ROFile and updates the eviction control.
func (tfs *TierFS) openFile(ctx context.Context, fileRef localFileRef, fh *os.File) (*ROFile, error) {
stat, err := fh.Stat()
if err != nil {
return nil, fmt.Errorf("file stat: %w", err)
}
if !tfs.eviction.Store(fileRef.fsRelativePath, stat.Size()) {
tfs.fileTracker.Delete(fileRef.fsRelativePath)
tfs.log(ctx).WithFields(logging.Fields{
"namespace": fileRef.namespace,
"file": fileRef.filename,
"full_path": fileRef.fullPath,
}).Info("stored file immediately rejected from cache (delete but continue)")
}
return &ROFile{
File: fh,
rPath: fileRef.fsRelativePath,
eviction: tfs.eviction,
}, nil
}
// openWithLock reads the referenced file from the block storage
// and places it in the local FS for further reading.
// It returns a file handle to the local file.
func (tfs *TierFS) openWithLock(ctx context.Context, fileRef localFileRef) (*os.File, error) {
log := tfs.log(ctx)
if tfs.logger.IsTracing() {
log.WithFields(logging.Fields{
"namespace": fileRef.namespace,
"file": fileRef.filename,
"fullpath": fileRef.fullPath,
}).Trace("try to lock for open")
}
closer := tfs.fileTracker.Open(fileRef.fsRelativePath)
defer closer()
fileFullPath, err := tfs.keyLock.Compute(fileRef.filename, func() (interface{}, error) {
// check again file existence, now that we have the lock
_, err := os.Stat(fileRef.fullPath)
if err == nil {
if log.IsTracing() {
log.WithFields(logging.Fields{
"namespace": fileRef.namespace,
"file": fileRef.filename,
"fullpath": fileRef.fullPath,
}).Trace("got lock; file exists after all")
}
cacheAccess.WithLabelValues(tfs.fsName, "Hit").Inc()
return fileRef.fullPath, nil
}
if !os.IsNotExist(err) {
return nil, fmt.Errorf("stat file: %w", err)
}
if log.IsTracing() {
log.WithFields(logging.Fields{
"namespace": fileRef.namespace,
"file": fileRef.filename,
"fullpath": fileRef.fullPath,
}).Trace("get file from block storage")
}
reader, err := tfs.adapter.Get(ctx, tfs.objPointer(fileRef.namespace, fileRef.filename), 0)
if err != nil {
return nil, fmt.Errorf("read from block storage: %w", err)
}
defer func() { _ = reader.Close() }()
// write to temp file - otherwise the file is available to other readers with partial data
tmpFullPath := fileRef.fullPath + ".tmp"
writer, err := tfs.syncDir.createFile(tmpFullPath)
if err != nil {
return nil, fmt.Errorf("creating file: %w", err)
}
written, err := io.Copy(writer, reader)
if err != nil {
return nil, fmt.Errorf("copying data to file: %w", err)
}
downloadHistograms.WithLabelValues(tfs.fsName).Observe(float64(written))
if err = writer.Close(); err != nil {
return nil, fmt.Errorf("writer close: %w", err)
}
// copy from temp path to actual path
if log.IsTracing() {
log.WithFields(logging.Fields{
"namespace": fileRef.namespace,
"file": fileRef.filename,
"tmp_fullpath": tmpFullPath,
"fullpath": fileRef.fullPath,
}).Trace("rename downloaded file")
}
if err = tfs.syncDir.renameFile(tmpFullPath, fileRef.fullPath); err != nil {
return nil, fmt.Errorf("rename temp file: %w", err)
}
return fileRef.fullPath, nil
})
if err != nil {
return nil, err
}
fh, err := os.Open(fileFullPath.(string))
if err != nil {
return nil, fmt.Errorf("open file: %w", err)
}
return fh, nil
}
func validateFilename(filename string) error {
if strings.HasPrefix(filename, workspaceDir+string(os.PathSeparator)) {
return errPathInWorkspace
}
if strings.Contains(filename, strings.Repeat(string(os.PathSeparator), 2)) { //nolint: gomnd
return errEmptyDirInPath
}
return nil
}
// localFileRef consists of all possible local file references
type localFileRef struct {
namespace string
filename string
fullPath string
fsRelativePath params.RelativePath
}
func (tfs *TierFS) storeLocalFile(rPath params.RelativePath, size int64) {
if !tfs.eviction.Store(rPath, size) {
// Rejected from cache, so deleted. This is safe, but can only happen when
// the cache size was lowered -- so warn.
tfs.logger.WithFields(logging.Fields{
"path": rPath,
"size": size,
}).Warn("existing file immediately rejected from cache on startup (safe if cache size changed; continue)")
// A rare occurrence, (currently) happens when Ristretto cache is not set up
// to perform any caching. So be less strict: prefer to serve the file and
// delete it from the cache. It will be removed from disk when the last
// surviving file descriptor -- returned from this function -- is closed.
if err := os.Remove(string(rPath)); err != nil {
tfs.logger.WithFields(logging.Fields{
"path": rPath,
"size": size,
}).Error("failed to delete immediately-rejected existing file from cache on startup")
return
}
}
}
func (tfs *TierFS) newLocalFileRef(namespace, nsPath, filename string) localFileRef {
rPath := path.Join(nsPath, filename)
return localFileRef{
namespace: namespace,
filename: filename,
fsRelativePath: params.RelativePath(rPath),
fullPath: path.Join(tfs.fsLocalBaseDir, rPath),
}
}
func (tfs *TierFS) objPointer(namespace, filename string) block.ObjectPointer {
if runtime.GOOS == "windows" {
filename = strings.ReplaceAll(filename, `\\`, "/")
}
return block.ObjectPointer{
StorageNamespace: namespace,
Identifier: tfs.blockStoragePath(filename),
}
}
func (tfs *TierFS) blockStoragePath(filename string) string {
return path.Join(tfs.remotePrefix, filename)
}
func (tfs *TierFS) createNSWorkspaceDir(namespace string) error {
return os.MkdirAll(tfs.workspaceDirPath(namespace), os.ModePerm)
}
func (tfs *TierFS) workspaceDirPath(namespace string) string {
return path.Join(tfs.fsLocalBaseDir, namespace, workspaceDir)
}
func (tfs *TierFS) workspaceTempFilePath(namespace string) string {
return path.Join(tfs.workspaceDirPath(namespace), uuid.Must(uuid.NewRandom()).String())
}
func parseNamespacePath(namespace string) (string, error) {
u, err := url.Parse(namespace)
if err != nil {
return "", fmt.Errorf("parse namespace: %w", err)
}
// extract host without port
h := u.Host
idx := strings.Index(h, ":")
if idx != -1 {
h = h[:idx]
}
// namespace path include host if found
var nsPath string
if h == "" {
nsPath = u.Path
} else {
nsPath = h + "/" + u.Path
}
return nsPath, nil
}