sectorbuilder: fs: address review

This commit is contained in:
Łukasz Magiera 2019-12-16 19:49:32 +01:00
parent 825e3a0774
commit 1383bfa0eb
5 changed files with 36 additions and 44 deletions

View File

@ -3,7 +3,7 @@ package build
var CurrentCommit string var CurrentCommit string
// Version is the local build version, set by build system // Version is the local build version, set by build system
const Version = "0.1.1" const Version = "0.1.2"
var UserVersion = Version + CurrentCommit var UserVersion = Version + CurrentCommit
@ -16,7 +16,7 @@ var UserVersion = Version + CurrentCommit
// R R H // R R H
// |\vv/| // |\vv/|
// vv vv // vv vv
const APIVersion = 0x000101 const APIVersion = 0x000102
const ( const (
MajorMask = 0xff0000 MajorMask = 0xff0000

View File

@ -132,7 +132,7 @@ func (w *worker) fetchSector(sectorID uint64, typ sectorbuilder.WorkerTaskType)
var err error var err error
switch typ { switch typ {
case sectorbuilder.WorkerPreCommit: case sectorbuilder.WorkerPreCommit:
err = w.fetch("staged", sectorID) err = w.fetch("staging", sectorID)
case sectorbuilder.WorkerCommit: case sectorbuilder.WorkerCommit:
err = w.fetch("sealed", sectorID) err = w.fetch("sealed", sectorID)
if err != nil { if err != nil {

View File

@ -17,11 +17,11 @@ func (sb *SectorBuilder) SectorName(sectorID uint64) string {
} }
func (sb *SectorBuilder) StagedSectorPath(sectorID uint64) string { func (sb *SectorBuilder) StagedSectorPath(sectorID uint64) string {
return filepath.Join(sb.filesystem.staging(), sb.SectorName(sectorID)) return filepath.Join(sb.filesystem.pathFor(dataStaging), sb.SectorName(sectorID))
} }
func (sb *SectorBuilder) unsealedSectorPath(sectorID uint64) string { func (sb *SectorBuilder) unsealedSectorPath(sectorID uint64) string {
return filepath.Join(sb.filesystem.unsealed(), sb.SectorName(sectorID)) return filepath.Join(sb.filesystem.pathFor(dataUnsealed), sb.SectorName(sectorID))
} }
func (sb *SectorBuilder) stagedSectorFile(sectorID uint64) (*os.File, error) { func (sb *SectorBuilder) stagedSectorFile(sectorID uint64) (*os.File, error) {
@ -29,13 +29,13 @@ func (sb *SectorBuilder) stagedSectorFile(sectorID uint64) (*os.File, error) {
} }
func (sb *SectorBuilder) SealedSectorPath(sectorID uint64) (string, error) { func (sb *SectorBuilder) SealedSectorPath(sectorID uint64) (string, error) {
path := filepath.Join(sb.filesystem.sealed(), sb.SectorName(sectorID)) path := filepath.Join(sb.filesystem.pathFor(dataSealed), sb.SectorName(sectorID))
return path, nil return path, nil
} }
func (sb *SectorBuilder) sectorCacheDir(sectorID uint64) (string, error) { func (sb *SectorBuilder) sectorCacheDir(sectorID uint64) (string, error) {
dir := filepath.Join(sb.filesystem.cache(), sb.SectorName(sectorID)) dir := filepath.Join(sb.filesystem.pathFor(dataCache), sb.SectorName(sectorID))
err := os.Mkdir(dir, 0755) err := os.Mkdir(dir, 0755)
if os.IsExist(err) { if os.IsExist(err) {
@ -46,16 +46,12 @@ func (sb *SectorBuilder) sectorCacheDir(sectorID uint64) (string, error) {
} }
func (sb *SectorBuilder) GetPath(typ string, sectorName string) (string, error) { func (sb *SectorBuilder) GetPath(typ string, sectorName string) (string, error) {
switch typ { _, found := overheadMul[dataType(typ)]
case "staged": if !found {
return filepath.Join(sb.filesystem.staging(), sectorName), nil return "", xerrors.Errorf("unknown sector type: %s", typ)
case "sealed":
return filepath.Join(sb.filesystem.sealed(), sectorName), nil
case "cache":
return filepath.Join(sb.filesystem.cache(), sectorName), nil
default:
return "", xerrors.Errorf("unknown sector type for write: %s", typ)
} }
return sb.filesystem.pathFor(dataType(typ)), nil
} }
func (sb *SectorBuilder) TrimCache(sectorID uint64) error { func (sb *SectorBuilder) TrimCache(sectorID uint64) error {

View File

@ -9,18 +9,16 @@ import (
"syscall" "syscall"
) )
type dataType int type dataType string
const ( const (
dataCache dataType = iota dataCache dataType = "cache"
dataStaging dataStaging dataType = "staging"
dataSealed dataSealed dataType = "sealed"
dataUnsealed dataUnsealed dataType = "unsealed"
nDataTypes
) )
var overheadMul = []uint64{ // * sectorSize var overheadMul = map[dataType]uint64{ // * sectorSize
dataCache: 11, // TODO: check if true for 32G sectors dataCache: 11, // TODO: check if true for 32G sectors
dataStaging: 1, dataStaging: 1,
dataSealed: 1, dataSealed: 1,
@ -32,19 +30,24 @@ type fs struct {
// in progress actions // in progress actions
reserved [nDataTypes]uint64 reserved map[dataType]uint64
lk sync.Mutex lk sync.Mutex
} }
func openFs(dir string) *fs { func openFs(dir string) *fs {
return &fs{ return &fs{
path: dir, path: dir,
reserved: map[dataType]uint64{},
} }
} }
func (f *fs) init() error { func (f *fs) init() error {
for _, dir := range []string{f.path, f.cache(), f.staging(), f.sealed(), f.unsealed()} { for _, dir := range []string{f.path,
f.pathFor(dataCache),
f.pathFor(dataStaging),
f.pathFor(dataSealed),
f.pathFor(dataUnsealed)} {
if err := os.Mkdir(dir, 0755); err != nil { if err := os.Mkdir(dir, 0755); err != nil {
if os.IsExist(err) { if os.IsExist(err) {
continue continue
@ -56,20 +59,13 @@ func (f *fs) init() error {
return nil return nil
} }
func (f *fs) cache() string { func (f *fs) pathFor(typ dataType) string {
return filepath.Join(f.path, "cache") _, found := overheadMul[typ]
} if !found {
panic("unknown data path requested")
}
func (f *fs) staging() string { return filepath.Join(f.path, string(typ))
return filepath.Join(f.path, "staging")
}
func (f *fs) sealed() string {
return filepath.Join(f.path, "sealed")
}
func (f *fs) unsealed() string {
return filepath.Join(f.path, "unsealed")
} }
func (f *fs) reservedBytes() int64 { func (f *fs) reservedBytes() int64 {
@ -86,7 +82,7 @@ func (f *fs) reserve(typ dataType, size uint64) error {
var fsstat syscall.Statfs_t var fsstat syscall.Statfs_t
if err := syscall.Statfs(f.path, &fsstat); err != nil { if err := syscall.Statfs(f.pathFor(typ), &fsstat); err != nil {
return err return err
} }

View File

@ -699,15 +699,15 @@ func fallbackPostChallengeCount(sectors uint64) uint64 {
} }
func (sb *SectorBuilder) ImportFrom(osb *SectorBuilder, symlink bool) error { func (sb *SectorBuilder) ImportFrom(osb *SectorBuilder, symlink bool) error {
if err := migrate(osb.filesystem.cache(), sb.filesystem.cache(), symlink); err != nil { if err := migrate(osb.filesystem.pathFor(dataCache), sb.filesystem.pathFor(dataCache), symlink); err != nil {
return err return err
} }
if err := migrate(osb.filesystem.sealed(), sb.filesystem.sealed(), symlink); err != nil { if err := migrate(osb.filesystem.pathFor(dataStaging), sb.filesystem.pathFor(dataStaging), symlink); err != nil {
return err return err
} }
if err := migrate(osb.filesystem.staging(), sb.filesystem.staging(), symlink); err != nil { if err := migrate(osb.filesystem.pathFor(dataSealed), sb.filesystem.pathFor(dataSealed), symlink); err != nil {
return err return err
} }