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.

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