configuration.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package filer
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/glog"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. "os"
  6. "reflect"
  7. "strings"
  8. )
  9. var (
  10. Stores []FilerStore
  11. )
  12. func (f *Filer) LoadConfiguration(config *util.ViperProxy) {
  13. validateOneEnabledStore(config)
  14. // load configuration for default filer store
  15. hasDefaultStoreConfigured := false
  16. for _, store := range Stores {
  17. if config.GetBool(store.GetName() + ".enabled") {
  18. store = reflect.New(reflect.ValueOf(store).Elem().Type()).Interface().(FilerStore)
  19. if err := store.Initialize(config, store.GetName()+"."); err != nil {
  20. glog.Fatalf("failed to initialize store for %s: %+v", store.GetName(), err)
  21. }
  22. f.SetStore(store)
  23. glog.V(0).Infof("configured filer store to %s", store.GetName())
  24. hasDefaultStoreConfigured = true
  25. break
  26. }
  27. }
  28. if !hasDefaultStoreConfigured {
  29. println()
  30. println("Supported filer stores are:")
  31. for _, store := range Stores {
  32. println(" " + store.GetName())
  33. }
  34. os.Exit(-1)
  35. }
  36. // load path-specific filer store here
  37. // f.Store.AddPathSpecificStore(path, store)
  38. storeNames := make(map[string]FilerStore)
  39. for _, store := range Stores {
  40. storeNames[store.GetName()] = store
  41. }
  42. allKeys := config.AllKeys()
  43. for _, key := range allKeys {
  44. if !strings.HasSuffix(key, ".enabled") {
  45. continue
  46. }
  47. key = key[:len(key)-len(".enabled")]
  48. if !strings.Contains(key, ".") {
  49. continue
  50. }
  51. parts := strings.Split(key, ".")
  52. storeName, storeId := parts[0], parts[1]
  53. store, found := storeNames[storeName]
  54. if !found {
  55. continue
  56. }
  57. store = reflect.New(reflect.ValueOf(store).Elem().Type()).Interface().(FilerStore)
  58. if err := store.Initialize(config, key+"."); err != nil {
  59. glog.Fatalf("Failed to initialize store for %s: %+v", key, err)
  60. }
  61. location := config.GetString(key + ".location")
  62. if location == "" {
  63. glog.Errorf("path-specific filer store needs %s", key+".location")
  64. os.Exit(-1)
  65. }
  66. f.Store.AddPathSpecificStore(location, storeId, store)
  67. glog.V(0).Infof("configure filer %s for %s", store.GetName(), location)
  68. }
  69. }
  70. func validateOneEnabledStore(config *util.ViperProxy) {
  71. enabledStore := ""
  72. for _, store := range Stores {
  73. if config.GetBool(store.GetName() + ".enabled") {
  74. if enabledStore == "" {
  75. enabledStore = store.GetName()
  76. } else {
  77. glog.Fatalf("Filer store is enabled for both %s and %s", enabledStore, store.GetName())
  78. }
  79. }
  80. }
  81. }