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.

136 lines
3.6 KiB

11 years ago
7 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/filer2"
  9. _ "github.com/chrislusf/seaweedfs/weed/filer2/cassandra"
  10. _ "github.com/chrislusf/seaweedfs/weed/filer2/leveldb"
  11. _ "github.com/chrislusf/seaweedfs/weed/filer2/memdb"
  12. _ "github.com/chrislusf/seaweedfs/weed/filer2/mysql"
  13. _ "github.com/chrislusf/seaweedfs/weed/filer2/postgres"
  14. _ "github.com/chrislusf/seaweedfs/weed/filer2/redis"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/security"
  17. "github.com/chrislusf/seaweedfs/weed/storage"
  18. "github.com/chrislusf/seaweedfs/weed/util"
  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("/favicon.ico", faviconHandler)
  51. defaultMux.HandleFunc("/", fs.filerHandler)
  52. if defaultMux != readonlyMux {
  53. readonlyMux.HandleFunc("/", fs.readonlyFilerHandler)
  54. }
  55. go func() {
  56. connected := true
  57. fs.masterNodes = storage.NewMasterNodes(fs.master)
  58. glog.V(0).Infof("Filer server bootstraps with master %s", fs.getMasterNode())
  59. for {
  60. glog.V(4).Infof("Filer server sending to master %s", fs.getMasterNode())
  61. master, err := fs.detectHealthyMaster(fs.getMasterNode())
  62. if err == nil {
  63. if !connected {
  64. connected = true
  65. if fs.getMasterNode() != master {
  66. fs.setMasterNode(master)
  67. }
  68. glog.V(0).Infoln("Filer Server Connected with master at", master)
  69. }
  70. } else {
  71. glog.V(1).Infof("Filer Server Failed to talk with master %s: %v", fs.getMasterNode(), err)
  72. if connected {
  73. connected = false
  74. }
  75. }
  76. if connected {
  77. time.Sleep(time.Duration(float32(10*1e3)*(1+rand.Float32())) * time.Millisecond)
  78. } else {
  79. time.Sleep(time.Duration(float32(10*1e3)*0.25) * time.Millisecond)
  80. }
  81. }
  82. }()
  83. return fs, nil
  84. }
  85. func (fs *FilerServer) jwt(fileId string) security.EncodedJwt {
  86. return security.GenJwt(fs.secret, fileId)
  87. }
  88. func (fs *FilerServer) getMasterNode() string {
  89. fs.mnLock.RLock()
  90. defer fs.mnLock.RUnlock()
  91. return fs.master
  92. }
  93. func (fs *FilerServer) setMasterNode(masterNode string) {
  94. fs.mnLock.Lock()
  95. defer fs.mnLock.Unlock()
  96. fs.master = masterNode
  97. }
  98. func (fs *FilerServer) detectHealthyMaster(masterNode string) (master string, e error) {
  99. if e = checkMaster(masterNode); e != nil {
  100. fs.masterNodes.Reset()
  101. for i := 0; i <= 3; i++ {
  102. master, e = fs.masterNodes.FindMaster()
  103. if e != nil {
  104. continue
  105. } else {
  106. if e = checkMaster(master); e == nil {
  107. break
  108. }
  109. }
  110. }
  111. } else {
  112. master = masterNode
  113. }
  114. return
  115. }
  116. func checkMaster(masterNode string) error {
  117. statUrl := "http://" + masterNode + "/stats/health"
  118. glog.V(4).Infof("Connecting to %s ...", statUrl)
  119. _, e := util.Get(statUrl)
  120. return e
  121. }