forked from berty/go-orbit-db
/
base_store.go
1245 lines (993 loc) · 32.4 KB
/
base_store.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
package basestore
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"sync"
"time"
files "github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
cid "github.com/ipfs/go-cid"
datastore "github.com/ipfs/go-datastore"
coreiface "github.com/ipfs/kubo/core/coreiface"
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
"github.com/pkg/errors"
ipfslog "github.com/thusharprakash/go-ipfs-log"
logac "github.com/thusharprakash/go-ipfs-log/accesscontroller"
"github.com/thusharprakash/go-ipfs-log/entry"
"github.com/thusharprakash/go-ipfs-log/identityprovider"
ifacelog "github.com/thusharprakash/go-ipfs-log/iface"
"github.com/thusharprakash/go-orbit-db/accesscontroller"
"github.com/thusharprakash/go-orbit-db/accesscontroller/simple"
"github.com/thusharprakash/go-orbit-db/address"
"github.com/thusharprakash/go-orbit-db/events"
"github.com/thusharprakash/go-orbit-db/iface"
"github.com/thusharprakash/go-orbit-db/messagemarshaler"
"github.com/thusharprakash/go-orbit-db/pubsub/pubsubcoreapi"
"github.com/thusharprakash/go-orbit-db/stores"
"github.com/thusharprakash/go-orbit-db/stores/operation"
"github.com/thusharprakash/go-orbit-db/stores/replicator"
otkv "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)
// BaseStore The base of other stores
type BaseStore struct {
emitters struct {
evtWrite event.Emitter
evtReady event.Emitter
evtReplicateProgress event.Emitter
evtLoad event.Emitter
evtLoadProgress event.Emitter
evtReplicated event.Emitter
evtReplicate event.Emitter
}
id string
peerID peer.ID
identity *identityprovider.Identity
address address.Address
dbName string
ipfs coreiface.CoreAPI
cache datastore.Datastore
access accesscontroller.Interface
oplog ipfslog.Log
replicator replicator.Replicator
index iface.StoreIndex
replicationStatus replicator.ReplicationInfo
referenceCount int
directory string
options *iface.NewStoreOptions
cacheDestroy func() error
eventBus event.Bus
pubsub iface.PubSubInterface
messageMarshaler iface.MessageMarshaler
directChannel iface.DirectChannel
muCache sync.RWMutex
muIndex sync.RWMutex
muJoining sync.Mutex
sortFn ipfslog.SortFn
logger *zap.Logger
tracer trace.Tracer
ctx context.Context
cancel context.CancelFunc
closeFunc func()
// Deprecated: if possible don't use this, use EventBus() directly instead
events.EventEmitter
}
func (b *BaseStore) DBName() string {
return b.dbName
}
func (b *BaseStore) IPFS() coreiface.CoreAPI {
return b.ipfs
}
func (b *BaseStore) Identity() *identityprovider.Identity {
return b.identity
}
func (b *BaseStore) OpLog() ipfslog.Log {
b.muIndex.RLock()
defer b.muIndex.RUnlock()
return b.oplog
}
func (b *BaseStore) AccessController() accesscontroller.Interface {
return b.access
}
func (b *BaseStore) Replicator() replicator.Replicator {
return b.replicator
}
func (b *BaseStore) Cache() datastore.Datastore {
b.muCache.RLock()
defer b.muCache.RUnlock()
return b.cache
}
func (b *BaseStore) Logger() *zap.Logger {
return b.logger
}
func (b *BaseStore) Tracer() trace.Tracer {
return b.tracer
}
func (b *BaseStore) IO() ipfslog.IO {
return b.options.IO
}
func (b *BaseStore) EventBus() event.Bus {
return b.eventBus
}
// InitBaseStore Initializes the store base
func (b *BaseStore) InitBaseStore(ipfs coreiface.CoreAPI, identity *identityprovider.Identity, addr address.Address, options *iface.NewStoreOptions) error {
var err error
b.ctx, b.cancel = context.WithCancel(context.Background())
if options == nil {
options = &iface.NewStoreOptions{}
}
if options.EventBus == nil {
options.EventBus = eventbus.NewBus()
} else if err := b.SetBus(options.EventBus); err != nil {
return fmt.Errorf("unable set event bus: %w", err)
}
b.eventBus = options.EventBus
if options.Logger == nil {
options.Logger = zap.NewNop()
}
b.directChannel = options.DirectChannel
k, err := ipfs.Key().Self(b.ctx)
if err != nil {
return err
}
if options.PeerID.Validate() == peer.ErrEmptyPeerID {
id := k.ID()
options.PeerID = id
}
b.peerID = options.PeerID
if options.PubSub == nil {
options.PubSub = pubsubcoreapi.NewPubSub(ipfs, k.ID(), time.Second, options.Logger, options.Tracer)
}
b.pubsub = options.PubSub
if options.MessageMarshaler == nil {
options.MessageMarshaler = &messagemarshaler.JSONMarshaler{}
}
b.messageMarshaler = options.MessageMarshaler
if options.Tracer == nil {
options.Tracer = trace.NewNoopTracerProvider().Tracer("")
}
if identity == nil {
return fmt.Errorf("identity required")
}
if err := b.generateEmitter(b.eventBus); err != nil {
return err
}
b.logger = options.Logger
b.tracer = options.Tracer
b.id = addr.String()
b.identity = identity
b.address = addr
if options.AccessController != nil {
b.access = options.AccessController
} else {
manifestParams := accesscontroller.NewManifestParams(cid.Cid{}, true, "simple")
manifestParams.SetAccess("write", []string{identity.ID})
b.access, err = simple.NewSimpleAccessController(b.ctx, nil, manifestParams)
if err != nil {
return fmt.Errorf("unable to create simple access controller: %w", err)
}
}
b.dbName = addr.GetPath()
b.ipfs = ipfs
b.replicationStatus = replicator.NewReplicationInfo()
b.muCache.Lock()
b.cache = options.Cache
b.cacheDestroy = options.CacheDestroy
b.sortFn = options.SortFn
b.muCache.Unlock()
b.muIndex.Lock()
b.oplog, err = ipfslog.NewLog(ipfs, identity, &ipfslog.LogOptions{
ID: addr.String(),
AccessController: b.AccessController(),
SortFn: b.sortFn,
IO: options.IO,
})
if err != nil {
b.muIndex.Unlock()
return fmt.Errorf("unable to instantiate an IPFS log")
}
if options.Index == nil {
options.Index = NewBaseIndex
}
b.index = options.Index(b.Identity().PublicKey)
b.muIndex.Unlock()
b.replicator, err = replicator.NewReplicator(b, options.ReplicationConcurrency, &replicator.Options{
Logger: b.logger,
EventBus: b.eventBus,
Tracer: b.tracer,
})
if err != nil {
return fmt.Errorf("unable to init error: %w", err)
}
b.referenceCount = 64
if options.ReferenceCount != nil {
b.referenceCount = *options.ReferenceCount
}
// TODO: Doesn't seem to be used
b.directory = "./orbitdb"
if options.Directory != "" {
b.directory = options.Directory
}
if options.Replicate == nil {
options.Replicate = boolPtr(true)
}
if options.CloseFunc == nil {
options.CloseFunc = func() {}
}
b.closeFunc = options.CloseFunc
b.options = options
sub, err := b.replicator.EventBus().Subscribe(replicator.Events,
eventbus.BufSize(128), eventbus.Name("odb/base-store-main-loop"))
if err != nil {
return fmt.Errorf("unable to subscribe to replicator events: %w", err)
}
go func() {
defer sub.Close()
ctx, span := b.tracer.Start(b.ctx, "base-store-main-loop", trace.WithAttributes(otkv.String("store-address", b.Address().String())))
defer span.End()
var e interface{}
for {
select {
case e = <-sub.Out():
case <-ctx.Done():
return
}
switch evt := e.(type) {
case replicator.EventLoadAdded:
maxTotal := 0
if evt.Entry != nil && evt.Entry.GetClock().Defined() {
maxTotal = evt.Entry.GetClock().GetTime()
}
b.recalculateReplicationMax(maxTotal)
if err := b.emitters.evtReplicate.Emit(stores.NewEventReplicate(b.Address(), evt.Hash)); err != nil {
b.logger.Warn("unable to emit event replicate", zap.Error(err))
}
case replicator.EventLoadEnd:
span.AddEvent("replicator-load-end")
// @FIXME(gfanton): should we run this in a goroutine ?
b.replicationLoadComplete(ctx, evt.Logs)
case replicator.EventLoadProgress:
span.AddEvent("replicator-load-progress")
// @FIXME(gfanton): this currently doesn't work and wont emit replicate progress
// previousProgress := b.ReplicationStatus().GetProgress()
// previousMax := b.ReplicationStatus().GetMax()
// maxTotal := evt.Entry.GetClock().GetTime()
// b.recalculateReplicationStatus(maxTotal)
// fmt.Printf("%d: after %d > %d || %d > %d\n", maxTotal,
// b.ReplicationStatus().GetProgress(), previousProgress, b.ReplicationStatus().GetMax(), previousMax)
// if b.ReplicationStatus().GetProgress() > previousProgress || b.ReplicationStatus().GetMax() > previousMax {
// hash := evt.Entry.GetHash()
// err := b.emitters.evtReplicateProgress.Emit(stores.NewEventReplicateProgress(b.Address(), hash, evt.Entry, b.ReplicationStatus()))
// if err != nil {
// b.logger.Warn("unable to emit event replicate progress", zap.Error(err))
// }
// }
maxTotal := 0
if evt.Entry != nil {
maxTotal = evt.Entry.GetClock().GetTime()
}
b.recalculateReplicationStatus(maxTotal)
hash := evt.Entry.GetHash()
err := b.emitters.evtReplicateProgress.Emit(stores.NewEventReplicateProgress(b.Address(), hash, evt.Entry, b.ReplicationStatus()))
if err != nil {
b.logger.Warn("unable to emit event replicate progress", zap.Error(err))
}
}
}
}()
// Subscribe to pubsub to get updates from peers,
// this is what hooks us into the message propagation layer
// and the p2p network
if *options.Replicate {
if options.DirectChannel == nil {
return errors.New("replication needs DirectChannel")
}
if err := b.replicate(); err != nil {
return fmt.Errorf("unable to start store replication: %w", err)
}
}
return nil
}
func (b *BaseStore) isClosed() bool {
select {
case <-b.ctx.Done():
return true
default:
return false
}
}
func (b *BaseStore) Close() error {
if b.isClosed() {
return nil
}
b.cancel()
b.closeFunc()
// Replicator teardown logic
b.Replicator().Stop()
// close emitters
emitters := []event.Emitter{
b.emitters.evtWrite, b.emitters.evtReady,
b.emitters.evtReplicateProgress, b.emitters.evtLoad,
b.emitters.evtReplicated, b.emitters.evtReplicate,
}
for _, emitter := range emitters {
if err := emitter.Close(); err != nil {
b.logger.Warn("unable to close emitter", zap.Error(err))
}
}
// Reset replication statistics
b.ReplicationStatus().Reset()
err := b.Cache().Close()
if err != nil {
return fmt.Errorf("unable to close cache: %w", err)
}
return nil
}
func (b *BaseStore) Address() address.Address {
return b.address
}
func (b *BaseStore) Index() iface.StoreIndex {
b.muIndex.RLock()
defer b.muIndex.RUnlock()
return b.index
}
func (b *BaseStore) Type() string {
return "store"
}
func (b *BaseStore) ReplicationStatus() replicator.ReplicationInfo {
return b.replicationStatus
}
func (b *BaseStore) Drop() error {
var err error
if err = b.Close(); err != nil {
return fmt.Errorf("unable to close store: %w", err)
}
err = b.cacheDestroy()
if err != nil {
return fmt.Errorf("unable to destroy cache: %w", err)
}
// TODO: Destroy cache? b.cache.Delete()
// Reset
b.muIndex.Lock()
b.index = b.options.Index(b.Identity().PublicKey)
b.oplog, err = ipfslog.NewLog(b.IPFS(), b.Identity(), &ipfslog.LogOptions{
ID: b.id,
AccessController: b.AccessController(),
SortFn: b.SortFn(),
IO: b.options.IO,
})
b.muIndex.Unlock()
if err != nil {
return fmt.Errorf("unable to create log: %w", err)
}
b.muCache.Lock()
b.cache = b.options.Cache
b.muCache.Unlock()
return nil
}
func (b *BaseStore) Load(ctx context.Context, amount int) error {
ctx, span := b.tracer.Start(ctx, "store-load")
defer span.End()
if amount <= 0 && b.options.MaxHistory != nil {
amount = *b.options.MaxHistory
}
var localHeads, remoteHeads []*entry.Entry
localHeadsBytes, err := b.Cache().Get(ctx, datastore.NewKey("_localHeads"))
if err != nil && err != datastore.ErrNotFound {
span.AddEvent("local-heads-load-failed")
return fmt.Errorf("unable to get local heads from cache: %w", err)
}
err = nil
if localHeadsBytes != nil {
span.AddEvent("local-heads-unmarshall")
err = json.Unmarshal(localHeadsBytes, &localHeads)
if err != nil {
span.AddEvent("local-heads-unmarshall-failed")
b.logger.Warn("unable to unmarshal cached local heads", zap.Error(err))
}
span.AddEvent("local-heads-unmarshalled")
}
remoteHeadsBytes, err := b.Cache().Get(ctx, datastore.NewKey("_remoteHeads"))
if err != nil && err != datastore.ErrNotFound {
span.AddEvent("remote-heads-load-failed")
return fmt.Errorf("unable to get data from cache: %w", err)
}
err = nil
if remoteHeadsBytes != nil {
span.AddEvent("remote-heads-unmarshall")
err = json.Unmarshal(remoteHeadsBytes, &remoteHeads)
if err != nil {
span.AddEvent("remote-heads-unmarshall-failed")
return fmt.Errorf("unable to unmarshal cached remote heads: %w", err)
}
span.AddEvent("remote-heads-unmarshalled")
}
heads := append(localHeads, remoteHeads...)
if len(heads) > 0 {
headsForEvent := make([]ipfslog.Entry, len(heads))
for i := range heads {
headsForEvent[i] = heads[i]
}
if err := b.emitters.evtLoad.Emit(stores.NewEventLoad(b.Address(), headsForEvent)); err != nil {
b.logger.Warn("unable to emit event load", zap.Error(err))
}
}
wg := sync.WaitGroup{}
wg.Add(len(heads))
// @FIXME(gfanton): chan progress should be created and close on ipfs-log
progress := make(chan ifacelog.IPFSLogEntry)
defer close(progress)
go func() {
for {
var entry ifacelog.IPFSLogEntry
select {
case <-ctx.Done():
return
case entry = <-progress:
if entry == nil {
// should not happen
return
}
}
b.recalculateReplicationStatus(entry.GetClock().GetTime())
evt := stores.NewEventLoadProgress(b.Address(), entry.GetHash(), entry, b.replicationStatus.GetProgress(), b.replicationStatus.GetMax())
if err := b.emitters.evtLoadProgress.Emit(evt); err != nil {
b.logger.Warn("unable to emit event load", zap.Error(err))
}
}
}()
for _, h := range heads {
go func(h *entry.Entry) {
ctx, span := b.tracer.Start(ctx, "store-handling-head", trace.WithAttributes(otkv.String("cid", h.GetHash().String())))
b.muJoining.Lock()
defer b.muJoining.Unlock()
defer wg.Done()
oplog := b.OpLog()
span.AddEvent("store-head-loading")
l, inErr := ipfslog.NewFromEntryHash(ctx, b.IPFS(), b.Identity(), h.GetHash(), &ipfslog.LogOptions{
ID: oplog.GetID(),
AccessController: b.AccessController(),
SortFn: b.SortFn(),
IO: b.options.IO,
}, &ipfslog.FetchOptions{
Length: &amount,
Exclude: oplog.GetEntries().Slice(),
ProgressChan: progress,
})
if inErr != nil {
span.AddEvent("store-head-loading-error")
err = fmt.Errorf("unable to create log from entry hash: %w", inErr)
return
}
b.recalculateReplicationStatus(h.GetClock().GetTime())
span.AddEvent("store-head-loaded")
span.AddEvent("store-heads-joining")
if _, inErr = oplog.Join(l, amount); inErr != nil {
span.AddEvent("store-heads-joining-failed")
// err = fmt.Errorf("unable to join log: %w", err)
// TODO: log
_ = inErr
} else {
span.AddEvent("store-heads-joined")
}
}(h)
}
wg.Wait()
if err != nil {
span.AddEvent("store-handling-head-error", trace.WithAttributes(otkv.String("error", err.Error())))
return err
}
// Update the index
if len(heads) > 0 {
span.AddEvent("store-index-updating")
if err := b.updateIndex(ctx); err != nil {
span.AddEvent("store-index-updating-error", trace.WithAttributes(otkv.String("error", err.Error())))
return fmt.Errorf("unable to update index: %w", err)
}
span.AddEvent("store-index-updated")
}
if err := b.emitters.evtReady.Emit(stores.NewEventReady(b.Address(), b.OpLog().Heads().Slice())); err != nil {
return fmt.Errorf("unable to emit event ready: %w", err)
}
return nil
}
func (b *BaseStore) Sync(ctx context.Context, heads []ipfslog.Entry) error {
ctx, span := b.tracer.Start(ctx, "store-sync")
defer span.End()
if len(heads) == 0 {
return nil
}
for _, h := range heads {
if h == nil {
b.Logger().Debug("warning: Given input entry was 'null'.")
continue
}
if h.GetNext() == nil {
h.SetNext([]cid.Cid{})
}
if h.GetRefs() == nil {
h.SetRefs([]cid.Cid{})
}
identityProvider := b.Identity().Provider
if identityProvider == nil {
return fmt.Errorf("identity-provider is required, cannot verify entry")
}
canAppend := b.AccessController().CanAppend(h, identityProvider, &CanAppendContext{log: b.OpLog()})
if canAppend != nil {
span.AddEvent("store-sync-cant-append", trace.WithAttributes(otkv.String("error", canAppend.Error())))
b.Logger().Debug("warning: Given input entry is not allowed in this log and was discarded (no write access)", zap.Error(canAppend))
continue
}
hash, err := b.IO().Write(ctx, b.IPFS(), h, nil)
if err != nil {
span.AddEvent("store-sync-cant-write", trace.WithAttributes(otkv.String("error", err.Error())))
return fmt.Errorf("unable to write entry on dag: %w", err)
}
if hash.String() != h.GetHash().String() {
span.AddEvent("store-sync-cant-verify-hash")
return fmt.Errorf("WARNING! Head hash didn't match the contents")
}
span.AddEvent("store-sync-head-verified")
}
go b.Replicator().Load(ctx, heads)
return nil
}
func (b *BaseStore) LoadMoreFrom(ctx context.Context, amount uint, entries []ipfslog.Entry) {
b.Replicator().Load(ctx, entries)
// TODO: can this return an error?
}
type storeSnapshot struct {
ID string `json:"id,omitempty"`
Heads []*entry.Entry `json:"heads,omitempty"`
Size int `json:"size,omitempty"`
Type string `json:"type,omitempty"`
}
func (b *BaseStore) LoadFromSnapshot(ctx context.Context) error {
b.muJoining.Lock()
defer b.muJoining.Unlock()
ctx, span := b.tracer.Start(ctx, "load-from-snapshot")
defer span.End()
if err := b.emitters.evtLoad.Emit(stores.NewEventLoad(b.Address(), nil)); err != nil {
b.logger.Warn("unable to emit event load event", zap.Error(err))
}
queueJSON, err := b.Cache().Get(ctx, datastore.NewKey("queue"))
if err != nil && err != datastore.ErrNotFound {
return fmt.Errorf("unable to get value from cache: %w", err)
}
if err != datastore.ErrNotFound {
var queue []cid.Cid
var entries []ipfslog.Entry
if err := json.Unmarshal(queueJSON, &queue); err != nil {
return fmt.Errorf("unable to deserialize queued CIDs: %w", err)
}
for _, h := range queue {
entries = append(entries, &entry.Entry{Hash: h})
}
if err := b.Sync(ctx, entries); err != nil {
return fmt.Errorf("unable to sync queued CIDs: %w", err)
}
}
snapshot, err := b.Cache().Get(ctx, datastore.NewKey("snapshot"))
if err == datastore.ErrNotFound {
return fmt.Errorf("not found: %w", err)
}
if err != nil {
return fmt.Errorf("unable to get value from cache: %w", err)
}
b.Logger().Debug("loading snapshot from path", zap.String("snapshot", string(snapshot)))
pathForRsNode, err := path.NewPath(string(snapshot))
if err != nil {
return fmt.Errorf("unable to create path from snapshot: %w", err)
}
resNode, err := b.IPFS().Unixfs().Get(ctx, pathForRsNode)
if err != nil {
return fmt.Errorf("unable to get snapshot from ipfs: %w", err)
}
res, ok := resNode.(files.File)
if !ok {
return fmt.Errorf("unable to cast fetched data as a file")
}
headerLengthRaw := make([]byte, 2)
if _, err := res.Read(headerLengthRaw); err != nil {
return fmt.Errorf("unable to read from stream: %w", err)
}
headerLength := binary.BigEndian.Uint16(headerLengthRaw)
header := &storeSnapshot{}
headerRaw := make([]byte, headerLength)
if _, err := res.Read(headerRaw); err != nil {
return fmt.Errorf("unable to read from stream: %w", err)
}
if err := json.Unmarshal(headerRaw, &header); err != nil {
return fmt.Errorf("unable to decode header from ipfs data: %w", err)
}
var entries []ipfslog.Entry
maxClock := 0
for i := 0; i < header.Size; i++ {
entryLengthRaw := make([]byte, 2)
if _, err := res.Read(entryLengthRaw); err != nil {
return fmt.Errorf("unable to read from stream: %w", err)
}
entryLength := binary.BigEndian.Uint16(entryLengthRaw)
e := &entry.Entry{}
entryRaw := make([]byte, entryLength)
if _, err := res.Read(entryRaw); err != nil {
return fmt.Errorf("unable to read from stream: %w", err)
}
b.Logger().Debug(fmt.Sprintf("Entry raw: %s", string(entryRaw)))
if err = json.Unmarshal(entryRaw, e); err != nil {
return fmt.Errorf("unable to unmarshal entry from ipfs data: %w", err)
}
entries = append(entries, e)
if maxClock < e.Clock.GetTime() {
maxClock = e.Clock.GetTime()
}
}
b.recalculateReplicationMax(maxClock)
var headsCids []cid.Cid
for _, h := range header.Heads {
headsCids = append(headsCids, h.GetHash())
}
log, err := ipfslog.NewFromJSON(ctx, b.IPFS(), b.Identity(), &ipfslog.JSONLog{
Heads: headsCids,
ID: header.ID,
}, &ipfslog.LogOptions{
Entries: entry.NewOrderedMapFromEntries(entries),
ID: header.ID,
AccessController: b.AccessController(),
SortFn: b.SortFn(),
IO: b.options.IO,
}, &entry.FetchOptions{
Length: intPtr(-1),
// @FIXME(gfanton): should we increase this ?
Timeout: time.Minute,
})
if err != nil {
return fmt.Errorf("unable to load log: %w", err)
}
if _, err = b.OpLog().Join(log, -1); err != nil {
return fmt.Errorf("unable to join log: %w", err)
}
if err := b.updateIndex(ctx); err != nil {
return fmt.Errorf("unable to update index: %w", err)
}
return nil
}
func intPtr(i int) *int {
return &i
}
func boolPtr(val bool) *bool {
return &val
}
func (b *BaseStore) AddOperation(ctx context.Context, op operation.Operation, onProgressCallback chan<- ipfslog.Entry) (ipfslog.Entry, error) {
ctx, span := b.tracer.Start(ctx, "add-operation")
defer span.End()
data, err := op.Marshal()
if err != nil {
return nil, fmt.Errorf("unable to marshal operation: %w", err)
}
oplog := b.OpLog()
e, err := oplog.Append(ctx, data, &ipfslog.AppendOptions{PointerCount: b.referenceCount})
if err != nil {
return nil, fmt.Errorf("unable to append data on log: %w", err)
}
b.recalculateReplicationStatus(e.GetClock().GetTime())
marshaledEntry, err := json.Marshal([]ipfslog.Entry{e})
if err != nil {
return nil, fmt.Errorf("unable to marshal entry: %w", err)
}
err = b.Cache().Put(ctx, datastore.NewKey("_localHeads"), marshaledEntry)
if err != nil {
return nil, fmt.Errorf("unable to add data to cache: %w", err)
}
if err := b.updateIndex(ctx); err != nil {
return nil, fmt.Errorf("unable to update index: %w", err)
}
if err := b.emitters.evtWrite.Emit(stores.NewEventWrite(b.Address(), e, oplog.Heads().Slice())); err != nil {
b.logger.Warn("unable to emit event write", zap.Error(err))
}
if onProgressCallback != nil {
onProgressCallback <- e
}
return e, nil
}
func (b *BaseStore) recalculateReplicationProgress() {
max := b.ReplicationStatus().GetMax()
if progress := b.ReplicationStatus().GetProgress() + 1; progress < max {
max = progress
}
if opLogLen := b.OpLog().Len(); opLogLen > max {
max = opLogLen
}
b.ReplicationStatus().SetProgress(max)
}
func (b *BaseStore) recalculateReplicationMax(max int) {
if opLogLen := b.OpLog().Len(); opLogLen > max {
max = opLogLen
} else if replMax := b.ReplicationStatus().GetMax(); replMax > max {
max = replMax
}
b.ReplicationStatus().SetMax(max)
}
func (b *BaseStore) recalculateReplicationStatus(maxTotal int) {
b.recalculateReplicationMax(maxTotal)
b.recalculateReplicationProgress()
}
func (b *BaseStore) updateIndex(ctx context.Context) error {
_, span := b.tracer.Start(ctx, "update-index")
defer span.End()
if err := b.Index().UpdateIndex(b.OpLog(), []ipfslog.Entry{}); err != nil {
return fmt.Errorf("unable to update index: %w", err)
}
return nil
}
func (b *BaseStore) generateEmitter(bus event.Bus) error {
var err error
if b.emitters.evtWrite, err = bus.Emitter(new(stores.EventWrite)); err != nil {
return fmt.Errorf("unable to create EventWrite emitter: %w", err)
}
if b.emitters.evtReady, err = bus.Emitter(new(stores.EventReady)); err != nil {
return fmt.Errorf("unable to create EventReady emitter: %w", err)
}
if b.emitters.evtReplicateProgress, err = bus.Emitter(new(stores.EventReplicateProgress)); err != nil {
return fmt.Errorf("unable to create EventReplicateProgress emitter: %w", err)
}
if b.emitters.evtLoad, err = bus.Emitter(new(stores.EventLoad)); err != nil {
return fmt.Errorf("unable to create EventLoad emitter: %w", err)
}
if b.emitters.evtLoadProgress, err = bus.Emitter(new(stores.EventLoadProgress)); err != nil {
return fmt.Errorf("unable to create EventLoad emitter: %w", err)
}
if b.emitters.evtReplicated, err = bus.Emitter(new(stores.EventReplicated)); err != nil {
return fmt.Errorf("unable to create EventReplicated emitter: %w", err)
}
if b.emitters.evtReplicate, err = bus.Emitter(new(stores.EventReplicate)); err != nil {
return fmt.Errorf("unable to create EventReplicate emitter: %w", err)
}
return nil
}
func (b *BaseStore) replicationLoadComplete(ctx context.Context, logs []ipfslog.Log) {
b.muJoining.Lock()
defer b.muJoining.Unlock()
oplog := b.OpLog()
b.Logger().Debug("replication load complete")
entries := []ipfslog.Entry{}
for _, log := range logs {
_, err := oplog.Join(log, -1)
if err != nil {
b.Logger().Error("unable to join logs", zap.Error(err))
return
}
entries = append(entries, log.GetEntries().Slice()...)
}
err := b.updateIndex(ctx)
if err != nil {
b.Logger().Error("unable to update index", zap.Error(err))
return
}
// only store heads that has been verified and merges
heads := oplog.Heads()
headsBytes, err := json.Marshal(heads.Slice())
if err != nil {
b.Logger().Error("unable to serialize heads cache", zap.Error(err))
return
}
err = b.Cache().Put(ctx, datastore.NewKey("_remoteHeads"), headsBytes)
if err != nil {
b.Logger().Error("unable to update heads cache", zap.Error(err))
return
}
if oplog.Len() > b.replicationStatus.GetProgress() {
b.recalculateReplicationStatus(oplog.Len())
}
b.Logger().Debug(fmt.Sprintf("Saved heads %d", heads.Len()))
// logger.debug(`<replicated>`)
if err := b.emitters.evtReplicated.Emit(stores.NewEventReplicated(b.Address(), entries, len(logs))); err != nil {
b.Logger().Warn("unable to emit event replicated", zap.Error(err))
}
}
func (b *BaseStore) SortFn() ipfslog.SortFn {
return b.sortFn
}
func (b *BaseStore) replicate() error {
topic, err := b.pubsub.TopicSubscribe(b.ctx, b.id)
if err != nil {