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.

151 lines
4.5 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. "sync"
  8. "time"
  9. "google.golang.org/grpc"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  13. "github.com/chrislusf/seaweedfs/weed/stats"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. "github.com/chrislusf/seaweedfs/weed/filer2"
  16. _ "github.com/chrislusf/seaweedfs/weed/filer2/cassandra"
  17. _ "github.com/chrislusf/seaweedfs/weed/filer2/etcd"
  18. _ "github.com/chrislusf/seaweedfs/weed/filer2/leveldb"
  19. _ "github.com/chrislusf/seaweedfs/weed/filer2/leveldb2"
  20. _ "github.com/chrislusf/seaweedfs/weed/filer2/mysql"
  21. _ "github.com/chrislusf/seaweedfs/weed/filer2/postgres"
  22. _ "github.com/chrislusf/seaweedfs/weed/filer2/redis"
  23. "github.com/chrislusf/seaweedfs/weed/glog"
  24. "github.com/chrislusf/seaweedfs/weed/notification"
  25. _ "github.com/chrislusf/seaweedfs/weed/notification/aws_sqs"
  26. _ "github.com/chrislusf/seaweedfs/weed/notification/gocdk_pub_sub"
  27. _ "github.com/chrislusf/seaweedfs/weed/notification/google_pub_sub"
  28. _ "github.com/chrislusf/seaweedfs/weed/notification/kafka"
  29. _ "github.com/chrislusf/seaweedfs/weed/notification/log"
  30. "github.com/chrislusf/seaweedfs/weed/security"
  31. )
  32. type FilerOption struct {
  33. Masters []string
  34. Collection string
  35. DefaultReplication string
  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. // notifying clients
  52. clientChansLock sync.RWMutex
  53. clientChans map[string]chan *filer_pb.FullEventNotification
  54. }
  55. func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) (fs *FilerServer, err error) {
  56. fs = &FilerServer{
  57. option: option,
  58. grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.filer"),
  59. }
  60. if len(option.Masters) == 0 {
  61. glog.Fatal("master list is required!")
  62. }
  63. fs.filer = filer2.NewFiler(option.Masters, fs.grpcDialOption, option.Port+10000)
  64. fs.filer.Cipher = option.Cipher
  65. go fs.filer.KeepConnectedToMaster()
  66. v := util.GetViper()
  67. if !util.LoadConfiguration("filer", false) {
  68. v.Set("leveldb2.enabled", true)
  69. v.Set("leveldb2.dir", option.DefaultLevelDbDir)
  70. _, err := os.Stat(option.DefaultLevelDbDir)
  71. if os.IsNotExist(err) {
  72. os.MkdirAll(option.DefaultLevelDbDir, 0755)
  73. }
  74. }
  75. util.LoadConfiguration("notification", false)
  76. fs.option.recursiveDelete = v.GetBool("filer.options.recursive_delete")
  77. v.Set("filer.option.buckets_folder", "/buckets")
  78. v.Set("filer.option.queues_folder", "/queues")
  79. fs.filer.DirBucketsPath = v.GetString("filer.option.buckets_folder")
  80. fs.filer.DirQueuesPath = v.GetString("filer.option.queues_folder")
  81. fs.filer.LoadConfiguration(v)
  82. notification.LoadConfiguration(v, "notification.")
  83. handleStaticResources(defaultMux)
  84. if !option.DisableHttp {
  85. defaultMux.HandleFunc("/", fs.filerHandler)
  86. }
  87. if defaultMux != readonlyMux {
  88. readonlyMux.HandleFunc("/", fs.readonlyFilerHandler)
  89. }
  90. fs.filer.LoadBuckets(fs.filer.DirBucketsPath)
  91. maybeStartMetrics(fs, option)
  92. util.OnInterrupt(func() {
  93. fs.filer.Shutdown()
  94. })
  95. return fs, nil
  96. }
  97. func maybeStartMetrics(fs *FilerServer, option *FilerOption) {
  98. isConnected := false
  99. var metricsAddress string
  100. var metricsIntervalSec int
  101. var readErr error
  102. for !isConnected {
  103. metricsAddress, metricsIntervalSec, readErr = readFilerConfiguration(fs.grpcDialOption, option.Masters[0])
  104. if readErr == nil {
  105. isConnected = true
  106. } else {
  107. time.Sleep(7 * time.Second)
  108. }
  109. }
  110. if metricsAddress == "" && metricsIntervalSec <= 0 {
  111. return
  112. }
  113. go stats.LoopPushingMetric("filer", stats.SourceName(option.Port), stats.FilerGather,
  114. func() (addr string, intervalSeconds int) {
  115. return metricsAddress, metricsIntervalSec
  116. })
  117. }
  118. func readFilerConfiguration(grpcDialOption grpc.DialOption, masterGrpcAddress string) (metricsAddress string, metricsIntervalSec int, err error) {
  119. err = operation.WithMasterServerClient(masterGrpcAddress, grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  120. resp, err := masterClient.GetMasterConfiguration(context.Background(), &master_pb.GetMasterConfigurationRequest{})
  121. if err != nil {
  122. return fmt.Errorf("get master %s configuration: %v", masterGrpcAddress, err)
  123. }
  124. metricsAddress, metricsIntervalSec = resp.MetricsAddress, int(resp.MetricsIntervalSeconds)
  125. return nil
  126. })
  127. return
  128. }