seaweedfs/weed/storage/backend/backend.go

122 lines
3.2 KiB
Go
Raw Normal View History

package backend
import (
"io"
2019-11-27 19:09:42 +08:00
"os"
"strings"
"time"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
"github.com/spf13/viper"
)
type BackendStorageFile interface {
io.ReaderAt
io.WriterAt
Truncate(off int64) error
io.Closer
GetStat() (datSize int64, modTime time.Time, err error)
String() string
2019-11-27 19:09:42 +08:00
Instantiate(src *os.File) error
}
2019-11-19 11:24:34 +08:00
type BackendStorage interface {
ToProperties() map[string]string
NewStorageFile(key string) BackendStorageFile
}
type StringProperties interface {
GetString(key string) string
}
type StorageType string
type BackendStorageFactory interface {
StorageType() StorageType
BuildStorage(configuration StringProperties, id string) (BackendStorage, error)
}
2019-11-19 11:24:34 +08:00
var (
BackendStorageFactories = make(map[StorageType]BackendStorageFactory)
BackendStorages = make(map[string]BackendStorage)
2019-11-19 11:24:34 +08:00
)
func LoadConfiguration(config *viper.Viper) {
StorageBackendPrefix := "storage.backend"
backendSub := config.Sub(StorageBackendPrefix)
for backendTypeName, _ := range config.GetStringMap(StorageBackendPrefix) {
backendStorageFactory, found := BackendStorageFactories[StorageType(backendTypeName)]
if !found {
glog.Fatalf("backend storage type %s not found", backendTypeName)
}
backendTypeSub := backendSub.Sub(backendTypeName)
for backendStorageId, _ := range backendSub.GetStringMap(backendTypeName) {
if !backendTypeSub.GetBool(backendStorageId + ".enabled") {
continue
}
backendStorage, buildErr := backendStorageFactory.BuildStorage(backendTypeSub.Sub(backendStorageId), backendStorageId)
if buildErr != nil {
glog.Fatalf("fail to create backend storage %s.%s", backendTypeName, backendStorageId)
}
BackendStorages[backendTypeName+"."+backendStorageId] = backendStorage
2019-11-29 10:47:51 +08:00
if backendStorageId == "default" {
BackendStorages[backendTypeName] = backendStorage
}
}
}
}
func LoadFromPbStorageBackends(storageBackends []*master_pb.StorageBackend) {
for _, storageBackend := range storageBackends {
backendStorageFactory, found := BackendStorageFactories[StorageType(storageBackend.Type)]
if !found {
glog.Warningf("storage type %s not found", storageBackend.Type)
continue
}
backendStorage, buildErr := backendStorageFactory.BuildStorage(newProperties(storageBackend.Properties), storageBackend.Id)
if buildErr != nil {
glog.Fatalf("fail to create backend storage %s.%s", storageBackend.Type, storageBackend.Id)
}
BackendStorages[storageBackend.Type+"."+storageBackend.Id] = backendStorage
if storageBackend.Id == "default" {
BackendStorages[storageBackend.Type] = backendStorage
}
}
}
type Properties struct {
m map[string]string
}
func newProperties(m map[string]string) *Properties {
return &Properties{m: m}
}
func (p *Properties) GetString(key string) string {
if v, found := p.m[key]; found {
return v
}
return ""
}
func ToPbStorageBackends() (backends []*master_pb.StorageBackend) {
for sName, s := range BackendStorages {
parts := strings.Split(sName, ".")
if len(parts) != 2 {
continue
}
sType, sId := parts[0], parts[1]
backends = append(backends, &master_pb.StorageBackend{
Type: sType,
Id: sId,
Properties: s.ToProperties(),
})
}
return
}