filer_server.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 int
  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)
  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. fs.filer.LoadConfiguration(v)
  73. notification.LoadConfiguration(v, "notification.")
  74. handleStaticResources(defaultMux)
  75. if !option.DisableHttp {
  76. defaultMux.HandleFunc("/", fs.filerHandler)
  77. }
  78. if defaultMux != readonlyMux {
  79. readonlyMux.HandleFunc("/", fs.readonlyFilerHandler)
  80. }
  81. maybeStartMetrics(fs, option)
  82. return fs, nil
  83. }
  84. func maybeStartMetrics(fs *FilerServer, option *FilerOption) {
  85. isConnected := false
  86. var metricsAddress string
  87. var metricsIntervalSec int
  88. var readErr error
  89. for !isConnected {
  90. metricsAddress, metricsIntervalSec, readErr = readFilerConfiguration(fs.grpcDialOption, option.Masters[0])
  91. if readErr == nil {
  92. isConnected = true
  93. } else {
  94. time.Sleep(7 * time.Second)
  95. }
  96. }
  97. if metricsAddress == "" && metricsIntervalSec <= 0 {
  98. return
  99. }
  100. go stats.LoopPushingMetric("filer", stats.SourceName(option.Port), stats.FilerGather,
  101. func() (addr string, intervalSeconds int) {
  102. return metricsAddress, metricsIntervalSec
  103. })
  104. }
  105. func readFilerConfiguration(grpcDialOption grpc.DialOption, masterGrpcAddress string) (metricsAddress string, metricsIntervalSec int, err error) {
  106. err = operation.WithMasterServerClient(masterGrpcAddress, grpcDialOption, func(ctx context.Context, masterClient master_pb.SeaweedClient) error {
  107. resp, err := masterClient.GetMasterConfiguration(ctx, &master_pb.GetMasterConfigurationRequest{})
  108. if err != nil {
  109. return fmt.Errorf("get master %s configuration: %v", masterGrpcAddress, err)
  110. }
  111. metricsAddress, metricsIntervalSec = resp.MetricsAddress, int(resp.MetricsIntervalSeconds)
  112. return nil
  113. })
  114. return
  115. }