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.

145 lines
3.3 KiB

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