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.

135 lines
3.5 KiB

11 years ago
7 years ago
7 years ago
10 years ago
10 years ago
10 years ago
  1. package weed_server
  2. import (
  3. "math/rand"
  4. "net/http"
  5. "strconv"
  6. "sync"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/security"
  10. "github.com/chrislusf/seaweedfs/weed/storage"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/chrislusf/seaweedfs/weed/filer2"
  13. _ "github.com/chrislusf/seaweedfs/weed/filer2/cassandra"
  14. _ "github.com/chrislusf/seaweedfs/weed/filer2/leveldb"
  15. _ "github.com/chrislusf/seaweedfs/weed/filer2/memdb"
  16. _ "github.com/chrislusf/seaweedfs/weed/filer2/mysql"
  17. _ "github.com/chrislusf/seaweedfs/weed/filer2/postgres"
  18. _ "github.com/chrislusf/seaweedfs/weed/filer2/redis"
  19. )
  20. type FilerServer struct {
  21. port string
  22. master string
  23. mnLock sync.RWMutex
  24. collection string
  25. defaultReplication string
  26. redirectOnRead bool
  27. disableDirListing bool
  28. secret security.Secret
  29. filer *filer2.Filer
  30. maxMB int
  31. masterNodes *storage.MasterNodes
  32. }
  33. func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, ip string, port int, master string, collection string,
  34. replication string, redirectOnRead bool, disableDirListing bool,
  35. maxMB int,
  36. secret string,
  37. ) (fs *FilerServer, err error) {
  38. fs = &FilerServer{
  39. master: master,
  40. collection: collection,
  41. defaultReplication: replication,
  42. redirectOnRead: redirectOnRead,
  43. disableDirListing: disableDirListing,
  44. maxMB: maxMB,
  45. port: ip + ":" + strconv.Itoa(port),
  46. }
  47. fs.filer = filer2.NewFiler(master)
  48. fs.filer.LoadConfiguration()
  49. defaultMux.HandleFunc("/admin/register", fs.registerHandler)
  50. defaultMux.HandleFunc("/", fs.filerHandler)
  51. if defaultMux != readonlyMux {
  52. readonlyMux.HandleFunc("/", fs.readonlyFilerHandler)
  53. }
  54. go func() {
  55. connected := true
  56. fs.masterNodes = storage.NewMasterNodes(fs.master)
  57. glog.V(0).Infof("Filer server bootstraps with master %s", fs.getMasterNode())
  58. for {
  59. glog.V(4).Infof("Filer server sending to master %s", fs.getMasterNode())
  60. master, err := fs.detectHealthyMaster(fs.getMasterNode())
  61. if err == nil {
  62. if !connected {
  63. connected = true
  64. if fs.getMasterNode() != master {
  65. fs.setMasterNode(master)
  66. }
  67. glog.V(0).Infoln("Filer Server Connected with master at", master)
  68. }
  69. } else {
  70. glog.V(1).Infof("Filer Server Failed to talk with master %s: %v", fs.getMasterNode(), err)
  71. if connected {
  72. connected = false
  73. }
  74. }
  75. if connected {
  76. time.Sleep(time.Duration(float32(10*1e3)*(1+rand.Float32())) * time.Millisecond)
  77. } else {
  78. time.Sleep(time.Duration(float32(10*1e3)*0.25) * time.Millisecond)
  79. }
  80. }
  81. }()
  82. return fs, nil
  83. }
  84. func (fs *FilerServer) jwt(fileId string) security.EncodedJwt {
  85. return security.GenJwt(fs.secret, fileId)
  86. }
  87. func (fs *FilerServer) getMasterNode() string {
  88. fs.mnLock.RLock()
  89. defer fs.mnLock.RUnlock()
  90. return fs.master
  91. }
  92. func (fs *FilerServer) setMasterNode(masterNode string) {
  93. fs.mnLock.Lock()
  94. defer fs.mnLock.Unlock()
  95. fs.master = masterNode
  96. }
  97. func (fs *FilerServer) detectHealthyMaster(masterNode string) (master string, e error) {
  98. if e = checkMaster(masterNode); e != nil {
  99. fs.masterNodes.Reset()
  100. for i := 0; i <= 3; i++ {
  101. master, e = fs.masterNodes.FindMaster()
  102. if e != nil {
  103. continue
  104. } else {
  105. if e = checkMaster(master); e == nil {
  106. break
  107. }
  108. }
  109. }
  110. } else {
  111. master = masterNode
  112. }
  113. return
  114. }
  115. func checkMaster(masterNode string) error {
  116. statUrl := "http://" + masterNode + "/stats/health"
  117. glog.V(4).Infof("Connecting to %s ...", statUrl)
  118. _, e := util.Get(statUrl)
  119. return e
  120. }