~tsileo/blobstash

ref: b178995e346d blobstash/pkg/stash/stash.go -rw-r--r-- 11.8 KiB
b178995eThomas Sileo vendor: update deps 1 year, 11 months ago
                                                                                
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
package stash // import "a4.io/blobstash/pkg/stash"

import (
	"context"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"sync"

	log "github.com/inconshreveable/log15"

	"a4.io/blobsfile"
	"a4.io/blobstash/pkg/blob"
	"a4.io/blobstash/pkg/blobstore"
	"a4.io/blobstash/pkg/ctxutil"
	"a4.io/blobstash/pkg/filetree/filetreeutil/node"
	"a4.io/blobstash/pkg/hub"
	"a4.io/blobstash/pkg/kvstore"
	"a4.io/blobstash/pkg/meta"
	"a4.io/blobstash/pkg/stash/store"
	"a4.io/blobstash/pkg/vkv"
)

type dataContext struct {
	bs       store.BlobStore
	kvs      store.KvStore
	bsDst    store.BlobStore
	bsProxy  store.BlobStore
	kvsProxy store.KvStore
	hub      *hub.Hub
	meta     *meta.Meta
	log      log.Logger
	dir      string
	root     bool
	closed   bool
}

func (dc *dataContext) StashBlobStore() store.BlobStore {
	return dc.bsDst
}

func (dc *dataContext) BlobStore() store.BlobStore {
	return dc.bs
}

func (dc *dataContext) KvStore() store.KvStore {
	return dc.kvs
}

func (dc *dataContext) BlobStoreProxy() store.BlobStore {
	return dc.bsProxy
}

func (dc *dataContext) KvStoreProxy() store.KvStore {
	return dc.kvsProxy
}

func (dc *dataContext) Closed() bool {
	return dc.closed
}

func (dc *dataContext) Merge(ctx context.Context) error {
	if dc.root {
		return nil
	}

	blobs, _, err := dc.bs.Enumerate(ctx, "", "\xff", 0)
	if err != nil {
		return err
	}
	for _, blobRef := range blobs {
		data, err := dc.bs.Get(ctx, blobRef.Hash)
		if err != nil {
			return err
		}
		b := &blob.Blob{Hash: blobRef.Hash, Data: data}
		if _, err := dc.bsProxy.(*store.BlobStoreProxy).ReadSrc.Put(ctx, b); err != nil {
			return err
		}
	}

	return nil
}

// orderedRefs holds the "sorted" references
type orderedRefs struct {
	refs []string
	idx  map[string]struct{}
}

func newOrderedRefs() *orderedRefs {
	return &orderedRefs{
		refs: []string{},
		idx:  map[string]struct{}{},
	}
}

func (r *orderedRefs) Add(ref string) {
	if _, ok := r.idx[ref]; !ok {
		r.idx[ref] = struct{}{}
		r.refs = append(r.refs, ref)
	}
}

func (dc *dataContext) MergeFileTreeNode(ctx context.Context, refs *orderedRefs, bs store.BlobStore, ref string) error {
	data, err := dc.bsProxy.Get(ctx, ref)
	if err != nil {
		return err
	}

	n, err := node.NewNodeFromBlob(ref, data)
	if err != nil {
		return err
	}

	if n.Type == "file" {
		for _, dref := range n.Refs {
			// Save each blob content
			data := dref.([]interface{})
			bref := data[1].(string)
			refs.Add(bref)
		}
	} else {
		// Iter the dir
		for _, cref := range n.Refs {
			// Merge the children recursively
			if err := dc.MergeFileTreeNode(ctx, refs, bs, cref.(string)); err != nil {
				return err
			}
		}
	}

	// Only save the node ref once all it's children has been saved
	refs.Add(ref)

	return nil
}

func (dc *dataContext) MergeFileTreeVersion(ctx context.Context, key string, version int64) (*orderedRefs, error) {
	if dc.root {
		return nil, fmt.Errorf("cannot merge filtree version in root data context")
	}

	refs := newOrderedRefs()

	// Fetch the root BlobStore (as fetching the original struct behind the interface is costly)
	rootBs := dc.bsProxy.(*store.BlobStoreProxy).ReadSrc

	// Fetch the blob that contains the KV entry for the FileTree version
	kvBlobRef, err := dc.kvs.GetMetaBlob(ctx, key, version)
	if err != nil {
		return nil, err
	}

	// Now, traverse the tree, starting at the root
	kv, err := dc.kvs.Get(ctx, key, version)
	if err != nil {
		return nil, err
	}

	// Merge the root node recursively
	ftRoot := kv.HexHash()
	if err := dc.MergeFileTreeNode(ctx, refs, rootBs, ftRoot); err != nil {
		return nil, err
	}

	refs.Add(kvBlobRef)

	return refs, nil
}

func (dc *dataContext) Close() error {
	if dc.closed || dc.root {
		return nil
	}
	// TODO(tsileo): multi error
	if err := dc.kvs.Close(); err != nil {
		return err
	}
	if err := dc.bs.Close(); err != nil {
		return err
	}
	dc.closed = true
	return nil
}

