2020-11-15 16:26:05 +08:00
|
|
|
package filer
|
|
|
|
|
|
|
|
import (
|
2020-11-16 13:48:17 +08:00
|
|
|
"bytes"
|
2020-11-16 06:06:03 +08:00
|
|
|
"context"
|
2021-10-11 18:03:56 +08:00
|
|
|
"fmt"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
2021-10-11 18:03:56 +08:00
|
|
|
"google.golang.org/grpc"
|
2020-11-16 10:09:35 +08:00
|
|
|
"io"
|
2020-11-16 06:06:03 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2020-11-15 16:26:05 +08:00
|
|
|
"github.com/viant/ptrie"
|
2022-08-18 03:42:03 +08:00
|
|
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
2020-11-15 16:26:05 +08:00
|
|
|
)
|
|
|
|
|
2020-11-16 06:06:03 +08:00
|
|
|
const (
|
2022-08-22 08:02:16 +08:00
|
|
|
DirectoryEtcRoot = "/etc/"
|
2021-07-22 05:38:12 +08:00
|
|
|
DirectoryEtcSeaweedFS = "/etc/seaweedfs"
|
|
|
|
DirectoryEtcRemote = "/etc/remote"
|
|
|
|
FilerConfName = "filer.conf"
|
2022-09-15 01:11:31 +08:00
|
|
|
IamConfigDirectory = "/etc/iam"
|
2021-07-22 05:38:12 +08:00
|
|
|
IamIdentityFile = "identity.json"
|
|
|
|
IamPoliciesFile = "policies.json"
|
2020-11-16 06:06:03 +08:00
|
|
|
)
|
|
|
|
|
2020-11-15 16:26:05 +08:00
|
|
|
type FilerConf struct {
|
2024-04-30 09:42:31 +08:00
|
|
|
rules ptrie.Trie[*filer_pb.FilerConf_PathConf]
|
2020-11-15 16:26:05 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 18:03:56 +08:00
|
|
|
func ReadFilerConf(filerGrpcAddress pb.ServerAddress, grpcDialOption grpc.DialOption, masterClient *wdclient.MasterClient) (*FilerConf, error) {
|
|
|
|
var buf bytes.Buffer
|
2023-01-20 17:48:12 +08:00
|
|
|
if err := pb.WithGrpcFilerClient(false, 0, filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2021-10-11 18:03:56 +08:00
|
|
|
if masterClient != nil {
|
|
|
|
return ReadEntry(masterClient, client, DirectoryEtcSeaweedFS, FilerConfName, &buf)
|
|
|
|
} else {
|
|
|
|
content, err := ReadInsideFiler(client, DirectoryEtcSeaweedFS, FilerConfName)
|
|
|
|
buf = *bytes.NewBuffer(content)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}); err != nil && err != filer_pb.ErrNotFound {
|
|
|
|
return nil, fmt.Errorf("read %s/%s: %v", DirectoryEtcSeaweedFS, FilerConfName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fc := NewFilerConf()
|
|
|
|
if buf.Len() > 0 {
|
|
|
|
if err := fc.LoadFromBytes(buf.Bytes()); err != nil {
|
|
|
|
return nil, fmt.Errorf("parse %s/%s: %v", DirectoryEtcSeaweedFS, FilerConfName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fc, nil
|
|
|
|
}
|
|
|
|
|
2020-11-16 06:06:03 +08:00
|
|
|
func NewFilerConf() (fc *FilerConf) {
|
2020-11-15 16:26:05 +08:00
|
|
|
fc = &FilerConf{
|
2024-04-30 09:42:31 +08:00
|
|
|
rules: ptrie.New[*filer_pb.FilerConf_PathConf](),
|
2020-11-15 16:26:05 +08:00
|
|
|
}
|
2020-11-16 06:06:03 +08:00
|
|
|
return fc
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:59:28 +08:00
|
|
|
func (fc *FilerConf) loadFromFiler(filer *Filer) (err error) {
|
2020-12-11 03:11:02 +08:00
|
|
|
filerConfPath := util.NewFullPath(DirectoryEtcSeaweedFS, FilerConfName)
|
2020-11-16 06:06:03 +08: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 20:34:04 +08:00
|
|
|
if len(entry.Content) > 0 {
|
|
|
|
return fc.LoadFromBytes(entry.Content)
|
|
|
|
}
|
|
|
|
|
2022-11-15 22:33:36 +08:00
|
|
|
return fc.loadFromChunks(filer, entry.Content, entry.GetChunks(), entry.Size())
|
2020-11-16 06:06:03 +08:00
|
|
|
}
|
|
|
|
|
2022-02-08 15:08:54 +08:00
|
|
|
func (fc *FilerConf) loadFromChunks(filer *Filer, content []byte, chunks []*filer_pb.FileChunk, size uint64) (err error) {
|
2021-02-19 09:07:02 +08:00
|
|
|
if len(content) == 0 {
|
2022-02-08 15:08:54 +08:00
|
|
|
content, err = filer.readEntry(chunks, size)
|
2021-02-19 09:07:02 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("read filer conf content: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2020-11-16 06:06:03 +08:00
|
|
|
}
|
|
|
|
|
2021-02-19 09:07:02 +08:00
|
|
|
return fc.LoadFromBytes(content)
|
2020-11-16 06:06:03 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 10:09:35 +08:00
|
|
|
func (fc *FilerConf) LoadFromBytes(data []byte) (err error) {
|
2020-11-15 16:26:05 +08:00
|
|
|
conf := &filer_pb.FilerConf{}
|
2020-11-16 13:48:17 +08:00
|
|
|
|
2022-08-18 03:42:03 +08:00
|
|
|
if err := jsonpb.Unmarshal(data, conf); err != nil {
|
2020-12-07 11:47:06 +08:00
|
|
|
return err
|
2020-11-15 16:26:05 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 06:06:03 +08:00
|
|
|
return fc.doLoadConf(conf)
|
2020-11-15 16:26:05 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 06:06:03 +08:00
|
|
|
func (fc *FilerConf) doLoadConf(conf *filer_pb.FilerConf) (err error) {
|
2020-11-15 16:26:05 +08:00
|
|
|
for _, location := range conf.Locations {
|
2020-11-16 10:09:35 +08:00
|
|
|
err = fc.AddLocationConf(location)
|
2020-11-15 16:26:05 +08:00
|
|
|
if err != nil {
|
2020-11-16 06:06:03 +08:00
|
|
|
// this is not recoverable
|
|
|
|
return nil
|
2020-11-15 16:26:05 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 06:06:03 +08:00
|
|
|
return nil
|
2020-11-15 16:26:05 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 10:09:35 +08: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 12:15:47 +08:00
|
|
|
func (fc *FilerConf) DeleteLocationConf(locationPrefix string) {
|
2024-04-30 09:42:31 +08:00
|
|
|
rules := ptrie.New[*filer_pb.FilerConf_PathConf]()
|
|
|
|
fc.rules.Walk(func(key []byte, value *filer_pb.FilerConf_PathConf) bool {
|
2020-11-16 12:15:47 +08:00
|
|
|
if string(key) == locationPrefix {
|
|
|
|
return true
|
|
|
|
}
|
2023-03-30 02:29:44 +08:00
|
|
|
key = bytes.Clone(key)
|
|
|
|
_ = rules.Put(key, value)
|
2020-11-16 12:15:47 +08:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
fc.rules = rules
|
|
|
|
return
|
|
|
|
}
|
2020-11-16 10:09:35 +08:00
|
|
|
|
2020-11-16 08:59:28 +08:00
|
|
|
func (fc *FilerConf) MatchStorageRule(path string) (pathConf *filer_pb.FilerConf_PathConf) {
|
2020-11-17 08:50:12 +08:00
|
|
|
pathConf = &filer_pb.FilerConf_PathConf{}
|
2024-04-30 09:42:31 +08:00
|
|
|
fc.rules.MatchPrefix([]byte(path), func(key []byte, value *filer_pb.FilerConf_PathConf) bool {
|
|
|
|
mergePathConf(pathConf, value)
|
2020-11-15 16:26:05 +08:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return pathConf
|
|
|
|
}
|
2020-11-16 10:09:35 +08:00
|
|
|
|
2021-10-14 04:35:33 +08:00
|
|
|
func (fc *FilerConf) GetCollectionTtls(collection string) (ttls map[string]string) {
|
|
|
|
ttls = make(map[string]string)
|
2024-04-30 09:42:31 +08:00
|
|
|
fc.rules.Walk(func(key []byte, value *filer_pb.FilerConf_PathConf) bool {
|
|
|
|
if value.Collection == collection {
|
|
|
|
ttls[value.LocationPrefix] = value.GetTtl()
|
2021-10-14 04:35:33 +08:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return ttls
|
|
|
|
}
|
|
|
|
|
2020-11-17 08:50:12 +08: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)
|
2021-12-23 23:24:10 +08:00
|
|
|
a.DiskType = util.Nvl(b.DiskType, a.DiskType)
|
2020-11-17 08:50:12 +08:00
|
|
|
a.Fsync = b.Fsync || a.Fsync
|
2020-11-17 17:00:02 +08:00
|
|
|
if b.VolumeGrowthCount > 0 {
|
|
|
|
a.VolumeGrowthCount = b.VolumeGrowthCount
|
|
|
|
}
|
2021-12-23 23:24:10 +08:00
|
|
|
a.ReadOnly = b.ReadOnly || a.ReadOnly
|
2023-10-13 05:29:55 +08:00
|
|
|
if b.MaxFileNameLength > 0 {
|
|
|
|
a.MaxFileNameLength = b.MaxFileNameLength
|
|
|
|
}
|
2021-12-23 23:24:10 +08:00
|
|
|
a.DataCenter = util.Nvl(b.DataCenter, a.DataCenter)
|
|
|
|
a.Rack = util.Nvl(b.Rack, a.Rack)
|
|
|
|
a.DataNode = util.Nvl(b.DataNode, a.DataNode)
|
2020-11-17 08:50:12 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 10:09:35 +08:00
|
|
|
func (fc *FilerConf) ToProto() *filer_pb.FilerConf {
|
|
|
|
m := &filer_pb.FilerConf{}
|
2024-04-30 09:42:31 +08:00
|
|
|
fc.rules.Walk(func(key []byte, value *filer_pb.FilerConf_PathConf) bool {
|
|
|
|
m.Locations = append(m.Locations, value)
|
2020-11-16 10:09:35 +08:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *FilerConf) ToText(writer io.Writer) error {
|
2022-08-18 03:42:03 +08:00
|
|
|
return ProtoToText(writer, fc.ToProto())
|
2020-11-16 12:15:47 +08:00
|
|
|
}
|