forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_journal.go
1341 lines (1154 loc) · 34.6 KB
/
block_journal.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"fmt"
"math"
"path/filepath"
"reflect"
"github.com/keybase/client/go/kbfs/data"
"github.com/keybase/client/go/kbfs/ioutil"
"github.com/keybase/client/go/kbfs/kbfsblock"
"github.com/keybase/client/go/kbfs/kbfscodec"
"github.com/keybase/client/go/kbfs/kbfscrypto"
"github.com/keybase/client/go/kbfs/kbfsmd"
"github.com/keybase/client/go/kbfs/tlf"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-codec/codec"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// blockJournal stores a single ordered list of block operations for a
// single TLF, along with the associated block data, in flat files in
// a directory on disk.
//
// The directory layout looks like:
//
// dir/block_aggregate_info
// dir/block_journal/EARLIEST
// dir/block_journal/LATEST
// dir/block_journal/0...000
// dir/block_journal/0...001
// dir/block_journal/0...fff
// dir/blocks/...
// dir/gc_block_journal/EARLIEST
// dir/gc_block_journal/LATEST
// dir/gc_block_journal/...
//
// block_aggregate_info holds aggregate info about the block journal;
// currently it just holds the count of stored and unflushed bytes.
//
// Each entry in the journal in dir/block_journal contains the
// mutating operation and arguments for a single operation, except for
// block data. (See diskJournal comments for more details about the
// journal.)
//
// The block data is stored separately in dir/blocks. See
// blockDiskStore comments for more details.
//
// The maximum number of characters added to the root dir by a block
// journal is 51:
//
// /blocks/(max 44 characters)
//
// blockJournal is not goroutine-safe, so any code that uses it must
// guarantee that only one goroutine at a time calls its functions.
type blockJournal struct {
codec kbfscodec.Codec
dir string
log traceLogger
deferLog traceLogger
vlog *libkb.VDebugLog
// j is the main journal.
j *diskJournal
// saveUntilMDFlush, when non-nil, prevents garbage collection
// of blocks. When removed, all the referenced blocks are
// garbage-collected.
//
// TODO: We only really need to save a list of IDs, and not a
// full journal.
deferredGC *diskJournal
// s stores all the block data. s should always reflect the
// state you get by replaying all the entries in j.
s *blockDiskStore
aggregateInfo blockAggregateInfo
}
type blockOpType int
const (
blockPutOp blockOpType = 1
addRefOp blockOpType = 2
removeRefsOp blockOpType = 3
archiveRefsOp blockOpType = 4
mdRevMarkerOp blockOpType = 5
)
func (t blockOpType) String() string {
switch t {
case blockPutOp:
return "blockPut"
case addRefOp:
return "addReference"
case removeRefsOp:
return "removeReferences"
case archiveRefsOp:
return "archiveReferences"
case mdRevMarkerOp:
return "mdRevisionMarker"
default:
return fmt.Sprintf("blockOpType(%d)", t)
}
}
// A blockJournalEntry is just the name of the operation and the
// associated block ID and contexts. Fields are exported only for
// serialization.
type blockJournalEntry struct {
// Must be one of the four ops above.
Op blockOpType
// Must have exactly one entry with one context for blockPutOp and
// addRefOp. Used for all ops except for mdRevMarkerOp.
Contexts kbfsblock.ContextMap `codec:",omitempty"`
// Only used for mdRevMarkerOps.
Revision kbfsmd.Revision `codec:",omitempty"`
// Ignore this entry while flushing if this is true.
Ignore bool `codec:",omitempty"`
// This is an MD rev marker that represents a local squash. TODO:
// combine this with Ignore using a more generic flags or state
// field, once we can change the journal format.
IsLocalSquash bool `codec:",omitempty"`
// This is legacy and only present for backwards compatibility.
// It can be removed as soon as we are sure there are no more
// journal entries in the wild with this set.
Unignorable bool `codec:",omitempty"`
codec.UnknownFieldSetHandler
}
// Get the single context stored in this entry. Only applicable to
// blockPutOp and addRefOp.
func (e blockJournalEntry) getSingleContext() (
kbfsblock.ID, kbfsblock.Context, error) {
switch e.Op {
case blockPutOp, addRefOp:
if len(e.Contexts) != 1 {
return kbfsblock.ID{}, kbfsblock.Context{}, errors.Errorf(
"Op %s doesn't have exactly one context: %v",
e.Op, e.Contexts)
}
for id, idContexts := range e.Contexts {
if len(idContexts) != 1 {
return kbfsblock.ID{}, kbfsblock.Context{}, errors.Errorf(
"Op %s doesn't have exactly one context for id=%s: %v",
e.Op, id, idContexts)
}
return id, idContexts[0], nil
}
}
return kbfsblock.ID{}, kbfsblock.Context{}, errors.Errorf(
"getSingleContext() erroneously called on op %s", e.Op)
}
func blockJournalDir(dir string) string {
return filepath.Join(dir, "block_journal")
}
func blockJournalStoreDir(dir string) string {
return filepath.Join(dir, "blocks")
}
func deferredGCBlockJournalDir(dir string) string {
return filepath.Join(dir, "gc_block_journal")
}
// makeBlockJournal returns a new blockJournal for the given
// directory. Any existing journal entries are read.
func makeBlockJournal(
ctx context.Context, codec kbfscodec.Codec, dir string,
log logger.Logger, vlog *libkb.VDebugLog) (*blockJournal, error) {
journalPath := blockJournalDir(dir)
deferLog := log.CloneWithAddedDepth(1)
j, err := makeDiskJournal(
codec, journalPath, reflect.TypeOf(blockJournalEntry{}))
if err != nil {
return nil, err
}
gcJournalPath := deferredGCBlockJournalDir(dir)
gcj, err := makeDiskJournal(
codec, gcJournalPath, reflect.TypeOf(blockJournalEntry{}))
if err != nil {
return nil, err
}
storeDir := blockJournalStoreDir(dir)
s := makeBlockDiskStore(codec, storeDir)
journal := &blockJournal{
codec: codec,
dir: dir,
log: traceLogger{log},
vlog: vlog,
deferLog: traceLogger{deferLog},
j: j,
deferredGC: gcj,
s: s,
}
// Get initial aggregate info.
err = kbfscodec.DeserializeFromFile(
codec, aggregateInfoPath(dir), &journal.aggregateInfo)
if !ioutil.IsNotExist(err) && err != nil {
return nil, err
}
return journal, nil
}
func (j *blockJournal) blockJournalFiles() []string {
return []string{
blockJournalDir(j.dir), deferredGCBlockJournalDir(j.dir),
blockJournalStoreDir(j.dir), aggregateInfoPath(j.dir),
}
}
// The functions below are for reading and writing aggregate info.
// Ideally, this would be a JSON file, but we'd need a JSON
// encoder/decoder that supports unknown fields.
type blockAggregateInfo struct {
// StoredBytes counts the number of bytes of block data stored
// on disk.
StoredBytes int64
// StoredFiles counts an upper bound for the number of files
// of block data stored on disk.
StoredFiles int64
// UnflushedBytes counts the number of bytes of block data
// that is intended to be flushed to the server, but hasn't
// been yet. This should be always less than or equal to
// StoredBytes.
UnflushedBytes int64
codec.UnknownFieldSetHandler
}
func aggregateInfoPath(dir string) string {
return filepath.Join(dir, "block_aggregate_info")
}
// saturateAdd adds the given delta to the int64 at x; if the result
// would be over MaxInt64, *x is instead set to MaxInt64, and if the
// result would be negative, *x is instead set to 0. If *x is already
// negative, *x is first set to 0 before doing the addition.
func saturateAdd(x *int64, delta int64) {
if *x < 0 {
*x = 0
}
switch {
case delta > 0 && *x > (math.MaxInt64-delta):
*x = math.MaxInt64
case delta < 0 && *x+delta < 0:
*x = 0
default:
*x += delta
}
}
func (j *blockJournal) changeCounts(
deltaStoredBytes, deltaStoredFiles, deltaUnflushedBytes int64) error {
saturateAdd(&j.aggregateInfo.StoredBytes, deltaStoredBytes)
saturateAdd(&j.aggregateInfo.StoredFiles, deltaStoredFiles)
saturateAdd(&j.aggregateInfo.UnflushedBytes, deltaUnflushedBytes)
return kbfscodec.SerializeToFile(
j.codec, j.aggregateInfo, aggregateInfoPath(j.dir))
}
func (j *blockJournal) accumulateBlock(bytes, files int64) error {
if bytes < 0 {
panic("bytes unexpectedly negative")
}
if files < 0 {
panic("files unexpectedly negative")
}
return j.changeCounts(bytes, files, bytes)
}
func (j *blockJournal) flushBlock(bytes int64) error {
if bytes < 0 {
panic("bytes unexpectedly negative")
}
return j.changeCounts(0, 0, -bytes)
}
func (j *blockJournal) unstoreBlocks(bytes, files int64) error {
if bytes < 0 {
panic("bytes unexpectedly negative")
}
if files < 0 {
panic("files unexpectedly negative")
}
return j.changeCounts(-bytes, -files, 0)
}
// The functions below are for reading and writing journal entries.
func (j *blockJournal) readJournalEntry(ordinal journalOrdinal) (
blockJournalEntry, error) {
entry, err := j.j.readJournalEntry(ordinal)
if err != nil {
return blockJournalEntry{}, err
}
return entry.(blockJournalEntry), nil
}
func (j *blockJournal) appendJournalEntry(
ctx context.Context, entry blockJournalEntry) (
journalOrdinal, error) {
ordinal, err := j.j.appendJournalEntry(nil, entry)
if err != nil {
return 0, err
}
return ordinal, nil
}
func (j *blockJournal) length() uint64 {
return j.j.length()
}
func (j *blockJournal) next() (journalOrdinal, error) {
last, err := j.j.readLatestOrdinal()
if ioutil.IsNotExist(err) {
return firstValidJournalOrdinal, nil
} else if err != nil {
return 0, err
}
return last + 1, nil
}
func (j *blockJournal) end() (journalOrdinal, error) {
last, err := j.j.readLatestOrdinal()
if ioutil.IsNotExist(err) {
return 0, nil
} else if err != nil {
return 0, err
}
return last + 1, nil
}
func (j *blockJournal) hasData(
ctx context.Context, id kbfsblock.ID) (bool, error) {
return j.s.hasData(ctx, id)
}
func (j *blockJournal) isUnflushed(
ctx context.Context, id kbfsblock.ID) (bool, error) {
return j.s.isUnflushed(ctx, id)
}
func (j *blockJournal) remove(ctx context.Context, id kbfsblock.ID) (
removedBytes, removedFiles int64, err error) {
bytesToRemove, err := j.s.getDataSize(ctx, id)
if err != nil {
return 0, 0, err
}
err = j.s.remove(ctx, id)
if err != nil {
return 0, 0, err
}
var filesToRemove int64
if bytesToRemove > 0 {
filesToRemove = filesPerBlockMax
}
return bytesToRemove, filesToRemove, nil
}
// All functions below are public functions.
func (j *blockJournal) empty() bool {
return j.j.empty() && j.deferredGC.empty()
}
func (j *blockJournal) getDataWithContext(
ctx context.Context, id kbfsblock.ID, context kbfsblock.Context) (
[]byte, kbfscrypto.BlockCryptKeyServerHalf, error) {
return j.s.getDataWithContext(ctx, id, context)
}
func (j *blockJournal) getData(ctx context.Context, id kbfsblock.ID) (
[]byte, kbfscrypto.BlockCryptKeyServerHalf, error) {
return j.s.getData(ctx, id)
}
func (j *blockJournal) getDataSize(
ctx context.Context, id kbfsblock.ID) (int64, error) {
return j.s.getDataSize(ctx, id)
}
func (j *blockJournal) getStoredBytes() int64 {
return j.aggregateInfo.StoredBytes
}
func (j *blockJournal) getUnflushedBytes() int64 {
return j.aggregateInfo.UnflushedBytes
}
func (j *blockJournal) getStoredFiles() int64 {
return j.aggregateInfo.StoredFiles
}
// putBlockData puts the given block data. If err is non-nil, putData will
// always be false.
func (j *blockJournal) putBlockData(
ctx context.Context, id kbfsblock.ID, context kbfsblock.Context,
buf []byte, serverHalf kbfscrypto.BlockCryptKeyServerHalf) (
putData bool, err error) {
j.vlog.CLogf(
ctx, libkb.VLog1,
"Putting %d bytes of data for block %s with context %v",
len(buf), id, context)
defer func() {
if err != nil {
j.deferLog.CDebugf(ctx,
"Put for block %s with context %v failed with %+v",
id, context, err)
}
}()
putData, err = j.s.put(ctx, true, id, context, buf, serverHalf)
if err != nil {
return false, err
}
return putData, nil
}
// appendBlock appends an entry for the previously-put block to the
// journal, and records the size for the put block.
func (j *blockJournal) appendBlock(
ctx context.Context, id kbfsblock.ID, context kbfsblock.Context,
bufLenToAdd int64) error {
j.vlog.CLogf(ctx, libkb.VLog1, "Appending block %s to journal", id)
if bufLenToAdd > 0 {
var putFiles int64 = filesPerBlockMax
err := j.accumulateBlock(bufLenToAdd, putFiles)
if err != nil {
return err
}
}
next, err := j.next()
if err != nil {
return err
}
err = j.s.addReference(ctx, id, context, next.String())
if err != nil {
return err
}
_, err = j.appendJournalEntry(ctx, blockJournalEntry{
Op: blockPutOp,
Contexts: kbfsblock.ContextMap{id: {context}},
})
return err
}
func (j *blockJournal) addReference(
ctx context.Context, id kbfsblock.ID, context kbfsblock.Context) (
err error) {
j.vlog.CLogf(
ctx, libkb.VLog1, "Adding reference for block %s with context %v",
id, context)
defer func() {
if err != nil {
j.deferLog.CDebugf(ctx,
"Adding reference for block %s with context %v failed with %+v",
id, context, err)
}
}()
next, err := j.next()
if err != nil {
return err
}
err = j.s.addReference(ctx, id, context, next.String())
if err != nil {
return err
}
_, err = j.appendJournalEntry(ctx, blockJournalEntry{
Op: addRefOp,
Contexts: kbfsblock.ContextMap{id: {context}},
})
if err != nil {
return err
}
return nil
}
func (j *blockJournal) archiveReferences(
ctx context.Context, contexts kbfsblock.ContextMap) (err error) {
j.vlog.CLogf(ctx, libkb.VLog1, "Archiving references for %v", contexts)
defer func() {
if err != nil {
j.deferLog.CDebugf(ctx,
"Archiving references for %+v,", contexts, err)
}
}()
next, err := j.next()
if err != nil {
return err
}
err = j.s.archiveReferences(ctx, contexts, next.String())
if err != nil {
return err
}
_, err = j.appendJournalEntry(ctx, blockJournalEntry{
Op: archiveRefsOp,
Contexts: contexts,
})
if err != nil {
return err
}
return nil
}
// removeReferences removes references for the given contexts from
// their respective IDs.
func (j *blockJournal) removeReferences(
ctx context.Context, contexts kbfsblock.ContextMap) (
liveCounts map[kbfsblock.ID]int, err error) {
j.vlog.CLogf(ctx, libkb.VLog1, "Removing references for %v", contexts)
defer func() {
if err != nil {
j.deferLog.CDebugf(ctx,
"Removing references for %+v", contexts, err)
}
}()
// Add the journal entry first, so that if we crash before
// removing the refs, we have at worst un-GCed blocks.
_, err = j.appendJournalEntry(ctx, blockJournalEntry{
Op: removeRefsOp,
Contexts: contexts,
})
if err != nil {
return nil, err
}
liveCounts = make(map[kbfsblock.ID]int)
for id, idContexts := range contexts {
// Remove the references unconditionally here (i.e.,
// with an empty tag), since j.s should reflect the
// most recent state.
liveCount, err := j.s.removeReferences(ctx, id, idContexts, "")
if err != nil {
return nil, err
}
liveCounts[id] = liveCount
}
return liveCounts, nil
}
func (j *blockJournal) markMDRevision(ctx context.Context,
rev kbfsmd.Revision, isPendingLocalSquash bool) (err error) {
j.vlog.CLogf(
ctx, libkb.VLog1, "Marking MD revision %d in the block journal", rev)
defer func() {
if err != nil {
j.deferLog.CDebugf(ctx, "Marking MD revision %d error: %+v",
rev, err)
}
}()
_, err = j.appendJournalEntry(ctx, blockJournalEntry{
Op: mdRevMarkerOp,
Revision: rev,
// If this MD represents a pending local squash, it should
// never be ignored since the revision it refers to can't be
// squashed again.
IsLocalSquash: isPendingLocalSquash,
})
if err != nil {
return err
}
return nil
}
// blockEntriesToFlush is an internal data structure for blockJournal;
// its fields shouldn't be accessed outside this file.
type blockEntriesToFlush struct {
all []blockJournalEntry
first journalOrdinal
puts blockPutState
adds blockPutState
other []blockJournalEntry
}
func (be blockEntriesToFlush) length() int {
return len(be.all)
}
func (be blockEntriesToFlush) flushNeeded() bool {
return be.length() > 0
}
func (be blockEntriesToFlush) revIsLocalSquash(rev kbfsmd.Revision) bool {
for _, entry := range be.other {
if !entry.Ignore && entry.Op == mdRevMarkerOp && entry.Revision == rev {
return entry.IsLocalSquash || entry.Unignorable
}
}
return false
}
func (be blockEntriesToFlush) markFlushingBlockIDs(ids map[kbfsblock.ID]bool) {
for _, ptr := range be.puts.Ptrs() {
ids[ptr.ID] = true
}
}
func (be blockEntriesToFlush) clearFlushingBlockIDs(ids map[kbfsblock.ID]bool) {
for _, ptr := range be.puts.Ptrs() {
delete(ids, ptr.ID)
}
}
// Only entries with ordinals less than the given ordinal (assumed to
// be <= latest ordinal + 1) are returned. Also returns the maximum
// MD revision that can be merged after the returned entries are
// successfully flushed; if no entries are returned (i.e., the block
// journal is empty) then any MD revision may be flushed even when
// kbfsmd.RevisionUninitialized is returned.
func (j *blockJournal) getNextEntriesToFlush(
ctx context.Context, end journalOrdinal, maxToFlush int) (
entries blockEntriesToFlush, bytesToFlush int64,
maxMDRevToFlush kbfsmd.Revision, err error) {
first, err := j.j.readEarliestOrdinal()
if ioutil.IsNotExist(err) {
return blockEntriesToFlush{}, 0, kbfsmd.RevisionUninitialized, nil
} else if err != nil {
return blockEntriesToFlush{}, 0, kbfsmd.RevisionUninitialized, err
}
if first >= end {
return blockEntriesToFlush{}, 0, kbfsmd.RevisionUninitialized,
errors.Errorf("Trying to flush past the "+
"start of the journal (first=%d, end=%d)", first, end)
}
realEnd, err := j.end()
if realEnd == 0 {
return blockEntriesToFlush{}, 0, kbfsmd.RevisionUninitialized,
errors.Errorf("There was an earliest "+
"ordinal %d, but no latest ordinal", first)
} else if err != nil {
return blockEntriesToFlush{}, 0, kbfsmd.RevisionUninitialized, err
}
if end > realEnd {
return blockEntriesToFlush{}, 0, kbfsmd.RevisionUninitialized,
errors.Errorf("Trying to flush past the "+
"end of the journal (realEnd=%d, end=%d)", realEnd, end)
}
entries.puts = newBlockPutStateMemory(int(end - first))
entries.adds = newBlockPutStateMemory(int(end - first))
maxMDRevToFlush = kbfsmd.RevisionUninitialized
loopEnd := end
if first+journalOrdinal(maxToFlush) < end {
loopEnd = first + journalOrdinal(maxToFlush)
}
for ordinal := first; ordinal < loopEnd; ordinal++ {
entry, err := j.readJournalEntry(ordinal)
if err != nil {
return blockEntriesToFlush{}, 0, kbfsmd.RevisionUninitialized, err
}
if entry.Ignore {
if loopEnd < end {
loopEnd++
}
entries.other = append(entries.other, entry)
entries.all = append(entries.all, entry)
continue
}
var blockData []byte
var serverHalf kbfscrypto.BlockCryptKeyServerHalf
switch entry.Op {
case blockPutOp:
id, bctx, err := entry.getSingleContext()
if err != nil {
return blockEntriesToFlush{}, 0,
kbfsmd.RevisionUninitialized, err
}
blockData, serverHalf, err = j.s.getData(ctx, id)
if err != nil {
return blockEntriesToFlush{}, 0,
kbfsmd.RevisionUninitialized, err
}
bytesToFlush += int64(len(blockData))
err = entries.puts.AddNewBlock(
ctx, data.BlockPointer{ID: id, Context: bctx},
nil, /* only used by folderBranchOps */
data.ReadyBlockData{
Buf: blockData,
ServerHalf: serverHalf,
}, nil)
if err != nil {
return blockEntriesToFlush{}, 0,
kbfsmd.RevisionUninitialized, err
}
case addRefOp:
id, bctx, err := entry.getSingleContext()
if err != nil {
return blockEntriesToFlush{}, 0,
kbfsmd.RevisionUninitialized, err
}
err = entries.adds.AddNewBlock(
ctx, data.BlockPointer{ID: id, Context: bctx},
nil, /* only used by folderBranchOps */
data.ReadyBlockData{}, nil)
if err != nil {
return blockEntriesToFlush{}, 0,
kbfsmd.RevisionUninitialized, err
}
case mdRevMarkerOp:
if entry.Revision < maxMDRevToFlush {
return blockEntriesToFlush{}, 0, kbfsmd.RevisionUninitialized,
errors.Errorf("Max MD revision decreased in block journal "+
"from %d to %d", entry.Revision, maxMDRevToFlush)
}
maxMDRevToFlush = entry.Revision
entries.other = append(entries.other, entry)
default:
entries.other = append(entries.other, entry)
}
entries.all = append(entries.all, entry)
}
entries.first = first
return entries, bytesToFlush, maxMDRevToFlush, nil
}
// flushNonBPSBlockJournalEntry flushes journal entries that can't be
// parallelized via a blockPutState.
func flushNonBPSBlockJournalEntry(
ctx context.Context, log logger.Logger,
bserver BlockServer, tlfID tlf.ID, entry blockJournalEntry) error {
log.CDebugf(ctx, "Flushing other block op %v", entry)
switch entry.Op {
case removeRefsOp:
_, err := bserver.RemoveBlockReferences(
ctx, tlfID, entry.Contexts)
if err != nil {
return err
}
case archiveRefsOp:
err := bserver.ArchiveBlockReferences(
ctx, tlfID, entry.Contexts)
if err != nil {
return err
}
case blockPutOp:
if !entry.Ignore {
return errors.New("Trying to flush unignored blockPut as other")
}
// Otherwise nothing to do.
case mdRevMarkerOp:
// Nothing to do.
default:
return errors.Errorf("Unknown op %s", entry.Op)
}
return nil
}
func flushBlockEntries(ctx context.Context, log, deferLog traceLogger,
bserver BlockServer, bcache data.BlockCache, reporter Reporter, tlfID tlf.ID,
tlfName tlf.CanonicalName, entries blockEntriesToFlush,
cacheType DiskBlockCacheType) error {
if !entries.flushNeeded() {
// Avoid logging anything when there's nothing to flush.
return nil
}
// Do all the put state stuff first, in parallel. We need to do
// the puts strictly before the addRefs, since the latter might
// reference the former.
log.CDebugf(ctx, "Putting %d blocks", entries.puts.numBlocks())
blocksToRemove, err := doBlockPuts(ctx, bserver, bcache, reporter,
log, deferLog, tlfID, tlfName, entries.puts, cacheType)
if err != nil {
if isRecoverableBlockError(err) {
log.CWarningf(ctx,
"Recoverable block error encountered on puts: %+v, ptrs=%v",
err, blocksToRemove)
}
return err
}
// Next, do the addrefs.
log.CDebugf(ctx, "Adding %d block references", entries.adds.numBlocks())
blocksToRemove, err = doBlockPuts(ctx, bserver, bcache, reporter,
log, deferLog, tlfID, tlfName, entries.adds, cacheType)
if err != nil {
if isRecoverableBlockError(err) {
log.CWarningf(ctx,
"Recoverable block error encountered on addRefs: %+v, ptrs=%v",
err, blocksToRemove)
}
return err
}
// Now do all the other, non-put/addref entries. TODO:
// parallelize these as well.
for _, entry := range entries.other {
err := flushNonBPSBlockJournalEntry(ctx, log, bserver, tlfID, entry)
if err != nil {
return err
}
}
return nil
}
func (j *blockJournal) removeFlushedEntry(ctx context.Context,
ordinal journalOrdinal, entry blockJournalEntry) (
flushedBytes int64, err error) {
earliestOrdinal, err := j.j.readEarliestOrdinal()
if err != nil {
return 0, err
}
if ordinal != earliestOrdinal {
return 0, errors.Errorf("Expected ordinal %d, got %d",
ordinal, earliestOrdinal)
}
// Store the block byte count if we've finished a Put.
if entry.Op == blockPutOp && !entry.Ignore {
id, _, err := entry.getSingleContext()
if err != nil {
return 0, err
}
err = j.s.markFlushed(ctx, id)
if err != nil {
return 0, err
}
flushedBytes, err = j.s.getDataSize(ctx, id)
if err != nil {
return 0, err
}
err = j.flushBlock(flushedBytes)
if err != nil {
return 0, err
}
}
// Remove any of the entry's refs that hasn't been modified by
// a subsequent block op (i.e., that has earliestOrdinal as a
// tag). Has no effect for removeRefsOp (since those are
// already removed) or mdRevMarkerOp (which has no
// references).
for id, idContexts := range entry.Contexts {
liveCount, err := j.s.removeReferences(
ctx, id, idContexts, earliestOrdinal.String())
if err != nil {
return 0, err
}
// Postpone garbage collection until the next MD flush.
if liveCount == 0 {
_, err := j.deferredGC.appendJournalEntry(nil, entry)
if err != nil {
return 0, err
}
}
}
_, err = j.j.removeEarliest()
if err != nil {
return 0, err
}
return flushedBytes, nil
}
func (j *blockJournal) removeFlushedEntries(ctx context.Context,
entries blockEntriesToFlush, tlfID tlf.ID, reporter Reporter) (
totalFlushedBytes int64, err error) {
// Remove them all!
for i, entry := range entries.all {
flushedBytes, err := j.removeFlushedEntry(
ctx, entries.first+journalOrdinal(i), entry)
if err != nil {
return 0, err
}
totalFlushedBytes += flushedBytes
reporter.NotifySyncStatus(ctx, &keybase1.FSPathSyncStatus{
FolderType: tlfID.Type().FolderType(),
// Path: TODO,
// SyncingBytes: TODO,
// SyncingOps: TODO,
SyncedBytes: flushedBytes,
})
}
// The block journal might be empty, but deferredGC might
// still be non-empty, so we have to wait for that to be empty
// before nuking the whole journal (see clearDeferredGCRange).
return totalFlushedBytes, nil
}
func (j *blockJournal) ignoreBlocksAndMDRevMarkersInJournal(ctx context.Context,
idsToIgnore map[kbfsblock.ID]bool, rev kbfsmd.Revision,
dj *diskJournal) (totalIgnoredBytes int64, err error) {
first, err := dj.readEarliestOrdinal()
if ioutil.IsNotExist(err) {
return 0, nil
} else if err != nil {
return 0, err
}
last, err := dj.readLatestOrdinal()
if err != nil {
return 0, err
}
isMainJournal := dj.dir == j.j.dir
// Iterate backwards since the blocks to ignore are likely to be
// at the end of the journal.
ignored := 0
ignoredRev := false
// i is unsigned, so make sure to handle overflow when `first` is
// 0 by checking that it's less than `last`. TODO: handle
// first==0 and last==maxuint?
for i := last; i >= first && i <= last; i-- {
entry, err := dj.readJournalEntry(i)
if err != nil {
// If we can't read a particular entry while ignoring old
// revisions, the entry might be corrupt. But returning
// an error is harsh and dangerous because at this point a
// new MD revision has already been appended to the MD
// journal; if we error to the caller then they won't
// write another marker and on a restart the whole MD
// journal can be flushed without waiting for the
// corresponding blocks to flush. See HOTPOT-193. So
// instead, log the error and keep going in that case. If
// the entry continues to be unreadable during flush, then
// the journal (and eventually another round of conflict
// resolution) will become stuck and the error will
// surface up to the user.
j.log.CWarningf(ctx, "Couldn't read journal entry %d: %+v", i, err)
continue
}
e := entry.(blockJournalEntry)
switch e.Op {
case blockPutOp, addRefOp:
id, _, err := e.getSingleContext()
if err != nil {