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.

99 lines
3.6 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. package main
  2. import (
  3. "net/http"
  4. "os"
  5. "runtime"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/chrislusf/weed-fs/go/glog"
  10. "github.com/chrislusf/weed-fs/go/util"
  11. "github.com/chrislusf/weed-fs/go/weed/weed_server"
  12. "github.com/gorilla/mux"
  13. )
  14. func init() {
  15. cmdMaster.Run = runMaster // break init cycle
  16. }
  17. var cmdMaster = &Command{
  18. UsageLine: "master -port=9333",
  19. Short: "start a master server",
  20. Long: `start a master server to provide volume=>location mapping service
  21. and sequence number of file ids
  22. `,
  23. }
  24. var (
  25. mport = cmdMaster.Flag.Int("port", 9333, "http listen port")
  26. masterIp = cmdMaster.Flag.String("ip", "", "master listening ip address, default to listen on all network interfaces")
  27. masterBindIp = cmdMaster.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  28. mPublicIp = cmdMaster.Flag.String("publicIp", "", "peer accessible <ip>|<server_name>")
  29. metaFolder = cmdMaster.Flag.String("mdir", os.TempDir(), "data directory to store meta data")
  30. masterPeers = cmdMaster.Flag.String("peers", "", "other master nodes in comma separated ip:port list")
  31. volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  32. mpulse = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  33. confFile = cmdMaster.Flag.String("conf", "/etc/weedfs/weedfs.conf", "xml configuration file")
  34. defaultReplicaPlacement = cmdMaster.Flag.String("defaultReplication", "000", "Default replication type if not specified.")
  35. mTimeout = cmdMaster.Flag.Int("idleTimeout", 10, "connection idle seconds")
  36. mMaxCpu = cmdMaster.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  37. garbageThreshold = cmdMaster.Flag.String("garbageThreshold", "0.3", "threshold to vacuum and reclaim spaces")
  38. masterWhiteListOption = cmdMaster.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  39. masterSecureKey = cmdMaster.Flag.String("secure.key", "", "secret key to check permission")
  40. masterWhiteList []string
  41. )
  42. func runMaster(cmd *Command, args []string) bool {
  43. if *mMaxCpu < 1 {
  44. *mMaxCpu = runtime.NumCPU()
  45. }
  46. runtime.GOMAXPROCS(*mMaxCpu)
  47. if err := util.TestFolderWritable(*metaFolder); err != nil {
  48. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *metaFolder, err)
  49. }
  50. if *masterWhiteListOption != "" {
  51. masterWhiteList = strings.Split(*masterWhiteListOption, ",")
  52. }
  53. r := mux.NewRouter()
  54. ms := weed_server.NewMasterServer(r, *mport, *metaFolder,
  55. *volumeSizeLimitMB, *mpulse, *confFile, *defaultReplicaPlacement, *garbageThreshold,
  56. masterWhiteList, *masterSecureKey,
  57. )
  58. listeningAddress := *masterBindIp + ":" + strconv.Itoa(*mport)
  59. glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", listeningAddress)
  60. listener, e := util.NewListener(listeningAddress, time.Duration(*mTimeout)*time.Second)
  61. if e != nil {
  62. glog.Fatalf(e.Error())
  63. }
  64. go func() {
  65. time.Sleep(100 * time.Millisecond)
  66. if *mPublicIp == "" {
  67. if *masterIp == "" {
  68. *mPublicIp = "localhost"
  69. } else {
  70. *mPublicIp = *masterIp
  71. }
  72. }
  73. myPublicMasterAddress := *mPublicIp + ":" + strconv.Itoa(*mport)
  74. var peers []string
  75. if *masterPeers != "" {
  76. peers = strings.Split(*masterPeers, ",")
  77. }
  78. raftServer := weed_server.NewRaftServer(r, peers, myPublicMasterAddress, *metaFolder, ms.Topo, *mpulse)
  79. ms.SetRaftServer(raftServer)
  80. }()
  81. if e := http.Serve(listener, r); e != nil {
  82. glog.Fatalf("Fail to serve:%s", e.Error())
  83. }
  84. return true
  85. }