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.

142 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/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. Cipher bool
  45. }
  46. type FilerServer struct {
  47. option *FilerOption
  48. secret security.SigningKey
  49. filer *filer2.Filer
  50. grpcDialOption grpc.DialOption
  51. }
  52. func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) (fs *FilerServer, err error) {
  53. fs = &FilerServer{
  54. option: option,
  55. grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.filer"),
  56. }
  57. if len(option.Masters) == 0 {
  58. glog.Fatal("master list is required!")
  59. }
  60. fs.filer = filer2.NewFiler(option.Masters, fs.grpcDialOption, option.Port+10000)
  61. fs.filer.Cipher = option.Cipher
  62. go fs.filer.KeepConnectedToMaster()
  63. v := util.GetViper()
  64. if !util.LoadConfiguration("filer", false) {
  65. v.Set("leveldb2.enabled", true)
  66. v.Set("leveldb2.dir", option.DefaultLevelDbDir)
  67. _, err := os.Stat(option.DefaultLevelDbDir)
  68. if os.IsNotExist(err) {
  69. os.MkdirAll(option.DefaultLevelDbDir, 0755)
  70. }
  71. }
  72. util.LoadConfiguration("notification", false)
  73. fs.option.recursiveDelete = v.GetBool("filer.options.recursive_delete")
  74. v.Set("filer.option.buckets_folder", "/buckets")
  75. v.Set("filer.option.queues_folder", "/queues")
  76. fs.filer.DirBucketsPath = v.GetString("filer.option.buckets_folder")
  77. fs.filer.DirQueuesPath = v.GetString("filer.option.queues_folder")
  78. fs.filer.LoadConfiguration(v)
  79. notification.LoadConfiguration(v, "notification.")
  80. handleStaticResources(defaultMux)
  81. if !option.DisableHttp {
  82. defaultMux.HandleFunc("/", fs.filerHandler)
  83. }
  84. if defaultMux != readonlyMux {
  85. readonlyMux.HandleFunc("/", fs.readonlyFilerHandler)
  86. }
  87. fs.filer.LoadBuckets(fs.filer.DirBucketsPath)
  88. maybeStartMetrics(fs, option)
  89. return fs, nil
  90. }
  91. func maybeStartMetrics(fs *FilerServer, option *FilerOption) {
  92. isConnected := false
  93. var metricsAddress string
  94. var metricsIntervalSec int
  95. var readErr error
  96. for !isConnected {
  97. metricsAddress, metricsIntervalSec, readErr = readFilerConfiguration(fs.grpcDialOption, option.Masters[0])
  98. if readErr == nil {
  99. isConnected = true
  100. } else {
  101. time.Sleep(7 * time.Second)
  102. }
  103. }
  104. if metricsAddress == "" && metricsIntervalSec <= 0 {
  105. return
  106. }
  107. go stats.LoopPushingMetric("filer", stats.SourceName(option.Port), stats.FilerGather,
  108. func() (addr string, intervalSeconds int) {
  109. return metricsAddress, metricsIntervalSec
  110. })
  111. }
  112. func readFilerConfiguration(grpcDialOption grpc.DialOption, masterGrpcAddress string) (metricsAddress string, metricsIntervalSec int, err error) {
  113. err = operation.WithMasterServerClient(masterGrpcAddress, grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  114. resp, err := masterClient.GetMasterConfiguration(context.Background(), &master_pb.GetMasterConfigurationRequest{})
  115. if err != nil {
  116. return fmt.Errorf("get master %s configuration: %v", masterGrpcAddress, err)
  117. }
  118. metricsAddress, metricsIntervalSec = resp.MetricsAddress, int(resp.MetricsIntervalSeconds)
  119. return nil
  120. })
  121. return
  122. }