Merge pull request #4070 from filecoin-project/feat/import-ux

snapshot import progress bar, http support
This commit is contained in:
Łukasz Magiera 2020-09-28 20:25:53 +02:00 committed by GitHub
commit 4b61f1756e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 14 deletions

View File

@ -3,11 +3,14 @@
package main package main
import ( import (
"bufio"
"context" "context"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"runtime/pprof" "runtime/pprof"
"strings" "strings"
@ -23,6 +26,7 @@ import (
"go.opencensus.io/stats/view" "go.opencensus.io/stats/view"
"go.opencensus.io/tag" "go.opencensus.io/tag"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"gopkg.in/cheggaaa/pb.v1"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
@ -100,11 +104,11 @@ var DaemonCmd = &cli.Command{
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "import-chain", Name: "import-chain",
Usage: "on first run, load chain from given file and validate", Usage: "on first run, load chain from given file or url and validate",
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "import-snapshot", Name: "import-snapshot",
Usage: "import chain state from a given chain export file", Usage: "import chain state from a given chain export file or url",
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "halt-after-import", Name: "halt-after-import",
@ -206,11 +210,6 @@ var DaemonCmd = &cli.Command{
issnapshot = true issnapshot = true
} }
chainfile, err := homedir.Expand(chainfile)
if err != nil {
return err
}
if err := ImportChain(r, chainfile, issnapshot); err != nil { if err := ImportChain(r, chainfile, issnapshot); err != nil {
return err return err
} }
@ -326,12 +325,42 @@ func importKey(ctx context.Context, api api.FullNode, f string) error {
return nil return nil
} }
func ImportChain(r repo.Repo, fname string, snapshot bool) error { func ImportChain(r repo.Repo, fname string, snapshot bool) (err error) {
fi, err := os.Open(fname) var rd io.Reader
if err != nil { var l int64
return err if strings.HasPrefix(fname, "http://") || strings.HasPrefix(fname, "https://") {
resp, err := http.Get(fname) //nolint:gosec
if err != nil {
return err
}
defer resp.Body.Close() //nolint:errcheck
if resp.StatusCode != http.StatusOK {
return xerrors.Errorf("non-200 response: %d", resp.StatusCode)
}
rd = resp.Body
l = resp.ContentLength
} else {
fname, err = homedir.Expand(fname)
if err != nil {
return err
}
fi, err := os.Open(fname)
if err != nil {
return err
}
defer fi.Close() //nolint:errcheck
st, err := os.Stat(fname)
if err != nil {
return err
}
rd = fi
l = st.Size()
} }
defer fi.Close() //nolint:errcheck
lr, err := r.Lock(repo.FullNode) lr, err := r.Lock(repo.FullNode)
if err != nil { if err != nil {
@ -353,8 +382,21 @@ func ImportChain(r repo.Repo, fname string, snapshot bool) error {
cst := store.NewChainStore(bs, mds, vm.Syscalls(ffiwrapper.ProofVerifier)) cst := store.NewChainStore(bs, mds, vm.Syscalls(ffiwrapper.ProofVerifier))
log.Info("importing chain from file...") log.Infof("importing chain from %s...", fname)
ts, err := cst.Import(fi)
bufr := bufio.NewReaderSize(rd, 1<<20)
bar := pb.New64(l)
br := bar.NewProxyReader(bufr)
bar.ShowTimeLeft = true
bar.ShowPercent = true
bar.ShowSpeed = true
bar.Units = pb.U_BYTES
bar.Start()
ts, err := cst.Import(br)
bar.Finish()
if err != nil { if err != nil {
return xerrors.Errorf("importing chain failed: %w", err) return xerrors.Errorf("importing chain failed: %w", err)
} }

1
go.mod
View File

@ -125,6 +125,7 @@ require (
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 golang.org/x/time v0.0.0-20191024005414-555d28b269f0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
gopkg.in/cheggaaa/pb.v1 v1.0.28
gotest.tools v2.2.0+incompatible gotest.tools v2.2.0+incompatible
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
) )