-
Notifications
You must be signed in to change notification settings - Fork 351
/
manager.go
342 lines (307 loc) · 10.5 KB
/
manager.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
package committed
import (
"bytes"
"context"
"errors"
"fmt"
"sort"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/logging"
)
type committedManager struct {
metaRangeManager MetaRangeManager
RangeManager RangeManager
params *Params
}
func NewCommittedManager(m MetaRangeManager, r RangeManager, p Params) graveler.CommittedManager {
return &committedManager{
metaRangeManager: m,
RangeManager: r,
params: &p,
}
}
func (c *committedManager) Exists(ctx context.Context, ns graveler.StorageNamespace, id graveler.MetaRangeID) (bool, error) {
return c.metaRangeManager.Exists(ctx, ns, id)
}
func (c *committedManager) Get(ctx context.Context, ns graveler.StorageNamespace, rangeID graveler.MetaRangeID, key graveler.Key) (*graveler.Value, error) {
it, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, rangeID)
if err != nil {
return nil, err
}
valIt := NewValueIterator(it)
defer valIt.Close()
valIt.SeekGE(key)
// return the next value
if !valIt.Next() {
// error or not found
if err := valIt.Err(); err != nil {
return nil, err
}
return nil, graveler.ErrNotFound
}
// compare the key we found
rec := valIt.Value()
if !bytes.Equal(rec.Key, key) {
return nil, graveler.ErrNotFound
}
return rec.Value, nil
}
func (c *committedManager) List(ctx context.Context, ns graveler.StorageNamespace, rangeID graveler.MetaRangeID) (graveler.ValueIterator, error) {
it, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, rangeID)
if err != nil {
return nil, err
}
return NewValueIterator(it), nil
}
func (c *committedManager) WriteRange(ctx context.Context, ns graveler.StorageNamespace, it graveler.ValueIterator) (*graveler.RangeInfo, error) {
writer, err := c.RangeManager.GetWriter(ctx, Namespace(ns), nil)
if err != nil {
return nil, fmt.Errorf("failed creating range writer: %w", err)
}
writer.SetMetadata(MetadataTypeKey, MetadataRangesType)
defer func() {
if err := writer.Abort(); err != nil {
logging.FromContext(ctx).WithError(err).Error("Aborting write to range")
}
}()
for it.Next() {
record := *it.Value()
v, err := MarshalValue(record.Value)
if err != nil {
return nil, err
}
if err := writer.WriteRecord(Record{Key: Key(record.Key), Value: v}); err != nil {
return nil, fmt.Errorf("writing record: %w", err)
}
if writer.ShouldBreakAtKey(record.Key, c.params) {
break
}
}
if err := it.Err(); err != nil {
return nil, fmt.Errorf("getting value from iterator: %w", err)
}
info, err := writer.Close()
if err != nil {
return nil, fmt.Errorf("closing writer: %w", err)
}
return &graveler.RangeInfo{
ID: graveler.RangeID(info.RangeID),
MinKey: graveler.Key(info.First),
MaxKey: graveler.Key(info.Last),
Count: info.Count,
EstimatedRangeSizeBytes: info.EstimatedRangeSizeBytes,
}, nil
}
func (c *committedManager) WriteMetaRange(ctx context.Context, ns graveler.StorageNamespace, ranges []*graveler.RangeInfo) (*graveler.MetaRangeInfo, error) {
writer := c.metaRangeManager.NewWriter(ctx, ns, nil)
defer func() {
if err := writer.Abort(); err != nil {
logging.FromContext(ctx).WithError(err).Error("Aborting write to meta range")
}
}()
sort.Slice(ranges, func(i, j int) bool {
return bytes.Compare(ranges[i].MinKey, ranges[j].MinKey) < 0
})
for _, r := range ranges {
if err := writer.WriteRange(Range{
ID: ID(r.ID),
MinKey: Key(r.MinKey),
MaxKey: Key(r.MaxKey),
EstimatedSize: r.EstimatedRangeSizeBytes,
Count: int64(r.Count),
Tombstone: false,
}); err != nil {
logging.FromContext(ctx).WithError(err).Error("Aborting writing range to meta range")
return nil, fmt.Errorf("writing range: %w", err)
}
}
id, err := writer.Close(ctx)
if err != nil {
return nil, fmt.Errorf("closing metarange: %w", err)
}
return &graveler.MetaRangeInfo{
ID: *id,
}, nil
}
func (c *committedManager) WriteMetaRangeByIterator(ctx context.Context, ns graveler.StorageNamespace, it graveler.ValueIterator, metadata graveler.Metadata) (*graveler.MetaRangeID, error) {
writer := c.metaRangeManager.NewWriter(ctx, ns, metadata)
defer func() {
if err := writer.Abort(); err != nil {
logging.FromContext(ctx).WithError(err).Error("Aborting write to meta range")
}
}()
for it.Next() {
if err := writer.WriteRecord(*it.Value()); err != nil {
return nil, fmt.Errorf("writing record: %w", err)
}
}
if err := it.Err(); err != nil {
return nil, fmt.Errorf("getting value from iterator: %w", err)
}
id, err := writer.Close(ctx)
if err != nil {
return nil, fmt.Errorf("closing writer: %w", err)
}
return id, nil
}
func (c *committedManager) Diff(ctx context.Context, ns graveler.StorageNamespace, left, right graveler.MetaRangeID) (graveler.DiffIterator, error) {
leftIt, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, left)
if err != nil {
return nil, err
}
rightIt, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, right)
if err != nil {
return nil, err
}
return NewDiffValueIterator(ctx, leftIt, rightIt), nil
}
func (c *committedManager) Import(ctx context.Context, ns graveler.StorageNamespace, destination, source graveler.MetaRangeID, prefixes []graveler.Prefix) (graveler.MetaRangeID, error) {
destIt, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, destination)
if err != nil {
return "", fmt.Errorf("get destination iterator: %w", err)
}
destIt = NewSkipPrefixIterator(prefixes, destIt)
defer destIt.Close()
mctx := mergeContext{
destIt: destIt,
strategy: graveler.MergeStrategyNone,
ns: ns,
destinationID: destination,
sourceID: source,
baseID: "",
}
return c.merge(ctx, mctx)
}
func (c *committedManager) Merge(ctx context.Context, ns graveler.StorageNamespace, destination, source, base graveler.MetaRangeID, strategy graveler.MergeStrategy) (graveler.MetaRangeID, error) {
if source == base {
// no changes on source
return "", graveler.ErrNoChanges
}
if destination == base {
// changes introduced only on source
return source, nil
}
mctx := mergeContext{
strategy: strategy,
ns: ns,
destinationID: destination,
sourceID: source,
baseID: base,
}
return c.merge(ctx, mctx)
}
type mergeContext struct {
destIt Iterator
srcIt Iterator
baseIt Iterator
strategy graveler.MergeStrategy
ns graveler.StorageNamespace
destinationID graveler.MetaRangeID
sourceID graveler.MetaRangeID
baseID graveler.MetaRangeID
}
func (c *committedManager) merge(ctx context.Context, mctx mergeContext) (graveler.MetaRangeID, error) {
var err error = nil
baseIt := mctx.baseIt
if baseIt == nil {
baseIt, err = c.metaRangeManager.NewMetaRangeIterator(ctx, mctx.ns, mctx.baseID)
if err != nil {
return "", fmt.Errorf("get base iterator: %w", err)
}
defer baseIt.Close()
}
destIt := mctx.destIt
if destIt == nil {
destIt, err = c.metaRangeManager.NewMetaRangeIterator(ctx, mctx.ns, mctx.destinationID)
if err != nil {
return "", fmt.Errorf("get destination iterator: %w", err)
}
defer destIt.Close()
}
srcIt := mctx.srcIt
if srcIt == nil {
srcIt, err = c.metaRangeManager.NewMetaRangeIterator(ctx, mctx.ns, mctx.sourceID)
if err != nil {
return "", fmt.Errorf("get source iterator: %w", err)
}
defer srcIt.Close()
}
mwWriter := c.metaRangeManager.NewWriter(ctx, mctx.ns, nil)
defer func() {
err = mwWriter.Abort()
if err != nil {
logging.FromContext(ctx).WithError(err).Error("Abort failed after Merge")
}
}()
err = Merge(ctx, mwWriter, baseIt, srcIt, destIt, mctx.strategy)
if err != nil {
if !errors.Is(err, graveler.ErrUserVisible) {
err = fmt.Errorf("merge ns=%s id=%s: %w", mctx.ns, mctx.destinationID, err)
}
return "", err
}
newID, err := mwWriter.Close(ctx)
if newID == nil {
return "", fmt.Errorf("close writer ns=%s id=%s: %w", mctx.ns, mctx.destinationID, err)
}
return *newID, err
}
func (c *committedManager) Commit(ctx context.Context, ns graveler.StorageNamespace, baseMetaRangeID graveler.MetaRangeID, changes graveler.ValueIterator) (graveler.MetaRangeID, graveler.DiffSummary, error) {
mwWriter := c.metaRangeManager.NewWriter(ctx, ns, nil)
defer func() {
err := mwWriter.Abort()
if err != nil {
logging.FromContext(ctx).WithError(err).Error("Abort failed after Commit")
}
}()
metaRangeIterator, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, baseMetaRangeID)
summary := graveler.DiffSummary{
Count: map[graveler.DiffType]int{},
}
if err != nil {
return "", summary, fmt.Errorf("get metarange ns=%s id=%s: %w", ns, baseMetaRangeID, err)
}
defer metaRangeIterator.Close()
summary, err = Commit(ctx, mwWriter, metaRangeIterator, changes, &CommitOptions{})
if err != nil {
if !errors.Is(err, graveler.ErrUserVisible) {
err = fmt.Errorf("commit ns=%s id=%s: %w", ns, baseMetaRangeID, err)
}
return "", summary, err
}
newID, err := mwWriter.Close(ctx)
if newID == nil {
return "", summary, fmt.Errorf("close writer ns=%s metarange id=%s: %w", ns, baseMetaRangeID, err)
}
return *newID, summary, err
}
func (c *committedManager) Compare(ctx context.Context, ns graveler.StorageNamespace, destination, source, base graveler.MetaRangeID) (graveler.DiffIterator, error) {
diffIt, err := c.Diff(ctx, ns, destination, source)
if err != nil {
return nil, fmt.Errorf("diff: %w", err)
}
baseIt, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, base)
if err != nil {
diffIt.Close()
return nil, fmt.Errorf("get base iterator: %w", err)
}
return NewCompareValueIterator(ctx, NewDiffIteratorWrapper(diffIt), baseIt), nil
}
func (c *committedManager) GetMetaRange(ctx context.Context, ns graveler.StorageNamespace, id graveler.MetaRangeID) (graveler.MetaRangeAddress, error) {
uri, err := c.metaRangeManager.GetMetaRangeURI(ctx, ns, id)
return graveler.MetaRangeAddress(uri), err
}
func (c *committedManager) GetRange(ctx context.Context, ns graveler.StorageNamespace, id graveler.RangeID) (graveler.RangeAddress, error) {
uri, err := c.metaRangeManager.GetRangeURI(ctx, ns, id)
return graveler.RangeAddress(uri), err
}
func (c *committedManager) GetRangeIDByKey(ctx context.Context, ns graveler.StorageNamespace, id graveler.MetaRangeID, key graveler.Key) (graveler.RangeID, error) {
if id == "" {
return "", graveler.ErrNotFound
}
r, err := c.metaRangeManager.GetRangeByKey(ctx, ns, id, key)
if err != nil {
return "", fmt.Errorf("get range for key: %w", err)
}
return graveler.RangeID(r.ID), nil
}