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.

140 lines
4.3 KiB

6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "time"
  8. "google.golang.org/grpc"
  9. "github.com/chrislusf/seaweedfs/weed/operation"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/stats"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. "github.com/chrislusf/seaweedfs/weed/filer2"
  14. _ "github.com/chrislusf/seaweedfs/weed/filer2/cassandra"
  15. _ "github.com/chrislusf/seaweedfs/weed/filer2/etcd"
  16. _ "github.com/chrislusf/seaweedfs/weed/filer2/leveldb"
  17. _ "github.com/chrislusf/seaweedfs/weed/filer2/leveldb2"
  18. _ "github.com/chrislusf/seaweedfs/weed/filer2/mysql"
  19. _ "github.com/chrislusf/seaweedfs/weed/filer2/postgres"
  20. _ "github.com/chrislusf/seaweedfs/weed/filer2/redis"
  21. "github.com/chrislusf/seaweedfs/weed/glog"
  22. "github.com/chrislusf/seaweedfs/weed/notification"
  23. _ "github.com/chrislusf/seaweedfs/weed/notification/aws_sqs"
  24. _ "github.com/chrislusf/seaweedfs/weed/notification/gocdk_pub_sub"
  25. _ "github.com/chrislusf/seaweedfs/weed/notification/google_pub_sub"
  26. _ "github.com/chrislusf/seaweedfs/weed/notification/kafka"
  27. _ "github.com/chrislusf/seaweedfs/weed/notification/log"
  28. "github.com/chrislusf/seaweedfs/weed/security"
  29. )
  30. type FilerOption struct {
  31. Masters []string
  32. Collection string
  33. DefaultReplication string
  34. DisableDirListing bool
  35. MaxMB int
  36. DirListingLimit int
  37. DataCenter string
  38. DefaultLevelDbDir string
  39. DisableHttp bool
  40. Port uint32
  41. recursiveDelete bool
  42. Cipher bool
  43. }
  44. type FilerServer struct {
  45. option *FilerOption
  46. secret security.SigningKey
  47. filer *filer2.Filer
  48. grpcDialOption grpc.DialOption
  49. }
  50. func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) (fs *FilerServer, err error) {
  51. fs = &FilerServer{
  52. option: option,
  53. grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.filer"),
  54. }
  55. if len(option.Masters) == 0 {
  56. glog.Fatal("master list is required!")
  57. }
  58. fs.filer = filer2.NewFiler(option.Masters, fs.grpcDialOption, option.Port+10000)
  59. fs.filer.Cipher = option.Cipher
  60. go fs.filer.KeepConnectedToMaster()
  61. v := util.GetViper()
  62. if !util.LoadConfiguration("filer", false) {
  63. v.Set("leveldb2.enabled", true)
  64. v.Set("leveldb2.dir", option.DefaultLevelDbDir)
  65. _, err := os.Stat(option.DefaultLevelDbDir)
  66. if os.IsNotExist(err) {
  67. os.MkdirAll(option.DefaultLevelDbDir, 0755)
  68. }
  69. }
  70. util.LoadConfiguration("notification", false)
  71. fs.option.recursiveDelete = v.GetBool("filer.options.recursive_delete")
  72. v.Set("filer.option.buckets_folder", "/buckets")
  73. v.Set("filer.option.queues_folder", "/queues")
  74. fs.filer.DirBucketsPath = v.GetString("filer.option.buckets_folder")
  75. fs.filer.DirQueuesPath = v.GetString("filer.option.queues_folder")
  76. fs.filer.LoadConfiguration(v)
  77. notification.LoadConfiguration(v, "notification.")
  78. handleStaticResources(defaultMux)
  79. if !option.DisableHttp {
  80. defaultMux.HandleFunc("/", fs.filerHandler)
  81. }
  82. if defaultMux != readonlyMux {
  83. readonlyMux.HandleFunc("/", fs.readonlyFilerHandler)
  84. }
  85. fs.filer.LoadBuckets(fs.filer.DirBucketsPath)
  86. maybeStartMetrics(fs, option)
  87. return fs, nil
  88. }
  89. func maybeStartMetrics(fs *FilerServer, option *FilerOption) {
  90. isConnected := false
  91. var metricsAddress string
  92. var metricsIntervalSec int
  93. var readErr error
  94. for !isConnected {
  95. metricsAddress, metricsIntervalSec, readErr = readFilerConfiguration(fs.grpcDialOption, option.Masters[0])
  96. if readErr == nil {
  97. isConnected = true
  98. } else {
  99. time.Sleep(7 * time.Second)
  100. }
  101. }
  102. if metricsAddress == "" && metricsIntervalSec <= 0 {
  103. return
  104. }
  105. go stats.LoopPushingMetric("filer", stats.SourceName(option.Port), stats.FilerGather,
  106. func() (addr string, intervalSeconds int) {
  107. return metricsAddress, metricsIntervalSec
  108. })
  109. }
  110. func readFilerConfiguration(grpcDialOption grpc.DialOption, masterGrpcAddress string) (metricsAddress string, metricsIntervalSec int, err error) {
  111. err = operation.WithMasterServerClient(masterGrpcAddress, grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  112. resp, err := masterClient.GetMasterConfiguration(context.Background(), &master_pb.GetMasterConfigurationRequest{})
  113. if err != nil {
  114. return fmt.Errorf("get master %s configuration: %v", masterGrpcAddress, err)
  115. }
  116. metricsAddress, metricsIntervalSec = resp.MetricsAddress, int(resp.MetricsIntervalSeconds)
  117. return nil
  118. })
  119. return
  120. }