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.

144 lines
5.0 KiB

13 years ago
13 years ago
6 years ago
13 years ago
13 years ago
6 years ago
13 years ago
13 years ago
  1. package command
  2. import (
  3. "net/http"
  4. "os"
  5. "runtime"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/server"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. "github.com/gorilla/mux"
  14. "google.golang.org/grpc/reflection"
  15. )
  16. func init() {
  17. cmdMaster.Run = runMaster // break init cycle
  18. }
  19. var cmdMaster = &Command{
  20. UsageLine: "master -port=9333",
  21. Short: "start a master server",
  22. Long: `start a master server to provide volume=>location mapping service
  23. and sequence number of file ids
  24. `,
  25. }
  26. var (
  27. mport = cmdMaster.Flag.Int("port", 9333, "http listen port")
  28. mGrpcPort = cmdMaster.Flag.Int("port.grpc", 0, "grpc server listen port, default to http port + 10000")
  29. masterIp = cmdMaster.Flag.String("ip", "localhost", "master <ip>|<server> address")
  30. masterBindIp = cmdMaster.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  31. metaFolder = cmdMaster.Flag.String("mdir", os.TempDir(), "data directory to store meta data")
  32. masterPeers = cmdMaster.Flag.String("peers", "", "all master nodes in comma separated ip:port list, example: 127.0.0.1:9093,127.0.0.1:9094")
  33. volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  34. volumePreallocate = cmdMaster.Flag.Bool("volumePreallocate", false, "Preallocate disk space for volumes.")
  35. mpulse = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  36. defaultReplicaPlacement = cmdMaster.Flag.String("defaultReplication", "000", "Default replication type if not specified.")
  37. // mTimeout = cmdMaster.Flag.Int("idleTimeout", 30, "connection idle seconds")
  38. mMaxCpu = cmdMaster.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  39. garbageThreshold = cmdMaster.Flag.Float64("garbageThreshold", 0.3, "threshold to vacuum and reclaim spaces")
  40. masterWhiteListOption = cmdMaster.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  41. masterSecureKey = cmdMaster.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
  42. masterCpuProfile = cmdMaster.Flag.String("cpuprofile", "", "cpu profile output file")
  43. masterMemProfile = cmdMaster.Flag.String("memprofile", "", "memory profile output file")
  44. masterWhiteList []string
  45. )
  46. func runMaster(cmd *Command, args []string) bool {
  47. if *mMaxCpu < 1 {
  48. *mMaxCpu = runtime.NumCPU()
  49. }
  50. runtime.GOMAXPROCS(*mMaxCpu)
  51. util.SetupProfiling(*masterCpuProfile, *masterMemProfile)
  52. if err := util.TestFolderWritable(*metaFolder); err != nil {
  53. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *metaFolder, err)
  54. }
  55. if *masterWhiteListOption != "" {
  56. masterWhiteList = strings.Split(*masterWhiteListOption, ",")
  57. }
  58. if *volumeSizeLimitMB > 30*1000 {
  59. glog.Fatalf("volumeSizeLimitMB should be smaller than 30000")
  60. }
  61. r := mux.NewRouter()
  62. ms := weed_server.NewMasterServer(r, *mport, *metaFolder,
  63. *volumeSizeLimitMB, *volumePreallocate,
  64. *mpulse, *defaultReplicaPlacement, *garbageThreshold,
  65. masterWhiteList, *masterSecureKey,
  66. )
  67. listeningAddress := *masterBindIp + ":" + strconv.Itoa(*mport)
  68. glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", listeningAddress)
  69. masterListener, e := util.NewListener(listeningAddress, 0)
  70. if e != nil {
  71. glog.Fatalf("Master startup error: %v", e)
  72. }
  73. go func() {
  74. time.Sleep(100 * time.Millisecond)
  75. myMasterAddress, peers := checkPeers(*masterIp, *mport, *masterPeers)
  76. raftServer := weed_server.NewRaftServer(r, peers, myMasterAddress, *metaFolder, ms.Topo, *mpulse)
  77. ms.SetRaftServer(raftServer)
  78. }()
  79. go func() {
  80. // starting grpc server
  81. grpcPort := *mGrpcPort
  82. if grpcPort == 0 {
  83. grpcPort = *mport + 10000
  84. }
  85. grpcL, err := util.NewListener(*masterBindIp+":"+strconv.Itoa(grpcPort), 0)
  86. if err != nil {
  87. glog.Fatalf("master failed to listen on grpc port %d: %v", grpcPort, err)
  88. }
  89. // Create your protocol servers.
  90. grpcS := util.NewGrpcServer()
  91. master_pb.RegisterSeaweedServer(grpcS, ms)
  92. reflection.Register(grpcS)
  93. glog.V(0).Infof("Start Seaweed Master %s grpc server at %s:%d", util.VERSION, *masterBindIp, grpcPort)
  94. grpcS.Serve(grpcL)
  95. }()
  96. // start http server
  97. httpS := &http.Server{Handler: r}
  98. if err := httpS.Serve(masterListener); err != nil {
  99. glog.Fatalf("master server failed to serve: %v", err)
  100. }
  101. return true
  102. }
  103. func checkPeers(masterIp string, masterPort int, peers string) (masterAddress string, cleanedPeers []string) {
  104. masterAddress = masterIp + ":" + strconv.Itoa(masterPort)
  105. if peers != "" {
  106. cleanedPeers = strings.Split(peers, ",")
  107. }
  108. hasSelf := false
  109. for _, peer := range cleanedPeers {
  110. if peer == masterAddress {
  111. hasSelf = true
  112. break
  113. }
  114. }
  115. peerCount := len(cleanedPeers)
  116. if !hasSelf {
  117. peerCount += 1
  118. }
  119. if peerCount%2 == 0 {
  120. glog.Fatalf("Only odd number of masters are supported!")
  121. }
  122. return
  123. }