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.

214 lines
7.6 KiB

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