filer_conf.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. DirectoryEtcRemote = "/etc/remote"
  16. FilerConfName = "filer.conf"
  17. IamConfigDirecotry = "/etc/iam"
  18. IamIdentityFile = "identity.json"
  19. IamPoliciesFile = "policies.json"
  20. )
  21. type FilerConf struct {
  22. rules ptrie.Trie
  23. }
  24. func NewFilerConf() (fc *FilerConf) {
  25. fc = &FilerConf{
  26. rules: ptrie.New(),
  27. }
  28. return fc
  29. }
  30. func (fc *FilerConf) loadFromFiler(filer *Filer) (err error) {
  31. filerConfPath := util.NewFullPath(DirectoryEtcSeaweedFS, FilerConfName)
  32. entry, err := filer.FindEntry(context.Background(), filerConfPath)
  33. if err != nil {
  34. if err == filer_pb.ErrNotFound {
  35. return nil
  36. }
  37. glog.Errorf("read filer conf entry %s: %v", filerConfPath, err)
  38. return
  39. }
  40. if len(entry.Content) > 0 {
  41. return fc.LoadFromBytes(entry.Content)
  42. }
  43. return fc.loadFromChunks(filer, entry.Content, entry.Chunks)
  44. }
  45. func (fc *FilerConf) loadFromChunks(filer *Filer, content []byte, chunks []*filer_pb.FileChunk) (err error) {
  46. if len(content) == 0 {
  47. content, err = filer.readEntry(chunks)
  48. if err != nil {
  49. glog.Errorf("read filer conf content: %v", err)
  50. return
  51. }
  52. }
  53. return fc.LoadFromBytes(content)
  54. }
  55. func (fc *FilerConf) LoadFromBytes(data []byte) (err error) {
  56. conf := &filer_pb.FilerConf{}
  57. if err := jsonpb.Unmarshal(bytes.NewReader(data), conf); err != nil {
  58. return err
  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 != "" {
  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. if b.ReadOnly {
  113. a.ReadOnly = b.ReadOnly
  114. }
  115. }
  116. func (fc *FilerConf) ToProto() *filer_pb.FilerConf {
  117. m := &filer_pb.FilerConf{}
  118. fc.rules.Walk(func(key []byte, value interface{}) bool {
  119. pathConf := value.(*filer_pb.FilerConf_PathConf)
  120. m.Locations = append(m.Locations, pathConf)
  121. return true
  122. })
  123. return m
  124. }
  125. func (fc *FilerConf) ToText(writer io.Writer) error {
  126. m := jsonpb.Marshaler{
  127. EmitDefaults: false,
  128. Indent: " ",
  129. }
  130. return m.Marshal(writer, fc.ToProto())
  131. }