2020-11-15 08:26:05 +00:00
|
|
|
package filer
|
|
|
|
|
|
|
|
import (
|
2020-11-16 05:48:17 +00:00
|
|
|
"bytes"
|
2020-11-15 22:06:03 +00:00
|
|
|
"context"
|
2020-11-16 02:09:35 +00:00
|
|
|
"io"
|
2020-11-15 22:06:03 +00:00
|
|
|
|
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-16 05:48:17 +00:00
|
|
|
"github.com/golang/protobuf/jsonpb"
|
2020-11-15 08:26:05 +00:00
|
|
|
"github.com/viant/ptrie"
|
|
|
|
)
|
|
|
|
|
2020-11-15 22:06:03 +00:00
|
|
|
const (
|
2021-07-21 21:38:12 +00:00
|
|
|
DirectoryEtcRoot = "/etc"
|
|
|
|
DirectoryEtcSeaweedFS = "/etc/seaweedfs"
|
|
|
|
DirectoryEtcRemote = "/etc/remote"
|
|
|
|
FilerConfName = "filer.conf"
|
|
|
|
IamConfigDirecotry = "/etc/iam"
|
|
|
|
IamIdentityFile = "identity.json"
|
|
|
|
IamPoliciesFile = "policies.json"
|
2020-11-15 22:06:03 +00:00
|
|
|
)
|
|
|
|
|
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-12-10 19:11:02 +00:00
|
|
|
filerConfPath := util.NewFullPath(DirectoryEtcSeaweedFS, FilerConfName)
|
2020-11-15 22:06:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-30 12:34:04 +00:00
|
|
|
if len(entry.Content) > 0 {
|
|
|
|
return fc.LoadFromBytes(entry.Content)
|
|
|
|
}
|
|
|
|
|
2021-02-19 01:07:02 +00:00
|
|
|
return fc.loadFromChunks(filer, entry.Content, entry.Chunks)
|
2020-11-15 22:06:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 01:07:02 +00:00
|
|
|
func (fc *FilerConf) loadFromChunks(filer *Filer, content []byte, chunks []*filer_pb.FileChunk) (err error) {
|
|
|
|
if len(content) == 0 {
|
|
|
|
content, err = filer.readEntry(chunks)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("read filer conf content: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2020-11-15 22:06:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 01:07:02 +00:00
|
|
|
return fc.LoadFromBytes(content)
|
2020-11-15 22:06:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 02:09:35 +00:00
|
|
|
func (fc *FilerConf) LoadFromBytes(data []byte) (err error) {
|
2020-11-15 08:26:05 +00:00
|
|
|
conf := &filer_pb.FilerConf{}
|
2020-11-16 05:48:17 +00:00
|
|
|
|
|
|
|
if err := jsonpb.Unmarshal(bytes.NewReader(data), conf); err != nil {
|
2020-12-07 03:47:06 +00:00
|
|
|
return err
|
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-16 02:09:35 +00:00
|
|
|
err = fc.AddLocationConf(location)
|
2020-11-15 08:26:05 +00:00
|
|
|
if err != nil {
|
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-16 02:09:35 +00:00
|
|
|
func (fc *FilerConf) AddLocationConf(locConf *filer_pb.FilerConf_PathConf) (err error) {
|
|
|
|
err = fc.rules.Put([]byte(locConf.LocationPrefix), locConf)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("put location prefix: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-16 04:15:47 +00:00
|
|
|
func (fc *FilerConf) DeleteLocationConf(locationPrefix string) {
|
|
|
|
rules := ptrie.New()
|
|
|
|
fc.rules.Walk(func(key []byte, value interface{}) bool {
|
|
|
|
if string(key) == locationPrefix {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
rules.Put(key, value)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
fc.rules = rules
|
|
|
|
return
|
|
|
|
}
|
2020-11-16 02:09:35 +00:00
|
|
|
|
2020-11-16 00:59:28 +00:00
|
|
|
func (fc *FilerConf) MatchStorageRule(path string) (pathConf *filer_pb.FilerConf_PathConf) {
|
2020-11-17 00:50:12 +00:00
|
|
|
pathConf = &filer_pb.FilerConf_PathConf{}
|
2020-11-15 08:26:05 +00:00
|
|
|
fc.rules.MatchPrefix([]byte(path), func(key []byte, value interface{}) bool {
|
2020-11-17 00:50:12 +00:00
|
|
|
t := value.(*filer_pb.FilerConf_PathConf)
|
|
|
|
mergePathConf(pathConf, t)
|
2020-11-15 08:26:05 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return pathConf
|
|
|
|
}
|
2020-11-16 02:09:35 +00:00
|
|
|
|
2020-11-17 00:50:12 +00:00
|
|
|
// merge if values in b is not empty, merge them into a
|
|
|
|
func mergePathConf(a, b *filer_pb.FilerConf_PathConf) {
|
|
|
|
a.Collection = util.Nvl(b.Collection, a.Collection)
|
|
|
|
a.Replication = util.Nvl(b.Replication, a.Replication)
|
|
|
|
a.Ttl = util.Nvl(b.Ttl, a.Ttl)
|
2020-12-13 19:59:32 +00:00
|
|
|
if b.DiskType != "" {
|
2020-11-17 00:50:12 +00:00
|
|
|
a.DiskType = b.DiskType
|
|
|
|
}
|
|
|
|
a.Fsync = b.Fsync || a.Fsync
|
2020-11-17 09:00:02 +00:00
|
|
|
if b.VolumeGrowthCount > 0 {
|
|
|
|
a.VolumeGrowthCount = b.VolumeGrowthCount
|
|
|
|
}
|
2021-06-04 08:03:41 +00:00
|
|
|
if b.ReadOnly {
|
|
|
|
a.ReadOnly = b.ReadOnly
|
|
|
|
}
|
2020-11-17 00:50:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 02:09:35 +00:00
|
|
|
func (fc *FilerConf) ToProto() *filer_pb.FilerConf {
|
|
|
|
m := &filer_pb.FilerConf{}
|
|
|
|
fc.rules.Walk(func(key []byte, value interface{}) bool {
|
|
|
|
pathConf := value.(*filer_pb.FilerConf_PathConf)
|
|
|
|
m.Locations = append(m.Locations, pathConf)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FilerConf) ToText(writer io.Writer) error {
|
2020-11-16 05:48:17 +00:00
|
|
|
|
|
|
|
m := jsonpb.Marshaler{
|
|
|
|
EmitDefaults: false,
|
|
|
|
Indent: " ",
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.Marshal(writer, fc.ToProto())
|
2020-11-16 04:15:47 +00:00
|
|
|
}
|