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.

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