2020-03-23 11:40:02 +00:00
|
|
|
package stores
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-23 22:43:38 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2020-03-24 20:28:07 +00:00
|
|
|
"math/bits"
|
2020-03-23 11:40:02 +00:00
|
|
|
"mime"
|
|
|
|
"net/http"
|
2020-03-23 22:43:38 +00:00
|
|
|
"net/url"
|
2020-03-23 11:40:02 +00:00
|
|
|
"os"
|
2020-03-23 22:43:38 +00:00
|
|
|
gopath "path"
|
2020-03-23 11:40:02 +00:00
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
files "github.com/ipfs/go-ipfs-files"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/lib/tarutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Remote struct {
|
|
|
|
local *Local
|
|
|
|
index SectorIndex
|
|
|
|
auth http.Header
|
|
|
|
|
|
|
|
fetchLk sync.Mutex // TODO: this can be much smarter
|
|
|
|
// TODO: allow multiple parallel fetches
|
|
|
|
// (make sure to not fetch the same sector data twice)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemote(local *Local, index SectorIndex, auth http.Header) *Remote {
|
|
|
|
return &Remote{
|
|
|
|
local: local,
|
|
|
|
index: index,
|
|
|
|
auth: auth,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 02:50:56 +00:00
|
|
|
func (r *Remote) AcquireSector(ctx context.Context, s abi.SectorID, existing SectorFileType, allocate SectorFileType, sealing bool) (SectorPaths, SectorPaths, func(), error) {
|
2020-03-23 11:40:02 +00:00
|
|
|
if existing|allocate != existing^allocate {
|
2020-03-26 02:50:56 +00:00
|
|
|
return SectorPaths{}, SectorPaths{}, nil, xerrors.New("can't both find and allocate a sector")
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.fetchLk.Lock()
|
|
|
|
defer r.fetchLk.Unlock()
|
|
|
|
|
|
|
|
paths, stores, done, err := r.local.AcquireSector(ctx, s, existing, allocate, sealing)
|
|
|
|
if err != nil {
|
2020-03-26 02:50:56 +00:00
|
|
|
return SectorPaths{}, SectorPaths{}, nil, xerrors.Errorf("local acquire error: %w", err)
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileType := range pathTypes {
|
|
|
|
if fileType&existing == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-26 02:50:56 +00:00
|
|
|
if PathByType(paths, fileType) != "" {
|
2020-03-23 11:40:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:40:36 +00:00
|
|
|
ap, storageID, url, rdone, err := r.acquireFromRemote(ctx, s, fileType, sealing)
|
2020-03-23 11:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
done()
|
2020-03-26 02:50:56 +00:00
|
|
|
return SectorPaths{}, SectorPaths{}, nil, err
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done = mergeDone(done, rdone)
|
2020-03-26 02:50:56 +00:00
|
|
|
SetPathByType(&paths, fileType, ap)
|
|
|
|
SetPathByType(&stores, fileType, string(storageID))
|
2020-03-23 11:40:02 +00:00
|
|
|
|
|
|
|
if err := r.index.StorageDeclareSector(ctx, storageID, s, fileType); err != nil {
|
|
|
|
log.Warnf("declaring sector %v in %s failed: %+v", s, storageID, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: some way to allow having duplicated sectors in the system for perf
|
2020-03-23 22:43:38 +00:00
|
|
|
if err := r.deleteFromRemote(ctx, url); err != nil {
|
2020-03-23 11:40:02 +00:00
|
|
|
log.Warnf("deleting sector %v from %s (delete %s): %+v", s, storageID, url, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths, stores, done, nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 02:50:56 +00:00
|
|
|
func (r *Remote) acquireFromRemote(ctx context.Context, s abi.SectorID, fileType SectorFileType, sealing bool) (string, ID, string, func(), error) {
|
2020-03-23 11:40:02 +00:00
|
|
|
si, err := r.index.StorageFindSector(ctx, s, fileType, false)
|
|
|
|
if err != nil {
|
2020-03-23 15:40:36 +00:00
|
|
|
return "", "", "", nil, err
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(si, func(i, j int) bool {
|
|
|
|
return si[i].Weight < si[j].Weight
|
|
|
|
})
|
|
|
|
|
2020-03-25 20:19:58 +00:00
|
|
|
apaths, ids, done, err := r.local.AcquireSector(ctx, s, FTNone, fileType, sealing)
|
2020-03-23 11:40:02 +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-23 11:40:02 +00:00
|
|
|
}
|
2020-03-26 02:50:56 +00:00
|
|
|
dest := PathByType(apaths, fileType)
|
|
|
|
storageID := PathByType(ids, fileType)
|
2020-03-23 11:40:02 +00:00
|
|
|
|
|
|
|
var merr error
|
|
|
|
for _, info := range si {
|
2020-03-25 18:21:53 +00:00
|
|
|
// TODO: see what we have local, prefer that
|
|
|
|
|
2020-03-23 11:40:02 +00:00
|
|
|
for _, url := range info.URLs {
|
2020-03-23 22:43:38 +00:00
|
|
|
err := r.fetch(ctx, url, dest)
|
2020-03-23 11:40:02 +00:00
|
|
|
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-23 11:40:02 +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-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 22:43:38 +00:00
|
|
|
func (r *Remote) fetch(ctx context.Context, url, outname string) error {
|
2020-03-23 11:40:02 +00:00
|
|
|
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
|
2020-03-23 22:43:38 +00:00
|
|
|
req = req.WithContext(ctx)
|
2020-03-23 11:40:02 +00:00
|
|
|
|
|
|
|
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-26 02:50:56 +00:00
|
|
|
func (r *Remote) MoveStorage(ctx context.Context, s abi.SectorID, types SectorFileType) error {
|
2020-03-25 18:21:53 +00:00
|
|
|
// Make sure we have the data local
|
2020-03-25 20:19:58 +00:00
|
|
|
_, _, ddone, err := r.AcquireSector(ctx, s, types, FTNone, false)
|
2020-03-25 18:21:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("acquire src storage (remote): %w", err)
|
|
|
|
}
|
|
|
|
ddone()
|
|
|
|
|
|
|
|
return r.local.MoveStorage(ctx, s, types)
|
|
|
|
}
|
|
|
|
|
2020-03-26 02:50:56 +00:00
|
|
|
func (r *Remote) Remove(ctx context.Context, sid abi.SectorID, typ SectorFileType) error {
|
2020-03-24 20:28:07 +00:00
|
|
|
if bits.OnesCount(uint(typ)) != 1 {
|
|
|
|
return xerrors.New("delete expects one file type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.local.Remove(ctx, sid, typ); err != nil {
|
|
|
|
return xerrors.Errorf("remove from local: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
si, err := r.index.StorageFindSector(ctx, sid, typ, false)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("finding existing sector %d(t:%d) failed: %w", sid, typ, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range si {
|
|
|
|
for _, url := range info.URLs {
|
|
|
|
if err := r.deleteFromRemote(ctx, url); err != nil {
|
|
|
|
log.Warnf("remove %s: %+v", url, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 22:43:38 +00:00
|
|
|
func (r *Remote) deleteFromRemote(ctx context.Context, url string) error {
|
2020-03-23 11:40:02 +00:00
|
|
|
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
|
2020-03-23 22:43:38 +00:00
|
|
|
req = req.WithContext(ctx)
|
2020-03-23 11:40:02 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 22:43:38 +00:00
|
|
|
func (r *Remote) FsStat(ctx context.Context, id ID) (FsStat, error) {
|
2020-03-24 20:28:07 +00:00
|
|
|
st, err := r.local.FsStat(ctx, id)
|
2020-03-23 22:43:38 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return st, nil
|
|
|
|
case errPathNotFound:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return FsStat{}, xerrors.Errorf("local stat: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
si, err := r.index.StorageInfo(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return FsStat{}, xerrors.Errorf("getting remote storage info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(si.URLs) == 0 {
|
|
|
|
return FsStat{}, xerrors.Errorf("no known URLs for remote storage %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
rl, err := url.Parse(si.URLs[0])
|
|
|
|
if err != nil {
|
|
|
|
return FsStat{}, xerrors.Errorf("failed to parse url: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rl.Path = gopath.Join(rl.Path, "stat", string(id))
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", rl.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return FsStat{}, xerrors.Errorf("request: %w", err)
|
|
|
|
}
|
|
|
|
req.Header = r.auth
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return FsStat{}, xerrors.Errorf("do request: %w", err)
|
|
|
|
}
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case 200:
|
|
|
|
break
|
|
|
|
case 404:
|
|
|
|
return FsStat{}, errPathNotFound
|
|
|
|
case 500:
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return FsStat{}, xerrors.Errorf("fsstat: got http 500, then failed to read the error: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-03-24 23:37:40 +00:00
|
|
|
return FsStat{}, xerrors.Errorf("fsstat: got http 500: %s", string(b))
|
2020-03-23 22:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var out FsStat
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
|
|
|
return FsStat{}, xerrors.Errorf("decoding fsstat: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 11:40:02 +00:00
|
|
|
func mergeDone(a func(), b func()) func() {
|
|
|
|
return func() {
|
|
|
|
a()
|
|
|
|
b()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Store = &Remote{}
|