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/filer2/tikv"
  22. "github.com/chrislusf/seaweedfs/weed/glog"
  23. "github.com/chrislusf/seaweedfs/weed/notification"
  24. _ "github.com/chrislusf/seaweedfs/weed/notification/aws_sqs"
  25. _ "github.com/chrislusf/seaweedfs/weed/notification/gocdk_pub_sub"
  26. _ "github.com/chrislusf/seaweedfs/weed/notification/google_pub_sub"
  27. _ "github.com/chrislusf/seaweedfs/weed/notification/kafka"
  28. _ "github.com/chrislusf/seaweedfs/weed/notification/log"
  29. "github.com/chrislusf/seaweedfs/weed/security"
  30. )
  31. type FilerOption struct {
  32. Masters []string
  33. Collection string
  34. DefaultReplication string
  35. RedirectOnRead bool
  36. DisableDirListing bool
  37. MaxMB int
  38. DirListingLimit int
  39. DataCenter string
  40. DefaultLevelDbDir string
  41. DisableHttp bool
  42. Port uint32
  43. recursiveDelete bool
  44. }
  45. type FilerServer struct {
  46. option *FilerOption
  47. secret security.SigningKey
  48. filer *filer2.Filer
  49. grpcDialOption grpc.DialOption
  50. }
  51. func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) (fs *FilerServer, err error) {
  52. fs = &FilerServer{
  53. option: option,
  54. grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.filer"),
  55. }
  56. if len(option.Masters) == 0 {
  57. glog.Fatal("master list is required!")
  58. }
  59. fs.filer = filer2.NewFiler(option.Masters, fs.grpcDialOption, option.Port+10000)
  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. }