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.

88 lines
3.1 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. package main
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/util"
  5. "code.google.com/p/weed-fs/go/weed/weed_server"
  6. "github.com/gorilla/mux"
  7. "net/http"
  8. "os"
  9. "runtime"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. func init() {
  15. cmdMaster.Run = runMaster // break init cycle
  16. }
  17. var cmdMaster = &Command{
  18. UsageLine: "master -port=9333",
  19. Short: "start a master server",
  20. Long: `start a master server to provide volume=>location mapping service
  21. and sequence number of file ids
  22. `,
  23. }
  24. var (
  25. mport = cmdMaster.Flag.Int("port", 9333, "http listen port")
  26. masterIp = cmdMaster.Flag.String("ip", "localhost", "master ip address")
  27. metaFolder = cmdMaster.Flag.String("mdir", os.TempDir(), "data directory to store meta data")
  28. masterPeers = cmdMaster.Flag.String("peers", "", "other master nodes in comma separated ip:port list")
  29. volumeSizeLimitMB = cmdMaster.Flag.Uint("volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  30. mpulse = cmdMaster.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  31. confFile = cmdMaster.Flag.String("conf", "/etc/weedfs/weedfs.conf", "xml configuration file")
  32. defaultReplicaPlacement = cmdMaster.Flag.String("defaultReplication", "000", "Default replication type if not specified.")
  33. mTimeout = cmdMaster.Flag.Int("idleTimeout", 10, "connection idle seconds")
  34. mMaxCpu = cmdMaster.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  35. garbageThreshold = cmdMaster.Flag.String("garbageThreshold", "0.3", "threshold to vacuum and reclaim spaces")
  36. masterWhiteListOption = cmdMaster.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  37. masterWhiteList []string
  38. )
  39. func runMaster(cmd *Command, args []string) bool {
  40. if *mMaxCpu < 1 {
  41. *mMaxCpu = runtime.NumCPU()
  42. }
  43. runtime.GOMAXPROCS(*mMaxCpu)
  44. if err := util.TestFolderWritable(*metaFolder); err != nil {
  45. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *metaFolder, err)
  46. }
  47. if *masterWhiteListOption != "" {
  48. masterWhiteList = strings.Split(*masterWhiteListOption, ",")
  49. }
  50. r := mux.NewRouter()
  51. ms := weed_server.NewMasterServer(r, *mport, *metaFolder,
  52. *volumeSizeLimitMB, *mpulse, *confFile, *defaultReplicaPlacement, *garbageThreshold, masterWhiteList,
  53. )
  54. glog.V(0).Infoln("Start Weed Master", util.VERSION, "at port", *masterIp+":"+strconv.Itoa(*mport))
  55. listener, e := util.NewListener(
  56. *masterIp+":"+strconv.Itoa(*mport),
  57. time.Duration(*mTimeout)*time.Second,
  58. )
  59. if e != nil {
  60. glog.Fatalf(e.Error())
  61. }
  62. go func() {
  63. time.Sleep(100 * time.Millisecond)
  64. myAddress := *masterIp + ":" + strconv.Itoa(*mport)
  65. var peers []string
  66. if *masterPeers != "" {
  67. peers = strings.Split(*masterPeers, ",")
  68. }
  69. raftServer := weed_server.NewRaftServer(r, peers, myAddress, *metaFolder, ms.Topo, *mpulse)
  70. ms.SetRaftServer(raftServer)
  71. }()
  72. if e := http.Serve(listener, r); e != nil {
  73. glog.Fatalf("Fail to serve:%s", e.Error())
  74. }
  75. return true
  76. }