func (dc *dataContext) Destroy() error {
	if dc.root {
		return nil
	}
	if err := dc.Close(); err != nil {
		return err
	}
	// TODO(tsileo): only call Destroy from Stash and unexport this one, also remove from index
	return os.RemoveAll(dc.dir)
}

type Stash struct {
	rootDataContext *dataContext
	contexes        map[string]*dataContext
	path            string
	sync.Mutex
}

func (s *Stash) destroy(dataContext *dataContext, name string) error {
	if dataContext.root {
		return fmt.Errorf("cannot destroy the root data context")
	}

	delete(s.contexes, name)

	if err := dataContext.Destroy(); err != nil {
		return err
	}

	return nil
}

func New(dir string, m *meta.Meta, bs *blobstore.BlobStore, kvs *kvstore.KvStore, h *hub.Hub, l log.Logger) (*Stash, error) {
	s := &Stash{
		contexes: map[string]*dataContext{},
		path:     dir,
		rootDataContext: &dataContext{
			bs:       bs,
			kvs:      kvs,
			bsProxy:  bs,
			kvsProxy: kvs,
			hub:      h,
			meta:     m,
			log:      l,

			root: true,
		},
	}

	stashes, err := ioutil.ReadDir(dir)
	if err != nil && !os.IsNotExist(err) {
		return nil, err
	}
	if err == nil {
		for _, dir := range stashes {
			if _, err := s.NewDataContext(dir.Name()); err != nil {
				return nil, err
			}
		}
	}

	// FIXME(tsileo): BlobStore.Scan should be triggered here??, and for all available stashes

	return s, nil

}

func (s *Stash) NewDataContext(name string) (*dataContext, error) {
	s.Lock()
	defer s.Unlock()
	path := filepath.Join(s.path, name)
	if _, err := os.Stat(path); os.IsNotExist(err) {
		if err := os.MkdirAll(path, 0700); err != nil {
			return nil, err
		}
	}
	l := s.rootDataContext.log.New("data_ctx", name)
	h := hub.New(l.New("app", "hub"), false)
	m, err := meta.New(l.New("app", "meta"), h)
	if err != nil {
		return nil, err
	}
	// XXX(tsileo): use a dumb single file cache instead of the blobstore?
	bsDst, err := blobstore.New(l.New("app", "blobstore"), false, path, nil, h)
	if err != nil {
		return nil, err
	}
	bs := &store.BlobStoreProxy{
		BlobStore: bsDst,
		ReadSrc:   s.rootDataContext.bs,
	}
	kvsDst, err := kvstore.New(l.New("app", "kvstore"), path, bs, m)
	if err != nil {
		return nil, err
	}
	kvs := &store.KvStoreProxy{
		KvStore: kvsDst,
		ReadSrc: s.rootDataContext.kvs,
	}
	dataCtx := &dataContext{
		bsDst:    bsDst,
		log:      l,
		meta:     m,
		hub:      h,
		bs:       bsDst,
		kvs:      kvsDst,
		kvsProxy: kvs,
		bsProxy:  bs,
		dir:      path,
	}
	s.contexes[name] = dataCtx
	return dataCtx, nil
}

func (s *Stash) Close() error {
	s.rootDataContext.Close()
	s.Lock()
	defer s.Unlock()
	for _, dc := range s.contexes {
		dc.Close()
	}
	return nil
}

func (s *Stash) Root() store.DataContext {
	return s.rootDataContext
}

func (s *Stash) DoAndDestroy(ctx context.Context, name string, do func(context.Context, store.DataContext) error) error {
	s.Lock()
	dc, ok := s.contexes[name]
	if !ok {
		s.Unlock()
		return fmt.Errorf("data context not found")
	}
	s.Unlock()

	if err := do(ctx, dc); err != nil {
		return err
	}

	s.Lock()
	defer s.Unlock()
	if err := s.destroy(dc, name); err != nil {
		return err
	}

	return nil
}

func (s *Stash) MergeFileTreeVersionAndDestroy(ctx context.Context, name string, key string, version int64) error {
	s.Lock()
	defer s.Unlock()
	dc, ok := s.contexes[name]
	if !ok {
		return fmt.Errorf("data context not found")
	}

	refs, err := dc.MergeFileTreeVersion(ctx, key, version)
	if err != nil {
		return err
	}

	var blobsCnt int
	var totalSize uint64
	for _, ref := range refs.refs {
		// Get the marked blob from the blobstore proxy
		data, err := dc.StashBlobStore().Get(ctx, ref)
		if err != nil {
			if err == blobsfile.ErrBlobNotFound {
				continue
			}
			return err
		}

		// Save it in the root blobstore
		saved, err := s.Root().BlobStore().Put(ctx, &blob.Blob{Hash: ref, Data: data})
		if err != nil {
			return err
		}

		if saved {
			blobsCnt++
			totalSize += uint64(len(data))
		}
	}
	fmt.Printf("GC/merge filetree refs=%d blobs, saved %d blobs\n", len(refs.refs), blobsCnt)

	if err := s.destroy(dc, name); err != nil {
		return err
	}

	return nil
}

