filer_conf.go 3.4 KB

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