forked from application-research/estuary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
816 lines (716 loc) · 23.3 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"time"
"go.opencensus.io/stats/view"
"github.com/application-research/estuary/build"
"github.com/application-research/estuary/config"
drpc "github.com/application-research/estuary/drpc"
"github.com/application-research/estuary/metrics"
"github.com/application-research/estuary/node"
"github.com/application-research/estuary/pinner"
"github.com/application-research/estuary/stagingbs"
"github.com/application-research/estuary/util"
"github.com/application-research/estuary/util/gateway"
"github.com/application-research/filclient"
"github.com/google/uuid"
"github.com/ipfs/go-cid"
gsimpl "github.com/ipfs/go-graphsync/impl"
logging "github.com/ipfs/go-log/v2"
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
"github.com/mitchellh/go-homedir"
"github.com/multiformats/go-multiaddr"
"github.com/whyrusleeping/memo"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"golang.org/x/xerrors"
datatransfer "github.com/filecoin-project/go-data-transfer"
"github.com/filecoin-project/lotus/api"
lcli "github.com/filecoin-project/lotus/cli"
cli "github.com/urfave/cli/v2"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func init() {
if os.Getenv("FULLNODE_API_INFO") == "" {
os.Setenv("FULLNODE_API_INFO", "wss://api.chain.love")
}
}
var log = logging.Logger("estuary")
type storageMiner struct {
gorm.Model
Address util.DbAddr `gorm:"unique"`
Suspended bool
SuspendedReason string
Name string
Version string
Location string
Owner uint
}
type Content struct {
ID uint `gorm:"primarykey" json:"id"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
Cid util.DbCID `json:"cid"`
Name string `json:"name"`
UserID uint `json:"userId" gorm:"index"`
Description string `json:"description"`
Size int64 `json:"size"`
Type util.ContentType `json:"type"`
Path string `json:"path"`
Active bool `json:"active"`
Offloaded bool `json:"offloaded"`
Replication int `json:"replication"`
// TODO: shift most of the 'state' booleans in here into a single state
// field, should make reasoning about things much simpler
AggregatedIn uint `json:"aggregatedIn" gorm:"index:,option:CONCURRENTLY"`
Aggregate bool `json:"aggregate"`
Pinning bool `json:"pinning"`
PinMeta string `json:"pinMeta"`
Failed bool `json:"failed"`
Location string `json:"location"`
// TODO: shift location tracking to just use the ID of the shuttle
// Also move towards recording content movement intentions in the database,
// making that process more resilient to failures
// LocID uint `json:"locID"`
// LocIntent uint `json:"locIntent"`
// If set, this content is part of a split dag.
// In such a case, the 'root' content should be advertised on the dht, but
// not have deals made for it, and the children should have deals made for
// them (unlike with aggregates)
DagSplit bool `json:"dagSplit"`
SplitFrom uint `json:"splitFrom"`
}
type Object struct {
ID uint `gorm:"primarykey"`
Cid util.DbCID `gorm:"index"`
Size int
Reads int
LastAccess time.Time
}
type ObjRef struct {
ID uint `gorm:"primarykey"`
Content uint `gorm:"index:,option:CONCURRENTLY"`
Object uint `gorm:"index:,option:CONCURRENTLY"`
Offloaded uint
}
// updateAutoretrieveIndex ticks every tickInterval and checks for new information to add to autoretrieve
// If so, it updates the filecoin index with the new CIDs, saying they are present on autoretrieve
// With that, clients using bitswap can query autoretrieve servers using bitswap and get data from estuary
func (s *Server) updateAutoretrieveIndex(tickInterval time.Duration, quit chan struct{}) error {
var autoretrieves []Autoretrieve
var lastTickTime time.Time
ticker := time.NewTicker(tickInterval)
defer ticker.Stop()
for {
lastTickTime = time.Now().UTC().Add(-tickInterval)
// Find all autoretrieve servers that are online (that sent heartbeat)
err := s.DB.Find(&autoretrieves, "last_connection > ?", lastTickTime).Error
if err != nil {
log.Errorf("unable to query autoretrieve servers from database: %s", err)
return err
}
if len(autoretrieves) > 0 {
for _, ar := range autoretrieves {
fmt.Println("online: ", ar) // TODO: remove
}
} else {
log.Info("no autoretrieve servers online")
}
// wait for next tick, or quit
select {
case <-ticker.C:
continue
case <-quit:
break
}
}
}
func overrideSetOptions(flags []cli.Flag, cctx *cli.Context, cfg *config.Estuary) error {
for _, flag := range flags {
name := flag.Names()[0]
if cctx.IsSet(name) {
log.Debugf("estuary cli flag %s is set to %s", name, cctx.String(name))
} else {
continue
}
switch name {
case "datadir":
cfg.DataDir = cctx.String("datadir")
case "blockstore":
cfg.NodeConfig.Blockstore = cctx.String("blockstore")
case "no-blockstore-cache":
cfg.NodeConfig.NoBlockstoreCache = cctx.Bool("no-blockstore-cache")
case "write-log-truncate":
cfg.NodeConfig.WriteLogTruncate = cctx.Bool("write-log-truncate")
case "write-log-flush":
cfg.NodeConfig.HardFlushWriteLog = cctx.Bool("write-log-flush")
case "write-log":
if wl := cctx.String("write-log"); wl != "" {
if wl[0] == '/' {
cfg.NodeConfig.WriteLogDir = wl
} else {
cfg.NodeConfig.WriteLogDir = filepath.Join(cctx.String("datadir"), wl)
}
}
case "database":
cfg.DatabaseConnString = cctx.String("database")
case "apilisten":
cfg.ApiListen = cctx.String("apilisten")
case "announce":
_, err := multiaddr.NewMultiaddr(cctx.String("announce"))
if err != nil {
return fmt.Errorf("failed to parse announce address %s: %w", cctx.String("announce"), err)
}
cfg.NodeConfig.AnnounceAddrs = []string{cctx.String("announce")}
case "lightstep-token":
cfg.LightstepToken = cctx.String("lightstep-token")
case "hostname":
cfg.Hostname = cctx.String("hostname")
case "replication":
cfg.Replication = cctx.Int("replication")
case "lowmem":
cfg.LowMem = cctx.Bool("lowmem")
case "no-storage-cron":
cfg.DisableFilecoinStorage = cctx.Bool("no-storage-cron")
case "disable-deal-making":
cfg.DealConfig.Disable = cctx.Bool("disable-deal-making")
case "verified-deal":
cfg.DealConfig.Verified = cctx.Bool("verified-deal")
case "fail-deals-on-transfer-failure":
cfg.DealConfig.FailOnTransferFailure = cctx.Bool("fail-deals-on-transfer-failure")
case "disable-local-content-adding":
cfg.ContentConfig.DisableLocalAdding = cctx.Bool("disable-local-content-adding")
case "disable-content-adding":
cfg.ContentConfig.DisableGlobalAdding = cctx.Bool("disable-content-adding")
case "jaeger-tracing":
cfg.JaegerConfig.EnableTracing = cctx.Bool("jaeger-tracing")
case "jaeger-provider-url":
cfg.JaegerConfig.ProviderUrl = cctx.String("jaeger-provider-url")
case "jaeger-sampler-ratio":
cfg.JaegerConfig.SamplerRatio = cctx.Float64("jaeger-sampler-ratio")
case "logging":
cfg.LoggingConfig.ApiEndpointLogging = cctx.Bool("logging")
case "enable-auto-retrieve":
cfg.EnableAutoRetrieve = cctx.Bool("enable-auto-retrieve")
case "bitswap-max-work-per-peer":
cfg.NodeConfig.BitswapConfig.MaxOutstandingBytesPerPeer = cctx.Int64("bitswap-max-work-per-peer")
case "bitswap-target-message-size":
cfg.NodeConfig.BitswapConfig.TargetMessageSize = cctx.Int("bitswap-target-message-size")
case "announce-addr":
cfg.NodeConfig.AnnounceAddrs = cctx.StringSlice("announce-addr")
default:
}
}
return cfg.SetRequiredOptions()
}
func main() {
logging.SetLogLevel("dt-impl", "debug")
logging.SetLogLevel("estuary", "debug")
logging.SetLogLevel("paych", "debug")
logging.SetLogLevel("filclient", "debug")
logging.SetLogLevel("dt_graphsync", "debug")
//logging.SetLogLevel("graphsync_allocator", "debug")
logging.SetLogLevel("dt-chanmon", "debug")
logging.SetLogLevel("markets", "debug")
logging.SetLogLevel("data_transfer_network", "debug")
logging.SetLogLevel("rpc", "info")
logging.SetLogLevel("bs-wal", "info")
logging.SetLogLevel("provider.batched", "info")
logging.SetLogLevel("bs-migrate", "info")
hDir, err := homedir.Dir()
if err != nil {
log.Fatalf("could not determine homedir for estuary app: %+v", err)
}
app := cli.NewApp()
cfg := config.NewEstuary()
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "repo",
Value: "~/.lotus",
},
&cli.StringFlag{
Name: "config",
Usage: "specify configuration file location",
Value: filepath.Join(hDir, ".estuary"),
},
&cli.StringFlag{
Name: "database",
Usage: "specify connection string for estuary database",
Value: cfg.DatabaseConnString,
EnvVars: []string{"ESTUARY_DATABASE"},
},
&cli.StringFlag{
Name: "apilisten",
Usage: "address for the api server to listen on",
Value: cfg.ApiListen,
EnvVars: []string{"ESTUARY_API_LISTEN"},
},
&cli.StringFlag{
Name: "announce",
Usage: "announce address for the libp2p server to listen on",
EnvVars: []string{"ESTUARY_ANNOUNCE"},
},
&cli.StringFlag{
Name: "datadir",
Usage: "directory to store data in",
Value: cfg.DataDir,
EnvVars: []string{"ESTUARY_DATADIR"},
},
&cli.StringFlag{
Name: "write-log",
Usage: "enable write log blockstore in specified directory",
Value: cfg.NodeConfig.WriteLogDir,
Hidden: true,
},
&cli.BoolFlag{
Name: "no-storage-cron",
Usage: "run estuary without processing files into deals",
Value: cfg.DisableFilecoinStorage,
},
&cli.BoolFlag{
Name: "logging",
Usage: "enable api endpoint logging",
Value: cfg.LoggingConfig.ApiEndpointLogging,
},
&cli.BoolFlag{
Name: "enable-auto-retrieve",
Usage: "enables autoretrieve",
Value: cfg.EnableAutoRetrieve,
Hidden: true,
},
&cli.StringFlag{
Name: "lightstep-token",
Usage: "specify lightstep access token for enabling trace exports",
EnvVars: []string{"ESTUARY_LIGHTSTEP_TOKEN"},
Value: cfg.LightstepToken,
},
&cli.StringFlag{
Name: "hostname",
Usage: "specify hostname this node will be reachable at",
Value: cfg.Hostname,
},
&cli.BoolFlag{
Name: "fail-deals-on-transfer-failure",
Usage: "consider deals failed when the transfer to the miner fails",
Value: cfg.DealConfig.FailOnTransferFailure,
},
&cli.BoolFlag{
Name: "disable-deal-making",
Usage: "do not create any new deals (existing deals will still be processed)",
Value: cfg.DealConfig.Disable,
},
&cli.BoolFlag{
Name: "verified-deal",
Usage: "Defaults to makes deals as verified deal using datacap. Set to false to make deal as regular deal using real FIL(no datacap)",
Value: cfg.DealConfig.Verified,
},
&cli.BoolFlag{
Name: "disable-content-adding",
Usage: "disallow new content ingestion globally",
Value: cfg.ContentConfig.DisableGlobalAdding,
},
&cli.BoolFlag{
Name: "disable-local-content-adding",
Usage: "disallow new content ingestion on this node (shuttles are unaffected)",
Value: cfg.ContentConfig.DisableLocalAdding,
},
&cli.StringFlag{
Name: "blockstore",
Usage: "specify blockstore parameters",
Value: cfg.NodeConfig.Blockstore,
},
&cli.BoolFlag{
Name: "write-log-truncate",
Usage: "enables log truncating",
Value: cfg.NodeConfig.WriteLogTruncate,
},
&cli.BoolFlag{
Name: "write-log-flush",
Usage: "enable hard flushing blockstore",
Value: cfg.NodeConfig.HardFlushWriteLog,
},
&cli.BoolFlag{
Name: "no-blockstore-cache",
Usage: "disable blockstore caching",
Value: cfg.NodeConfig.NoBlockstoreCache,
},
&cli.IntFlag{
Name: "replication",
Usage: "sets replication factor",
Value: cfg.Replication,
},
&cli.BoolFlag{
Name: "lowmem",
Usage: "TEMP: turns down certain parameters to attempt to use less memory (will be replaced by a more specific flag later)",
Value: cfg.LowMem,
},
&cli.BoolFlag{
Name: "jaeger-tracing",
Usage: "enables jaeger tracing",
Value: cfg.JaegerConfig.EnableTracing,
},
&cli.StringFlag{
Name: "jaeger-provider-url",
Usage: "sets the jaeger provider url",
Value: cfg.JaegerConfig.ProviderUrl,
},
&cli.Float64Flag{
Name: "jaeger-sampler-ratio",
Usage: "If less than 1 probabilistic metrics will be used.",
Value: cfg.JaegerConfig.SamplerRatio,
},
&cli.Int64Flag{
Name: "bitswap-max-work-per-peer",
Value: cfg.NodeConfig.BitswapConfig.MaxOutstandingBytesPerPeer,
},
&cli.IntFlag{
Name: "bitswap-target-message-size",
Value: cfg.NodeConfig.BitswapConfig.TargetMessageSize,
},
&cli.StringSliceFlag{
Name: "announce-addr",
Usage: "specify multiaddrs that this node can be connected to on",
Value: cli.NewStringSlice(cfg.NodeConfig.AnnounceAddrs...),
},
}
app.Commands = []*cli.Command{
{
Name: "setup",
Usage: "Creates an initial auth token under new user \"admin\"",
Action: func(cctx *cli.Context) error {
if err := cfg.Load(cctx.String("config")); err != nil && err != config.ErrNotInitialized { // still want to report parsing errors
return err
}
if err := overrideSetOptions(app.Flags, cctx, cfg); err != nil {
return nil
}
db, err := setupDatabase(cfg.DatabaseConnString)
if err != nil {
return err
}
quietdb := db.Session(&gorm.Session{
Logger: logger.Discard,
})
username := "admin"
passHash := ""
if err := quietdb.First(&User{}, "username = ?", username).Error; err == nil {
return fmt.Errorf("an admin user already exists")
}
newUser := &User{
UUID: uuid.New().String(),
Username: username,
PassHash: passHash,
Perm: 100,
}
if err := db.Create(newUser).Error; err != nil {
return fmt.Errorf("admin user creation failed: %w", err)
}
authToken := &AuthToken{
Token: "EST" + uuid.New().String() + "ARY",
User: newUser.ID,
Expiry: time.Now().Add(time.Hour * 24 * 365),
}
if err := db.Create(authToken).Error; err != nil {
return fmt.Errorf("admin token creation failed: %w", err)
}
fmt.Printf("Auth Token: %v\n", authToken.Token)
return nil
},
}, {
Name: "configure",
Usage: "Saves a configuration file to the location specified by the config parameter",
Action: func(cctx *cli.Context) error {
configFile := cctx.String("config")
if err := cfg.Load(configFile); err != nil && err != config.ErrNotInitialized { // still want to report parsing errors
return err
}
if err := overrideSetOptions(app.Flags, cctx, cfg); err != nil {
return err
}
return cfg.Save(configFile)
},
},
}
app.Action = func(cctx *cli.Context) error {
if err := cfg.Load(cctx.String("config")); err != nil && err != config.ErrNotInitialized { // For backward compatibility, don't error if no config file
return err
}
if err := overrideSetOptions(app.Flags, cctx, cfg); err != nil {
return err
}
db, err := setupDatabase(cfg.DatabaseConnString)
if err != nil {
return err
}
init := Initializer{&cfg.NodeConfig, db, nil}
nd, err := node.Setup(context.Background(), &init)
if err != nil {
return err
}
if err = view.Register(metrics.DefaultViews...); err != nil {
log.Fatalf("Cannot register the OpenCensus view: %v", err)
return err
}
addr, err := nd.Wallet.GetDefault()
if err != nil {
return err
}
sbmgr, err := stagingbs.NewStagingBSMgr(cfg.StagingDataDir)
if err != nil {
return err
}
api, closer, err := lcli.GetGatewayAPI(cctx)
if err != nil {
return err
}
defer closer()
// setup tracing to jaeger if enabled
if cfg.JaegerConfig.EnableTracing {
tp, err := metrics.NewJaegerTraceProvider("estuary",
cfg.JaegerConfig.ProviderUrl, cfg.JaegerConfig.SamplerRatio)
if err != nil {
return err
}
otel.SetTracerProvider(tp)
}
s := &Server{
DB: db,
Node: nd,
Api: api,
StagingMgr: sbmgr,
tracer: otel.Tracer("api"),
cacher: memo.NewCacher(),
gwayHandler: gateway.NewGatewayHandler(nd.Blockstore),
}
// TODO: this is an ugly self referential hack... should fix
pinmgr := pinner.NewPinManager(s.doPinning, nil, &pinner.PinManagerOpts{
MaxActivePerUser: 20,
})
go pinmgr.Run(50)
rhost := routed.Wrap(nd.Host, nd.FilDht)
var opts []func(*filclient.Config)
if cfg.LowMem {
opts = append(opts, func(cfg *filclient.Config) {
cfg.GraphsyncOpts = []gsimpl.Option{
gsimpl.MaxInProgressIncomingRequests(100),
gsimpl.MaxInProgressOutgoingRequests(100),
gsimpl.MaxMemoryResponder(4 << 30),
gsimpl.MaxMemoryPerPeerResponder(16 << 20),
gsimpl.MaxInProgressIncomingRequestsPerPeer(10),
gsimpl.MessageSendRetries(2),
gsimpl.SendMessageTimeout(2 * time.Minute),
}
})
}
fc, err := filclient.NewClient(rhost, api, nd.Wallet, addr, nd.Blockstore, nd.Datastore, cfg.DataDir, opts...)
if err != nil {
return err
}
for _, a := range nd.Host.Addrs() {
fmt.Printf("%s/p2p/%s\n", a, nd.Host.ID())
}
go func() {
for _, ai := range node.BootstrapPeers {
if err := nd.Host.Connect(context.TODO(), ai); err != nil {
fmt.Println("failed to connect to bootstrapper: ", err)
continue
}
}
if err := nd.Dht.Bootstrap(context.TODO()); err != nil {
fmt.Println("dht bootstrapping failed: ", err)
}
}()
cm, err := NewContentManager(db, api, fc, init.trackingBstore, nd.NotifBlockstore, nd.Provider, pinmgr, nd, cfg)
if err != nil {
return err
}
s.CM = cm
fc.SetPieceCommFunc(cm.getPieceCommitment)
s.FilClient = fc
if cfg.EnableAutoRetrieve {
init.trackingBstore.SetCidReqFunc(cm.RefreshContentForCid)
}
if !cfg.DisableFilecoinStorage {
go cm.ContentWatcher()
}
if !cm.contentAddingDisabled {
go func() {
// wait for shuttles to reconnect
// This is a bit of a hack, and theres probably a better way to
// solve this. but its good enough for now
time.Sleep(time.Second * 10)
if err := cm.refreshPinQueue(); err != nil {
log.Errorf("failed to refresh pin queue: %s", err)
}
}()
}
// start autoretrieve index updater task every INDEX_UPDATE_INTERVAL minutes
updateInterval, ok := os.LookupEnv("INDEX_UPDATE_INTERVAL")
if !ok {
updateInterval = "720"
}
intervalMinutes, err := strconv.Atoi(updateInterval)
if err != nil {
return err
}
stopUpdateIndex := make(chan struct{})
go s.updateAutoretrieveIndex(time.Duration(intervalMinutes)*time.Minute, stopUpdateIndex)
go func() {
time.Sleep(time.Second * 10)
if err := s.RestartAllTransfersForLocation(context.TODO(), "local"); err != nil {
log.Errorf("failed to restart transfers: %s", err)
}
}()
return s.ServeAPI(cfg.ApiListen, cfg.LoggingConfig.ApiEndpointLogging, cfg.LightstepToken, cfg.ServerCacheDir)
}
if err := app.Run(os.Args); err != nil {
log.Fatalf("could not run esturay app: %+v", err)
}
}
type Autoretrieve struct {
gorm.Model
Handle string `gorm:"unique"`
Token string `gorm:"unique"`
LastConnection time.Time
PeerID string `gorm:"unique"`
Addresses string
}
func setupDatabase(dbConnStr string) (*gorm.DB, error) {
db, err := util.SetupDatabase(dbConnStr)
if err != nil {
return nil, err
}
db.AutoMigrate(&Content{})
db.AutoMigrate(&Object{})
db.AutoMigrate(&ObjRef{})
db.AutoMigrate(&Collection{})
db.AutoMigrate(&CollectionRef{})
db.AutoMigrate(&contentDeal{})
db.AutoMigrate(&dfeRecord{})
db.AutoMigrate(&PieceCommRecord{})
db.AutoMigrate(&proposalRecord{})
db.AutoMigrate(&util.RetrievalFailureRecord{})
db.AutoMigrate(&retrievalSuccessRecord{})
db.AutoMigrate(&minerStorageAsk{})
db.AutoMigrate(&storageMiner{})
db.AutoMigrate(&User{})
db.AutoMigrate(&AuthToken{})
db.AutoMigrate(&InviteCode{})
db.AutoMigrate(&Shuttle{})
db.AutoMigrate(&Autoretrieve{})
// 'manually' add unique composite index on collection fields because gorms syntax for it is tricky
if err := db.Exec("create unique index if not exists collection_refs_paths on collection_refs (path,collection)").Error; err != nil {
return nil, fmt.Errorf("failed to create collection paths index: %w", err)
}
var count int64
if err := db.Model(&storageMiner{}).Count(&count).Error; err != nil {
return nil, err
}
if count == 0 {
fmt.Println("adding default miner list to database...")
for _, m := range build.DefaultMiners {
db.Create(&storageMiner{Address: util.DbAddr{Addr: m}})
}
}
return db, nil
}
type Server struct {
tracer trace.Tracer
Node *node.Node
DB *gorm.DB
FilClient *filclient.FilClient
Api api.Gateway
CM *ContentManager
StagingMgr *stagingbs.StagingBSMgr
gwayHandler *gateway.GatewayHandler
cacher *memo.Cacher
}
func (s *Server) GarbageCollect(ctx context.Context) error {
// since we're reference counting all the content, garbage collection becomes easy
// its even easier if we don't care that its 'perfect'
// We can probably even just remove stuff when its references are removed from the database
keych, err := s.Node.Blockstore.AllKeysChan(ctx)
if err != nil {
return err
}
for c := range keych {
keep, err := s.trackingObject(c)
if err != nil {
return err
}
if !keep {
// can batch these deletes and execute them at the datastore layer for more perfs
if err := s.Node.Blockstore.DeleteBlock(ctx, c); err != nil {
return err
}
}
}
return nil
}
func (s *Server) trackingObject(c cid.Cid) (bool, error) {
var count int64
if err := s.DB.Model(&Object{}).Where("cid = ?", c.Bytes()).Count(&count).Error; err != nil {
if xerrors.Is(err, gorm.ErrRecordNotFound) {
return false, nil
}
return false, err
}
return count > 0, nil
}
func jsondump(o interface{}) {
data, _ := json.MarshalIndent(o, "", " ")
fmt.Println(string(data))
}
func (s *Server) RestartAllTransfersForLocation(ctx context.Context, loc string) error {
var deals []contentDeal
if err := s.DB.Model(contentDeal{}).
Joins("left join contents on contents.id = content_deals.content").
Where("not content_deals.failed and content_deals.deal_id = 0 and content_deals.dt_chan != '' and location = ?", loc).
Scan(&deals).Error; err != nil {
return err
}
for _, d := range deals {
chid, err := d.ChannelID()
if err != nil {
// Only legacy (push) transfers need to be restarted by Estuary.
// Newer (pull) transfers are restarted by the Storage Provider.
// So if it's not a legacy channel ID, ignore it.
continue
}
if err := s.CM.RestartTransfer(ctx, loc, chid); err != nil {
log.Errorf("failed to restart transfer: %s", err)
continue
}
}
return nil
}
func (cm *ContentManager) RestartTransfer(ctx context.Context, loc string, chanid datatransfer.ChannelID) error {
if loc == "local" {
st, err := cm.FilClient.TransferStatus(ctx, &chanid)
if err != nil {
return err
}
if util.TransferTerminated(st) {
return fmt.Errorf("deal in database as being in progress, but data transfer is terminated: %d", st.Status)
}
return cm.FilClient.RestartTransfer(ctx, &chanid)
}
return cm.sendRestartTransferCmd(ctx, loc, chanid)
}
func (cm *ContentManager) sendRestartTransferCmd(ctx context.Context, loc string, chanid datatransfer.ChannelID) error {
return cm.sendShuttleCommand(ctx, loc, &drpc.Command{
Op: drpc.CMD_RestartTransfer,
Params: drpc.CmdParams{
RestartTransfer: &drpc.RestartTransfer{
ChanID: chanid,
},
},
})
}