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.

145 lines
5.9 KiB

  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. cmdServer.Run = runServer // break init cycle
  16. }
  17. var cmdServer = &Command{
  18. UsageLine: "server -port=8080 -dir=/tmp -max=5 -ip=server_name",
  19. Short: "start a server, including volume server, and automatically elect a master server",
  20. Long: `start both a volume server to provide storage spaces
  21. and a master server to provide volume=>location mapping service and sequence number of file ids
  22. This is provided as a convenient way to start both volume server and master server.
  23. The servers are the same as starting them separately.
  24. So other volume servers can use this embedded master server also.
  25. However, this may change very soon.
  26. The target is to start both volume server and embedded master server on all instances,
  27. and use a leader election process to auto choose a master server.
  28. `,
  29. }
  30. var (
  31. serverIp = cmdServer.Flag.String("ip", "localhost", "ip or server name")
  32. serverMaxCpu = cmdServer.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  33. serverReadTimeout = cmdServer.Flag.Int("readTimeout", 30, "connection read timeout in seconds. Increase this if uploading large files.")
  34. serverDataCenter = cmdServer.Flag.String("dataCenter", "", "current volume server's data center name")
  35. serverRack = cmdServer.Flag.String("rack", "", "current volume server's rack name")
  36. serverWhiteListOption = cmdServer.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  37. serverPeers = cmdServer.Flag.String("peers", "", "other master nodes in comma separated ip:masterPort list")
  38. masterPort = cmdServer.Flag.Int("masterPort", 9333, "master server http listen port")
  39. masterMetaFolder = cmdServer.Flag.String("mdir", "", "data directory to store meta data, default to same as -dir specified")
  40. masterVolumeSizeLimitMB = cmdServer.Flag.Uint("volumeSizeLimitMB", 32*1000, "Default Volume Size in MegaBytes")
  41. masterConfFile = cmdServer.Flag.String("conf", "/etc/weedfs/weedfs.conf", "xml configuration file")
  42. masterDefaultReplicaPlacement = cmdServer.Flag.String("defaultReplicaPlacement", "000", "Default replication type if not specified.")
  43. volumePort = cmdServer.Flag.Int("port", 8080, "volume server http listen port")
  44. volumePublicUrl = cmdServer.Flag.String("publicUrl", "", "Publicly accessible <ip|server_name>:<port>")
  45. volumeDataFolders = cmdServer.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  46. volumeMaxDataVolumeCounts = cmdServer.Flag.String("max", "7", "maximum numbers of volumes, count[,count]...")
  47. volumePulse = cmdServer.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than the master's setting")
  48. serverWhiteList []string
  49. )
  50. func runServer(cmd *Command, args []string) bool {
  51. if *serverMaxCpu < 1 {
  52. *serverMaxCpu = runtime.NumCPU()
  53. }
  54. runtime.GOMAXPROCS(*serverMaxCpu)
  55. if *masterMetaFolder == "" {
  56. *masterMetaFolder = *volumeDataFolders
  57. }
  58. if err := util.TestFolderWritable(*masterMetaFolder); err != nil {
  59. glog.Fatalf("Check Meta Folder (-mdir) Writable %s : %s", *masterMetaFolder, err)
  60. }
  61. folders := strings.Split(*volumeDataFolders, ",")
  62. maxCountStrings := strings.Split(*volumeMaxDataVolumeCounts, ",")
  63. maxCounts := make([]int, 0)
  64. for _, maxString := range maxCountStrings {
  65. if max, e := strconv.Atoi(maxString); e == nil {
  66. maxCounts = append(maxCounts, max)
  67. } else {
  68. glog.Fatalf("The max specified in -max not a valid number %s", max)
  69. }
  70. }
  71. if len(folders) != len(maxCounts) {
  72. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
  73. }
  74. for _, folder := range folders {
  75. if err := util.TestFolderWritable(folder); err != nil {
  76. glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
  77. }
  78. }
  79. if *volumePublicUrl == "" {
  80. *volumePublicUrl = *serverIp + ":" + strconv.Itoa(*volumePort)
  81. }
  82. if *serverWhiteListOption != "" {
  83. serverWhiteList = strings.Split(*serverWhiteListOption, ",")
  84. }
  85. go func() {
  86. r := mux.NewRouter()
  87. ms := weed_server.NewMasterServer(r, VERSION, *masterPort, *masterMetaFolder,
  88. *masterVolumeSizeLimitMB, *volumePulse, *masterConfFile, *masterDefaultReplicaPlacement, *garbageThreshold, serverWhiteList,
  89. )
  90. glog.V(0).Infoln("Start Weed Master", VERSION, "at port", *serverIp+":"+strconv.Itoa(*masterPort))
  91. masterServer := &http.Server{
  92. Addr: *serverIp + ":" + strconv.Itoa(*masterPort),
  93. Handler: r,
  94. ReadTimeout: time.Duration(*serverReadTimeout) * time.Second,
  95. }
  96. go func() {
  97. time.Sleep(100 * time.Millisecond)
  98. var peers []string
  99. if *serverPeers != "" {
  100. peers = strings.Split(*serverPeers, ",")
  101. }
  102. raftServer := weed_server.NewRaftServer(r, VERSION, peers, *serverIp+":"+strconv.Itoa(*masterPort), *masterMetaFolder)
  103. ms.SetRaftServer(raftServer)
  104. }()
  105. e := masterServer.ListenAndServe()
  106. if e != nil {
  107. glog.Fatalf("Fail to start master:%s", e)
  108. }
  109. }()
  110. time.Sleep(100 * time.Millisecond)
  111. r := http.NewServeMux()
  112. weed_server.NewVolumeServer(r, VERSION, *serverIp, *volumePort, *volumePublicUrl, folders, maxCounts,
  113. *serverIp+":"+strconv.Itoa(*masterPort), *volumePulse, *serverDataCenter, *serverRack, serverWhiteList,
  114. )
  115. glog.V(0).Infoln("Start Weed volume server", VERSION, "at http://"+*serverIp+":"+strconv.Itoa(*volumePort))
  116. volumeServer := &http.Server{
  117. Addr: *serverIp + ":" + strconv.Itoa(*volumePort),
  118. Handler: r,
  119. ReadTimeout: (time.Duration(*serverReadTimeout) * time.Second),
  120. }
  121. e := volumeServer.ListenAndServe()
  122. if e != nil {
  123. glog.Fatalf("Fail to start volume:%s", e.Error())
  124. }
  125. return true
  126. }