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.

212 lines
7.9 KiB

5 years ago
6 years ago
5 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
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
6 years ago
5 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. "sort"
  9. "strings"
  10. "time"
  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. portGrpc *int
  26. ip *string
  27. ipBind *string
  28. metaFolder *string
  29. peers *string
  30. volumeSizeLimitMB *uint
  31. volumePreallocate *bool
  32. // pulseSeconds *int
  33. defaultReplication *string
  34. garbageThreshold *float64
  35. whiteList *string
  36. disableHttp *bool
  37. metricsAddress *string
  38. metricsIntervalSec *int
  39. raftResumeState *bool
  40. }
  41. func init() {
  42. cmdMaster.Run = runMaster // break init cycle
  43. m.port = cmdMaster.Flag.Int("port", 9333, "http listen port")
  44. m.portGrpc = cmdMaster.Flag.Int("port.grpc", 19333, "grpc listen port")
  45. m.ip = cmdMaster.Flag.String("ip", util.DetectedHostAddress(), "master <ip>|<server> address, also used as identifier")
  46. m.ipBind = cmdMaster.Flag.String("ip.bind", "", "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", "", "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. grace.SetupProfiling(*masterCpuProfile, *masterMemProfile)
  76. parent, _ := util.FullPath(*m.metaFolder).DirAndName()
  77. if util.FileExists(string(parent)) && !util.FileExists(*m.metaFolder) {
  78. os.MkdirAll(*m.metaFolder, 0755)
  79. }
  80. if err := util.TestFolderWritable(util.ResolvePath(*m.metaFolder)); err != nil {
  81. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *m.metaFolder, err)
  82. }
  83. var masterWhiteList []string
  84. if *m.whiteList != "" {
  85. masterWhiteList = strings.Split(*m.whiteList, ",")
  86. }
  87. if *m.volumeSizeLimitMB > util.VolumeSizeLimitGB*1000 {
  88. glog.Fatalf("volumeSizeLimitMB should be smaller than 30000")
  89. }
  90. startMaster(m, masterWhiteList)
  91. return true
  92. }
  93. func startMaster(masterOption MasterOptions, masterWhiteList []string) {
  94. backend.LoadConfiguration(util.GetViper())
  95. myMasterAddress, peers := checkPeers(*masterOption.ip, *masterOption.port, *masterOption.portGrpc, *masterOption.peers)
  96. r := mux.NewRouter()
  97. ms := weed_server.NewMasterServer(r, masterOption.toMasterOption(masterWhiteList), peers)
  98. listeningAddress := util.JoinHostPort(*masterOption.ipBind, *masterOption.port)
  99. glog.V(0).Infof("Start Seaweed Master %s at %s", util.Version(), listeningAddress)
  100. masterListener, e := util.NewListener(listeningAddress, 0)
  101. if e != nil {
  102. glog.Fatalf("Master startup error: %v", e)
  103. }
  104. // start raftServer
  105. raftServer, err := weed_server.NewRaftServer(security.LoadClientTLS(util.GetViper(), "grpc.master"),
  106. peers, myMasterAddress, util.ResolvePath(*masterOption.metaFolder), ms.Topo, *masterOption.raftResumeState)
  107. if raftServer == nil {
  108. glog.Fatalf("please verify %s is writable, see https://github.com/chrislusf/seaweedfs/issues/717: %s", *masterOption.metaFolder, err)
  109. }
  110. ms.SetRaftServer(raftServer)
  111. r.HandleFunc("/cluster/status", raftServer.StatusHandler).Methods("GET")
  112. // starting grpc server
  113. grpcPort := *masterOption.portGrpc
  114. grpcL, err := util.NewListener(util.JoinHostPort(*masterOption.ipBind, grpcPort), 0)
  115. if err != nil {
  116. glog.Fatalf("master failed to listen on grpc port %d: %v", grpcPort, err)
  117. }
  118. grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.master"))
  119. master_pb.RegisterSeaweedServer(grpcS, ms)
  120. protobuf.RegisterRaftServer(grpcS, raftServer)
  121. reflection.Register(grpcS)
  122. glog.V(0).Infof("Start Seaweed Master %s grpc server at %s:%d", util.Version(), *masterOption.ipBind, grpcPort)
  123. go grpcS.Serve(grpcL)
  124. go func() {
  125. time.Sleep(1500 * time.Millisecond)
  126. if ms.Topo.RaftServer.Leader() == "" && ms.Topo.RaftServer.IsLogEmpty() && isTheFirstOne(myMasterAddress, peers) {
  127. if ms.MasterClient.FindLeaderFromOtherPeers(myMasterAddress) == "" {
  128. raftServer.DoJoinCommand()
  129. }
  130. }
  131. }()
  132. go ms.MasterClient.KeepConnectedToMaster()
  133. // start http server
  134. httpS := &http.Server{Handler: r}
  135. go httpS.Serve(masterListener)
  136. select {}
  137. }
  138. func checkPeers(masterIp string, masterPort int, masterGrpcPort int, peers string) (masterAddress pb.ServerAddress, cleanedPeers []pb.ServerAddress) {
  139. glog.V(0).Infof("current: %s:%d peers:%s", masterIp, masterPort, peers)
  140. masterAddress = pb.NewServerAddress(masterIp, masterPort, masterGrpcPort)
  141. cleanedPeers = pb.ServerAddresses(peers).ToAddresses()
  142. hasSelf := false
  143. for _, peer := range cleanedPeers {
  144. if peer.ToHttpAddress() == masterAddress.ToHttpAddress() {
  145. hasSelf = true
  146. break
  147. }
  148. }
  149. if !hasSelf {
  150. cleanedPeers = append(cleanedPeers, masterAddress)
  151. }
  152. if len(cleanedPeers)%2 == 0 {
  153. glog.Fatalf("Only odd number of masters are supported: %+v", cleanedPeers)
  154. }
  155. return
  156. }
  157. func isTheFirstOne(self pb.ServerAddress, peers []pb.ServerAddress) bool {
  158. sort.Slice(peers, func(i, j int) bool {
  159. return strings.Compare(string(peers[i]), string(peers[j])) < 0
  160. })
  161. if len(peers) <= 0 {
  162. return true
  163. }
  164. return self == peers[0]
  165. }
  166. func (m *MasterOptions) toMasterOption(whiteList []string) *weed_server.MasterOption {
  167. masterAddress := pb.NewServerAddress(*m.ip, *m.port, *m.portGrpc)
  168. return &weed_server.MasterOption{
  169. Master: masterAddress,
  170. MetaFolder: *m.metaFolder,
  171. VolumeSizeLimitMB: uint32(*m.volumeSizeLimitMB),
  172. VolumePreallocate: *m.volumePreallocate,
  173. // PulseSeconds: *m.pulseSeconds,
  174. DefaultReplicaPlacement: *m.defaultReplication,
  175. GarbageThreshold: *m.garbageThreshold,
  176. WhiteList: whiteList,
  177. DisableHttp: *m.disableHttp,
  178. MetricsAddress: *m.metricsAddress,
  179. MetricsIntervalSec: *m.metricsIntervalSec,
  180. }
  181. }