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.

116 lines
3.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
  1. package weed_server
  2. import (
  3. "net/http"
  4. "os"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. "github.com/prometheus/client_golang/prometheus"
  8. "github.com/prometheus/client_golang/prometheus/push"
  9. "google.golang.org/grpc"
  10. "github.com/chrislusf/seaweedfs/weed/filer2"
  11. _ "github.com/chrislusf/seaweedfs/weed/filer2/cassandra"
  12. _ "github.com/chrislusf/seaweedfs/weed/filer2/leveldb"
  13. _ "github.com/chrislusf/seaweedfs/weed/filer2/memdb"
  14. _ "github.com/chrislusf/seaweedfs/weed/filer2/mysql"
  15. _ "github.com/chrislusf/seaweedfs/weed/filer2/postgres"
  16. _ "github.com/chrislusf/seaweedfs/weed/filer2/redis"
  17. "github.com/chrislusf/seaweedfs/weed/glog"
  18. "github.com/chrislusf/seaweedfs/weed/notification"
  19. _ "github.com/chrislusf/seaweedfs/weed/notification/aws_sqs"
  20. _ "github.com/chrislusf/seaweedfs/weed/notification/gocdk_pub_sub"
  21. _ "github.com/chrislusf/seaweedfs/weed/notification/google_pub_sub"
  22. _ "github.com/chrislusf/seaweedfs/weed/notification/kafka"
  23. _ "github.com/chrislusf/seaweedfs/weed/notification/log"
  24. "github.com/chrislusf/seaweedfs/weed/security"
  25. "github.com/spf13/viper"
  26. )
  27. type FilerOption struct {
  28. Masters []string
  29. Collection string
  30. DefaultReplication string
  31. RedirectOnRead bool
  32. DisableDirListing bool
  33. MaxMB int
  34. DirListingLimit int
  35. DataCenter string
  36. DefaultLevelDbDir string
  37. DisableHttp bool
  38. MetricsAddress string
  39. MetricsIntervalSec int
  40. }
  41. type FilerServer struct {
  42. option *FilerOption
  43. secret security.SigningKey
  44. filer *filer2.Filer
  45. grpcDialOption grpc.DialOption
  46. }
  47. func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) (fs *FilerServer, err error) {
  48. fs = &FilerServer{
  49. option: option,
  50. grpcDialOption: security.LoadClientTLS(viper.Sub("grpc"), "filer"),
  51. }
  52. if len(option.Masters) == 0 {
  53. glog.Fatal("master list is required!")
  54. }
  55. fs.filer = filer2.NewFiler(option.Masters, fs.grpcDialOption)
  56. go fs.filer.KeepConnectedToMaster()
  57. v := viper.GetViper()
  58. if !util.LoadConfiguration("filer", false) {
  59. v.Set("leveldb.enabled", true)
  60. v.Set("leveldb.dir", option.DefaultLevelDbDir)
  61. _, err := os.Stat(option.DefaultLevelDbDir)
  62. if os.IsNotExist(err) {
  63. os.MkdirAll(option.DefaultLevelDbDir, 0755)
  64. }
  65. }
  66. util.LoadConfiguration("notification", false)
  67. fs.filer.LoadConfiguration(v)
  68. notification.LoadConfiguration(v.Sub("notification"))
  69. handleStaticResources(defaultMux)
  70. if !option.DisableHttp {
  71. defaultMux.HandleFunc("/", fs.filerHandler)
  72. }
  73. if defaultMux != readonlyMux {
  74. readonlyMux.HandleFunc("/", fs.readonlyFilerHandler)
  75. }
  76. startPushingMetric("filer", filerGather, option.MetricsAddress, option.MetricsIntervalSec)
  77. return fs, nil
  78. }
  79. func startPushingMetric(name string, gatherer *prometheus.Registry, addr string, intervalSeconds int) {
  80. if intervalSeconds == 0 || addr == "" {
  81. glog.V(0).Info("disable metrics reporting")
  82. return
  83. }
  84. glog.V(0).Infof("push metrics to %s every %d seconds", addr, intervalSeconds)
  85. go loopPushMetrics(name, gatherer, addr, intervalSeconds)
  86. }
  87. func loopPushMetrics(name string, gatherer *prometheus.Registry, addr string, intervalSeconds int) {
  88. pusher := push.New(addr, name).Gatherer(gatherer)
  89. for {
  90. err := pusher.Push()
  91. if err != nil {
  92. glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err)
  93. }
  94. time.Sleep(time.Duration(intervalSeconds) * time.Second)
  95. }
  96. }