func (s *Stash) MergeAndDestroy(ctx context.Context, name string) error {
	s.Lock()
	defer s.Unlock()
	dc, ok := s.contexes[name]
	if !ok {
		return fmt.Errorf("data context not found")
	}

	if err := dc.Merge(ctx); err != nil {
		return err
	}

	if err := s.destroy(dc, name); err != nil {
		return err
	}

	return nil
}

func (s *Stash) Destroy(ctx context.Context, name string) error {
	s.Lock()
	defer s.Unlock()
	dc, ok := s.contexes[name]
	if !ok {
		return fmt.Errorf("data context not found")
	}

	if err := s.destroy(dc, name); err != nil {
		return err
	}

	return nil
}

func (s *Stash) dataContext(ctx context.Context) (*dataContext, error) {
	// TODO(tsileo): handle destroyed context
	name, _ := ctxutil.Namespace(ctx)
	if ctx, ok := s.DataContextByName(name); ok {
		return ctx, nil
	}

	// If it does not exist, create it now
	return s.NewDataContext(name)
}

func (s *Stash) ContextNames() []string {
	s.Lock()
	defer s.Unlock()
	var out []string
	for k, _ := range s.contexes {
		out = append(out, k)
	}
	return out
}

func (s *Stash) DataContextByName(name string) (*dataContext, bool) {
	if name == "" {
		return s.rootDataContext, true
	}

	// FIXME(tsileo): fix the deadlock
	s.Lock()
	defer s.Unlock()
	if dc, ok := s.contexes[name]; ok {
		return dc, true
	}

	return nil, false
}

func (s *Stash) BlobStore() *BlobStore {
	return &BlobStore{s}
}

func (s *Stash) KvStore() *KvStore {
	return &KvStore{s}
}

type BlobStore struct {
	s *Stash
}

func (bs *BlobStore) Close() error { return nil } // TODO(tsileo): check if no closing is needed?

func (bs *BlobStore) Put(ctx context.Context, blob *blob.Blob) (bool, error) {
	dataContext, err := bs.s.dataContext(ctx)
	if err != nil {
		return false, err
	}
	return dataContext.BlobStoreProxy().Put(ctx, blob)
}

func (bs *BlobStore) Get(ctx context.Context, hash string) ([]byte, error) {
	dataContext, err := bs.s.dataContext(ctx)
	if err != nil {
		return nil, err
	}
	return dataContext.BlobStoreProxy().Get(ctx, hash)

}

func (bs *BlobStore) Stat(ctx context.Context, hash string) (bool, error) {
	dataContext, err := bs.s.dataContext(ctx)
	if err != nil {
		return false, err
	}
	return dataContext.BlobStoreProxy().Stat(ctx, hash)

}

func (bs *BlobStore) Enumerate(ctx context.Context, start, end string, limit int) ([]*blob.SizedBlobRef, string, error) {
	dataContext, err := bs.s.dataContext(ctx)
	if err != nil {
		return nil, "", err
	}
	return dataContext.BlobStoreProxy().Enumerate(ctx, start, end, limit)
}

type KvStore struct {
	s *Stash
}

func (kv *KvStore) Close() error { return nil }

func (kv *KvStore) Put(ctx context.Context, key, ref string, data []byte, version int64) (*vkv.KeyValue, error) {
	dataContext, err := kv.s.dataContext(ctx)
	if err != nil {
		return nil, err
	}
	return dataContext.KvStoreProxy().Put(ctx, key, ref, data, version)
}

func (kv *KvStore) Get(ctx context.Context, key string, version int64) (*vkv.KeyValue, error) {
	dataContext, err := kv.s.dataContext(ctx)
	if err != nil {
		return nil, err
	}
	return dataContext.KvStoreProxy().Get(ctx, key, version)
}

func (kv *KvStore) GetMetaBlob(ctx context.Context, key string, version int64) (string, error) {
	dataContext, err := kv.s.dataContext(ctx)
	if err != nil {
		return "", err
	}
	return dataContext.KvStoreProxy().GetMetaBlob(ctx, key, version)
}

func (kv *KvStore) Versions(ctx context.Context, key, start string, limit int) (*vkv.KeyValueVersions, string, error) {
	dataContext, err := kv.s.dataContext(ctx)
	if err != nil {
		return nil, "", err
	}
	return dataContext.KvStoreProxy().Versions(ctx, key, start, limit)
}

func (kv *KvStore) Keys(ctx context.Context, start, end string, limit int) ([]*vkv.KeyValue, string, error) {
	dataContext, err := kv.s.dataContext(ctx)
	if err != nil {
		return nil, "", err
	}
	return dataContext.KvStoreProxy().Keys(ctx, start, end, limit)
}

func (kv *KvStore) ReverseKeys(ctx context.Context, start, end string, limit int) ([]*vkv.KeyValue, string, error) {
	dataContext, err := kv.s.dataContext(ctx)
	if err != nil {
		return nil, "", err
	}
	return dataContext.KvStoreProxy().ReverseKeys(ctx, start, end, limit)
}