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.

188 lines
6.8 KiB

5 years ago
6 years ago
12 years ago
6 years ago
6 years ago
5 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
6 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/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", util.DetectedHostAddress(), "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,127.0.0.1:9095")
  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. glog.V(0).Infof("current: %s:%d peers:%s", masterIp, masterPort, peers)
  125. masterAddress = masterIp + ":" + strconv.Itoa(masterPort)
  126. if peers != "" {
  127. cleanedPeers = strings.Split(peers, ",")
  128. }
  129. hasSelf := false
  130. for _, peer := range cleanedPeers {
  131. if peer == masterAddress {
  132. hasSelf = true
  133. break
  134. }
  135. }
  136. if !hasSelf {
  137. cleanedPeers = append(cleanedPeers, masterAddress)
  138. }
  139. if len(cleanedPeers)%2 == 0 {
  140. glog.Fatalf("Only odd number of masters are supported!")
  141. }
  142. return
  143. }
  144. func (m *MasterOptions) toMasterOption(whiteList []string) *weed_server.MasterOption {
  145. return &weed_server.MasterOption{
  146. Host: *m.ip,
  147. Port: *m.port,
  148. MetaFolder: *m.metaFolder,
  149. VolumeSizeLimitMB: *m.volumeSizeLimitMB,
  150. VolumePreallocate: *m.volumePreallocate,
  151. PulseSeconds: *m.pulseSeconds,
  152. DefaultReplicaPlacement: *m.defaultReplication,
  153. GarbageThreshold: *m.garbageThreshold,
  154. WhiteList: whiteList,
  155. DisableHttp: *m.disableHttp,
  156. MetricsAddress: *m.metricsAddress,
  157. MetricsIntervalSec: *m.metricsIntervalSec,
  158. }
  159. }