From cd892105f6a64d14496dfcccc6af33450111e8e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 24 Mar 2020 21:28:07 +0100 Subject: [PATCH] sectorstorage: Remove unsealed sector in FinalizeSector --- stores/http_handler.go | 4 ++-- stores/interface.go | 1 + stores/local.go | 6 ++++-- stores/remote.go | 30 +++++++++++++++++++++++++++++- worker_local.go | 10 +++++++++- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/stores/http_handler.go b/stores/http_handler.go index 97c6c34f6..657da0f9b 100644 --- a/stores/http_handler.go +++ b/stores/http_handler.go @@ -36,7 +36,7 @@ func (handler *FetchHandler) remoteStatFs(w http.ResponseWriter, r *http.Request vars := mux.Vars(r) id := ID(vars["id"]) - st, err := handler.Local.FsStat(id) + st, err := handler.Local.FsStat(r.Context(), id) switch err { case errPathNotFound: w.WriteHeader(404) @@ -129,7 +129,7 @@ func (handler *FetchHandler) remoteDeleteSector(w http.ResponseWriter, r *http.R return } - if err := handler.delete(r.Context(), id, ft); err != nil { + if err := handler.Remove(r.Context(), id, ft); err != nil { log.Error("%+v", err) w.WriteHeader(500) return diff --git a/stores/interface.go b/stores/interface.go index 149cb9e5f..e56a6d74a 100644 --- a/stores/interface.go +++ b/stores/interface.go @@ -12,6 +12,7 @@ import ( type Store interface { AcquireSector(ctx context.Context, s abi.SectorID, existing sectorbuilder.SectorFileType, allocate sectorbuilder.SectorFileType, sealing bool) (paths sectorbuilder.SectorPaths, stores sectorbuilder.SectorPaths, done func(), err error) + Remove(ctx context.Context, s abi.SectorID, types sectorbuilder.SectorFileType) error FsStat(ctx context.Context, id ID) (FsStat, error) } diff --git a/stores/local.go b/stores/local.go index 581afb13f..d891f86fa 100644 --- a/stores/local.go +++ b/stores/local.go @@ -270,7 +270,7 @@ func (st *Local) Local(ctx context.Context) ([]StoragePath, error) { return out, nil } -func (st *Local) delete(ctx context.Context, sid abi.SectorID, typ sectorbuilder.SectorFileType) error { +func (st *Local) Remove(ctx context.Context, sid abi.SectorID, typ sectorbuilder.SectorFileType) error { if bits.OnesCount(uint(typ)) != 1 { return xerrors.New("delete expects one file type") } @@ -311,7 +311,7 @@ func (st *Local) delete(ctx context.Context, sid abi.SectorID, typ sectorbuilder var errPathNotFound = xerrors.Errorf("fsstat: path not found") -func (st *Local) FsStat(id ID) (FsStat, error) { +func (st *Local) FsStat(ctx context.Context, id ID) (FsStat, error) { st.localLk.RLock() defer st.localLk.RUnlock() @@ -322,3 +322,5 @@ func (st *Local) FsStat(id ID) (FsStat, error) { return Stat(p.local) } + +var _ Store = &Local{} diff --git a/stores/remote.go b/stores/remote.go index a0648d972..ab7d465b3 100644 --- a/stores/remote.go +++ b/stores/remote.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "io/ioutil" + "math/bits" "mime" "net/http" "net/url" @@ -173,6 +174,33 @@ func (r *Remote) fetch(ctx context.Context, url, outname string) error { } } +func (r *Remote) Remove(ctx context.Context, sid abi.SectorID, typ sectorbuilder.SectorFileType) error { + 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 +} + func (r *Remote) deleteFromRemote(ctx context.Context, url string) error { log.Infof("Delete %s", url) @@ -197,7 +225,7 @@ func (r *Remote) deleteFromRemote(ctx context.Context, url string) error { } func (r *Remote) FsStat(ctx context.Context, id ID) (FsStat, error) { - st, err := r.local.FsStat(id) + st, err := r.local.FsStat(ctx, id) switch err { case nil: return st, nil diff --git a/worker_local.go b/worker_local.go index d691f150e..18a0305fd 100644 --- a/worker_local.go +++ b/worker_local.go @@ -152,7 +152,15 @@ func (l *LocalWorker) FinalizeSector(ctx context.Context, sector abi.SectorID) e return err } - return sb.FinalizeSector(ctx, sector) + if err := sb.FinalizeSector(ctx, sector); err != nil { + return xerrors.Errorf("finalizing sector: %w", err) + } + + if err := l.storage.Remove(ctx, sector, sectorbuilder.FTUnsealed); err != nil { + return xerrors.Errorf("removing unsealed data: %w", err) + } + + return nil } func (l *LocalWorker) TaskTypes(context.Context) (map[sealtasks.TaskType]struct{}, error) {