configuration.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package filer
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/glog"
  4. "github.com/seaweedfs/seaweedfs/weed/util"
  5. "os"
  6. "reflect"
  7. "strings"
  8. )
  9. var (
  10. Stores []FilerStore
  11. )
  12. func (f *Filer) LoadConfiguration(config *util.ViperProxy) (isFresh bool) {
  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. isFresh = 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 the following. If not found, check the full version.")
  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. if !config.GetBool(key + ".enabled") {
  58. continue
  59. }
  60. store = reflect.New(reflect.ValueOf(store).Elem().Type()).Interface().(FilerStore)
  61. if err := store.Initialize(config, key+"."); err != nil {
  62. glog.Fatalf("Failed to initialize store for %s: %+v", key, err)
  63. }
  64. location := config.GetString(key + ".location")
  65. if location == "" {
  66. glog.Errorf("path-specific filer store needs %s", key+".location")
  67. os.Exit(-1)
  68. }
  69. f.Store.AddPathSpecificStore(location, storeId, store)
  70. glog.V(0).Infof("configure filer %s for %s", store.GetName(), location)
  71. }
  72. return
  73. }
  74. func validateOneEnabledStore(config *util.ViperProxy) {
  75. enabledStore := ""
  76. for _, store := range Stores {
  77. if config.GetBool(store.GetName() + ".enabled") {
  78. if enabledStore == "" {
  79. enabledStore = store.GetName()
  80. } else {
  81. glog.Fatalf("Filer store is enabled for both %s and %s", enabledStore, store.GetName())
  82. }
  83. }
  84. }
  85. }