fix(miner): always create miner deal staging directory (#7098)

- fixes #7097

Co-authored-by: Raúl Kripalani <raul@protocol.ai>

Co-authored-by: Raúl Kripalani <raul@protocol.ai>
This commit is contained in:
Frrist 2021-08-16 16:16:06 -07:00 committed by GitHub
parent d7076778e2
commit 518b6f1d41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 56 deletions

View File

@ -12,7 +12,6 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"github.com/docker/go-units"
"github.com/google/uuid"
@ -414,10 +413,6 @@ func storageMinerInit(ctx context.Context, cctx *cli.Context, api v1api.FullNode
}
defer lr.Close() //nolint:errcheck
if err := makeDealStaging(lr); err != nil {
return err
}
log.Info("Initializing libp2p identity")
p2pSk, err := makeHostKey(lr)
@ -557,56 +552,6 @@ func storageMinerInit(ctx context.Context, cctx *cli.Context, api v1api.FullNode
return nil
}
func makeDealStaging(lr repo.LockedRepo) error {
dir := filepath.Join(lr.Path(), "deal-staging")
dirInfo, err := os.Stat(dir)
if err == nil {
if !dirInfo.IsDir() {
return xerrors.Errorf("%s is not a directory", dir)
}
// The dir exists already, below migration has already occurred.
return nil
}
// if the directory doesn't exist, create it
if os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0755); err != nil {
return xerrors.Errorf("failed to mk directory %s for deal staging: %w", dir, err)
}
} else { // if we failed for other reasons, abort.
return err
}
// if this is the first time we created the directory, symlink all staged deals into it. "Migration"
// get a list of files in the miner repo
dirEntries, err := os.ReadDir(lr.Path())
if err != nil {
return xerrors.Errorf("failed to list directory %s for deal staging: %w", lr.Path(), err)
}
for _, entry := range dirEntries {
// ignore directories, they are not the deals.
if entry.IsDir() {
continue
}
// the FileStore from fil-storage-market creates temporary staged deal files with the pattern "fstmp"
// https://github.com/filecoin-project/go-fil-markets/blob/00ff81e477d846ac0cb58a0c7d1c2e9afb5ee1db/filestore/filestore.go#L69
if strings.Contains(entry.Name(), "fstmp") {
// from the miner repo
oldPath := filepath.Join(lr.Path(), entry.Name())
// to its subdir "deal-staging"
newPath := filepath.Join(dir, entry.Name())
// create a symbolic link in the new deal staging directory to preserve existing staged deals.
// all future staged deals will be created here.
if err := os.Symlink(oldPath, newPath); err != nil {
return xerrors.Errorf("failed to symlink %s to %s: %w", oldPath, newPath, err)
}
log.Infow("symlinked staged deal", "oldPath", oldPath, "newPath", newPath)
}
}
return nil
}
func makeHostKey(lr repo.LockedRepo) (crypto.PrivKey, error) {
pk, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {

View File

@ -560,8 +560,17 @@ func StorageProvider(minerAddress dtypes.MinerAddress,
dsw *dagstore.Wrapper,
) (storagemarket.StorageProvider, error) {
net := smnet.NewFromLibp2pHost(h)
store, err := piecefilestore.NewLocalFileStore(piecefilestore.OsPath(filepath.Join(r.Path(), "deal-staging")))
dir := filepath.Join(r.Path(), "deal-staging")
// migrate temporary files that were created directly under the repo, by
// moving them to the new directory and symlinking them.
oldDir := r.Path()
if err := migrateDealStaging(oldDir, dir); err != nil {
return nil, xerrors.Errorf("failed to make deal staging directory %w", err)
}
store, err := piecefilestore.NewLocalFileStore(piecefilestore.OsPath(dir))
if err != nil {
return nil, err
}
@ -999,3 +1008,56 @@ func ExtractEnabledMinerSubsystems(cfg config.MinerSubsystemConfig) (res api.Min
}
return res
}
func migrateDealStaging(oldPath, newPath string) error {
dirInfo, err := os.Stat(newPath)
if err == nil {
if !dirInfo.IsDir() {
return xerrors.Errorf("%s is not a directory", newPath)
}
// The newPath exists already, below migration has already occurred.
return nil
}
// if the directory doesn't exist, create it
if os.IsNotExist(err) {
if err := os.MkdirAll(newPath, 0755); err != nil {
return xerrors.Errorf("failed to mk directory %s for deal staging: %w", newPath, err)
}
} else { // if we failed for other reasons, abort.
return err
}
// if this is the first time we created the directory, symlink all staged deals into it. "Migration"
// get a list of files in the miner repo
dirEntries, err := os.ReadDir(oldPath)
if err != nil {
return xerrors.Errorf("failed to list directory %s for deal staging: %w", oldPath, err)
}
for _, entry := range dirEntries {
// ignore directories, they are not the deals.
if entry.IsDir() {
continue
}
// the FileStore from fil-storage-market creates temporary staged deal files with the pattern "fstmp"
// https://github.com/filecoin-project/go-fil-markets/blob/00ff81e477d846ac0cb58a0c7d1c2e9afb5ee1db/filestore/filestore.go#L69
name := entry.Name()
if strings.Contains(name, "fstmp") {
// from the miner repo
oldPath := filepath.Join(oldPath, name)
// to its subdir "deal-staging"
newPath := filepath.Join(newPath, name)
// create a symbolic link in the new deal staging directory to preserve existing staged deals.
// all future staged deals will be created here.
if err := os.Rename(oldPath, newPath); err != nil {
return xerrors.Errorf("failed to move %s to %s: %w", oldPath, newPath, err)
}
if err := os.Symlink(newPath, oldPath); err != nil {
return xerrors.Errorf("failed to symlink %s to %s: %w", oldPath, newPath, err)
}
log.Infow("symlinked staged deal", "from", oldPath, "to", newPath)
}
}
return nil
}