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.

121 lines
3.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package backend
  2. import (
  3. "io"
  4. "os"
  5. "strings"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  9. "github.com/spf13/viper"
  10. )
  11. type BackendStorageFile interface {
  12. io.ReaderAt
  13. io.WriterAt
  14. Truncate(off int64) error
  15. io.Closer
  16. GetStat() (datSize int64, modTime time.Time, err error)
  17. String() string
  18. Instantiate(src *os.File) error
  19. }
  20. type BackendStorage interface {
  21. ToProperties() map[string]string
  22. NewStorageFile(key string) BackendStorageFile
  23. }
  24. type StringProperties interface {
  25. GetString(key string) string
  26. }
  27. type StorageType string
  28. type BackendStorageFactory interface {
  29. StorageType() StorageType
  30. BuildStorage(configuration StringProperties, id string) (BackendStorage, error)
  31. }
  32. var (
  33. BackendStorageFactories = make(map[StorageType]BackendStorageFactory)
  34. BackendStorages = make(map[string]BackendStorage)
  35. )
  36. func LoadConfiguration(config *viper.Viper) {
  37. StorageBackendPrefix := "storage.backend"
  38. backendSub := config.Sub(StorageBackendPrefix)
  39. for backendTypeName, _ := range config.GetStringMap(StorageBackendPrefix) {
  40. backendStorageFactory, found := BackendStorageFactories[StorageType(backendTypeName)]
  41. if !found {
  42. glog.Fatalf("backend storage type %s not found", backendTypeName)
  43. }
  44. backendTypeSub := backendSub.Sub(backendTypeName)
  45. for backendStorageId, _ := range backendSub.GetStringMap(backendTypeName) {
  46. if !backendTypeSub.GetBool(backendStorageId + ".enabled") {
  47. continue
  48. }
  49. backendStorage, buildErr := backendStorageFactory.BuildStorage(backendTypeSub.Sub(backendStorageId), backendStorageId)
  50. if buildErr != nil {
  51. glog.Fatalf("fail to create backend storage %s.%s", backendTypeName, backendStorageId)
  52. }
  53. BackendStorages[backendTypeName+"."+backendStorageId] = backendStorage
  54. if backendStorageId == "default" {
  55. BackendStorages[backendTypeName] = backendStorage
  56. }
  57. }
  58. }
  59. }
  60. func LoadFromPbStorageBackends(storageBackends []*master_pb.StorageBackend) {
  61. for _, storageBackend := range storageBackends {
  62. backendStorageFactory, found := BackendStorageFactories[StorageType(storageBackend.Type)]
  63. if !found {
  64. glog.Warningf("storage type %s not found", storageBackend.Type)
  65. continue
  66. }
  67. backendStorage, buildErr := backendStorageFactory.BuildStorage(newProperties(storageBackend.Properties), storageBackend.Id)
  68. if buildErr != nil {
  69. glog.Fatalf("fail to create backend storage %s.%s", storageBackend.Type, storageBackend.Id)
  70. }
  71. BackendStorages[storageBackend.Type+"."+storageBackend.Id] = backendStorage
  72. if storageBackend.Id == "default" {
  73. BackendStorages[storageBackend.Type] = backendStorage
  74. }
  75. }
  76. }
  77. type Properties struct {
  78. m map[string]string
  79. }
  80. func newProperties(m map[string]string) *Properties {
  81. return &Properties{m: m}
  82. }
  83. func (p *Properties) GetString(key string) string {
  84. if v, found := p.m[key]; found {
  85. return v
  86. }
  87. return ""
  88. }
  89. func ToPbStorageBackends() (backends []*master_pb.StorageBackend) {
  90. for sName, s := range BackendStorages {
  91. parts := strings.Split(sName, ".")
  92. if len(parts) != 2 {
  93. continue
  94. }
  95. sType, sId := parts[0], parts[1]
  96. backends = append(backends, &master_pb.StorageBackend{
  97. Type: sType,
  98. Id: sId,
  99. Properties: s.ToProperties(),
  100. })
  101. }
  102. return
  103. }