remove double http handler decl

This commit is contained in:
Anton Evangelatov 2021-06-08 15:33:10 +02:00
parent 3f5f96dcbd
commit 30ba9a2751
3 changed files with 46 additions and 53 deletions

View File

@ -2,6 +2,7 @@ package stores
import ( import (
"encoding/json" "encoding/json"
"io"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -13,6 +14,7 @@ import (
"github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/extern/sector-storage/partialfile" "github.com/filecoin-project/lotus/extern/sector-storage/partialfile"
"github.com/filecoin-project/lotus/extern/sector-storage/storiface" "github.com/filecoin-project/lotus/extern/sector-storage/storiface"
"github.com/filecoin-project/lotus/extern/sector-storage/tarutil"
"github.com/filecoin-project/specs-storage/storage" "github.com/filecoin-project/specs-storage/storage"
) )
@ -55,8 +57,6 @@ func (handler *FetchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/remote/{type}/{id}", handler.remoteGetSector).Methods("GET") mux.HandleFunc("/remote/{type}/{id}", handler.remoteGetSector).Methods("GET")
mux.HandleFunc("/remote/{type}/{id}", handler.remoteDeleteSector).Methods("DELETE") mux.HandleFunc("/remote/{type}/{id}", handler.remoteDeleteSector).Methods("DELETE")
mux.HandleFunc("/remote/{type}/{id}/{spt}/allocated/{offset}/{size}", handler.remoteGetAllocated).Methods("GET")
mux.ServeHTTP(w, r) mux.ServeHTTP(w, r)
} }
@ -101,40 +101,9 @@ func (handler *FetchHandler) remoteGetSector(w http.ResponseWriter, r *http.Requ
w.WriteHeader(500) w.WriteHeader(500)
return return
} }
if ft != storiface.FTUnsealed {
log.Errorf("/allocated only supports unsealed sector files")
w.WriteHeader(500)
return
}
spti, err := strconv.ParseInt(vars["spt"], 10, 64)
if err != nil {
log.Errorf("parsing spt: %+v", err)
w.WriteHeader(500)
return
}
spt := abi.RegisteredSealProof(spti)
ssize, err := spt.SectorSize()
if err != nil {
log.Errorf("%+v", err)
w.WriteHeader(500)
return
}
offi, err := strconv.ParseInt(vars["offset"], 10, 64)
if err != nil {
log.Errorf("parsing offset: %+v", err)
w.WriteHeader(500)
return
}
szi, err := strconv.ParseInt(vars["size"], 10, 64)
if err != nil {
log.Errorf("parsing spt: %+v", err)
w.WriteHeader(500)
return
}
// The caller has a lock on this sector already, no need to get one here // The caller has a lock on this sector already, no need to get one here
// passing 0 spt because we don't allocate anything // passing 0 spt because we don't allocate anything
si := storage.SectorRef{ si := storage.SectorRef{
ID: id, ID: id,
@ -143,11 +112,13 @@ func (handler *FetchHandler) remoteGetSector(w http.ResponseWriter, r *http.Requ
paths, _, err := handler.Local.AcquireSector(r.Context(), si, ft, storiface.FTNone, storiface.PathStorage, storiface.AcquireMove) paths, _, err := handler.Local.AcquireSector(r.Context(), si, ft, storiface.FTNone, storiface.PathStorage, storiface.AcquireMove)
if err != nil { if err != nil {
log.Errorf("AcquireSector: %+v", err) log.Errorf("%+v", err)
w.WriteHeader(500) w.WriteHeader(500)
return return
} }
// TODO: reserve local storage here
path := storiface.PathByType(paths, ft) path := storiface.PathByType(paths, ft)
if path == "" { if path == "" {
log.Error("acquired path was empty") log.Error("acquired path was empty")
@ -155,30 +126,37 @@ func (handler *FetchHandler) remoteGetSector(w http.ResponseWriter, r *http.Requ
return return
} }
pf, err := partialfile.OpenPartialFile(abi.PaddedPieceSize(ssize), path) stat, err := os.Stat(path)
if err != nil { if err != nil {
log.Error("opening partial file: ", err) log.Errorf("%+v", err)
w.WriteHeader(500) w.WriteHeader(500)
return return
} }
defer func() {
if err := pf.Close(); err != nil { if stat.IsDir() {
log.Error("close partial file: ", err) if _, has := r.Header["Range"]; has {
log.Error("Range not supported on directories")
w.WriteHeader(500)
return
} }
}()
has, err := pf.HasAllocated(storiface.UnpaddedByteIndex(offi), abi.UnpaddedPieceSize(szi)) rd, err := tarutil.TarDirectory(path)
if err != nil { if err != nil {
log.Error("has allocated: ", err) log.Errorf("%+v", err)
w.WriteHeader(500) w.WriteHeader(500)
return return
} }
if has { w.Header().Set("Content-Type", "application/x-tar")
w.WriteHeader(http.StatusOK) w.WriteHeader(200)
return if _, err := io.CopyBuffer(w, rd, make([]byte, CopyBuf)); err != nil {
log.Errorf("%+v", err)
return
}
} else {
w.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(w, r, path)
} }
w.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
} }
func (handler *FetchHandler) remoteDeleteSector(w http.ResponseWriter, r *http.Request) { func (handler *FetchHandler) remoteDeleteSector(w http.ResponseWriter, r *http.Request) {

View File

@ -1,7 +1,7 @@
// Code generated by MockGen. DO NOT EDIT. // Code generated by MockGen. DO NOT EDIT.
// Source: index.go // Source: index.go
// Package mock_stores is a generated GoMock package. // Package mocks is a generated GoMock package.
package mocks package mocks
import ( import (
@ -125,6 +125,21 @@ func (mr *MockSectorIndexMockRecorder) StorageInfo(arg0, arg1 interface{}) *gomo
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StorageInfo", reflect.TypeOf((*MockSectorIndex)(nil).StorageInfo), arg0, arg1) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StorageInfo", reflect.TypeOf((*MockSectorIndex)(nil).StorageInfo), arg0, arg1)
} }
// StorageList mocks base method.
func (m *MockSectorIndex) StorageList(ctx context.Context) (map[stores.ID][]stores.Decl, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "StorageList", ctx)
ret0, _ := ret[0].(map[stores.ID][]stores.Decl)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// StorageList indicates an expected call of StorageList.
func (mr *MockSectorIndexMockRecorder) StorageList(ctx interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StorageList", reflect.TypeOf((*MockSectorIndex)(nil).StorageList), ctx)
}
// StorageLock mocks base method. // StorageLock mocks base method.
func (m *MockSectorIndex) StorageLock(ctx context.Context, sector abi.SectorID, read, write storiface.SectorFileType) error { func (m *MockSectorIndex) StorageLock(ctx context.Context, sector abi.SectorID, read, write storiface.SectorFileType) error {
m.ctrl.T.Helper() m.ctrl.T.Helper()

View File

@ -1,7 +1,7 @@
// Code generated by MockGen. DO NOT EDIT. // Code generated by MockGen. DO NOT EDIT.
// Source: interface.go // Source: interface.go
// Package mock_stores is a generated GoMock package. // Package mocks is a generated GoMock package.
package mocks package mocks
import ( import (