master.go 6.6 KB

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