filer_conf.go 3.5 KB

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