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.

301 lines
11 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
3 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
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. "net/http"
  4. "os"
  5. "sort"
  6. "strings"
  7. "time"
  8. "github.com/chrislusf/raft/protobuf"
  9. stats_collect "github.com/chrislusf/seaweedfs/weed/stats"
  10. "github.com/gorilla/mux"
  11. "github.com/spf13/viper"
  12. "google.golang.org/grpc/reflection"
  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. weed_server "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. portGrpc *int
  28. ip *string
  29. ipBind *string
  30. metaFolder *string
  31. peers *string
  32. volumeSizeLimitMB *uint
  33. volumePreallocate *bool
  34. // pulseSeconds *int
  35. defaultReplication *string
  36. garbageThreshold *float64
  37. whiteList *string
  38. disableHttp *bool
  39. metricsAddress *string
  40. metricsIntervalSec *int
  41. raftResumeState *bool
  42. metricsHttpPort *int
  43. heartbeatInterval *time.Duration
  44. electionTimeout *time.Duration
  45. raftHashicorp *bool
  46. raftBootstrap *bool
  47. }
  48. func init() {
  49. cmdMaster.Run = runMaster // break init cycle
  50. m.port = cmdMaster.Flag.Int("port", 9333, "http listen port")
  51. m.portGrpc = cmdMaster.Flag.Int("port.grpc", 0, "grpc listen port")
  52. m.ip = cmdMaster.Flag.String("ip", util.DetectedHostAddress(), "master <ip>|<server> address, also used as identifier")
  53. m.ipBind = cmdMaster.Flag.String("ip.bind", "", "ip address to bind to. If empty, default to same as -ip option.")
  54. m.metaFolder = cmdMaster.Flag.String("mdir", os.TempDir(), "data directory to store meta data")
  55. 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")
  56. m.volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  57. m.volumePreallocate = cmdMaster.Flag.Bool("volumePreallocate", false, "Preallocate disk space for volumes.")
  58. // m.pulseSeconds = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  59. m.defaultReplication = cmdMaster.Flag.String("defaultReplication", "", "Default replication type if not specified.")
  60. m.garbageThreshold = cmdMaster.Flag.Float64("garbageThreshold", 0.3, "threshold to vacuum and reclaim spaces")
  61. m.whiteList = cmdMaster.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  62. m.disableHttp = cmdMaster.Flag.Bool("disableHttp", false, "disable http requests, only gRPC operations are allowed.")
  63. m.metricsAddress = cmdMaster.Flag.String("metrics.address", "", "Prometheus gateway address <host>:<port>")
  64. m.metricsIntervalSec = cmdMaster.Flag.Int("metrics.intervalSeconds", 15, "Prometheus push interval in seconds")
  65. m.metricsHttpPort = cmdMaster.Flag.Int("metricsPort", 0, "Prometheus metrics listen port")
  66. m.raftResumeState = cmdMaster.Flag.Bool("resumeState", false, "resume previous state on start master server")
  67. m.heartbeatInterval = cmdMaster.Flag.Duration("heartbeatInterval", 300*time.Millisecond, "heartbeat interval of master servers, and will be randomly multiplied by [1, 1.25)")
  68. m.electionTimeout = cmdMaster.Flag.Duration("electionTimeout", 10*time.Second, "election timeout of master servers")
  69. m.raftHashicorp = cmdMaster.Flag.Bool("raftHashicorp", false, "use hashicorp raft")
  70. m.raftBootstrap = cmdMaster.Flag.Bool("raftBootstrap", false, "Whether to bootstrap the Raft cluster")
  71. }
  72. var cmdMaster = &Command{
  73. UsageLine: "master -port=9333",
  74. Short: "start a master server",
  75. Long: `start a master server to provide volume=>location mapping service and sequence number of file ids
  76. The configuration file "security.toml" is read from ".", "$HOME/.seaweedfs/", "/usr/local/etc/seaweedfs/", or "/etc/seaweedfs/", in that order.
  77. The example security.toml configuration file can be generated by "weed scaffold -config=security"
  78. `,
  79. }
  80. var (
  81. masterCpuProfile = cmdMaster.Flag.String("cpuprofile", "", "cpu profile output file")
  82. masterMemProfile = cmdMaster.Flag.String("memprofile", "", "memory profile output file")
  83. )
  84. func runMaster(cmd *Command, args []string) bool {
  85. util.LoadConfiguration("security", false)
  86. util.LoadConfiguration("master", false)
  87. grace.SetupProfiling(*masterCpuProfile, *masterMemProfile)
  88. parent, _ := util.FullPath(*m.metaFolder).DirAndName()
  89. if util.FileExists(string(parent)) && !util.FileExists(*m.metaFolder) {
  90. os.MkdirAll(*m.metaFolder, 0755)
  91. }
  92. if err := util.TestFolderWritable(util.ResolvePath(*m.metaFolder)); err != nil {
  93. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *m.metaFolder, err)
  94. }
  95. var masterWhiteList []string
  96. if *m.whiteList != "" {
  97. masterWhiteList = strings.Split(*m.whiteList, ",")
  98. }
  99. if *m.volumeSizeLimitMB > util.VolumeSizeLimitGB*1000 {
  100. glog.Fatalf("volumeSizeLimitMB should be smaller than 30000")
  101. }
  102. go stats_collect.StartMetricsServer(*m.metricsHttpPort)
  103. startMaster(m, masterWhiteList)
  104. return true
  105. }
  106. func startMaster(masterOption MasterOptions, masterWhiteList []string) {
  107. backend.LoadConfiguration(util.GetViper())
  108. if *masterOption.portGrpc == 0 {
  109. *masterOption.portGrpc = 10000 + *masterOption.port
  110. }
  111. if *masterOption.ipBind == "" {
  112. *masterOption.ipBind = *masterOption.ip
  113. }
  114. myMasterAddress, peers := checkPeers(*masterOption.ip, *masterOption.port, *masterOption.portGrpc, *masterOption.peers)
  115. masterPeers := make(map[string]pb.ServerAddress)
  116. for _, peer := range peers {
  117. masterPeers[string(peer)] = peer
  118. }
  119. r := mux.NewRouter()
  120. ms := weed_server.NewMasterServer(r, masterOption.toMasterOption(masterWhiteList), masterPeers)
  121. listeningAddress := util.JoinHostPort(*masterOption.ipBind, *masterOption.port)
  122. glog.V(0).Infof("Start Seaweed Master %s at %s", util.Version(), listeningAddress)
  123. masterListener, masterLocalListner, e := util.NewIpAndLocalListeners(*masterOption.ipBind, *masterOption.port, 0)
  124. if e != nil {
  125. glog.Fatalf("Master startup error: %v", e)
  126. }
  127. // start raftServer
  128. raftServerOption := &weed_server.RaftServerOption{
  129. GrpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.master"),
  130. Peers: masterPeers,
  131. ServerAddr: myMasterAddress,
  132. DataDir: util.ResolvePath(*masterOption.metaFolder),
  133. Topo: ms.Topo,
  134. RaftResumeState: *masterOption.raftResumeState,
  135. HeartbeatInterval: *masterOption.heartbeatInterval,
  136. ElectionTimeout: *masterOption.electionTimeout,
  137. RaftBootstrap: *m.raftBootstrap,
  138. }
  139. var raftServer *weed_server.RaftServer
  140. var err error
  141. if *m.raftHashicorp {
  142. if raftServer, err = weed_server.NewHashicorpRaftServer(raftServerOption); err != nil {
  143. glog.Fatalf("NewHashicorpRaftServer: %s", err)
  144. }
  145. } else {
  146. raftServer, err = weed_server.NewRaftServer(raftServerOption)
  147. if raftServer == nil {
  148. glog.Fatalf("please verify %s is writable, see https://github.com/chrislusf/seaweedfs/issues/717: %s", *masterOption.metaFolder, err)
  149. }
  150. }
  151. ms.SetRaftServer(raftServer)
  152. r.HandleFunc("/cluster/status", raftServer.StatusHandler).Methods("GET")
  153. if *m.raftHashicorp {
  154. r.HandleFunc("/raft/stats", raftServer.StatsRaftHandler).Methods("GET")
  155. }
  156. // starting grpc server
  157. grpcPort := *masterOption.portGrpc
  158. grpcL, grpcLocalL, err := util.NewIpAndLocalListeners(*masterOption.ipBind, grpcPort, 0)
  159. if err != nil {
  160. glog.Fatalf("master failed to listen on grpc port %d: %v", grpcPort, err)
  161. }
  162. grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.master"))
  163. master_pb.RegisterSeaweedServer(grpcS, ms)
  164. if *m.raftHashicorp {
  165. raftServer.TransportManager.Register(grpcS)
  166. } else {
  167. protobuf.RegisterRaftServer(grpcS, raftServer)
  168. }
  169. reflection.Register(grpcS)
  170. glog.V(0).Infof("Start Seaweed Master %s grpc server at %s:%d", util.Version(), *masterOption.ipBind, grpcPort)
  171. if grpcLocalL != nil {
  172. go grpcS.Serve(grpcLocalL)
  173. }
  174. go grpcS.Serve(grpcL)
  175. timeSleep := 1500 * time.Millisecond
  176. if !*m.raftHashicorp {
  177. go func() {
  178. time.Sleep(timeSleep)
  179. if ms.Topo.RaftServer.Leader() == "" && ms.Topo.RaftServer.IsLogEmpty() && isTheFirstOne(myMasterAddress, peers) {
  180. if ms.MasterClient.FindLeaderFromOtherPeers(myMasterAddress) == "" {
  181. raftServer.DoJoinCommand()
  182. }
  183. }
  184. }()
  185. }
  186. go ms.MasterClient.KeepConnectedToMaster()
  187. // start http server
  188. var (
  189. clientCertFile,
  190. certFile,
  191. keyFile string
  192. )
  193. useTLS := false
  194. useMTLS := false
  195. if viper.GetString("https.master.key") != "" {
  196. useTLS = true
  197. certFile = viper.GetString("https.master.cert")
  198. keyFile = viper.GetString("https.master.key")
  199. }
  200. if viper.GetString("https.master.ca") != "" {
  201. useMTLS = true
  202. clientCertFile = viper.GetString("https.master.ca")
  203. }
  204. httpS := &http.Server{Handler: r}
  205. if masterLocalListner != nil {
  206. go httpS.Serve(masterLocalListner)
  207. }
  208. if useMTLS {
  209. httpS.TLSConfig = security.LoadClientTLSHTTP(clientCertFile)
  210. }
  211. if useTLS {
  212. go httpS.ServeTLS(masterListener, certFile, keyFile)
  213. } else {
  214. go httpS.Serve(masterListener)
  215. }
  216. select {}
  217. }
  218. func checkPeers(masterIp string, masterPort int, masterGrpcPort int, peers string) (masterAddress pb.ServerAddress, cleanedPeers []pb.ServerAddress) {
  219. glog.V(0).Infof("current: %s:%d peers:%s", masterIp, masterPort, peers)
  220. masterAddress = pb.NewServerAddress(masterIp, masterPort, masterGrpcPort)
  221. cleanedPeers = pb.ServerAddresses(peers).ToAddresses()
  222. hasSelf := false
  223. for _, peer := range cleanedPeers {
  224. if peer.ToHttpAddress() == masterAddress.ToHttpAddress() {
  225. hasSelf = true
  226. break
  227. }
  228. }
  229. if !hasSelf {
  230. cleanedPeers = append(cleanedPeers, masterAddress)
  231. }
  232. if len(cleanedPeers)%2 == 0 {
  233. glog.Fatalf("Only odd number of masters are supported: %+v", cleanedPeers)
  234. }
  235. return
  236. }
  237. func isTheFirstOne(self pb.ServerAddress, peers []pb.ServerAddress) bool {
  238. sort.Slice(peers, func(i, j int) bool {
  239. return strings.Compare(string(peers[i]), string(peers[j])) < 0
  240. })
  241. if len(peers) <= 0 {
  242. return true
  243. }
  244. return self == peers[0]
  245. }
  246. func (m *MasterOptions) toMasterOption(whiteList []string) *weed_server.MasterOption {
  247. masterAddress := pb.NewServerAddress(*m.ip, *m.port, *m.portGrpc)
  248. return &weed_server.MasterOption{
  249. Master: masterAddress,
  250. MetaFolder: *m.metaFolder,
  251. VolumeSizeLimitMB: uint32(*m.volumeSizeLimitMB),
  252. VolumePreallocate: *m.volumePreallocate,
  253. // PulseSeconds: *m.pulseSeconds,
  254. DefaultReplicaPlacement: *m.defaultReplication,
  255. GarbageThreshold: *m.garbageThreshold,
  256. WhiteList: whiteList,
  257. DisableHttp: *m.disableHttp,
  258. MetricsAddress: *m.metricsAddress,
  259. MetricsIntervalSec: *m.metricsIntervalSec,
  260. }
  261. }