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.9 KiB

6 years ago
6 years ago
13 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
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. sequencerType *string
  37. etcdUrls *string
  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", "localhost", "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")
  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")
  54. m.metricsIntervalSec = cmdMaster.Flag.Int("metrics.intervalSeconds", 15, "Prometheus push interval in seconds")
  55. m.sequencerType = cmdMaster.Flag.String("sequencerType", "memory", "Choose [memory|etcd] type for store the file sequence")
  56. m.etcdUrls = cmdMaster.Flag.String("etcdUrls", "",
  57. "when sequencerType=etcd, set etcdUrls for etcd cluster that store file sequence, example : http://127.0.0.1:2379,http://127.0.0.1:2389")
  58. }
  59. var cmdMaster = &Command{
  60. UsageLine: "master -port=9333",
  61. Short: "start a master server",
  62. Long: `start a master server to provide volume=>location mapping service and sequence number of file ids
  63. The configuration file "security.toml" is read from ".", "$HOME/.seaweedfs/", or "/etc/seaweedfs/", in that order.
  64. The example security.toml configuration file can be generated by "weed scaffold -config=security"
  65. `,
  66. }
  67. var (
  68. masterCpuProfile = cmdMaster.Flag.String("cpuprofile", "", "cpu profile output file")
  69. masterMemProfile = cmdMaster.Flag.String("memprofile", "", "memory profile output file")
  70. )
  71. func runMaster(cmd *Command, args []string) bool {
  72. util.LoadConfiguration("security", false)
  73. util.LoadConfiguration("master", false)
  74. runtime.GOMAXPROCS(runtime.NumCPU())
  75. util.SetupProfiling(*masterCpuProfile, *masterMemProfile)
  76. if err := util.TestFolderWritable(*m.metaFolder); err != nil {
  77. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *m.metaFolder, err)
  78. }
  79. var masterWhiteList []string
  80. if *m.whiteList != "" {
  81. masterWhiteList = strings.Split(*m.whiteList, ",")
  82. }
  83. if *m.volumeSizeLimitMB > util.VolumeSizeLimitGB*1000 {
  84. glog.Fatalf("volumeSizeLimitMB should be smaller than 30000")
  85. }
  86. startMaster(m, masterWhiteList)
  87. return true
  88. }
  89. func startMaster(masterOption MasterOptions, masterWhiteList []string) {
  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 := weed_server.NewRaftServer(security.LoadClientTLS(viper.Sub("grpc"), "master"),
  101. peers, myMasterAddress, *masterOption.metaFolder, ms.Topo, *masterOption.pulseSeconds)
  102. if raftServer == nil {
  103. glog.Fatalf("please verify %s is writable, see https://github.com/chrislusf/seaweedfs/issues/717", *masterOption.metaFolder)
  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 := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("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. masterAddress = masterIp + ":" + strconv.Itoa(masterPort)
  128. if peers != "" {
  129. cleanedPeers = strings.Split(peers, ",")
  130. }
  131. hasSelf := false
  132. for _, peer := range cleanedPeers {
  133. if peer == masterAddress {
  134. hasSelf = true
  135. break
  136. }
  137. }
  138. if !hasSelf {
  139. cleanedPeers = append(cleanedPeers, masterAddress)
  140. }
  141. if len(cleanedPeers)%2 == 0 {
  142. glog.Fatalf("Only odd number of masters are supported!")
  143. }
  144. return
  145. }
  146. func (m *MasterOptions) toMasterOption(whiteList []string) *weed_server.MasterOption {
  147. return &weed_server.MasterOption{
  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. }