seaweedfs/weed/filer/filer_conf.go

91 lines
2 KiB
Go
Raw Normal View History

2020-11-15 08:26:05 +00:00
package filer
import (
2020-11-15 22:06:03 +00:00
"context"
2020-11-15 08:26:05 +00:00
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2020-11-15 22:06:03 +00:00
"github.com/chrislusf/seaweedfs/weed/util"
2020-11-15 08:26:05 +00:00
"github.com/golang/protobuf/proto"
"github.com/viant/ptrie"
)
2020-11-15 22:06:03 +00:00
const (
2020-11-16 00:59:28 +00:00
DirectoryEtc = "/etc"
2020-11-15 22:06:03 +00:00
FilerConfName = "filer.conf"
)
2020-11-15 08:26:05 +00:00
type FilerConf struct {
rules ptrie.Trie
}
2020-11-15 22:06:03 +00:00
func NewFilerConf() (fc *FilerConf) {
2020-11-15 08:26:05 +00:00
fc = &FilerConf{
rules: ptrie.New(),
}
2020-11-15 22:06:03 +00:00
return fc
}
2020-11-16 00:59:28 +00:00
func (fc *FilerConf) loadFromFiler(filer *Filer) (err error) {
2020-11-15 22:06:03 +00:00
filerConfPath := util.NewFullPath(DirectoryEtc, FilerConfName)
entry, err := filer.FindEntry(context.Background(), filerConfPath)
if err != nil {
if err == filer_pb.ErrNotFound {
return nil
}
glog.Errorf("read filer conf entry %s: %v", filerConfPath, err)
return
}
return fc.loadFromChunks(filer, entry.Chunks)
}
func (fc *FilerConf) loadFromChunks(filer *Filer, chunks []*filer_pb.FileChunk) (err error) {
data, err := filer.readEntry(chunks)
if err != nil {
glog.Errorf("read filer conf content: %v", err)
return
}
return fc.loadFromBytes(data)
}
func (fc *FilerConf) loadFromBytes(data []byte) (err error) {
2020-11-15 08:26:05 +00:00
conf := &filer_pb.FilerConf{}
2020-11-15 22:06:03 +00:00
err = proto.UnmarshalText(string(data), conf)
2020-11-15 08:26:05 +00:00
if err != nil {
glog.Errorf("unable to parse filer conf: %v", err)
2020-11-15 22:06:03 +00:00
// this is not recoverable
return nil
2020-11-15 08:26:05 +00:00
}
2020-11-15 22:06:03 +00:00
return fc.doLoadConf(conf)
2020-11-15 08:26:05 +00:00
}
2020-11-15 22:06:03 +00:00
func (fc *FilerConf) doLoadConf(conf *filer_pb.FilerConf) (err error) {
2020-11-15 08:26:05 +00:00
for _, location := range conf.Locations {
2020-11-15 22:06:03 +00:00
err = fc.rules.Put([]byte(location.LocationPrefix), location)
2020-11-15 08:26:05 +00:00
if err != nil {
glog.Errorf("put location prefix: %v", err)
2020-11-15 22:06:03 +00:00
// this is not recoverable
return nil
2020-11-15 08:26:05 +00:00
}
}
2020-11-15 22:06:03 +00:00
return nil
2020-11-15 08:26:05 +00:00
}
2020-11-15 22:06:03 +00:00
var (
EmptyFilerConfPathConf = &filer_pb.FilerConf_PathConf{}
)
2020-11-16 00:59:28 +00:00
func (fc *FilerConf) MatchStorageRule(path string) (pathConf *filer_pb.FilerConf_PathConf) {
2020-11-15 08:26:05 +00:00
fc.rules.MatchPrefix([]byte(path), func(key []byte, value interface{}) bool {
pathConf = value.(*filer_pb.FilerConf_PathConf)
return true
})
if pathConf == nil {
2020-11-15 22:06:03 +00:00
return EmptyFilerConfPathConf
2020-11-15 08:26:05 +00:00
}
return pathConf
}