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.

148 lines
3.4 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. return fc.loadFromChunks(filer, entry.Chunks)
  37. }
  38. func (fc *FilerConf) loadFromChunks(filer *Filer, chunks []*filer_pb.FileChunk) (err error) {
  39. data, err := filer.readEntry(chunks)
  40. if err != nil {
  41. glog.Errorf("read filer conf content: %v", err)
  42. return
  43. }
  44. return fc.LoadFromBytes(data)
  45. }
  46. func (fc *FilerConf) LoadFromBytes(data []byte) (err error) {
  47. conf := &filer_pb.FilerConf{}
  48. if err := jsonpb.Unmarshal(bytes.NewReader(data), conf); err != nil {
  49. err = proto.UnmarshalText(string(data), conf)
  50. if err != nil {
  51. glog.Errorf("unable to parse filer conf: %v", err)
  52. // this is not recoverable
  53. return nil
  54. }
  55. return nil
  56. }
  57. return fc.doLoadConf(conf)
  58. }
  59. func (fc *FilerConf) doLoadConf(conf *filer_pb.FilerConf) (err error) {
  60. for _, location := range conf.Locations {
  61. err = fc.AddLocationConf(location)
  62. if err != nil {
  63. // this is not recoverable
  64. return nil
  65. }
  66. }
  67. return nil
  68. }
  69. func (fc *FilerConf) AddLocationConf(locConf *filer_pb.FilerConf_PathConf) (err error) {
  70. err = fc.rules.Put([]byte(locConf.LocationPrefix), locConf)
  71. if err != nil {
  72. glog.Errorf("put location prefix: %v", err)
  73. }
  74. return
  75. }
  76. func (fc *FilerConf) DeleteLocationConf(locationPrefix string) {
  77. rules := ptrie.New()
  78. fc.rules.Walk(func(key []byte, value interface{}) bool {
  79. if string(key) == locationPrefix {
  80. return true
  81. }
  82. rules.Put(key, value)
  83. return true
  84. })
  85. fc.rules = rules
  86. return
  87. }
  88. func (fc *FilerConf) MatchStorageRule(path string) (pathConf *filer_pb.FilerConf_PathConf) {
  89. pathConf = &filer_pb.FilerConf_PathConf{}
  90. fc.rules.MatchPrefix([]byte(path), func(key []byte, value interface{}) bool {
  91. t := value.(*filer_pb.FilerConf_PathConf)
  92. mergePathConf(pathConf, t)
  93. return true
  94. })
  95. return pathConf
  96. }
  97. // merge if values in b is not empty, merge them into a
  98. func mergePathConf(a, b *filer_pb.FilerConf_PathConf) {
  99. a.Collection = util.Nvl(b.Collection, a.Collection)
  100. a.Replication = util.Nvl(b.Replication, a.Replication)
  101. a.Ttl = util.Nvl(b.Ttl, a.Ttl)
  102. if b.DiskType != filer_pb.FilerConf_PathConf_NONE {
  103. a.DiskType = b.DiskType
  104. }
  105. a.Fsync = b.Fsync || a.Fsync
  106. if b.VolumeGrowthCount > 0 {
  107. a.VolumeGrowthCount = b.VolumeGrowthCount
  108. }
  109. }
  110. func (fc *FilerConf) ToProto() *filer_pb.FilerConf {
  111. m := &filer_pb.FilerConf{}
  112. fc.rules.Walk(func(key []byte, value interface{}) bool {
  113. pathConf := value.(*filer_pb.FilerConf_PathConf)
  114. m.Locations = append(m.Locations, pathConf)
  115. return true
  116. })
  117. return m
  118. }
  119. func (fc *FilerConf) ToText(writer io.Writer) error {
  120. m := jsonpb.Marshaler{
  121. EmitDefaults: false,
  122. Indent: " ",
  123. }
  124. return m.Marshal(writer, fc.ToProto())
  125. }