forked from perkeep/perkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blobpacked.go
1161 lines (1044 loc) · 32.2 KB
/
blobpacked.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 2014 The Camlistore AUTHORS
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Package blobpacked registers the "blobpacked" blobserver storage type,
storing blobs initially as one physical blob per logical blob, but then
rearranging little physical blobs into large contiguous blobs organized by
how they'll likely be accessed. An index tracks the mapping from logical to
physical blobs.
Example low-level config:
"/storage/": {
"handler": "storage-blobpacked",
"handlerArgs": {
"smallBlobs": "/small/",
"largeBlobs": "/large/",
"metaIndex": {
"type": "mysql",
.....
}
}
}
The resulting large blobs are valid zip files. Those blobs may up be up to
16 MB and contain the original contiguous file (or fractions of it), as well
as metadata about how the file is cut up. The zip file will have the
following structure:
foo.jpg (or whatever)
camlistore/sha1-beb1df0b75952c7d277905ad14de71ef7ef90c44.json (some file ref)
camlistore/sha1-a0ceb10b04403c9cc1d032e07a9071db5e711c9a.json (some bytes ref)
camlistore/sha1-7b4d9c8529c27d592255c6dfb17188493db96ccc.json (another bytes ref)
camlistore/camlistore-pack-manifest.json
The camlistore-pack-manifest.json is documented on the exported
Manifest type. It looks like this:
{
"wholeRef": "sha1-0e64816d731a56915e8bb4ae4d0ac7485c0b84da",
"wholeSize": 2962227200, // 2.8GB; so will require ~176-180 16MB chunks
"wholePartIndex": 17, // 0-based
"dataBlobsOrigin": "sha1-355705cf62a56669303d2561f29e0620a676c36e",
"dataBlobs": [
{"blob": "sha1-f1d2d2f924e986ac86fdf7b36c94bcdf32beec15", "offset": 0, "size": 273048},
{"blob": "sha1-e242ed3bffccdf271b7fbaf34ed72d089537b42f", "offset": 273048, "size": 112783},
{"blob": "sha1-6eadeac2dade6347e87c0d24fd455feffa7069f0", "offset": 385831, ...},
{"blob": "sha1-beb1df0b75952c7d277905ad14de71ef7ef90c44", "offset": ...},
{"blob": "sha1-a0ceb10b04403c9cc1d032e07a9071db5e711c9a", "offset": ...},
{"blob": "sha1-7b4d9c8529c27d592255c6dfb17188493db96ccc", "offset": ...}
],
}
The manifest.json ensures that if the metadata index is lost, all the
data can be reconstructed from the raw zip files.
The 'wholeRef' property specifies which large file that this zip is building
up. If the file is less than 15.5 MB or so (leaving room for the zip
overhead and manifest size), it will probably all be in one zip and the
first file in the zip will be the whole thing. Otherwise it'll be cut across
multiple zip files, each no larger than 16MB. In that case, each part of the
file will have a different 'wholePartIndex' number, starting at index
0. Each will have the same 'wholeSize'.
*/
package blobpacked
// TODO: BlobStreamer using the zip manifests, for recovery.
import (
"bytes"
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"runtime"
"strings"
"sync"
"time"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/blobserver"
"camlistore.org/pkg/constants"
"camlistore.org/pkg/context"
"camlistore.org/pkg/jsonconfig"
"camlistore.org/pkg/pools"
"camlistore.org/pkg/schema"
"camlistore.org/pkg/sorted"
"camlistore.org/pkg/strutil"
"camlistore.org/pkg/syncutil"
"camlistore.org/third_party/go/pkg/archive/zip"
)
// TODO: evaluate whether this should even be 0, to keep the schema blobs together at least.
// Files under this size aren't packed.
const packThreshold = 512 << 10
// Overhead for zip files.
// These are only variables so they can be changed by tests, but
// they're effectively constant.
var (
zipFixedOverhead = 20 /*directory64EndLen*/ +
56 /*directory64LocLen */ +
22 /*directoryEndLen*/ +
512 /* conservative slop space, to get us away from 16 MB zip boundary */
zipPerEntryOverhead = 30 /*fileHeaderLen*/ +
24 /*dataDescriptor64Len*/ +
22 /*directoryEndLen*/ +
len("camlistore/sha1-f1d2d2f924e986ac86fdf7b36c94bcdf32beec15.dat")*3/2 /*padding for larger blobrefs*/
)
// meta key prefixes
const (
blobMetaPrefix = "b:"
blobMetaPrefixLimit = "b;"
wholeMetaPrefix = "w:"
)
const (
zipManifestPath = "camlistore/camlistore-pack-manifest.json"
)
type subFetcherStorage interface {
blobserver.Storage
blob.SubFetcher
}
type storage struct {
small blobserver.Storage
large subFetcherStorage
// meta key -> value rows are:
//
// For logical blobs packed within a large blog, "b:" prefix:
// b:sha1-xxxx -> "<size> <big-blobref> <offset_u32>"
//
// For wholerefs: (wholeMetaPrefix)
// w:sha1-xxxx(wholeref) -> "<nbytes_total_u64> <nchunks_u32>"
// Then for each big nchunk of the file:
// w:sha1-xxxx:0 -> "<zipchunk-blobref> <offset-in-zipchunk-blobref> <offset-in-whole_u64> <length_u32>"
// w:sha1-xxxx:...
// w:sha1-xxxx:(nchunks-1)
//
// For marking that zips that have blobs (possibly all)
// deleted from inside them: (deleted zip)
// d:sha1-xxxxxx -> <unix-time-of-delete>
meta sorted.KeyValue
// If non-zero, the maximum size of a zip blob.
// It defaults to constants.MaxBlobSize.
forceMaxZipBlobSize int
skipDelete bool // don't delete from small after packing
packGate *syncutil.Gate
loggerOnce sync.Once
log *log.Logger // nil means default
}
var (
_ blobserver.BlobStreamer = (*storage)(nil)
_ blobserver.Generationer = (*storage)(nil)
_ blobserver.WholeRefFetcher = (*storage)(nil)
)
func (s *storage) String() string {
return fmt.Sprintf("\"blobpacked\" storage")
}
func (s *storage) Logf(format string, args ...interface{}) {
s.logger().Printf(format, args...)
}
func (s *storage) logger() *log.Logger {
s.loggerOnce.Do(s.initLogger)
return s.log
}
func (s *storage) initLogger() {
if s.log == nil {
s.log = log.New(os.Stderr, "blobpacked: ", log.LstdFlags)
}
}
func (s *storage) init() {
s.packGate = syncutil.NewGate(10)
}
func (s *storage) maxZipBlobSize() int {
if s.forceMaxZipBlobSize > 0 {
return s.forceMaxZipBlobSize
}
return constants.MaxBlobSize
}
func init() {
blobserver.RegisterStorageConstructor("blobpacked", blobserver.StorageConstructor(newFromConfig))
}
func newFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (blobserver.Storage, error) {
var (
smallPrefix = conf.RequiredString("smallBlobs")
largePrefix = conf.RequiredString("largeBlobs")
metaConf = conf.RequiredObject("metaIndex")
)
if err := conf.Validate(); err != nil {
return nil, err
}
small, err := ld.GetStorage(smallPrefix)
if err != nil {
return nil, fmt.Errorf("failed to load smallBlobs at %s: %v", smallPrefix, err)
}
large, err := ld.GetStorage(largePrefix)
if err != nil {
return nil, fmt.Errorf("failed to load largeBlobs at %s: %v", largePrefix, err)
}
largeSubber, ok := large.(subFetcherStorage)
if !ok {
return nil, fmt.Errorf("largeBlobs at %q of type %T doesn't support fetching sub-ranges of blobs",
largePrefix, large)
}
meta, err := sorted.NewKeyValue(metaConf)
if err != nil {
return nil, fmt.Errorf("failed to setup blobpacked metaIndex: %v", err)
}
sto := &storage{
small: small,
large: largeSubber,
meta: meta,
}
sto.init()
// Check for a weird state: zip files exist, but no metadata about them
// is recorded. This is probably a corrupt state, and the user likely
// wants to recover.
if !sto.anyMeta() && sto.anyZipPacks() {
log.Printf("Warning: blobpacked storage detects non-zero packed zips, but no metadata. Please re-start in recovery mode.")
// TODO: add a recovery mode.
// Old TODO was:
// fail with a "known corrupt" message and refuse to
// start unless in recovery mode (perhaps a new environment
// var? or flag passed down?) using StreamBlobs starting at
// "l:". Could even do it automatically if total size is
// small or fast enough? But that's confusing if it only
// sometimes finishes recovery. We probably want various
// server start-up modes anyway: "check", "recover", "garbage
// collect", "readonly". So might as well introduce that
// concept now.
// TODO: test start-up recovery mode, once it works.
}
return sto, nil
}
func (s *storage) anyMeta() (v bool) {
// TODO: we only care about getting 1 row, but the
// sorted.KeyValue interface doesn't let us give it that
// hint. Care?
sorted.Foreach(s.meta, func(_, _ string) error {
v = true
return errors.New("stop")
})
return
}
func (s *storage) anyZipPacks() (v bool) {
ctx := context.New()
defer ctx.Cancel()
dest := make(chan blob.SizedRef, 1)
if err := s.large.EnumerateBlobs(ctx, dest, "", 1); err != nil {
// Not a great interface in general, but only needed
// by the start-up check for now, where it doesn't
// really matter.
return false
}
_, ok := <-dest
return ok
}
func (s *storage) Close() error {
return nil
}
func (s *storage) StorageGeneration() (initTime time.Time, random string, err error) {
sgen, sok := s.small.(blobserver.Generationer)
lgen, lok := s.large.(blobserver.Generationer)
if !sok || !lok {
return time.Time{}, "", blobserver.GenerationNotSupportedError("underlying storage engines don't support Generationer")
}
st, srand, err := sgen.StorageGeneration()
if err != nil {
return
}
lt, lrand, err := lgen.StorageGeneration()
if err != nil {
return
}
hash := sha1.New()
io.WriteString(hash, srand)
io.WriteString(hash, lrand)
maxTime := func(a, b time.Time) time.Time {
if a.After(b) {
return a
}
return b
}
return maxTime(lt, st), fmt.Sprintf("%x", hash.Sum(nil)), nil
}
func (s *storage) ResetStorageGeneration() error {
var retErr error
for _, st := range []blobserver.Storage{s.small, s.large} {
if g, ok := st.(blobserver.Generationer); ok {
if err := g.ResetStorageGeneration(); err != nil {
retErr = err
}
}
}
return retErr
}
type meta struct {
exists bool
size uint32
largeRef blob.Ref // if invalid, then on small if exists
largeOff uint32
}
func (m *meta) isPacked() bool { return m.largeRef.Valid() }
// if not found, err == nil.
func (s *storage) getMetaRow(br blob.Ref) (meta, error) {
v, err := s.meta.Get(blobMetaPrefix + br.String())
if err == sorted.ErrNotFound {
return meta{}, nil
}
return parseMetaRow([]byte(v))
}
var singleSpace = []byte{' '}
// parses:
// "<size_u32> <big-blobref> <big-offset>"
func parseMetaRow(v []byte) (m meta, err error) {
row := v
sp := bytes.IndexByte(v, ' ')
if sp < 1 || sp == len(v)-1 {
return meta{}, fmt.Errorf("invalid metarow %q", v)
}
m.exists = true
size, err := strutil.ParseUintBytes(v[:sp], 10, 32)
if err != nil {
return meta{}, fmt.Errorf("invalid metarow size %q", v)
}
m.size = uint32(size)
v = v[sp+1:]
// remains: "<big-blobref> <big-offset>"
if bytes.Count(v, singleSpace) != 1 {
return meta{}, fmt.Errorf("invalid metarow %q: wrong number of spaces", row)
}
sp = bytes.IndexByte(v, ' ')
largeRef, ok := blob.ParseBytes(v[:sp])
if !ok {
return meta{}, fmt.Errorf("invalid metarow %q: bad blobref %q", row, v[:sp])
}
m.largeRef = largeRef
off, err := strutil.ParseUintBytes(v[sp+1:], 10, 32)
if err != nil {
return meta{}, fmt.Errorf("invalid metarow %q: bad offset: %v", row, err)
}
m.largeOff = uint32(off)
return m, nil
}
func parseMetaRowSizeOnly(v []byte) (size uint32, err error) {
sp := bytes.IndexByte(v, ' ')
if sp < 1 || sp == len(v)-1 {
return 0, fmt.Errorf("invalid metarow %q", v)
}
size64, err := strutil.ParseUintBytes(v[:sp], 10, 32)
if err != nil {
return 0, fmt.Errorf("invalid metarow size %q", v)
}
return uint32(size64), nil
}
func (s *storage) ReceiveBlob(br blob.Ref, source io.Reader) (sb blob.SizedRef, err error) {
buf := pools.BytesBuffer()
defer pools.PutBuffer(buf)
if _, err := io.Copy(buf, source); err != nil {
return sb, err
}
size := uint32(buf.Len())
isFile := false
fileBlob, err := schema.BlobFromReader(br, bytes.NewReader(buf.Bytes()))
if err == nil && fileBlob.Type() == "file" {
isFile = true
}
meta, err := s.getMetaRow(br)
if err != nil {
return sb, err
}
if meta.exists {
sb = blob.SizedRef{Size: size, Ref: br}
} else {
sb, err = s.small.ReceiveBlob(br, buf)
if err != nil {
return sb, err
}
}
if !isFile || meta.isPacked() || fileBlob.PartsSize() < packThreshold {
return sb, nil
}
// Pack the blob.
s.packGate.Start()
defer s.packGate.Done()
// We ignore the return value from packFile since we can't
// really recover. At least be happy that we have all the
// data on 'small' already. packFile will log at least.
s.packFile(br)
return sb, nil
}
func (s *storage) Fetch(br blob.Ref) (io.ReadCloser, uint32, error) {
m, err := s.getMetaRow(br)
if err != nil {
return nil, 0, err
}
if !m.exists || !m.isPacked() {
return s.small.Fetch(br)
}
rc, err := s.large.SubFetch(m.largeRef, int64(m.largeOff), int64(m.size))
if err != nil {
return nil, 0, err
}
return rc, m.size, nil
}
const removeLookups = 50 // arbitrary
func (s *storage) RemoveBlobs(blobs []blob.Ref) error {
// Plan:
// -- delete from small (if it's there)
// -- if in big, update the meta index to note that it's there, but deleted.
// -- fetch big's zip file (constructed from a ReaderAt that is all dummy zeros +
// the zip's TOC only, relying on big being a SubFetcher, and keeping info in
// the meta about the offset of the TOC+total size of each big's zip)
// -- iterate over the zip's blobs (at some point). If all are marked deleted, actually RemoveBlob
// on big to delete the full zip and then delete all the meta rows.
var (
mu sync.Mutex
unpacked []blob.Ref
packed []blob.Ref
large = map[blob.Ref]bool{} // the large blobs that packed are in
)
var grp syncutil.Group
delGate := syncutil.NewGate(removeLookups)
for _, br := range blobs {
br := br
delGate.Start()
grp.Go(func() error {
defer delGate.Done()
m, err := s.getMetaRow(br)
if err != nil {
return err
}
mu.Lock()
defer mu.Unlock()
if m.isPacked() {
packed = append(packed, br)
large[m.largeRef] = true
} else {
unpacked = append(unpacked, br)
}
return nil
})
}
if err := grp.Err(); err != nil {
return err
}
if len(unpacked) > 0 {
grp.Go(func() error {
return s.small.RemoveBlobs(unpacked)
})
}
if len(packed) > 0 {
grp.Go(func() error {
bm := s.meta.BeginBatch()
now := time.Now()
for zipRef := range large {
bm.Set("d:"+zipRef.String(), fmt.Sprint(now.Unix()))
}
for _, br := range packed {
bm.Delete("b:" + br.String())
}
return s.meta.CommitBatch(bm)
})
}
return grp.Err()
}
func (s *storage) StatBlobs(dest chan<- blob.SizedRef, blobs []blob.Ref) error {
if len(blobs) == 0 {
return nil
}
var (
grp syncutil.Group
trySmallMu sync.Mutex
trySmall []blob.Ref
)
statGate := syncutil.NewGate(50) // arbitrary
for _, br := range blobs {
br := br
statGate.Start()
grp.Go(func() error {
defer statGate.Done()
m, err := s.getMetaRow(br)
if err != nil {
return err
}
if m.exists {
dest <- blob.SizedRef{Ref: br, Size: m.size}
} else {
trySmallMu.Lock()
trySmall = append(trySmall, br)
// Assume append cannot fail or panic
trySmallMu.Unlock()
}
return nil
})
}
if err := grp.Err(); err != nil {
return err
}
if len(trySmall) == 0 {
return nil
}
return s.small.StatBlobs(dest, trySmall)
}
func (s *storage) EnumerateBlobs(ctx *context.Context, dest chan<- blob.SizedRef, after string, limit int) (err error) {
return blobserver.MergedEnumerate(ctx, dest, []blobserver.BlobEnumerator{
s.small,
enumerator{s},
}, after, limit)
}
// enumerator implements EnumerateBlobs.
type enumerator struct {
*storage
}
func (s enumerator) EnumerateBlobs(ctx *context.Context, dest chan<- blob.SizedRef, after string, limit int) (err error) {
defer close(dest)
t := s.meta.Find(blobMetaPrefix+after, blobMetaPrefixLimit)
defer func() {
closeErr := t.Close()
if err == nil {
err = closeErr
}
}()
n := 0
afterb := []byte(after)
for n < limit && t.Next() {
key := t.KeyBytes()[len(blobMetaPrefix):]
if n == 0 && bytes.Equal(key, afterb) {
continue
}
n++
br, ok := blob.ParseBytes(key)
if !ok {
return fmt.Errorf("unknown key %q in meta index", t.Key())
}
size, err := parseMetaRowSizeOnly(t.ValueBytes())
if err != nil {
return err
}
dest <- blob.SizedRef{Ref: br, Size: size}
}
return nil
}
func (s *storage) packFile(fileRef blob.Ref) (err error) {
s.Logf("Packing file %s ...", fileRef)
defer func() {
if err == nil {
s.Logf("Packed file %s", fileRef)
} else {
s.Logf("Error packing file %s: %v", fileRef, err)
}
}()
fr, err := schema.NewFileReader(s, fileRef)
if err != nil {
return err
}
return newPacker(s, fileRef, fr).pack()
}
func newPacker(s *storage, fileRef blob.Ref, fr *schema.FileReader) *packer {
return &packer{
s: s,
fileRef: fileRef,
fr: fr,
dataSize: map[blob.Ref]uint32{},
schemaBlob: map[blob.Ref]*blob.Blob{},
schemaParent: map[blob.Ref][]blob.Ref{},
}
}
// A packer writes a file out
type packer struct {
s *storage
fileRef blob.Ref
fr *schema.FileReader
wholeRef blob.Ref
wholeSize int64
dataRefs []blob.Ref // in order
dataSize map[blob.Ref]uint32
schemaRefs []blob.Ref // in order, but irrelevant
schemaBlob map[blob.Ref]*blob.Blob
schemaParent map[blob.Ref][]blob.Ref // data blob -> its parent/ancestor schema blob(s)
chunksRemain []blob.Ref
zips []writtenZip
wholeBytesWritten int64 // sum of zips.dataRefs.size
}
type writtenZip struct {
blob.SizedRef
dataRefs []blob.Ref
}
var (
testHookSawTruncate func(blob.Ref)
testHookStopBeforeOverflowing func()
)
func (pk *packer) pack() error {
if err := pk.scanChunks(); err != nil {
return err
}
// TODO: decide as a fuction of schemaRefs and dataRefs
// already in s.large whether it makes sense to still compact
// this from a savings standpoint. For now we just always do.
// Maybe we'd have knobs in the future. Ideally not.
// Don't pack a file if we already have its wholeref stored
// otherwise (perhaps under a different filename). But that
// means we have to compute its wholeref first. We assume the
// blob source will cache these lookups so it's not too
// expensive to do two passes over the input.
h := blob.NewHash()
var err error
pk.wholeSize, err = io.Copy(h, pk.fr)
if err != nil {
return err
}
pk.wholeRef = blob.RefFromHash(h)
wholeKey := wholeMetaPrefix + pk.wholeRef.String()
_, err = pk.s.meta.Get(wholeKey)
if err == nil {
// Nil error means there was some knowledge of this wholeref.
return fmt.Errorf("already have wholeref %v packed; not packing again", pk.wholeRef)
} else if err != sorted.ErrNotFound {
return err
}
pk.chunksRemain = pk.dataRefs
var trunc blob.Ref
MakingZips:
for len(pk.chunksRemain) > 0 {
if err := pk.writeAZip(trunc); err != nil {
if needTrunc, ok := err.(needsTruncatedAfterError); ok {
trunc = needTrunc.Ref
if fn := testHookSawTruncate; fn != nil {
fn(trunc)
}
continue MakingZips
}
return err
}
trunc = blob.Ref{}
}
// Record the final wholeMetaPrefix record:
err = pk.s.meta.Set(wholeKey, fmt.Sprintf("%d %d", pk.wholeSize, len(pk.zips)))
if err != nil {
return fmt.Errorf("Error setting %s: %v", wholeKey, err)
}
return nil
}
func (pk *packer) scanChunks() error {
schemaSeen := map[blob.Ref]bool{}
return pk.fr.ForeachChunk(func(schemaPath []blob.Ref, p schema.BytesPart) error {
if !p.BlobRef.Valid() {
return errors.New("sparse files are not packed")
}
if p.Offset != 0 {
// TODO: maybe care about this later, if we ever start making
// these sorts of files.
return errors.New("file uses complicated schema. not packing.")
}
pk.schemaParent[p.BlobRef] = append([]blob.Ref(nil), schemaPath...) // clone it
pk.dataSize[p.BlobRef] = uint32(p.Size)
for _, schemaRef := range schemaPath {
if schemaSeen[schemaRef] {
continue
}
schemaSeen[schemaRef] = true
pk.schemaRefs = append(pk.schemaRefs, schemaRef)
if b, err := blob.FromFetcher(pk.s, schemaRef); err != nil {
return err
} else {
pk.schemaBlob[schemaRef] = b
}
}
pk.dataRefs = append(pk.dataRefs, p.BlobRef)
return nil
})
}
// needsTruncatedAfterError is returend by writeAZip if it failed in its estimation and the zip file
// was over the 16MB (or whatever) max blob size limit. In this case the caller tries again
type needsTruncatedAfterError struct{ blob.Ref }
func (e needsTruncatedAfterError) Error() string { return "needs truncation after " + e.Ref.String() }
// check should only be used for things which really shouldn't ever happen, but should
// still be checked. If there is interesting logic in the 'else', then don't use this.
func check(err error) {
if err != nil {
b := make([]byte, 2<<10)
b = b[:runtime.Stack(b, false)]
log.Printf("Unlikely error condition triggered: %v at %s", err, b)
panic(err)
}
}
// trunc is a hint about which blob to truncate after. It may be zero.
// If the returned error is of type 'needsTruncatedAfterError', then
// the zip should be attempted to be written again, but truncating the
// data after the listed blob.
func (pk *packer) writeAZip(trunc blob.Ref) (err error) {
defer func() {
if e := recover(); e != nil {
if v, ok := e.(error); ok && err == nil {
err = v
} else {
panic(e)
}
}
}()
mf := Manifest{
WholeRef: pk.wholeRef,
WholeSize: pk.wholeSize,
WholePartIndex: len(pk.zips),
}
var zbuf bytes.Buffer
cw := &countWriter{w: &zbuf}
zw := zip.NewWriter(cw)
var approxSize = zipFixedOverhead // can't use zbuf.Len because zw buffers
var dataRefsWritten []blob.Ref
var dataBytesWritten int64
var schemaBlobSeen = map[blob.Ref]bool{}
var schemaBlobs []blob.Ref // to add after the main file
baseFileName := pk.fr.FileName()
if strings.Contains(baseFileName, "/") || strings.Contains(baseFileName, "\\") {
return fmt.Errorf("File schema blob %v filename had a slash in it: %q", pk.fr.SchemaBlobRef(), baseFileName)
}
fh := &zip.FileHeader{
Name: baseFileName,
Method: zip.Store, // uncompressed
}
fh.SetModTime(pk.fr.ModTime())
fh.SetMode(0644)
fw, err := zw.CreateHeader(fh)
check(err)
check(zw.Flush())
dataStart := cw.n
approxSize += zipPerEntryOverhead // for the first FileHeader w/ the data
zipMax := pk.s.maxZipBlobSize()
chunks := pk.chunksRemain
chunkWholeHash := blob.NewHash()
for len(chunks) > 0 {
dr := chunks[0] // the next chunk to maybe write
if trunc.Valid() && trunc == dr {
if approxSize == 0 {
return errors.New("first blob is too large to pack, once you add the zip overhead")
}
break
}
schemaBlobsSave := schemaBlobs
for _, parent := range pk.schemaParent[dr] {
if !schemaBlobSeen[parent] {
schemaBlobSeen[parent] = true
schemaBlobs = append(schemaBlobs, parent)
approxSize += int(pk.schemaBlob[parent].Size()) + zipPerEntryOverhead
}
}
thisSize := pk.dataSize[dr]
approxSize += int(thisSize)
if approxSize+mf.approxSerializedSize() > zipMax {
if fn := testHookStopBeforeOverflowing; fn != nil {
fn()
}
schemaBlobs = schemaBlobsSave // restore it
break
}
// Copy the data to the zip.
rc, size, err := pk.s.Fetch(dr)
check(err)
if size != thisSize {
rc.Close()
return errors.New("unexpected size")
}
if n, err := io.Copy(io.MultiWriter(fw, chunkWholeHash), rc); err != nil || n != int64(size) {
rc.Close()
return fmt.Errorf("copy to zip = %v, %v; want %v bytes", n, err, size)
}
rc.Close()
dataRefsWritten = append(dataRefsWritten, dr)
dataBytesWritten += int64(size)
chunks = chunks[1:]
}
mf.DataBlobsOrigin = blob.RefFromHash(chunkWholeHash)
// zipBlobs is where a schema or data blob is relative to the beginning
// of the zip file.
var zipBlobs []BlobAndPos
var dataOffset int64
for _, br := range dataRefsWritten {
size := pk.dataSize[br]
mf.DataBlobs = append(mf.DataBlobs, BlobAndPos{blob.SizedRef{br, size}, dataOffset})
zipBlobs = append(zipBlobs, BlobAndPos{blob.SizedRef{br, size}, dataStart + dataOffset})
dataOffset += int64(size)
}
for _, br := range schemaBlobs {
fw, err := zw.CreateHeader(&zip.FileHeader{
Name: "camlistore/" + br.String() + ".json",
Method: zip.Store, // uncompressed
})
check(err)
check(zw.Flush())
b := pk.schemaBlob[br]
zipBlobs = append(zipBlobs, BlobAndPos{blob.SizedRef{br, b.Size()}, cw.n})
rc := b.Open()
n, err := io.Copy(fw, rc)
rc.Close()
check(err)
if n != int64(b.Size()) {
return fmt.Errorf("failed to write all of schema blob %v: %d bytes, not wanted %d", br, n, b.Size())
}
}
// Manifest file
fw, err = zw.Create(zipManifestPath)
check(err)
enc, err := json.MarshalIndent(mf, "", " ")
check(err)
_, err = fw.Write(enc)
check(err)
err = zw.Close()
check(err)
if zbuf.Len() > zipMax {
// We guessed wrong. Back up. Find out how many blobs we went over.
overage := zbuf.Len() - zipMax
for i := len(dataRefsWritten) - 1; i >= 0; i-- {
dr := dataRefsWritten[i]
if overage <= 0 {
return needsTruncatedAfterError{dr}
}
overage -= int(pk.dataSize[dr])
}
return errors.New("file is unpackable; first blob is too big to fit")
}
zipRef := blob.SHA1FromBytes(zbuf.Bytes())
zipSB, err := blobserver.ReceiveNoHash(pk.s.large, zipRef, bytes.NewReader(zbuf.Bytes()))
if err != nil {
return err
}
bm := pk.s.meta.BeginBatch()
bm.Set(fmt.Sprintf("%s%s:%d", wholeMetaPrefix, pk.wholeRef, len(pk.zips)),
fmt.Sprintf("%s %d %d %d",
zipRef,
dataStart,
pk.wholeBytesWritten,
dataBytesWritten))
pk.wholeBytesWritten += dataBytesWritten
pk.zips = append(pk.zips, writtenZip{
SizedRef: zipSB,
dataRefs: dataRefsWritten,
})
for _, zb := range zipBlobs {
bm.Set(blobMetaPrefix+zb.Ref.String(), fmt.Sprintf("%d %v %d", zb.Size, zipRef, zb.Offset))
}
if err := pk.s.meta.CommitBatch(bm); err != nil {
return err
}
// Delete from small
if !pk.s.skipDelete {
toDelete := make([]blob.Ref, 0, len(dataRefsWritten)+len(schemaBlobs))
toDelete = append(toDelete, dataRefsWritten...)
toDelete = append(toDelete, schemaBlobs...)
if err := pk.s.small.RemoveBlobs(toDelete); err != nil {
// Can't really do anything about it and doesn't really matter, so
// just log for now.
pk.s.Logf("Error removing blobs from %s: %v", pk.s.small, err)
}
}
// On success, consume the chunks we wrote from pk.chunksRemain.
pk.chunksRemain = pk.chunksRemain[len(dataRefsWritten):]
return nil
}
type zipOpenError struct {
zipRef blob.Ref
err error
}
func (ze zipOpenError) Error() string {
return fmt.Sprintf("Error opening packed zip blob %v: %v", ze.zipRef, ze.err)
}
// foreachZipBlob calls fn for each blob in the zip pack blob
// identified by zipRef. If fn returns a non-nil error,
// foreachZipBlob stops enumerating with that error.
func (s *storage) foreachZipBlob(zipRef blob.Ref, fn func(BlobAndPos) error) error {
sb, err := blobserver.StatBlob(s.large, zipRef)
if err != nil {
return err
}
zr, err := zip.NewReader(blob.ReaderAt(s.large, zipRef), int64(sb.Size))
if err != nil {
return zipOpenError{zipRef, err}
}
var maniFile *zip.File // or nil if not found
var firstOff int64 // offset of first file (the packed data chunks)
for i, f := range zr.File {
if i == 0 {
firstOff, err = f.DataOffset()
if err != nil {
return err
}
}
if f.Name == zipManifestPath {
maniFile = f
break
}
}
if maniFile == nil {
return errors.New("no camlistore manifest file found in zip")
}
for _, f := range zr.File {