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.

192 lines
7.0 KiB

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