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.

187 lines
5.8 KiB

6 years ago
6 years ago
4 years ago
3 years ago
4 years ago
6 years ago
6 years ago
7 years ago
4 years ago
5 years ago
4 years ago
4 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "net/http"
  7. "os"
  8. "sync"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/stats"
  11. "google.golang.org/grpc"
  12. "github.com/chrislusf/seaweedfs/weed/util/grace"
  13. "github.com/chrislusf/seaweedfs/weed/operation"
  14. "github.com/chrislusf/seaweedfs/weed/pb"
  15. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  16. "github.com/chrislusf/seaweedfs/weed/util"
  17. "github.com/chrislusf/seaweedfs/weed/filer"
  18. _ "github.com/chrislusf/seaweedfs/weed/filer/cassandra"
  19. _ "github.com/chrislusf/seaweedfs/weed/filer/elastic/v7"
  20. _ "github.com/chrislusf/seaweedfs/weed/filer/etcd"
  21. _ "github.com/chrislusf/seaweedfs/weed/filer/hbase"
  22. _ "github.com/chrislusf/seaweedfs/weed/filer/leveldb"
  23. _ "github.com/chrislusf/seaweedfs/weed/filer/leveldb2"
  24. _ "github.com/chrislusf/seaweedfs/weed/filer/leveldb3"
  25. _ "github.com/chrislusf/seaweedfs/weed/filer/mongodb"
  26. _ "github.com/chrislusf/seaweedfs/weed/filer/mysql"
  27. _ "github.com/chrislusf/seaweedfs/weed/filer/mysql2"
  28. _ "github.com/chrislusf/seaweedfs/weed/filer/postgres"
  29. _ "github.com/chrislusf/seaweedfs/weed/filer/postgres2"
  30. _ "github.com/chrislusf/seaweedfs/weed/filer/redis"
  31. _ "github.com/chrislusf/seaweedfs/weed/filer/redis2"
  32. _ "github.com/chrislusf/seaweedfs/weed/filer/redis3"
  33. _ "github.com/chrislusf/seaweedfs/weed/filer/sqlite"
  34. "github.com/chrislusf/seaweedfs/weed/glog"
  35. "github.com/chrislusf/seaweedfs/weed/notification"
  36. _ "github.com/chrislusf/seaweedfs/weed/notification/aws_sqs"
  37. _ "github.com/chrislusf/seaweedfs/weed/notification/gocdk_pub_sub"
  38. _ "github.com/chrislusf/seaweedfs/weed/notification/google_pub_sub"
  39. _ "github.com/chrislusf/seaweedfs/weed/notification/kafka"
  40. _ "github.com/chrislusf/seaweedfs/weed/notification/log"
  41. "github.com/chrislusf/seaweedfs/weed/security"
  42. )
  43. type FilerOption struct {
  44. Masters []pb.ServerAddress
  45. Collection string
  46. DefaultReplication string
  47. DisableDirListing bool
  48. MaxMB int
  49. DirListingLimit int
  50. DataCenter string
  51. Rack string
  52. DataNode string
  53. DefaultLevelDbDir string
  54. DisableHttp bool
  55. Host pb.ServerAddress
  56. recursiveDelete bool
  57. Cipher bool
  58. SaveToFilerLimit int64
  59. ConcurrentUploadLimit int64
  60. }
  61. type FilerServer struct {
  62. filer_pb.UnimplementedSeaweedFilerServer
  63. option *FilerOption
  64. secret security.SigningKey
  65. filer *filer.Filer
  66. grpcDialOption grpc.DialOption
  67. // metrics read from the master
  68. metricsAddress string
  69. metricsIntervalSec int
  70. // notifying clients
  71. listenersLock sync.Mutex
  72. listenersCond *sync.Cond
  73. brokers map[string]map[string]bool
  74. brokersLock sync.Mutex
  75. inFlightDataSize int64
  76. inFlightDataLimitCond *sync.Cond
  77. }
  78. func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) (fs *FilerServer, err error) {
  79. fs = &FilerServer{
  80. option: option,
  81. grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.filer"),
  82. brokers: make(map[string]map[string]bool),
  83. inFlightDataLimitCond: sync.NewCond(new(sync.Mutex)),
  84. }
  85. fs.listenersCond = sync.NewCond(&fs.listenersLock)
  86. if len(option.Masters) == 0 {
  87. glog.Fatal("master list is required!")
  88. }
  89. fs.filer = filer.NewFiler(option.Masters, fs.grpcDialOption, option.Host, option.Collection, option.DefaultReplication, option.DataCenter, func() {
  90. fs.listenersCond.Broadcast()
  91. })
  92. fs.filer.Cipher = option.Cipher
  93. fs.checkWithMaster()
  94. go stats.LoopPushingMetric("filer", string(fs.option.Host), fs.metricsAddress, fs.metricsIntervalSec)
  95. go fs.filer.KeepMasterClientConnected()
  96. v := util.GetViper()
  97. if !util.LoadConfiguration("filer", false) {
  98. v.Set("leveldb2.enabled", true)
  99. v.Set("leveldb2.dir", option.DefaultLevelDbDir)
  100. _, err := os.Stat(option.DefaultLevelDbDir)
  101. if os.IsNotExist(err) {
  102. os.MkdirAll(option.DefaultLevelDbDir, 0755)
  103. }
  104. glog.V(0).Infof("default to create filer store dir in %s", option.DefaultLevelDbDir)
  105. } else {
  106. glog.Warningf("skipping default store dir in %s", option.DefaultLevelDbDir)
  107. }
  108. util.LoadConfiguration("notification", false)
  109. fs.option.recursiveDelete = v.GetBool("filer.options.recursive_delete")
  110. v.SetDefault("filer.options.buckets_folder", "/buckets")
  111. fs.filer.DirBucketsPath = v.GetString("filer.options.buckets_folder")
  112. // TODO deprecated, will be be removed after 2020-12-31
  113. // replaced by https://github.com/chrislusf/seaweedfs/wiki/Path-Specific-Configuration
  114. // fs.filer.FsyncBuckets = v.GetStringSlice("filer.options.buckets_fsync")
  115. fs.filer.LoadConfiguration(v)
  116. notification.LoadConfiguration(v, "notification.")
  117. handleStaticResources(defaultMux)
  118. if !option.DisableHttp {
  119. defaultMux.HandleFunc("/", fs.filerHandler)
  120. }
  121. if defaultMux != readonlyMux {
  122. handleStaticResources(readonlyMux)
  123. readonlyMux.HandleFunc("/", fs.readonlyFilerHandler)
  124. }
  125. fs.filer.AggregateFromPeers(option.Host)
  126. fs.filer.LoadBuckets()
  127. fs.filer.LoadFilerConf()
  128. fs.filer.LoadRemoteStorageConfAndMapping()
  129. grace.OnInterrupt(func() {
  130. fs.filer.Shutdown()
  131. })
  132. return fs, nil
  133. }
  134. func (fs *FilerServer) checkWithMaster() {
  135. isConnected := false
  136. for !isConnected {
  137. for _, master := range fs.option.Masters {
  138. readErr := operation.WithMasterServerClient(master, fs.grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  139. resp, err := masterClient.GetMasterConfiguration(context.Background(), &master_pb.GetMasterConfigurationRequest{})
  140. if err != nil {
  141. return fmt.Errorf("get master %s configuration: %v", master, err)
  142. }
  143. fs.metricsAddress, fs.metricsIntervalSec = resp.MetricsAddress, int(resp.MetricsIntervalSeconds)
  144. if fs.option.DefaultReplication == "" {
  145. fs.option.DefaultReplication = resp.DefaultReplication
  146. }
  147. return nil
  148. })
  149. if readErr == nil {
  150. isConnected = true
  151. } else {
  152. time.Sleep(7 * time.Second)
  153. }
  154. }
  155. }
  156. }