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.

183 lines
6.3 KiB

6 years ago
12 years ago
6 years ago
12 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
12 years ago
12 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/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. masterWhiteList []string
  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. 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. r := mux.NewRouter()
  82. ms := weed_server.NewMasterServer(r, m.toMasterOption(masterWhiteList))
  83. listeningAddress := *m.ipBind + ":" + strconv.Itoa(*m.port)
  84. glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", listeningAddress)
  85. masterListener, e := util.NewListener(listeningAddress, 0)
  86. if e != nil {
  87. glog.Fatalf("Master startup error: %v", e)
  88. }
  89. go func() {
  90. // start raftServer
  91. myMasterAddress, peers := checkPeers(*m.ip, *m.port, *m.peers)
  92. raftServer := weed_server.NewRaftServer(security.LoadClientTLS(viper.Sub("grpc"), "master"),
  93. peers, myMasterAddress, *m.metaFolder, ms.Topo, *m.pulseSeconds)
  94. if raftServer == nil {
  95. glog.Fatalf("please verify %s is writable, see https://github.com/chrislusf/seaweedfs/issues/717", *m.metaFolder)
  96. }
  97. ms.SetRaftServer(raftServer)
  98. r.HandleFunc("/cluster/status", raftServer.StatusHandler).Methods("GET")
  99. // starting grpc server
  100. grpcPort := *m.port + 10000
  101. grpcL, err := util.NewListener(*m.ipBind+":"+strconv.Itoa(grpcPort), 0)
  102. if err != nil {
  103. glog.Fatalf("master failed to listen on grpc port %d: %v", grpcPort, err)
  104. }
  105. // Create your protocol servers.
  106. grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "master"))
  107. master_pb.RegisterSeaweedServer(grpcS, ms)
  108. protobuf.RegisterRaftServer(grpcS, raftServer)
  109. reflection.Register(grpcS)
  110. glog.V(0).Infof("Start Seaweed Master %s grpc server at %s:%d", util.VERSION, *m.ipBind, grpcPort)
  111. grpcS.Serve(grpcL)
  112. }()
  113. // start http server
  114. httpS := &http.Server{Handler: r}
  115. if err := httpS.Serve(masterListener); err != nil {
  116. glog.Fatalf("master server failed to serve: %v", err)
  117. }
  118. return true
  119. }
  120. func checkPeers(masterIp string, masterPort int, peers string) (masterAddress string, cleanedPeers []string) {
  121. masterAddress = masterIp + ":" + strconv.Itoa(masterPort)
  122. if peers != "" {
  123. cleanedPeers = strings.Split(peers, ",")
  124. }
  125. hasSelf := false
  126. for _, peer := range cleanedPeers {
  127. if peer == masterAddress {
  128. hasSelf = true
  129. break
  130. }
  131. }
  132. peerCount := len(cleanedPeers)
  133. if !hasSelf {
  134. peerCount += 1
  135. }
  136. if peerCount%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. }