-
Notifications
You must be signed in to change notification settings - Fork 568
/
storage.go
142 lines (126 loc) · 4.26 KB
/
storage.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
package chunk
import (
"bytes"
"context"
"encoding/hex"
"time"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/miscutil"
"github.com/pachyderm/pachyderm/v2/src/internal/obj"
"github.com/pachyderm/pachyderm/v2/src/internal/pacherr"
"github.com/pachyderm/pachyderm/v2/src/internal/pachhash"
"github.com/pachyderm/pachyderm/v2/src/internal/pachsql"
"github.com/pachyderm/pachyderm/v2/src/internal/storage/kv"
"github.com/pachyderm/pachyderm/v2/src/internal/storage/track"
)
const (
// TrackerPrefix is the prefix used when creating tracker objects for chunks
TrackerPrefix = "chunk/"
prefix = "chunk"
defaultChunkTTL = 30 * time.Minute
DefaultPrefetchLimit = 10
)
// Storage is an abstraction for interfacing with chunk storage.
// A storage instance:
// - Provides methods for uploading data to chunks and downloading data from chunks.
// - Manages tracker state to keep chunks alive while uploading.
// - Manages an internal chunk cache and work deduplicator (parallel downloads of the same chunk will be deduplicated).
type Storage struct {
objClient obj.Client
db *pachsql.DB
tracker track.Tracker
store kv.Store
memCache *memoryCache
deduper *miscutil.WorkDeduper[pachhash.Output]
prefetchLimit int
createOpts CreateOptions
}
// NewStorage creates a new Storage.
func NewStorage(objC obj.Client, db *pachsql.DB, tracker track.Tracker, opts ...StorageOption) *Storage {
s := &Storage{
objClient: objC,
db: db,
tracker: tracker,
memCache: newMemoryCache(50),
deduper: &miscutil.WorkDeduper[pachhash.Output]{},
prefetchLimit: DefaultPrefetchLimit,
createOpts: CreateOptions{
Compression: CompressionAlgo_GZIP_BEST_SPEED,
},
}
for _, opt := range opts {
opt(s)
}
s.store = kv.NewFromObjectClient(objC)
s.objClient = nil
return s
}
// NewReader creates a new Reader.
func (s *Storage) NewReader(ctx context.Context, dataRefs []*DataRef, opts ...ReaderOption) *Reader {
client := NewClient(s.store, s.db, s.tracker, nil)
defaultOpts := []ReaderOption{WithPrefetchLimit(s.prefetchLimit)}
return newReader(ctx, s, client, dataRefs, append(defaultOpts, opts...)...)
}
func (s *Storage) NewDataReader(ctx context.Context, dataRef *DataRef) *DataReader {
client := NewClient(s.store, s.db, s.tracker, nil)
return newDataReader(ctx, s, client, dataRef, 0)
}
func (s *Storage) PrefetchData(ctx context.Context, dataRef *DataRef) error {
return s.NewDataReader(ctx, dataRef).fetchData()
}
// List lists all of the chunks in object storage.
func (s *Storage) List(ctx context.Context, cb func(id ID) error) error {
return errors.EnsureStack(s.store.Walk(ctx, nil, func(key []byte) error {
return cb(ID(key))
}))
}
// NewDeleter creates a deleter for use with a tracker.GC
func (s *Storage) NewDeleter() track.Deleter {
return &deleter{}
}
// Check runs an integrity check on the objects in object storage.
// It will check objects for chunks with IDs in the range [first, last)
// As a special case: if len(end) == 0 then it is ignored.
func (s *Storage) Check(ctx context.Context, begin, end []byte, readChunks bool) (int, error) {
c := NewClient(s.store, s.db, s.tracker, nil).(*trackedClient)
first := append([]byte{}, begin...)
var count int
for {
n, last, err := c.CheckEntries(ctx, first, 100, readChunks)
count += n
if err != nil {
return count, err
}
if last == nil {
break
}
if len(end) > 0 && bytes.Compare(last, end) > 0 {
break
}
first = kv.KeyAfter(last)
}
return count, nil
}
type memoryCache = lru.Cache[pachhash.Output, []byte]
func newMemoryCache(size int) *memoryCache {
c, err := lru.New[pachhash.Output, []byte](size)
if err != nil {
panic(err)
}
return c
}
func getFromCache(cache *memoryCache, ref *Ref) ([]byte, error) {
key := ref.Key()
chunkData, ok := cache.Get(key)
if !ok {
return nil, pacherr.NewNotExist("chunk-memory", hex.EncodeToString(key[:]))
}
return chunkData, nil
}
// putInCache takes data and inserts it into the cache.
// putInCache consumes data, and data must not be modified after passing it to putInCache.
func putInCache(cache *memoryCache, ref *Ref, data []byte) {
key := ref.Key()
cache.Add(key, data)
}