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.

200 lines
5.7 KiB

11 years ago
10 years ago
10 years ago
10 years ago
  1. package weed_server
  2. import (
  3. "encoding/json"
  4. "math/rand"
  5. "net/http"
  6. "os"
  7. "strconv"
  8. "sync"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/filer"
  11. "github.com/chrislusf/seaweedfs/weed/filer/cassandra_store"
  12. "github.com/chrislusf/seaweedfs/weed/filer/embedded_filer"
  13. "github.com/chrislusf/seaweedfs/weed/filer/flat_namespace"
  14. "github.com/chrislusf/seaweedfs/weed/filer/mysql_store"
  15. "github.com/chrislusf/seaweedfs/weed/filer/postgres_store"
  16. "github.com/chrislusf/seaweedfs/weed/filer/redis_store"
  17. "github.com/chrislusf/seaweedfs/weed/glog"
  18. "github.com/chrislusf/seaweedfs/weed/security"
  19. "github.com/chrislusf/seaweedfs/weed/storage"
  20. "github.com/chrislusf/seaweedfs/weed/util"
  21. )
  22. type filerConf struct {
  23. MysqlConf []mysql_store.MySqlConf `json:"mysql"`
  24. mysql_store.ShardingConf
  25. PostgresConf []postgres_store.PostgresConf `json:"postgres"`
  26. }
  27. func parseConfFile(confPath string) (*filerConf, error) {
  28. var setting filerConf
  29. configFile, err := os.Open(confPath)
  30. defer configFile.Close()
  31. if err != nil {
  32. return nil, err
  33. }
  34. jsonParser := json.NewDecoder(configFile)
  35. if err = jsonParser.Decode(&setting); err != nil {
  36. return nil, err
  37. }
  38. return &setting, nil
  39. }
  40. type FilerServer struct {
  41. port string
  42. master string
  43. mnLock sync.RWMutex
  44. collection string
  45. defaultReplication string
  46. redirectOnRead bool
  47. disableDirListing bool
  48. secret security.Secret
  49. filer filer.Filer
  50. maxMB int
  51. masterNodes *storage.MasterNodes
  52. }
  53. func NewFilerServer(r *http.ServeMux, ip string, port int, master string, dir string, collection string,
  54. replication string, redirectOnRead bool, disableDirListing bool,
  55. confFile string,
  56. maxMB int,
  57. secret string,
  58. cassandra_server string, cassandra_keyspace string,
  59. redis_server string, redis_password string, redis_database int,
  60. ) (fs *FilerServer, err error) {
  61. fs = &FilerServer{
  62. master: master,
  63. collection: collection,
  64. defaultReplication: replication,
  65. redirectOnRead: redirectOnRead,
  66. disableDirListing: disableDirListing,
  67. maxMB: maxMB,
  68. port: ip + ":" + strconv.Itoa(port),
  69. }
  70. var setting *filerConf
  71. if confFile != "" {
  72. setting, err = parseConfFile(confFile)
  73. if err != nil {
  74. return nil, err
  75. }
  76. } else {
  77. setting = new(filerConf)
  78. }
  79. if setting.MysqlConf != nil && len(setting.MysqlConf) != 0 {
  80. mysql_store := mysql_store.NewMysqlStore(setting.MysqlConf, setting.IsSharding, setting.ShardCount)
  81. fs.filer = flat_namespace.NewFlatNamespaceFiler(master, mysql_store)
  82. } else if setting.PostgresConf != nil && len(setting.PostgresConf) != 0 {
  83. postgres_store := postgres_store.NewPostgresStore(setting.PostgresConf, setting.IsSharding, setting.ShardCount)
  84. fs.filer = flat_namespace.NewFlatNamespaceFiler(master, postgres_store)
  85. } else if cassandra_server != "" {
  86. cassandra_store, err := cassandra_store.NewCassandraStore(cassandra_keyspace, cassandra_server)
  87. if err != nil {
  88. glog.Fatalf("Can not connect to cassandra server %s with keyspace %s: %v", cassandra_server, cassandra_keyspace, err)
  89. }
  90. fs.filer = flat_namespace.NewFlatNamespaceFiler(master, cassandra_store)
  91. } else if redis_server != "" {
  92. redis_store := redis_store.NewRedisStore(redis_server, redis_password, redis_database)
  93. fs.filer = flat_namespace.NewFlatNamespaceFiler(master, redis_store)
  94. } else {
  95. if fs.filer, err = embedded_filer.NewFilerEmbedded(master, dir); err != nil {
  96. glog.Fatalf("Can not start filer in dir %s : %v", dir, err)
  97. return
  98. }
  99. r.HandleFunc("/admin/mv", fs.moveHandler)
  100. r.HandleFunc("/admin/register", fs.registerHandler)
  101. }
  102. r.HandleFunc("/", fs.filerHandler)
  103. go func() {
  104. connected := true
  105. fs.masterNodes = storage.NewMasterNodes(fs.master)
  106. glog.V(0).Infof("Filer server bootstraps with master %s", fs.getMasterNode())
  107. //force initialize with all available master nodes
  108. for {
  109. _, err := fs.masterNodes.FindMaster()
  110. if err != nil {
  111. glog.Infof("filer server failed to get master cluster info:%s", err.Error())
  112. time.Sleep(3 * time.Second)
  113. } else {
  114. break
  115. }
  116. }
  117. for {
  118. glog.V(4).Infof("Filer server sending to master %s", fs.getMasterNode())
  119. master, err := fs.detectHealthyMaster(fs.getMasterNode())
  120. if err == nil {
  121. if !connected {
  122. connected = true
  123. if fs.getMasterNode() != master {
  124. fs.setMasterNode(master)
  125. }
  126. glog.V(0).Infoln("Filer Server Connected with master at", master)
  127. }
  128. } else {
  129. glog.V(1).Infof("Filer Server Failed to talk with master %s: %v", fs.getMasterNode(), err)
  130. if connected {
  131. connected = false
  132. }
  133. }
  134. if connected {
  135. time.Sleep(time.Duration(float32(10*1e3)*(1+rand.Float32())) * time.Millisecond)
  136. } else {
  137. time.Sleep(time.Duration(float32(10*1e3)*0.25) * time.Millisecond)
  138. }
  139. }
  140. }()
  141. return fs, nil
  142. }
  143. func (fs *FilerServer) jwt(fileId string) security.EncodedJwt {
  144. return security.GenJwt(fs.secret, fileId)
  145. }
  146. func (fs *FilerServer) getMasterNode() string {
  147. fs.mnLock.RLock()
  148. defer fs.mnLock.RUnlock()
  149. return fs.master
  150. }
  151. func (fs *FilerServer) setMasterNode(masterNode string) {
  152. fs.mnLock.Lock()
  153. defer fs.mnLock.Unlock()
  154. fs.master = masterNode
  155. }
  156. func (fs *FilerServer) detectHealthyMaster(masterNode string) (master string, e error) {
  157. if e = checkMaster(masterNode); e != nil {
  158. fs.masterNodes.Reset()
  159. for i := 0; i <= 3; i++ {
  160. master, e = fs.masterNodes.FindMaster()
  161. if e != nil {
  162. continue
  163. } else {
  164. if e = checkMaster(master); e == nil {
  165. break
  166. }
  167. }
  168. }
  169. } else {
  170. master = masterNode
  171. }
  172. return
  173. }
  174. func checkMaster(masterNode string) error {
  175. statUrl := "http://" + masterNode + "/stats"
  176. glog.V(4).Infof("Connecting to %s ...", statUrl)
  177. _, e := util.Get(statUrl)
  178. return e
  179. }