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.

106 lines
3.9 KiB

12 years ago
12 years ago
12 years ago
12 years ago
10 years ago
12 years ago
  1. package command
  2. import (
  3. "net/http"
  4. "os"
  5. "runtime"
  6. "runtime/pprof"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/server"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. "github.com/gorilla/mux"
  14. )
  15. func init() {
  16. cmdMaster.Run = runMaster // break init cycle
  17. }
  18. var cmdMaster = &Command{
  19. UsageLine: "master -port=9333",
  20. Short: "start a master server",
  21. Long: `start a master server to provide volume=>location mapping service
  22. and sequence number of file ids
  23. `,
  24. }
  25. var (
  26. mport = cmdMaster.Flag.Int("port", 9333, "http listen port")
  27. masterIp = cmdMaster.Flag.String("ip", "localhost", "master <ip>|<server> address")
  28. masterBindIp = cmdMaster.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  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, example: 127.0.0.1:9093,127.0.0.1:9094")
  31. volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  32. volumePreallocate = cmdMaster.Flag.Bool("volumePreallocate", false, "Preallocate disk space for volumes.")
  33. mpulse = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  34. confFile = cmdMaster.Flag.String("conf", "/etc/weedfs/weedfs.conf", "Deprecating! xml configuration file")
  35. defaultReplicaPlacement = cmdMaster.Flag.String("defaultReplication", "000", "Default replication type if not specified.")
  36. mTimeout = cmdMaster.Flag.Int("idleTimeout", 10, "connection idle seconds")
  37. mMaxCpu = cmdMaster.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  38. garbageThreshold = cmdMaster.Flag.String("garbageThreshold", "0.3", "threshold to vacuum and reclaim spaces")
  39. masterWhiteListOption = cmdMaster.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  40. masterSecureKey = cmdMaster.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
  41. masterCpuProfile = cmdMaster.Flag.String("cpuprofile", "", "cpu profile output file")
  42. masterWhiteList []string
  43. )
  44. func runMaster(cmd *Command, args []string) bool {
  45. if *mMaxCpu < 1 {
  46. *mMaxCpu = runtime.NumCPU()
  47. }
  48. runtime.GOMAXPROCS(*mMaxCpu)
  49. if *masterCpuProfile != "" {
  50. f, err := os.Create(*masterCpuProfile)
  51. if err != nil {
  52. glog.Fatal(err)
  53. }
  54. pprof.StartCPUProfile(f)
  55. defer pprof.StopCPUProfile()
  56. OnInterrupt(func() {
  57. pprof.StopCPUProfile()
  58. })
  59. }
  60. if err := util.TestFolderWritable(*metaFolder); err != nil {
  61. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *metaFolder, err)
  62. }
  63. if *masterWhiteListOption != "" {
  64. masterWhiteList = strings.Split(*masterWhiteListOption, ",")
  65. }
  66. r := mux.NewRouter()
  67. ms := weed_server.NewMasterServer(r, *mport, *metaFolder,
  68. *volumeSizeLimitMB, *volumePreallocate,
  69. *mpulse, *confFile, *defaultReplicaPlacement, *garbageThreshold,
  70. masterWhiteList, *masterSecureKey,
  71. )
  72. listeningAddress := *masterBindIp + ":" + strconv.Itoa(*mport)
  73. glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", listeningAddress)
  74. listener, e := util.NewListener(listeningAddress, time.Duration(*mTimeout)*time.Second)
  75. if e != nil {
  76. glog.Fatalf("Master startup error: %v", e)
  77. }
  78. go func() {
  79. time.Sleep(100 * time.Millisecond)
  80. myMasterAddress := *masterIp + ":" + strconv.Itoa(*mport)
  81. var peers []string
  82. if *masterPeers != "" {
  83. peers = strings.Split(*masterPeers, ",")
  84. }
  85. raftServer := weed_server.NewRaftServer(r, peers, myMasterAddress, *metaFolder, ms.Topo, *mpulse)
  86. ms.SetRaftServer(raftServer)
  87. }()
  88. if e := http.Serve(listener, r); e != nil {
  89. glog.Fatalf("Fail to serve: %v", e)
  90. }
  91. return true
  92. }