2021-07-21 10:13:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-07-21 11:08:57 +00:00
|
|
|
"runtime"
|
2021-07-21 10:13:47 +00:00
|
|
|
|
|
|
|
"github.com/dgraph-io/badger/v2"
|
|
|
|
"github.com/urfave/cli/v2"
|
2021-08-01 15:59:43 +00:00
|
|
|
"go.uber.org/multierr"
|
2021-07-21 10:13:47 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2021-07-25 06:07:27 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-07-21 10:13:47 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/query"
|
|
|
|
|
2021-07-25 08:18:46 +00:00
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2021-07-21 10:13:47 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var splitstoreCmd = &cli.Command{
|
|
|
|
Name: "splitstore",
|
2021-07-22 17:53:03 +00:00
|
|
|
Description: "splitstore utilities",
|
2021-07-21 10:13:47 +00:00
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
splitstoreRollbackCmd,
|
2021-08-01 15:59:43 +00:00
|
|
|
splitstoreClearCmd,
|
2021-07-25 08:18:46 +00:00
|
|
|
splitstoreCheckCmd,
|
2021-07-26 05:47:02 +00:00
|
|
|
splitstoreInfoCmd,
|
2021-07-21 10:13:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var splitstoreRollbackCmd = &cli.Command{
|
|
|
|
Name: "rollback",
|
|
|
|
Description: "rollbacks a splitstore installation",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
Value: "~/.lotus",
|
|
|
|
},
|
2021-07-22 18:07:57 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "gc-coldstore",
|
|
|
|
Usage: "compact and garbage collect the coldstore after copying the hotstore",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "rewrite-config",
|
|
|
|
Usage: "rewrite the lotus configuration to disable splitstore",
|
|
|
|
},
|
2021-07-21 10:13:47 +00:00
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
r, err := repo.NewFS(cctx.String("repo"))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error opening fs repo: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := r.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return xerrors.Errorf("lotus repo doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
lr, err := r.Lock(repo.FullNode)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error locking repo: %w", err)
|
|
|
|
}
|
|
|
|
defer lr.Close() //nolint:errcheck
|
|
|
|
|
|
|
|
cfg, err := lr.Config()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error getting config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fncfg, ok := cfg.(*config.FullNode)
|
|
|
|
if !ok {
|
|
|
|
return xerrors.Errorf("wrong config type: %T", cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fncfg.Chainstore.EnableSplitstore {
|
|
|
|
return xerrors.Errorf("splitstore is not enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("copying hotstore to coldstore...")
|
2021-07-22 18:07:57 +00:00
|
|
|
err = copyHotstoreToColdstore(lr, cctx.Bool("gc-coldstore"))
|
2021-07-21 10:13:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error copying hotstore to coldstore: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:54:10 +00:00
|
|
|
fmt.Println("deleting splitstore directory...")
|
2021-07-21 10:13:47 +00:00
|
|
|
err = deleteSplitstoreDir(lr)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error deleting splitstore directory: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("deleting splitstore keys from metadata datastore...")
|
|
|
|
err = deleteSplitstoreKeys(lr)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error deleting splitstore keys: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:07:57 +00:00
|
|
|
if cctx.Bool("rewrite-config") {
|
|
|
|
fmt.Println("disabling splitstore in config...")
|
|
|
|
err = lr.SetConfig(func(cfg interface{}) {
|
|
|
|
cfg.(*config.FullNode).Chainstore.EnableSplitstore = false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error disabling splitstore in config: %w", err)
|
|
|
|
}
|
2021-07-21 10:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("splitstore has been rolled back.")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-08-01 15:59:43 +00:00
|
|
|
var splitstoreClearCmd = &cli.Command{
|
|
|
|
Name: "clear",
|
|
|
|
Description: "clears a splitstore installation for restart from snapshot",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
Value: "~/.lotus",
|
|
|
|
},
|
2021-08-03 12:32:53 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "keys-only",
|
|
|
|
Usage: "only delete splitstore keys",
|
|
|
|
},
|
2021-08-01 15:59:43 +00:00
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
r, err := repo.NewFS(cctx.String("repo"))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error opening fs repo: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := r.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return xerrors.Errorf("lotus repo doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
lr, err := r.Lock(repo.FullNode)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error locking repo: %w", err)
|
|
|
|
}
|
|
|
|
defer lr.Close() //nolint:errcheck
|
|
|
|
|
|
|
|
cfg, err := lr.Config()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error getting config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fncfg, ok := cfg.(*config.FullNode)
|
|
|
|
if !ok {
|
|
|
|
return xerrors.Errorf("wrong config type: %T", cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fncfg.Chainstore.EnableSplitstore {
|
|
|
|
return xerrors.Errorf("splitstore is not enabled")
|
|
|
|
}
|
|
|
|
|
2021-08-03 12:32:53 +00:00
|
|
|
if !cctx.Bool("keys-only") {
|
|
|
|
fmt.Println("clearing splitstore directory...")
|
|
|
|
err = clearSplitstoreDir(lr)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error clearing splitstore directory: %w", err)
|
|
|
|
}
|
2021-08-01 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("deleting splitstore keys from metadata datastore...")
|
|
|
|
err = deleteSplitstoreKeys(lr)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error deleting splitstore keys: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:07:57 +00:00
|
|
|
func copyHotstoreToColdstore(lr repo.LockedRepo, gcColdstore bool) error {
|
2021-07-21 10:13:47 +00:00
|
|
|
repoPath := lr.Path()
|
|
|
|
dataPath := filepath.Join(repoPath, "datastore")
|
|
|
|
coldPath := filepath.Join(dataPath, "chain")
|
|
|
|
hotPath := filepath.Join(dataPath, "splitstore", "hot.badger")
|
|
|
|
|
2021-07-25 06:07:27 +00:00
|
|
|
blog := &badgerLogger{
|
|
|
|
SugaredLogger: log.Desugar().WithOptions(zap.AddCallerSkip(1)).Sugar(),
|
|
|
|
skip2: log.Desugar().WithOptions(zap.AddCallerSkip(2)).Sugar(),
|
|
|
|
}
|
|
|
|
|
2021-07-21 10:13:47 +00:00
|
|
|
coldOpts, err := repo.BadgerBlockstoreOptions(repo.UniversalBlockstore, coldPath, false)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error getting coldstore badger options: %w", err)
|
|
|
|
}
|
|
|
|
coldOpts.SyncWrites = false
|
2021-07-25 06:07:27 +00:00
|
|
|
coldOpts.Logger = blog
|
2021-07-21 10:13:47 +00:00
|
|
|
|
|
|
|
hotOpts, err := repo.BadgerBlockstoreOptions(repo.HotBlockstore, hotPath, true)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error getting hotstore badger options: %w", err)
|
|
|
|
}
|
2021-07-25 06:07:27 +00:00
|
|
|
hotOpts.Logger = blog
|
2021-07-21 10:13:47 +00:00
|
|
|
|
|
|
|
cold, err := badger.Open(coldOpts.Options)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error opening coldstore: %w", err)
|
|
|
|
}
|
|
|
|
defer cold.Close() //nolint
|
|
|
|
|
|
|
|
hot, err := badger.Open(hotOpts.Options)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error opening hotstore: %w", err)
|
|
|
|
}
|
|
|
|
defer hot.Close() //nolint
|
|
|
|
|
|
|
|
rd, wr := io.Pipe()
|
|
|
|
g := new(errgroup.Group)
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
bwr := bufio.NewWriterSize(wr, 64<<20)
|
|
|
|
|
|
|
|
_, err := hot.Backup(bwr, 0)
|
|
|
|
if err != nil {
|
2021-07-21 10:24:53 +00:00
|
|
|
_ = wr.CloseWithError(err)
|
2021-07-21 10:13:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bwr.Flush()
|
|
|
|
if err != nil {
|
2021-07-21 10:24:53 +00:00
|
|
|
_ = wr.CloseWithError(err)
|
2021-07-21 10:13:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return wr.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
err := cold.Load(rd, 1024)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cold.Sync()
|
|
|
|
})
|
|
|
|
|
2021-07-21 11:08:57 +00:00
|
|
|
err = g.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:07:57 +00:00
|
|
|
// compact + gc the coldstore if so requested
|
|
|
|
if gcColdstore {
|
|
|
|
fmt.Println("compacting coldstore...")
|
|
|
|
nworkers := runtime.NumCPU()
|
|
|
|
if nworkers < 2 {
|
|
|
|
nworkers = 2
|
|
|
|
}
|
2021-07-21 11:08:57 +00:00
|
|
|
|
2021-07-22 18:07:57 +00:00
|
|
|
err = cold.Flatten(nworkers)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error compacting coldstore: %w", err)
|
|
|
|
}
|
2021-07-21 11:08:57 +00:00
|
|
|
|
2021-07-22 18:07:57 +00:00
|
|
|
fmt.Println("garbage collecting coldstore...")
|
|
|
|
for err == nil {
|
|
|
|
err = cold.RunValueLogGC(0.0625)
|
|
|
|
}
|
2021-07-21 11:08:57 +00:00
|
|
|
|
2021-07-22 18:07:57 +00:00
|
|
|
if err != badger.ErrNoRewrite {
|
|
|
|
return xerrors.Errorf("error garbage collecting coldstore: %w", err)
|
|
|
|
}
|
2021-07-21 11:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-07-21 10:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteSplitstoreDir(lr repo.LockedRepo) error {
|
|
|
|
path, err := lr.SplitstorePath()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error getting splitstore path: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.RemoveAll(path)
|
|
|
|
}
|
|
|
|
|
2021-08-01 15:59:43 +00:00
|
|
|
func clearSplitstoreDir(lr repo.LockedRepo) error {
|
|
|
|
path, err := lr.SplitstorePath()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error getting splitstore path: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := os.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error reading splitstore directory %s: %W", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result error
|
|
|
|
for _, e := range entries {
|
|
|
|
target := filepath.Join(path, e.Name())
|
|
|
|
err = os.RemoveAll(target)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error removing %s: %s", target, err)
|
|
|
|
result = multierr.Append(result, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-07-21 10:13:47 +00:00
|
|
|
func deleteSplitstoreKeys(lr repo.LockedRepo) error {
|
|
|
|
ds, err := lr.Datastore(context.TODO(), "/metadata")
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error opening datastore: %w", err)
|
|
|
|
}
|
|
|
|
if closer, ok := ds.(io.Closer); ok {
|
|
|
|
defer closer.Close() //nolint
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys []datastore.Key
|
|
|
|
res, err := ds.Query(query.Query{Prefix: "/splitstore"})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error querying datastore for splitstore keys: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for r := range res.Next() {
|
|
|
|
if r.Error != nil {
|
|
|
|
return xerrors.Errorf("datastore query error: %w", r.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = append(keys, datastore.NewKey(r.Key))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range keys {
|
2021-07-21 11:10:07 +00:00
|
|
|
fmt.Printf("deleting %s from datastore...\n", k)
|
2021-07-21 10:13:47 +00:00
|
|
|
err = ds.Delete(k)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error deleting key %s from datastore: %w", k, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-25 06:07:27 +00:00
|
|
|
|
|
|
|
// badger logging through go-log
|
|
|
|
type badgerLogger struct {
|
|
|
|
*zap.SugaredLogger
|
|
|
|
skip2 *zap.SugaredLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *badgerLogger) Warningf(format string, args ...interface{}) {}
|
|
|
|
func (b *badgerLogger) Infof(format string, args ...interface{}) {}
|
|
|
|
func (b *badgerLogger) Debugf(format string, args ...interface{}) {}
|
2021-07-25 08:18:46 +00:00
|
|
|
|
|
|
|
var splitstoreCheckCmd = &cli.Command{
|
|
|
|
Name: "check",
|
|
|
|
Description: "runs a healthcheck on a splitstore installation",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := lcli.GetFullNodeAPIV1(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
return api.ChainCheckBlockstore(ctx)
|
|
|
|
},
|
|
|
|
}
|
2021-07-26 05:47:02 +00:00
|
|
|
|
|
|
|
var splitstoreInfoCmd = &cli.Command{
|
|
|
|
Name: "info",
|
|
|
|
Description: "prints some basic splitstore information",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := lcli.GetFullNodeAPIV1(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
info, err := api.ChainBlockstoreInfo(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range info {
|
|
|
|
fmt.Print(k)
|
|
|
|
fmt.Print(": ")
|
|
|
|
fmt.Println(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|