-
Notifications
You must be signed in to change notification settings - Fork 352
/
merge.go
504 lines (475 loc) · 14.3 KB
/
merge.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package committed
import (
"bytes"
"context"
"fmt"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/logging"
)
type merger struct {
ctx context.Context
logger logging.Logger
writer MetaRangeWriter
base Iterator
source Iterator
dest Iterator
haveSource, haveDest bool
strategy graveler.MergeStrategy
}
// getNextGEKey moves base iterator from its current position to the next greater equal value
func (m *merger) getNextGEKey(key graveler.Key) (*graveler.ValueRecord, error) {
baseValue, _ := m.base.Value()
if baseValue != nil && bytes.Compare(key, baseValue.Key) <= 0 {
return baseValue, nil
}
for {
_, baseRange := m.base.Value()
if baseRange != nil && bytes.Compare(baseRange.MaxKey, key) >= 0 {
for {
baseValue, innerRange := m.base.Value()
if baseValue != nil && bytes.Compare(key, baseValue.Key) <= 0 {
return baseValue, nil
}
if !m.base.Next() || innerRange.ID != baseRange.ID {
break
}
}
} else if !m.base.NextRange() {
break
}
}
if err := m.base.Err(); err != nil {
return nil, err
}
return nil, nil
}
// getNextOverlappingFromBase moves base iterator from its current position to the next range overlapping with rangeToOverlap
func (m *merger) getNextOverlappingFromBase(rangeToOverlap *Range) (*Range, error) {
for {
_, baseRange := m.base.Value()
if baseRange != nil && bytes.Compare(baseRange.MaxKey, rangeToOverlap.MinKey) >= 0 {
if bytes.Compare(baseRange.MinKey, rangeToOverlap.MaxKey) > 0 {
return nil, nil
}
return baseRange, nil
}
if !m.base.NextRange() {
break
}
}
return nil, m.base.Err()
}
// writeRange writes Range using writer
func (m *merger) writeRange(writeRange *Range) error {
if m.logger.IsTracing() {
m.logger.WithFields(logging.Fields{
"from": string(writeRange.MinKey),
"to": string(writeRange.MaxKey),
"ID": writeRange.ID,
}).Trace("copy entire range")
}
if err := m.writer.WriteRange(*writeRange); err != nil {
return fmt.Errorf("copy range %s: %w", writeRange.ID, err)
}
return nil
}
// writeRecord writes graveler.ValueRecord using writer
func (m *merger) writeRecord(writeValue *graveler.ValueRecord) error {
if m.logger.IsTracing() {
m.logger.WithFields(logging.Fields{
"key": string(writeValue.Key),
"ID": string(writeValue.Identity),
}).Trace("write record")
}
if err := m.writer.WriteRecord(*writeValue); err != nil {
return fmt.Errorf("write record: %w", err)
}
return nil
}
func (m *merger) destBeforeSource(destValue *graveler.ValueRecord) error {
baseValue, err := m.getNextGEKey(destValue.Key)
if err != nil {
return err
}
if baseValue != nil && bytes.Equal(destValue.Identity, baseValue.Identity) && bytes.Equal(destValue.Key, baseValue.Key) { // source deleted this record
m.haveDest = m.dest.Next()
} else {
if baseValue != nil && bytes.Equal(destValue.Key, baseValue.Key) { // deleted by source changed by dest
switch m.strategy {
case graveler.MergeStrategyDest:
break
case graveler.MergeStrategySrc:
m.haveDest = m.dest.Next()
return nil
default: // graveler.MergeStrategyNone
return graveler.ErrConflictFound
}
}
// dest added this record
err := m.writeRecord(destValue)
if err != nil {
return fmt.Errorf("write dest record: %w", err)
}
m.haveDest = m.dest.Next()
}
return nil
}
func (m *merger) sourceBeforeDest(sourceValue *graveler.ValueRecord) error {
baseValue, err := m.getNextGEKey(sourceValue.Key)
if err != nil {
return err
}
if baseValue != nil && bytes.Equal(sourceValue.Identity, baseValue.Identity) && bytes.Equal(sourceValue.Key, baseValue.Key) { // dest deleted this record
m.haveSource = m.source.Next()
} else {
if baseValue != nil && bytes.Equal(sourceValue.Key, baseValue.Key) { // deleted by dest and changed by source
switch m.strategy {
case graveler.MergeStrategyDest:
m.haveSource = m.source.Next()
return nil
case graveler.MergeStrategySrc:
break
default: // graveler.MergeStrategyNone
return graveler.ErrConflictFound
}
}
// source added this record
err := m.writeRecord(sourceValue)
if err != nil {
return fmt.Errorf("write source record: %w", err)
}
m.haveSource = m.source.Next()
}
return nil
}
// handleAll handles the case where only one Iterator from source or dest remains
// Since the iterator can be for either the source ot the dest range, the function
// receives a graveler.MergeStrategy parameter - strategyToInclude - to indicate
// which strategy favors the given range. In case of a conflict, the configured m.strategy
// is compared to the given strategyToInclude, and if they match - the conflict will
// be resolved by taking the value from the given range. If not and the configured
// m.strategy is other than MergeStrategyNone, the record is ignored. If m.strategy is
// MergeStrategyNone - a conflict will be reported
func (m *merger) handleAll(iter Iterator, strategyToInclude graveler.MergeStrategy) error {
for {
select {
case <-m.ctx.Done():
return m.ctx.Err()
default:
}
iterValue, iterRange := iter.Value()
if iterValue == nil {
baseRange, err := m.getNextOverlappingFromBase(iterRange)
if err != nil {
return fmt.Errorf("base range GE: %w", err)
}
if baseRange == nil || baseRange.ID == iterRange.ID {
if baseRange == nil {
if err := m.writeRange(iterRange); err != nil {
return err
}
}
if !iter.NextRange() {
break
}
} else if !iter.Next() { // need to enter this range
break
}
} else {
baseValue, err := m.getNextGEKey(iterValue.Key)
if err != nil {
return fmt.Errorf("base value GE: %w", err)
}
if baseValue == nil || !bytes.Equal(baseValue.Identity, iterValue.Identity) {
shouldWriteRecord := true
if baseValue != nil && bytes.Equal(baseValue.Key, iterValue.Key) { // deleted by one changed by iter
if m.strategy == graveler.MergeStrategyNone { // conflict is only reported if no strategy is selected
return graveler.ErrConflictFound
}
// In case of conflict, if the strategy favors the given iter we
// still want to write the record. Otherwise, it will be ignored.
if m.strategy != strategyToInclude {
shouldWriteRecord = false
}
}
if shouldWriteRecord {
if err := m.writeRecord(iterValue); err != nil {
return err
}
}
}
if !iter.Next() {
break
}
}
}
return iter.Err()
}
// handleBothRanges handles the case where both source and dest iterators are at the header of a range
func (m *merger) handleBothRanges(sourceRange *Range, destRange *Range) error {
switch {
case sourceRange.ID == destRange.ID: // range hasn't changed or both added the same range
err := m.writeRange(sourceRange)
if err != nil {
return err
}
m.haveSource = m.source.NextRange()
m.haveDest = m.dest.NextRange()
case sourceRange.EqualBounds(destRange):
baseRange, err := m.getNextOverlappingFromBase(sourceRange)
if err != nil {
return err
}
if baseRange != nil && (sourceRange.ID == baseRange.ID || destRange.ID == baseRange.ID) {
if sourceRange.ID == baseRange.ID { // dest added changes
err = m.writeRange(destRange)
} else {
err = m.writeRange(sourceRange) // source added changes
}
if err != nil {
return err
}
m.haveSource = m.source.NextRange()
m.haveDest = m.dest.NextRange()
} else { // enter both ranges
m.haveSource = m.source.Next()
m.haveDest = m.dest.Next()
}
case sourceRange.BeforeRange(destRange):
baseRange, err := m.getNextOverlappingFromBase(sourceRange)
if err != nil {
return fmt.Errorf("base range GE: %w", err)
}
if baseRange == nil { // source added this range
err = m.writeRange(sourceRange)
if err != nil {
return err
}
m.haveSource = m.source.NextRange()
return nil
}
if sourceRange.ID == baseRange.ID { // dest deleted this range
m.haveSource = m.source.NextRange()
return nil
}
// both changed this range
m.haveSource = m.source.Next()
m.haveDest = m.dest.Next()
case destRange.BeforeRange(sourceRange) && m.validWritingRange(m.dest):
baseRange, err := m.getNextOverlappingFromBase(destRange)
if err != nil {
return fmt.Errorf("base range GE: %w", err)
}
if baseRange == nil { // dest added this range
err = m.writeRange(destRange)
if err != nil {
return err
}
m.haveDest = m.dest.NextRange()
return nil
}
if destRange.ID == baseRange.ID { // source deleted this range
m.haveDest = m.dest.NextRange()
return nil
}
// both changed this range
m.haveSource = m.source.Next()
m.haveDest = m.dest.Next()
default: // ranges overlapping
m.haveSource = m.source.Next()
m.haveDest = m.dest.Next()
}
return nil
}
func (m *merger) handleConflict(sourceValue *graveler.ValueRecord, destValue *graveler.ValueRecord) error {
switch m.strategy {
case graveler.MergeStrategyDest:
err := m.writeRecord(destValue)
if err != nil {
return fmt.Errorf("write record: %w", err)
}
case graveler.MergeStrategySrc:
err := m.writeRecord(sourceValue)
if err != nil {
return fmt.Errorf("write record: %w", err)
}
default: // graveler.MergeStrategyNone
return graveler.ErrConflictFound
}
m.haveSource = m.source.Next()
m.haveDest = m.dest.Next()
return nil
}
// handleBothKeys handles the case where both source and dest iterators are inside range
func (m *merger) handleBothKeys(sourceValue *graveler.ValueRecord, destValue *graveler.ValueRecord) error {
c := bytes.Compare(sourceValue.Key, destValue.Key)
switch {
case c < 0: // source before dest
return m.sourceBeforeDest(sourceValue)
case c > 0: // dest before source
return m.destBeforeSource(destValue)
default: // identical keys
baseValue, err := m.getNextGEKey(destValue.Key)
if err != nil {
return err
}
if !bytes.Equal(sourceValue.Identity, destValue.Identity) {
if baseValue != nil {
switch {
case bytes.Equal(sourceValue.Identity, baseValue.Identity):
err = m.writeRecord(destValue)
case bytes.Equal(destValue.Identity, baseValue.Identity):
err = m.writeRecord(sourceValue)
default: // both changed the same key
return m.handleConflict(sourceValue, destValue)
}
if err != nil {
return fmt.Errorf("write record: %w", err)
}
m.haveSource = m.source.Next()
m.haveDest = m.dest.Next()
return nil
} else { // both added the same key with different identity
return m.handleConflict(sourceValue, destValue)
}
}
// record hasn't changed or both added the same record
err = m.writeRecord(sourceValue)
if err != nil {
return fmt.Errorf("write record: %w", err)
}
m.haveSource = m.source.Next()
m.haveDest = m.dest.Next()
}
return nil
}
// handleDestRangeSourceKey handles the case where source Iterator inside range and dest Iterator at the header of a range
func (m *merger) handleDestRangeSourceKey(destRange *Range, sourceValue *graveler.ValueRecord) error {
if bytes.Compare(destRange.MinKey, sourceValue.Key) > 0 { // source before dest range
return m.sourceBeforeDest(sourceValue)
}
if bytes.Compare(destRange.MaxKey, sourceValue.Key) < 0 &&
m.validWritingRange(m.dest) { // dest range before source
baseRange, err := m.getNextOverlappingFromBase(destRange)
if err != nil {
return fmt.Errorf("base range GE: %w", err)
}
if baseRange == nil {
err = m.writeRange(destRange)
if err != nil {
return err
}
m.haveDest = m.dest.NextRange()
return nil
}
if destRange.ID == baseRange.ID { // source deleted this range
m.haveDest = m.dest.NextRange()
return nil
}
}
// dest is at start of range which we need to scan, enter it
m.haveDest = m.dest.Next()
return nil
}
// handleSourceRangeDestKey handles the case where dest Iterator inside range and source Iterator at the header of a range
func (m *merger) handleSourceRangeDestKey(sourceRange *Range, destValue *graveler.ValueRecord) error {
if bytes.Compare(sourceRange.MinKey, destValue.Key) > 0 { // dest before source range
return m.destBeforeSource(destValue)
}
if bytes.Compare(sourceRange.MaxKey, destValue.Key) < 0 { // source range before dest
baseRange, err := m.getNextOverlappingFromBase(sourceRange)
if err != nil {
return fmt.Errorf("base range GE: %w", err)
}
if baseRange == nil {
err = m.writeRange(sourceRange)
if err != nil {
return err
}
m.haveSource = m.source.NextRange()
return nil
}
if sourceRange.ID == baseRange.ID { // dest deleted this range
m.haveSource = m.source.NextRange()
return nil
}
}
// source is at start of range which we need to scan, enter it
m.haveSource = m.source.Next()
return nil
}
func (m *merger) merge() error {
m.haveSource, m.haveDest, _ = m.source.Next(), m.dest.Next(), m.base.Next()
if err := m.source.Err(); err != nil {
return err
}
if err := m.dest.Err(); err != nil {
return err
}
if err := m.base.Err(); err != nil {
return err
}
for m.haveSource && m.haveDest {
select {
case <-m.ctx.Done():
return m.ctx.Err()
default:
}
sourceValue, sourceRange := m.source.Value()
destValue, destRange := m.dest.Value()
var err error
switch {
case sourceValue == nil && destValue == nil:
err = m.handleBothRanges(sourceRange, destRange)
case destValue == nil && sourceValue != nil:
err = m.handleDestRangeSourceKey(destRange, sourceValue)
case sourceValue == nil && destValue != nil:
err = m.handleSourceRangeDestKey(sourceRange, destValue)
default:
err = m.handleBothKeys(sourceValue, destValue)
}
if err != nil {
return err
}
if err = m.source.Err(); err != nil {
return err
}
if err = m.dest.Err(); err != nil {
return err
}
if err = m.base.Err(); err != nil {
return err
}
}
if m.haveSource {
if err := m.handleAll(m.source, graveler.MergeStrategySrc); err != nil {
return err
}
}
if m.haveDest {
if err := m.handleAll(m.dest, graveler.MergeStrategyDest); err != nil {
return err
}
}
return nil
}
func (m *merger) validWritingRange(it Iterator) bool {
switch v := it.(type) {
case ImportIterator:
return !v.IsCurrentPrefixIncludedInRange()
default:
return true
}
}
func Merge(ctx context.Context, writer MetaRangeWriter, base Iterator, source Iterator, destination Iterator, strategy graveler.MergeStrategy) error {
m := merger{
ctx: ctx,
logger: logging.FromContext(ctx),
writer: writer,
base: base,
source: source,
dest: destination,
strategy: strategy,
}
return m.merge()
}