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.

190 lines
6.8 KiB

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