Copy over last sector ID key when migrating sectorbuilder

This commit is contained in:
whyrusleeping 2019-11-26 19:39:07 -06:00
parent 407f6fa6f8
commit 249bd8389f
2 changed files with 40 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import (
"path/filepath" "path/filepath"
"github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore"
badger "github.com/ipfs/go-ds-badger"
"github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
@ -22,6 +23,7 @@ import (
"github.com/filecoin-project/lotus/lib/sectorbuilder" "github.com/filecoin-project/lotus/lib/sectorbuilder"
"github.com/filecoin-project/lotus/miner" "github.com/filecoin-project/lotus/miner"
"github.com/filecoin-project/lotus/node/modules" "github.com/filecoin-project/lotus/node/modules"
"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/repo" "github.com/filecoin-project/lotus/node/repo"
"github.com/filecoin-project/lotus/storage" "github.com/filecoin-project/lotus/storage"
) )
@ -115,9 +117,21 @@ var initCmd = &cli.Command{
if pssb := cctx.String("pre-sealed-sectors"); pssb != "" { if pssb := cctx.String("pre-sealed-sectors"); pssb != "" {
log.Infof("moving pre-sealed-sectors from %s into newly created storage miner repo", pssb) log.Infof("moving pre-sealed-sectors from %s into newly created storage miner repo", pssb)
if err := migratePreSealedSectors(pssb, repoPath); err != nil { lr, err := r.Lock(repo.StorageMiner)
if err != nil {
return err return err
} }
mds, err := lr.Datastore("/metadata")
if err != nil {
return err
}
if err := migratePreSealedSectors(pssb, repoPath, mds); err != nil {
return err
}
if err := lr.Close(); err != nil {
return xerrors.Errorf("unlocking repo after preseal migration: %w", err)
}
} }
if err := storageMinerInit(ctx, cctx, api, r); err != nil { if err := storageMinerInit(ctx, cctx, api, r); err != nil {
@ -142,12 +156,18 @@ var initCmd = &cli.Command{
// TODO: this method should be a lot more robust for mainnet. For testnet, its // TODO: this method should be a lot more robust for mainnet. For testnet, its
// fine if we mess things up a few times // fine if we mess things up a few times
func migratePreSealedSectors(presealsb string, repoPath string) error { // Also probably makes sense for this method to be in the sectorbuilder package
func migratePreSealedSectors(presealsb string, repoPath string, mds dtypes.MetadataDS) error {
pspath, err := homedir.Expand(presealsb) pspath, err := homedir.Expand(presealsb)
if err != nil { if err != nil {
return err return err
} }
srcds, err := badger.NewDatastore(filepath.Join(pspath, "badger"), nil)
if err != nil {
return err
}
expRepo, err := homedir.Expand(repoPath) expRepo, err := homedir.Expand(repoPath)
if err != nil { if err != nil {
return err return err
@ -166,6 +186,16 @@ func migratePreSealedSectors(presealsb string, repoPath string) error {
return err return err
} }
} }
val, err := srcds.Get(sectorbuilder.LastSectorIdKey)
if err != nil {
return xerrors.Errorf("getting source last sector ID: %w", err)
}
if err := mds.Put(sectorbuilder.LastSectorIdKey, val); err != nil {
return xerrors.Errorf("failed to write last sector ID key to target datastore: %w", err)
}
return nil return nil
} }

View File

@ -3,15 +3,16 @@ package sectorbuilder
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"os"
"strconv"
"sync"
sectorbuilder "github.com/filecoin-project/go-sectorbuilder" sectorbuilder "github.com/filecoin-project/go-sectorbuilder"
"github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore"
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
"go.opencensus.io/trace" "go.opencensus.io/trace"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"io"
"os"
"strconv"
"sync"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/address" "github.com/filecoin-project/lotus/chain/address"
@ -21,7 +22,7 @@ import (
const PoStReservedWorkers = 1 const PoStReservedWorkers = 1
const PoRepProofPartitions = 2 const PoRepProofPartitions = 2
var lastSectorIdKey = datastore.NewKey("/sectorbuilder/last") var LastSectorIdKey = datastore.NewKey("/sectorbuilder/last")
var log = logging.Logger("sectorbuilder") var log = logging.Logger("sectorbuilder")
@ -94,7 +95,7 @@ func New(cfg *Config, ds dtypes.MetadataDS) (*SectorBuilder, error) {
} }
var lastUsedID uint64 var lastUsedID uint64
b, err := ds.Get(lastSectorIdKey) b, err := ds.Get(LastSectorIdKey)
switch err { switch err {
case nil: case nil:
i, err := strconv.ParseInt(string(b), 10, 64) i, err := strconv.ParseInt(string(b), 10, 64)
@ -152,7 +153,7 @@ func (sb *SectorBuilder) AcquireSectorId() (uint64, error) {
sb.lastID++ sb.lastID++
id := sb.lastID id := sb.lastID
err := sb.ds.Put(lastSectorIdKey, []byte(fmt.Sprint(id))) err := sb.ds.Put(LastSectorIdKey, []byte(fmt.Sprint(id)))
if err != nil { if err != nil {
return 0, err return 0, err
} }