2020-09-30 13:17:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-28 22:24:45 +00:00
|
|
|
"bufio"
|
2021-01-26 10:25:34 +00:00
|
|
|
"context"
|
2020-09-30 13:17:29 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-10-28 22:24:45 +00:00
|
|
|
"io"
|
2020-10-01 12:36:19 +00:00
|
|
|
"os"
|
2020-09-30 13:17:29 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-11-01 13:50:41 +00:00
|
|
|
"github.com/dgraph-io/badger/v2"
|
2020-10-01 12:36:19 +00:00
|
|
|
"github.com/docker/go-units"
|
2020-09-30 13:17:29 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
dsq "github.com/ipfs/go-datastore/query"
|
2021-04-06 13:04:32 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-10-28 22:24:45 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2020-09-30 13:17:29 +00:00
|
|
|
"github.com/polydawn/refmt/cbor"
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-10-28 22:24:45 +00:00
|
|
|
"go.uber.org/multierr"
|
2020-09-30 13:17:29 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-10-01 12:36:19 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/backupds"
|
2020-09-30 13:17:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var datastoreCmd = &cli.Command{
|
2020-09-30 15:26:24 +00:00
|
|
|
Name: "datastore",
|
2020-09-30 13:17:29 +00:00
|
|
|
Description: "access node datastores directly",
|
|
|
|
Subcommands: []*cli.Command{
|
2020-10-01 12:36:19 +00:00
|
|
|
datastoreBackupCmd,
|
2020-09-30 13:17:29 +00:00
|
|
|
datastoreListCmd,
|
|
|
|
datastoreGetCmd,
|
2020-10-28 22:24:45 +00:00
|
|
|
datastoreRewriteCmd,
|
2022-03-08 21:48:08 +00:00
|
|
|
datastoreVlog2CarCmd,
|
2020-09-30 13:17:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var datastoreListCmd = &cli.Command{
|
2020-09-30 15:26:24 +00:00
|
|
|
Name: "list",
|
2020-09-30 13:17:29 +00:00
|
|
|
Description: "list datastore keys",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.IntFlag{
|
2020-09-30 15:26:24 +00:00
|
|
|
Name: "repo-type",
|
2020-10-05 23:33:51 +00:00
|
|
|
Usage: "node type (1 - full, 2 - storage, 3 - worker)",
|
2020-09-30 13:17:29 +00:00
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
2020-09-30 15:26:24 +00:00
|
|
|
Name: "top-level",
|
2020-09-30 13:17:29 +00:00
|
|
|
Usage: "only print top-level keys",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2020-09-30 15:26:24 +00:00
|
|
|
Name: "get-enc",
|
2020-09-30 13:17:29 +00:00
|
|
|
Usage: "print values [esc/hex/cbor]",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ArgsUsage: "[namespace prefix]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-10-01 12:46:07 +00:00
|
|
|
logging.SetLogLevel("badger", "ERROR") // nolint:errcheck
|
2020-09-30 13:17:29 +00:00
|
|
|
|
|
|
|
r, err := repo.NewFS(cctx.String("repo"))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("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.RepoType(cctx.Int("repo-type")))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lr.Close() //nolint:errcheck
|
|
|
|
|
2021-01-26 10:25:34 +00:00
|
|
|
ds, err := lr.Datastore(context.Background(), datastore.NewKey(cctx.Args().First()).String())
|
2020-09-30 13:17:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
genc := cctx.String("get-enc")
|
|
|
|
|
2021-12-13 12:35:24 +00:00
|
|
|
q, err := ds.Query(context.Background(), dsq.Query{
|
2020-09-30 15:26:24 +00:00
|
|
|
Prefix: datastore.NewKey(cctx.Args().Get(1)).String(),
|
|
|
|
KeysOnly: genc == "",
|
2020-09-30 13:17:29 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("datastore query: %w", err)
|
|
|
|
}
|
|
|
|
defer q.Close() //nolint:errcheck
|
|
|
|
|
2020-10-01 12:36:19 +00:00
|
|
|
printKv := kvPrinter(cctx.Bool("top-level"), genc)
|
2020-09-30 13:17:29 +00:00
|
|
|
|
2020-10-01 12:36:19 +00:00
|
|
|
for res := range q.Next() {
|
|
|
|
if err := printKv(res.Key, res.Value); err != nil {
|
|
|
|
return err
|
2020-09-30 13:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var datastoreGetCmd = &cli.Command{
|
2020-09-30 15:26:24 +00:00
|
|
|
Name: "get",
|
2020-09-30 13:17:29 +00:00
|
|
|
Description: "list datastore keys",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.IntFlag{
|
2020-09-30 15:26:24 +00:00
|
|
|
Name: "repo-type",
|
2020-10-05 23:33:51 +00:00
|
|
|
Usage: "node type (1 - full, 2 - storage, 3 - worker)",
|
2020-09-30 13:17:29 +00:00
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2020-09-30 15:26:24 +00:00
|
|
|
Name: "enc",
|
2020-09-30 13:17:29 +00:00
|
|
|
Usage: "encoding (esc/hex/cbor)",
|
|
|
|
Value: "esc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ArgsUsage: "[namespace key]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2021-01-19 01:11:02 +00:00
|
|
|
logging.SetLogLevel("badger", "ERROR") // nolint:errcheck
|
2020-09-30 13:17:29 +00:00
|
|
|
|
|
|
|
r, err := repo.NewFS(cctx.String("repo"))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("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.RepoType(cctx.Int("repo-type")))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lr.Close() //nolint:errcheck
|
|
|
|
|
2021-01-26 10:25:34 +00:00
|
|
|
ds, err := lr.Datastore(context.Background(), datastore.NewKey(cctx.Args().First()).String())
|
2020-09-30 13:17:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-13 12:35:24 +00:00
|
|
|
val, err := ds.Get(context.Background(), datastore.NewKey(cctx.Args().Get(1)))
|
2020-09-30 13:17:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("get: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return printVal(cctx.String("enc"), val)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-01 12:36:19 +00:00
|
|
|
var datastoreBackupCmd = &cli.Command{
|
|
|
|
Name: "backup",
|
|
|
|
Description: "manage datastore backups",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
datastoreBackupStatCmd,
|
|
|
|
datastoreBackupListCmd,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var datastoreBackupStatCmd = &cli.Command{
|
|
|
|
Name: "stat",
|
|
|
|
Description: "validate and print info about datastore backup",
|
|
|
|
ArgsUsage: "[file]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.Args().Len() != 1 {
|
|
|
|
return xerrors.Errorf("expected 1 argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening backup file: %w", err)
|
|
|
|
}
|
|
|
|
defer f.Close() // nolint:errcheck
|
|
|
|
|
2021-03-24 20:26:05 +00:00
|
|
|
var keys, logs, kbytes, vbytes uint64
|
2021-03-27 09:05:12 +00:00
|
|
|
clean, err := backupds.ReadBackup(f, func(key datastore.Key, value []byte, log bool) error {
|
2021-03-24 20:26:05 +00:00
|
|
|
if log {
|
|
|
|
logs++
|
|
|
|
}
|
2020-10-01 12:36:19 +00:00
|
|
|
keys++
|
|
|
|
kbytes += uint64(len(key.String()))
|
|
|
|
vbytes += uint64(len(value))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-27 09:05:12 +00:00
|
|
|
fmt.Println("Truncated: ", !clean)
|
2020-10-01 12:36:19 +00:00
|
|
|
fmt.Println("Keys: ", keys)
|
2021-03-24 20:26:05 +00:00
|
|
|
fmt.Println("Log values: ", log)
|
2020-10-01 12:36:19 +00:00
|
|
|
fmt.Println("Key bytes: ", units.BytesSize(float64(kbytes)))
|
|
|
|
fmt.Println("Value bytes: ", units.BytesSize(float64(vbytes)))
|
|
|
|
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var datastoreBackupListCmd = &cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Description: "list data in a backup",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "top-level",
|
|
|
|
Usage: "only print top-level keys",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "get-enc",
|
|
|
|
Usage: "print values [esc/hex/cbor]",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ArgsUsage: "[file]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.Args().Len() != 1 {
|
|
|
|
return xerrors.Errorf("expected 1 argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening backup file: %w", err)
|
|
|
|
}
|
|
|
|
defer f.Close() // nolint:errcheck
|
|
|
|
|
|
|
|
printKv := kvPrinter(cctx.Bool("top-level"), cctx.String("get-enc"))
|
2021-03-27 09:05:12 +00:00
|
|
|
_, err = backupds.ReadBackup(f, func(key datastore.Key, value []byte, _ bool) error {
|
2020-10-01 12:36:19 +00:00
|
|
|
return printKv(key.String(), value)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func kvPrinter(toplevel bool, genc string) func(sk string, value []byte) error {
|
|
|
|
seen := map[string]struct{}{}
|
|
|
|
|
|
|
|
return func(s string, value []byte) error {
|
|
|
|
if toplevel {
|
|
|
|
k := datastore.NewKey(datastore.NewKey(s).List()[0])
|
|
|
|
if k.Type() != "" {
|
|
|
|
s = k.Type()
|
|
|
|
} else {
|
|
|
|
s = k.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, has := seen[s]
|
|
|
|
if has {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
seen[s] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
s = fmt.Sprintf("%q", s)
|
|
|
|
s = strings.Trim(s, "\"")
|
|
|
|
fmt.Println(s)
|
|
|
|
|
|
|
|
if genc != "" {
|
|
|
|
fmt.Print("\t")
|
|
|
|
if err := printVal(genc, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 13:17:29 +00:00
|
|
|
func printVal(enc string, val []byte) error {
|
|
|
|
switch enc {
|
|
|
|
case "esc":
|
|
|
|
s := fmt.Sprintf("%q", string(val))
|
|
|
|
s = strings.Trim(s, "\"")
|
|
|
|
fmt.Println(s)
|
|
|
|
case "hex":
|
|
|
|
fmt.Printf("%x\n", val)
|
|
|
|
case "cbor":
|
|
|
|
var out interface{}
|
|
|
|
if err := cbor.Unmarshal(cbor.DecodeOptions{}, val, &out); err != nil {
|
|
|
|
return xerrors.Errorf("unmarshaling cbor: %w", err)
|
|
|
|
}
|
|
|
|
s, err := json.Marshal(&out)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("remarshaling as json: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(s))
|
|
|
|
default:
|
|
|
|
return xerrors.New("unknown encoding")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-28 22:24:45 +00:00
|
|
|
|
|
|
|
var datastoreRewriteCmd = &cli.Command{
|
|
|
|
Name: "rewrite",
|
|
|
|
Description: "rewrites badger datastore to compact it and possibly change params",
|
|
|
|
ArgsUsage: "source destination",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.NArg() != 2 {
|
|
|
|
return xerrors.Errorf("expected 2 arguments, got %d", cctx.NArg())
|
|
|
|
}
|
|
|
|
fromPath, err := homedir.Expand(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("cannot get fromPath: %w", err)
|
|
|
|
}
|
|
|
|
toPath, err := homedir.Expand(cctx.Args().Get(1))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("cannot get toPath: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-01 13:50:41 +00:00
|
|
|
var (
|
|
|
|
from *badger.DB
|
|
|
|
to *badger.DB
|
|
|
|
)
|
|
|
|
|
|
|
|
// open the destination (to) store.
|
2021-02-28 22:48:36 +00:00
|
|
|
opts, err := repo.BadgerBlockstoreOptions(repo.UniversalBlockstore, toPath, false)
|
2020-10-28 22:24:45 +00:00
|
|
|
if err != nil {
|
2020-11-01 13:50:41 +00:00
|
|
|
return xerrors.Errorf("failed to get badger options: %w", err)
|
|
|
|
}
|
|
|
|
opts.SyncWrites = false
|
|
|
|
if to, err = badger.Open(opts.Options); err != nil {
|
|
|
|
return xerrors.Errorf("opening 'to' badger store: %w", err)
|
2020-10-28 22:24:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 13:50:41 +00:00
|
|
|
// open the source (from) store.
|
2021-02-28 22:48:36 +00:00
|
|
|
opts, err = repo.BadgerBlockstoreOptions(repo.UniversalBlockstore, fromPath, true)
|
2020-10-28 22:24:45 +00:00
|
|
|
if err != nil {
|
2020-11-01 13:50:41 +00:00
|
|
|
return xerrors.Errorf("failed to get badger options: %w", err)
|
|
|
|
}
|
|
|
|
if from, err = badger.Open(opts.Options); err != nil {
|
|
|
|
return xerrors.Errorf("opening 'from' datastore: %w", err)
|
2020-10-28 22:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
bw := bufio.NewWriterSize(pw, 64<<20)
|
2020-11-01 13:50:41 +00:00
|
|
|
_, err := from.Backup(bw, 0)
|
2020-10-28 22:30:28 +00:00
|
|
|
_ = bw.Flush()
|
|
|
|
_ = pw.CloseWithError(err)
|
2020-10-28 22:24:45 +00:00
|
|
|
errCh <- err
|
|
|
|
}()
|
|
|
|
go func() {
|
2020-11-01 13:50:41 +00:00
|
|
|
err := to.Load(pr, 256)
|
2020-10-28 22:24:45 +00:00
|
|
|
errCh <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = <-errCh
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case nerr := <-errCh:
|
|
|
|
err = multierr.Append(err, nerr)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = <-errCh
|
2020-10-29 00:05:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return multierr.Append(from.Close(), to.Close())
|
2020-10-28 22:24:45 +00:00
|
|
|
},
|
|
|
|
}
|