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