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.

144 lines
4.4 KiB

7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 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. util.OnInterrupt(func() {
  88. fs.filer.Shutdown()
  89. })
  90. return fs, nil
  91. }
  92. func maybeStartMetrics(fs *FilerServer, option *FilerOption) {
  93. isConnected := false
  94. var metricsAddress string
  95. var metricsIntervalSec int
  96. var readErr error
  97. for !isConnected {
  98. metricsAddress, metricsIntervalSec, readErr = readFilerConfiguration(fs.grpcDialOption, option.Masters[0])
  99. if readErr == nil {
  100. isConnected = true
  101. } else {
  102. time.Sleep(7 * time.Second)
  103. }
  104. }
  105. if metricsAddress == "" && metricsIntervalSec <= 0 {
  106. return
  107. }
  108. go stats.LoopPushingMetric("filer", stats.SourceName(option.Port), stats.FilerGather,
  109. func() (addr string, intervalSeconds int) {
  110. return metricsAddress, metricsIntervalSec
  111. })
  112. }
  113. func readFilerConfiguration(grpcDialOption grpc.DialOption, masterGrpcAddress string) (metricsAddress string, metricsIntervalSec int, err error) {
  114. err = operation.WithMasterServerClient(masterGrpcAddress, grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  115. resp, err := masterClient.GetMasterConfiguration(context.Background(), &master_pb.GetMasterConfigurationRequest{})
  116. if err != nil {
  117. return fmt.Errorf("get master %s configuration: %v", masterGrpcAddress, err)
  118. }
  119. metricsAddress, metricsIntervalSec = resp.MetricsAddress, int(resp.MetricsIntervalSeconds)
  120. return nil
  121. })
  122. return
  123. }