master.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package command
  2. import (
  3. "net/http"
  4. "os"
  5. "runtime"
  6. "strconv"
  7. "strings"
  8. "github.com/chrislusf/raft/protobuf"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/security"
  12. "github.com/chrislusf/seaweedfs/weed/server"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. "github.com/gorilla/mux"
  15. "github.com/spf13/viper"
  16. "google.golang.org/grpc/reflection"
  17. )
  18. var (
  19. m MasterOptions
  20. )
  21. type MasterOptions struct {
  22. port *int
  23. ip *string
  24. ipBind *string
  25. metaFolder *string
  26. peers *string
  27. volumeSizeLimitMB *uint
  28. volumePreallocate *bool
  29. pulseSeconds *int
  30. defaultReplication *string
  31. garbageThreshold *float64
  32. whiteList *string
  33. disableHttp *bool
  34. metricsAddress *string
  35. metricsIntervalSec *int
  36. }
  37. func init() {
  38. cmdMaster.Run = runMaster // break init cycle
  39. m.port = cmdMaster.Flag.Int("port", 9333, "http listen port")
  40. m.ip = cmdMaster.Flag.String("ip", "localhost", "master <ip>|<server> address")
  41. m.ipBind = cmdMaster.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  42. m.metaFolder = cmdMaster.Flag.String("mdir", os.TempDir(), "data directory to store meta data")
  43. m.peers = cmdMaster.Flag.String("peers", "", "all master nodes in comma separated ip:port list, example: 127.0.0.1:9093,127.0.0.1:9094")
  44. m.volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  45. m.volumePreallocate = cmdMaster.Flag.Bool("volumePreallocate", false, "Preallocate disk space for volumes.")
  46. m.pulseSeconds = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  47. m.defaultReplication = cmdMaster.Flag.String("defaultReplication", "000", "Default replication type if not specified.")
  48. m.garbageThreshold = cmdMaster.Flag.Float64("garbageThreshold", 0.3, "threshold to vacuum and reclaim spaces")
  49. m.whiteList = cmdMaster.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  50. m.disableHttp = cmdMaster.Flag.Bool("disableHttp", false, "disable http requests, only gRPC operations are allowed.")
  51. m.metricsAddress = cmdMaster.Flag.String("metrics.address", "", "Prometheus gateway address")
  52. m.metricsIntervalSec = cmdMaster.Flag.Int("metrics.intervalSeconds", 15, "Prometheus push interval in seconds")
  53. }
  54. var cmdMaster = &Command{
  55. UsageLine: "master -port=9333",
  56. Short: "start a master server",
  57. Long: `start a master server to provide volume=>location mapping service and sequence number of file ids
  58. The configuration file "security.toml" is read from ".", "$HOME/.seaweedfs/", or "/etc/seaweedfs/", in that order.
  59. The example security.toml configuration file can be generated by "weed scaffold -config=security"
  60. `,
  61. }
  62. var (
  63. masterCpuProfile = cmdMaster.Flag.String("cpuprofile", "", "cpu profile output file")
  64. masterMemProfile = cmdMaster.Flag.String("memprofile", "", "memory profile output file")
  65. )
  66. func runMaster(cmd *Command, args []string) bool {
  67. util.LoadConfiguration("security", false)
  68. util.LoadConfiguration("master", false)
  69. runtime.GOMAXPROCS(runtime.NumCPU())
  70. util.SetupProfiling(*masterCpuProfile, *masterMemProfile)
  71. if err := util.TestFolderWritable(*m.metaFolder); err != nil {
  72. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *m.metaFolder, err)
  73. }
  74. var masterWhiteList []string
  75. if *m.whiteList != "" {
  76. masterWhiteList = strings.Split(*m.whiteList, ",")
  77. }
  78. if *m.volumeSizeLimitMB > util.VolumeSizeLimitGB*1000 {
  79. glog.Fatalf("volumeSizeLimitMB should be smaller than 30000")
  80. }
  81. startMaster(m, masterWhiteList)
  82. return true
  83. }
  84. func startMaster(masterOption MasterOptions, masterWhiteList []string) {
  85. myMasterAddress, peers := checkPeers(*masterOption.ip, *masterOption.port, *masterOption.peers)
  86. r := mux.NewRouter()
  87. ms := weed_server.NewMasterServer(r, masterOption.toMasterOption(masterWhiteList), peers)
  88. listeningAddress := *masterOption.ipBind + ":" + strconv.Itoa(*masterOption.port)
  89. glog.V(0).Infof("Start Seaweed Master %s at %s", util.VERSION, listeningAddress)
  90. masterListener, e := util.NewListener(listeningAddress, 0)
  91. if e != nil {
  92. glog.Fatalf("Master startup error: %v", e)
  93. }
  94. // start raftServer
  95. raftServer := weed_server.NewRaftServer(security.LoadClientTLS(viper.Sub("grpc"), "master"),
  96. peers, myMasterAddress, *masterOption.metaFolder, ms.Topo, *masterOption.pulseSeconds)
  97. if raftServer == nil {
  98. glog.Fatalf("please verify %s is writable, see https://github.com/chrislusf/seaweedfs/issues/717", *masterOption.metaFolder)
  99. }
  100. ms.SetRaftServer(raftServer)
  101. r.HandleFunc("/cluster/status", raftServer.StatusHandler).Methods("GET")
  102. // starting grpc server
  103. grpcPort := *masterOption.port + 10000
  104. grpcL, err := util.NewListener(*masterOption.ipBind+":"+strconv.Itoa(grpcPort), 0)
  105. if err != nil {
  106. glog.Fatalf("master failed to listen on grpc port %d: %v", grpcPort, err)
  107. }
  108. // Create your protocol servers.
  109. grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "master"))
  110. master_pb.RegisterSeaweedServer(grpcS, ms)
  111. protobuf.RegisterRaftServer(grpcS, raftServer)
  112. reflection.Register(grpcS)
  113. glog.V(0).Infof("Start Seaweed Master %s grpc server at %s:%d", util.VERSION, *masterOption.ipBind, grpcPort)
  114. go grpcS.Serve(grpcL)
  115. go ms.MasterClient.KeepConnectedToMaster()
  116. // start http server
  117. httpS := &http.Server{Handler: r}
  118. go httpS.Serve(masterListener)
  119. select {}
  120. }
  121. func checkPeers(masterIp string, masterPort int, peers string) (masterAddress string, cleanedPeers []string) {
  122. masterAddress = masterIp + ":" + strconv.Itoa(masterPort)
  123. if peers != "" {
  124. cleanedPeers = strings.Split(peers, ",")
  125. }
  126. hasSelf := false
  127. for _, peer := range cleanedPeers {
  128. if peer == masterAddress {
  129. hasSelf = true
  130. break
  131. }
  132. }
  133. if !hasSelf {
  134. cleanedPeers = append(cleanedPeers, masterAddress)
  135. }
  136. if len(cleanedPeers)%2 == 0 {
  137. glog.Fatalf("Only odd number of masters are supported!")
  138. }
  139. return
  140. }
  141. func (m *MasterOptions) toMasterOption(whiteList []string) *weed_server.MasterOption {
  142. return &weed_server.MasterOption{
  143. Port: *m.port,
  144. MetaFolder: *m.metaFolder,
  145. VolumeSizeLimitMB: *m.volumeSizeLimitMB,
  146. VolumePreallocate: *m.volumePreallocate,
  147. PulseSeconds: *m.pulseSeconds,
  148. DefaultReplicaPlacement: *m.defaultReplication,
  149. GarbageThreshold: *m.garbageThreshold,
  150. WhiteList: whiteList,
  151. DisableHttp: *m.disableHttp,
  152. MetricsAddress: *m.metricsAddress,
  153. MetricsIntervalSec: *m.metricsIntervalSec,
  154. }
  155. }