Storage Manager refactor
This commit is contained in:
+3
-13
@@ -3,8 +3,6 @@ package config
|
||||
import (
|
||||
"encoding"
|
||||
"time"
|
||||
|
||||
"github.com/filecoin-project/go-sectorbuilder/fs"
|
||||
)
|
||||
|
||||
// Common is common config between full node and miner
|
||||
@@ -25,7 +23,7 @@ type FullNode struct {
|
||||
type StorageMiner struct {
|
||||
Common
|
||||
|
||||
SectorBuilder SectorBuilder
|
||||
Storage Storage
|
||||
}
|
||||
|
||||
// API contains configs for API endpoint
|
||||
@@ -54,14 +52,8 @@ type Metrics struct {
|
||||
}
|
||||
|
||||
// // Storage Miner
|
||||
type Storage struct {
|
||||
|
||||
type SectorBuilder struct {
|
||||
Path string // TODO: remove // FORK (-ish)
|
||||
Storage []fs.PathConfig
|
||||
WorkerCount uint
|
||||
|
||||
DisableLocalPreCommit bool
|
||||
DisableLocalCommit bool
|
||||
}
|
||||
|
||||
func defCommon() Common {
|
||||
@@ -95,9 +87,7 @@ func DefaultStorageMiner() *StorageMiner {
|
||||
cfg := &StorageMiner{
|
||||
Common: defCommon(),
|
||||
|
||||
SectorBuilder: SectorBuilder{
|
||||
WorkerCount: 5,
|
||||
},
|
||||
Storage: Storage{},
|
||||
}
|
||||
cfg.Common.API.ListenAddress = "/ip4/127.0.0.1/tcp/2345/http"
|
||||
return cfg
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
type LocalPath struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
// .lotusstorage/storage.json
|
||||
type StorageConfig struct {
|
||||
StoragePaths []LocalPath
|
||||
}
|
||||
|
||||
// [path]/metadata.json
|
||||
type StorageMeta struct {
|
||||
ID string
|
||||
Weight int // 0 = readonly
|
||||
|
||||
CanCommit bool
|
||||
CanStore bool
|
||||
}
|
||||
|
||||
func StorageFromFile(path string, def *StorageConfig) (*StorageConfig, error) {
|
||||
file, err := os.Open(path)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
if def == nil {
|
||||
return nil, xerrors.Errorf("couldn't load storage config: %w", err)
|
||||
}
|
||||
return def, nil
|
||||
case err != nil:
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer file.Close() //nolint:errcheck // The file is RO
|
||||
return StorageFromReader(file, *def)
|
||||
}
|
||||
|
||||
func StorageFromReader(reader io.Reader, def StorageConfig) (*StorageConfig, error) {
|
||||
cfg := def
|
||||
err := json.NewDecoder(reader).Decode(&cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func WriteStorageFile(path string, config StorageConfig) error {
|
||||
b, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("marshaling storage config: %w", err)
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(path, b, 0644); err != nil {
|
||||
return xerrors.Errorf("persisting storage config (%s): %w", path, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user