You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.8 KiB

4 years ago
4 years ago
4 years ago
  1. package filer
  2. import (
  3. "context"
  4. "io"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. "github.com/golang/protobuf/proto"
  9. "github.com/viant/ptrie"
  10. )
  11. const (
  12. DirectoryEtc = "/etc"
  13. FilerConfName = "filer.conf"
  14. )
  15. type FilerConf struct {
  16. rules ptrie.Trie
  17. }
  18. func NewFilerConf() (fc *FilerConf) {
  19. fc = &FilerConf{
  20. rules: ptrie.New(),
  21. }
  22. return fc
  23. }
  24. func (fc *FilerConf) loadFromFiler(filer *Filer) (err error) {
  25. filerConfPath := util.NewFullPath(DirectoryEtc, FilerConfName)
  26. entry, err := filer.FindEntry(context.Background(), filerConfPath)
  27. if err != nil {
  28. if err == filer_pb.ErrNotFound {
  29. return nil
  30. }
  31. glog.Errorf("read filer conf entry %s: %v", filerConfPath, err)
  32. return
  33. }
  34. return fc.loadFromChunks(filer, entry.Chunks)
  35. }
  36. func (fc *FilerConf) loadFromChunks(filer *Filer, chunks []*filer_pb.FileChunk) (err error) {
  37. data, err := filer.readEntry(chunks)
  38. if err != nil {
  39. glog.Errorf("read filer conf content: %v", err)
  40. return
  41. }
  42. return fc.LoadFromBytes(data)
  43. }
  44. func (fc *FilerConf) LoadFromBytes(data []byte) (err error) {
  45. conf := &filer_pb.FilerConf{}
  46. err = proto.UnmarshalText(string(data), conf)
  47. if err != nil {
  48. glog.Errorf("unable to parse filer conf: %v", err)
  49. // this is not recoverable
  50. return nil
  51. }
  52. return fc.doLoadConf(conf)
  53. }
  54. func (fc *FilerConf) doLoadConf(conf *filer_pb.FilerConf) (err error) {
  55. for _, location := range conf.Locations {
  56. err = fc.AddLocationConf(location)
  57. if err != nil {
  58. // this is not recoverable
  59. return nil
  60. }
  61. }
  62. return nil
  63. }
  64. func (fc *FilerConf) AddLocationConf(locConf *filer_pb.FilerConf_PathConf) (err error) {
  65. err = fc.rules.Put([]byte(locConf.LocationPrefix), locConf)
  66. if err != nil {
  67. glog.Errorf("put location prefix: %v", err)
  68. }
  69. return
  70. }
  71. func (fc *FilerConf) DeleteLocationConf(locationPrefix string) {
  72. rules := ptrie.New()
  73. fc.rules.Walk(func(key []byte, value interface{}) bool {
  74. if string(key) == locationPrefix {
  75. return true
  76. }
  77. rules.Put(key, value)
  78. return true
  79. })
  80. fc.rules = rules
  81. return
  82. }
  83. var (
  84. EmptyFilerConfPathConf = &filer_pb.FilerConf_PathConf{}
  85. )
  86. func (fc *FilerConf) MatchStorageRule(path string) (pathConf *filer_pb.FilerConf_PathConf) {
  87. fc.rules.MatchPrefix([]byte(path), func(key []byte, value interface{}) bool {
  88. pathConf = value.(*filer_pb.FilerConf_PathConf)
  89. return true
  90. })
  91. if pathConf == nil {
  92. return EmptyFilerConfPathConf
  93. }
  94. return pathConf
  95. }
  96. func (fc *FilerConf) ToProto() *filer_pb.FilerConf {
  97. m := &filer_pb.FilerConf{}
  98. fc.rules.Walk(func(key []byte, value interface{}) bool {
  99. pathConf := value.(*filer_pb.FilerConf_PathConf)
  100. m.Locations = append(m.Locations, pathConf)
  101. return true
  102. })
  103. return m
  104. }
  105. func (fc *FilerConf) ToText(writer io.Writer) error {
  106. return proto.MarshalText(writer, fc.ToProto())
  107. }