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.

143 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
  1. package filer
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/golang/protobuf/jsonpb"
  10. "github.com/viant/ptrie"
  11. )
  12. const (
  13. DirectoryEtc = "/etc"
  14. FilerConfName = "filer.conf"
  15. )
  16. type FilerConf struct {
  17. rules ptrie.Trie
  18. }
  19. func NewFilerConf() (fc *FilerConf) {
  20. fc = &FilerConf{
  21. rules: ptrie.New(),
  22. }
  23. return fc
  24. }
  25. func (fc *FilerConf) loadFromFiler(filer *Filer) (err error) {
  26. filerConfPath := util.NewFullPath(DirectoryEtc, FilerConfName)
  27. entry, err := filer.FindEntry(context.Background(), filerConfPath)
  28. if err != nil {
  29. if err == filer_pb.ErrNotFound {
  30. return nil
  31. }
  32. glog.Errorf("read filer conf entry %s: %v", filerConfPath, err)
  33. return
  34. }
  35. if len(entry.Content) > 0 {
  36. return fc.LoadFromBytes(entry.Content)
  37. }
  38. return fc.loadFromChunks(filer, entry.Chunks)
  39. }
  40. func (fc *FilerConf) loadFromChunks(filer *Filer, chunks []*filer_pb.FileChunk) (err error) {
  41. data, err := filer.readEntry(chunks)
  42. if err != nil {
  43. glog.Errorf("read filer conf content: %v", err)
  44. return
  45. }
  46. return fc.LoadFromBytes(data)
  47. }
  48. func (fc *FilerConf) LoadFromBytes(data []byte) (err error) {
  49. conf := &filer_pb.FilerConf{}
  50. if err := jsonpb.Unmarshal(bytes.NewReader(data), conf); err != nil {
  51. return err
  52. }
  53. return fc.doLoadConf(conf)
  54. }
  55. func (fc *FilerConf) doLoadConf(conf *filer_pb.FilerConf) (err error) {
  56. for _, location := range conf.Locations {
  57. err = fc.AddLocationConf(location)
  58. if err != nil {
  59. // this is not recoverable
  60. return nil
  61. }
  62. }
  63. return nil
  64. }
  65. func (fc *FilerConf) AddLocationConf(locConf *filer_pb.FilerConf_PathConf) (err error) {
  66. err = fc.rules.Put([]byte(locConf.LocationPrefix), locConf)
  67. if err != nil {
  68. glog.Errorf("put location prefix: %v", err)
  69. }
  70. return
  71. }
  72. func (fc *FilerConf) DeleteLocationConf(locationPrefix string) {
  73. rules := ptrie.New()
  74. fc.rules.Walk(func(key []byte, value interface{}) bool {
  75. if string(key) == locationPrefix {
  76. return true
  77. }
  78. rules.Put(key, value)
  79. return true
  80. })
  81. fc.rules = rules
  82. return
  83. }
  84. func (fc *FilerConf) MatchStorageRule(path string) (pathConf *filer_pb.FilerConf_PathConf) {
  85. pathConf = &filer_pb.FilerConf_PathConf{}
  86. fc.rules.MatchPrefix([]byte(path), func(key []byte, value interface{}) bool {
  87. t := value.(*filer_pb.FilerConf_PathConf)
  88. mergePathConf(pathConf, t)
  89. return true
  90. })
  91. return pathConf
  92. }
  93. // merge if values in b is not empty, merge them into a
  94. func mergePathConf(a, b *filer_pb.FilerConf_PathConf) {
  95. a.Collection = util.Nvl(b.Collection, a.Collection)
  96. a.Replication = util.Nvl(b.Replication, a.Replication)
  97. a.Ttl = util.Nvl(b.Ttl, a.Ttl)
  98. if b.DiskType != filer_pb.FilerConf_PathConf_NONE {
  99. a.DiskType = b.DiskType
  100. }
  101. a.Fsync = b.Fsync || a.Fsync
  102. if b.VolumeGrowthCount > 0 {
  103. a.VolumeGrowthCount = b.VolumeGrowthCount
  104. }
  105. }
  106. func (fc *FilerConf) ToProto() *filer_pb.FilerConf {
  107. m := &filer_pb.FilerConf{}
  108. fc.rules.Walk(func(key []byte, value interface{}) bool {
  109. pathConf := value.(*filer_pb.FilerConf_PathConf)
  110. m.Locations = append(m.Locations, pathConf)
  111. return true
  112. })
  113. return m
  114. }
  115. func (fc *FilerConf) ToText(writer io.Writer) error {
  116. m := jsonpb.Marshaler{
  117. EmitDefaults: false,
  118. Indent: " ",
  119. }
  120. return m.Marshal(writer, fc.ToProto())
  121. }