2020-03-13 00:23:05 +00:00
|
|
|
package stores
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
files "github.com/ipfs/go-ipfs-files"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-sectorbuilder"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/lib/tarutil"
|
2020-03-23 11:40:02 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sectorstorage/sectorutil"
|
2020-03-13 00:23:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Remote struct {
|
2020-03-19 17:11:45 +00:00
|
|
|
local *Local
|
|
|
|
index SectorIndex
|
|
|
|
auth http.Header
|
2020-03-13 00:23:05 +00:00
|
|
|
|
|
|
|
fetchLk sync.Mutex // TODO: this can be much smarter
|
|
|
|
// TODO: allow multiple parallel fetches
|
|
|
|
// (make sure to not fetch the same sector data twice)
|
|
|
|
}
|
|
|
|
|
2020-03-19 17:11:45 +00:00
|
|
|
func NewRemote(local *Local, index SectorIndex, auth http.Header) *Remote {
|
2020-03-13 01:37:38 +00:00
|
|
|
return &Remote{
|
2020-03-19 17:11:45 +00:00
|
|
|
local: local,
|
|
|
|
index: index,
|
|
|
|
auth: auth,
|
2020-03-13 01:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 16:54:55 +00:00
|
|
|
func (r *Remote) AcquireSector(ctx context.Context, s abi.SectorID, existing sectorbuilder.SectorFileType, allocate sectorbuilder.SectorFileType, sealing bool) (sectorbuilder.SectorPaths, sectorbuilder.SectorPaths, func(), error) {
|
2020-03-13 00:23:05 +00:00
|
|
|
if existing|allocate != existing^allocate {
|
2020-03-13 16:54:55 +00:00
|
|
|
return sectorbuilder.SectorPaths{}, sectorbuilder.SectorPaths{}, nil, xerrors.New("can't both find and allocate a sector")
|
2020-03-13 00:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.fetchLk.Lock()
|
|
|
|
defer r.fetchLk.Unlock()
|
|
|
|
|
2020-03-13 16:54:55 +00:00
|
|
|
paths, stores, done, err := r.local.AcquireSector(ctx, s, existing, allocate, sealing)
|
2020-03-13 00:23:05 +00:00
|
|
|
if err != nil {
|
2020-03-19 19:17:43 +00:00
|
|
|
return sectorbuilder.SectorPaths{}, sectorbuilder.SectorPaths{}, nil, xerrors.Errorf("local acquire error: %w", err)
|
2020-03-13 00:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileType := range pathTypes {
|
|
|
|
if fileType&existing == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if sectorutil.PathByType(paths, fileType) != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:40:36 +00:00
|
|
|
ap, storageID, url, rdone, err := r.acquireFromRemote(ctx, s, fileType, sealing)
|
2020-03-13 00:23:05 +00:00
|
|
|
if err != nil {
|
|
|
|
done()
|
2020-03-13 16:54:55 +00:00
|
|
|
return sectorbuilder.SectorPaths{}, sectorbuilder.SectorPaths{}, nil, err
|
2020-03-13 00:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done = mergeDone(done, rdone)
|
|
|
|
sectorutil.SetPathByType(&paths, fileType, ap)
|
2020-03-13 16:54:55 +00:00
|
|
|
sectorutil.SetPathByType(&stores, fileType, string(storageID))
|
2020-03-13 00:23:05 +00:00
|
|
|
|
2020-03-19 17:11:45 +00:00
|
|
|
if err := r.index.StorageDeclareSector(ctx, storageID, s, fileType); err != nil {
|
2020-03-13 10:40:24 +00:00
|
|
|
log.Warnf("declaring sector %v in %s failed: %+v", s, storageID, err)
|
2020-03-22 04:27:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: some way to allow having duplicated sectors in the system for perf
|
|
|
|
if err := r.deleteFromRemote(url); err != nil {
|
|
|
|
log.Warnf("deleting sector %v from %s (delete %s): %+v", s, storageID, url, err)
|
2020-03-13 10:40:24 +00:00
|
|
|
}
|
2020-03-13 00:23:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 16:54:55 +00:00
|
|
|
return paths, stores, done, nil
|
2020-03-13 00:23:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 15:40:36 +00:00
|
|
|
func (r *Remote) acquireFromRemote(ctx context.Context, s abi.SectorID, fileType sectorbuilder.SectorFileType, sealing bool) (string, ID, string, func(), error) {
|
2020-03-20 00:55:49 +00:00
|
|
|
si, err := r.index.StorageFindSector(ctx, s, fileType, false)
|
2020-03-13 00:23:05 +00:00
|
|
|
if err != nil {
|
2020-03-23 15:40:36 +00:00
|
|
|
return "", "", "", nil, err
|
2020-03-13 00:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(si, func(i, j int) bool {
|
2020-03-19 15:10:19 +00:00
|
|
|
return si[i].Weight < si[j].Weight
|
2020-03-13 00:23:05 +00:00
|
|
|
})
|
|
|
|
|
2020-03-13 16:54:55 +00:00
|
|
|
apaths, ids, done, err := r.local.AcquireSector(ctx, s, 0, fileType, sealing)
|
2020-03-13 00:23:05 +00:00
|
|
|
if err != nil {
|
2020-03-23 15:40:36 +00:00
|
|
|
return "", "", "", nil, xerrors.Errorf("allocate local sector for fetching: %w", err)
|
2020-03-13 00:23:05 +00:00
|
|
|
}
|
|
|
|
dest := sectorutil.PathByType(apaths, fileType)
|
2020-03-13 10:40:24 +00:00
|
|
|
storageID := sectorutil.PathByType(ids, fileType)
|
2020-03-13 00:23:05 +00:00
|
|
|
|
|
|
|
var merr error
|
|
|
|
for _, info := range si {
|
|
|
|
for _, url := range info.URLs {
|
|
|
|
err := r.fetch(url, dest)
|
|
|
|
if err != nil {
|
|
|
|
merr = multierror.Append(merr, xerrors.Errorf("fetch error %s (storage %s) -> %s: %w", url, info.ID, dest, err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if merr != nil {
|
|
|
|
log.Warnw("acquireFromRemote encountered errors when fetching sector from remote", "errors", merr)
|
|
|
|
}
|
2020-03-23 15:40:36 +00:00
|
|
|
return dest, ID(storageID), url, done, nil
|
2020-03-13 00:23:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done()
|
2020-03-23 15:40:36 +00:00
|
|
|
return "", "", "", nil, xerrors.Errorf("failed to acquire sector %v from remote (tried %v): %w", s, si, merr)
|
2020-03-13 00:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) fetch(url, outname string) error {
|
|
|
|
log.Infof("Fetch %s -> %s", url, outname)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("request: %w", err)
|
|
|
|
}
|
|
|
|
req.Header = r.auth
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("do request: %w", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return xerrors.Errorf("non-200 code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*bar := pb.New64(w.sizeForType(typ))
|
|
|
|
bar.ShowPercent = true
|
|
|
|
bar.ShowSpeed = true
|
|
|
|
bar.Units = pb.U_BYTES
|
|
|
|
|
|
|
|
barreader := bar.NewProxyReader(resp.Body)
|
|
|
|
|
|
|
|
bar.Start()
|
|
|
|
defer bar.Finish()*/
|
|
|
|
|
|
|
|
mediatype, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("parse media type: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(outname); err != nil {
|
|
|
|
return xerrors.Errorf("removing dest: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch mediatype {
|
|
|
|
case "application/x-tar":
|
|
|
|
return tarutil.ExtractTar(resp.Body, outname)
|
|
|
|
case "application/octet-stream":
|
|
|
|
return files.WriteTo(files.NewReaderFile(resp.Body), outname)
|
|
|
|
default:
|
|
|
|
return xerrors.Errorf("unknown content type: '%s'", mediatype)
|
|
|
|
}
|
2020-03-22 04:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) deleteFromRemote(url string) error {
|
|
|
|
log.Infof("Delete %s", url)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("DELETE", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("request: %w", err)
|
|
|
|
}
|
|
|
|
req.Header = r.auth
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("do request: %w", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return xerrors.Errorf("non-200 code: %d", resp.StatusCode)
|
|
|
|
}
|
2020-03-13 00:23:05 +00:00
|
|
|
|
2020-03-22 04:27:28 +00:00
|
|
|
return nil
|
2020-03-13 00:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func mergeDone(a func(), b func()) func() {
|
|
|
|
return func() {
|
|
|
|
a()
|
|
|
|
b()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Store = &Remote{}
|