From 71056dae07d59ca818dac3fe4f754fe51faedb6c Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 15 Nov 2020 18:09:35 -0800 Subject: [PATCH] fs.configure: read and local add filer configuration --- weed/filer/filer_conf.go | 31 +++++++++-- weed/shell/command_fs_configure.go | 87 ++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 weed/shell/command_fs_configure.go diff --git a/weed/filer/filer_conf.go b/weed/filer/filer_conf.go index b75fb5084..3e3e9f8a9 100644 --- a/weed/filer/filer_conf.go +++ b/weed/filer/filer_conf.go @@ -2,6 +2,7 @@ package filer import ( "context" + "io" "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" @@ -47,10 +48,10 @@ func (fc *FilerConf) loadFromChunks(filer *Filer, chunks []*filer_pb.FileChunk) return } - return fc.loadFromBytes(data) + return fc.LoadFromBytes(data) } -func (fc *FilerConf) loadFromBytes(data []byte) (err error) { +func (fc *FilerConf) LoadFromBytes(data []byte) (err error) { conf := &filer_pb.FilerConf{} err = proto.UnmarshalText(string(data), conf) if err != nil { @@ -64,9 +65,8 @@ func (fc *FilerConf) loadFromBytes(data []byte) (err error) { func (fc *FilerConf) doLoadConf(conf *filer_pb.FilerConf) (err error) { for _, location := range conf.Locations { - err = fc.rules.Put([]byte(location.LocationPrefix), location) + err = fc.AddLocationConf(location) if err != nil { - glog.Errorf("put location prefix: %v", err) // this is not recoverable return nil } @@ -74,6 +74,15 @@ func (fc *FilerConf) doLoadConf(conf *filer_pb.FilerConf) (err error) { return nil } +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 +} + + var ( EmptyFilerConfPathConf = &filer_pb.FilerConf_PathConf{} ) @@ -88,3 +97,17 @@ func (fc *FilerConf) MatchStorageRule(path string) (pathConf *filer_pb.FilerConf } return pathConf } + +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 { + return proto.MarshalText(writer, fc.ToProto()) +} \ No newline at end of file diff --git a/weed/shell/command_fs_configure.go b/weed/shell/command_fs_configure.go new file mode 100644 index 000000000..d8fcda83b --- /dev/null +++ b/weed/shell/command_fs_configure.go @@ -0,0 +1,87 @@ +package shell + +import ( + "bytes" + "flag" + "io" + "math" + + "github.com/chrislusf/seaweedfs/weed/filer" + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" +) + +func init() { + Commands = append(Commands, &commandFsConfigure{}) +} + +type commandFsConfigure struct { +} + +func (c *commandFsConfigure) Name() string { + return "fs.configure" +} + +func (c *commandFsConfigure) Help() string { + return `configure and apply storage options for each location + + fs.configure -locationPrfix=/my/folder - + +` +} + +func (c *commandFsConfigure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + + fsConfigureCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) + locationPrefix := fsConfigureCommand.String("locationPrefix", "", "path prefix") + collection := fsConfigureCommand.String("collection", "", "assign writes to this colletion") + replication := fsConfigureCommand.String("replication", "", "assign writes with this replication") + ttl := fsConfigureCommand.String("ttl", "", "assign writes with this ttl") + fsync := fsConfigureCommand.Bool("fsync", false, "fsync for the writes") + apply := fsConfigureCommand.Bool("apply", false, "update and apply filer configuration") + if err = fsConfigureCommand.Parse(args); err != nil { + return nil + } + + var buf bytes.Buffer + if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { + + request := &filer_pb.LookupDirectoryEntryRequest{ + Directory: filer.DirectoryEtc, + Name: filer.FilerConfName, + } + respLookupEntry, err := filer_pb.LookupEntry(client, request) + if err != nil { + return err + } + + return filer.StreamContent(commandEnv.MasterClient, &buf, respLookupEntry.Entry.Chunks, 0, math.MaxInt64) + + }); err != nil { + return err + } + + fc := filer.NewFilerConf() + if err = fc.LoadFromBytes(buf.Bytes()); err != nil { + return err + } + + if *locationPrefix != "" { + locConf := &filer_pb.FilerConf_PathConf{ + LocationPrefix: *locationPrefix, + Collection: *collection, + Replication: *replication, + Ttl: *ttl, + Fsync: *fsync, + } + fc.AddLocationConf(locConf) + } + + fc.ToText(writer) + + if *apply { + + } + + return nil + +}