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.

233 lines
8.8 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 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. "runtime/pprof"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "time"
  15. )
  16. type ServerOptions struct {
  17. cpuprofile *string
  18. }
  19. var (
  20. serverOptions ServerOptions
  21. filerOptions FilerOptions
  22. )
  23. func init() {
  24. cmdServer.Run = runServer // break init cycle
  25. }
  26. var cmdServer = &Command{
  27. UsageLine: "server -port=8080 -dir=/tmp -volume.max=5 -ip=server_name",
  28. Short: "start a server, including volume server, and automatically elect a master server",
  29. Long: `start both a volume server to provide storage spaces
  30. and a master server to provide volume=>location mapping service and sequence number of file ids
  31. This is provided as a convenient way to start both volume server and master server.
  32. The servers are exactly the same as starting them separately.
  33. So other volume servers can use this embedded master server also.
  34. Optionally, one filer server can be started. Logically, filer servers should not be in a cluster.
  35. They run with meta data on disk, not shared. So each filer server is different.
  36. `,
  37. }
  38. var (
  39. serverIp = cmdServer.Flag.String("ip", "", "ip or server name")
  40. serverPublicIp = cmdServer.Flag.String("publicIp", "", "ip or server name")
  41. serverMaxCpu = cmdServer.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  42. serverTimeout = cmdServer.Flag.Int("idleTimeout", 10, "connection idle seconds")
  43. serverDataCenter = cmdServer.Flag.String("dataCenter", "", "current volume server's data center name")
  44. serverRack = cmdServer.Flag.String("rack", "", "current volume server's rack name")
  45. serverWhiteListOption = cmdServer.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  46. serverPeers = cmdServer.Flag.String("master.peers", "", "other master nodes in comma separated ip:masterPort list")
  47. serverGarbageThreshold = cmdServer.Flag.String("garbageThreshold", "0.3", "threshold to vacuum and reclaim spaces")
  48. masterPort = cmdServer.Flag.Int("master.port", 9333, "master server http listen port")
  49. masterMetaFolder = cmdServer.Flag.String("master.dir", "", "data directory to store meta data, default to same as -dir specified")
  50. masterVolumeSizeLimitMB = cmdServer.Flag.Uint("master.volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
  51. masterConfFile = cmdServer.Flag.String("master.conf", "/etc/weedfs/weedfs.conf", "xml configuration file")
  52. masterDefaultReplicaPlacement = cmdServer.Flag.String("master.defaultReplicaPlacement", "000", "Default replication type if not specified.")
  53. volumePort = cmdServer.Flag.Int("volume.port", 8080, "volume server http listen port")
  54. volumeDataFolders = cmdServer.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  55. volumeMaxDataVolumeCounts = cmdServer.Flag.String("volume.max", "7", "maximum numbers of volumes, count[,count]...")
  56. volumePulse = cmdServer.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
  57. volumeFixJpgOrientation = cmdServer.Flag.Bool("volume.images.fix.orientation", true, "Adjust jpg orientation when uploading.")
  58. isStartingFiler = cmdServer.Flag.Bool("filer", false, "whether to start filer")
  59. serverWhiteList []string
  60. )
  61. func init() {
  62. serverOptions.cpuprofile = cmdServer.Flag.String("cpuprofile", "", "write cpu profile to file")
  63. filerOptions.master = cmdServer.Flag.String("filer.master", "", "default to current master server")
  64. filerOptions.collection = cmdServer.Flag.String("filer.collection", "", "all data will be stored in this collection")
  65. filerOptions.port = cmdServer.Flag.Int("filer.port", 8888, "filer server http listen port")
  66. filerOptions.dir = cmdServer.Flag.String("filer.dir", "", "directory to store meta data, default to a 'filer' sub directory of what -mdir is specified")
  67. filerOptions.defaultReplicaPlacement = cmdServer.Flag.String("filer.defaultReplicaPlacement", "", "Default replication type if not specified during runtime.")
  68. }
  69. func runServer(cmd *Command, args []string) bool {
  70. if *serverOptions.cpuprofile != "" {
  71. f, err := os.Create(*serverOptions.cpuprofile)
  72. if err != nil {
  73. glog.Fatal(err)
  74. }
  75. pprof.StartCPUProfile(f)
  76. defer pprof.StopCPUProfile()
  77. }
  78. if *serverPublicIp == "" {
  79. if *serverIp == "" {
  80. *serverPublicIp = "localhost"
  81. } else {
  82. *serverPublicIp = *serverIp
  83. }
  84. }
  85. *filerOptions.master = *serverPublicIp + ":" + strconv.Itoa(*masterPort)
  86. if *filerOptions.defaultReplicaPlacement == "" {
  87. *filerOptions.defaultReplicaPlacement = *masterDefaultReplicaPlacement
  88. }
  89. if *serverMaxCpu < 1 {
  90. *serverMaxCpu = runtime.NumCPU()
  91. }
  92. runtime.GOMAXPROCS(*serverMaxCpu)
  93. folders := strings.Split(*volumeDataFolders, ",")
  94. maxCountStrings := strings.Split(*volumeMaxDataVolumeCounts, ",")
  95. maxCounts := make([]int, 0)
  96. for _, maxString := range maxCountStrings {
  97. if max, e := strconv.Atoi(maxString); e == nil {
  98. maxCounts = append(maxCounts, max)
  99. } else {
  100. glog.Fatalf("The max specified in -max not a valid number %s", maxString)
  101. }
  102. }
  103. if len(folders) != len(maxCounts) {
  104. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
  105. }
  106. for _, folder := range folders {
  107. if err := util.TestFolderWritable(folder); err != nil {
  108. glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
  109. }
  110. }
  111. if *masterMetaFolder == "" {
  112. *masterMetaFolder = folders[0]
  113. }
  114. if *filerOptions.dir == "" {
  115. *filerOptions.dir = *masterMetaFolder + "/filer"
  116. os.MkdirAll(*filerOptions.dir, 0700)
  117. }
  118. if err := util.TestFolderWritable(*masterMetaFolder); err != nil {
  119. glog.Fatalf("Check Meta Folder (-mdir=\"%s\") Writable: %s", *masterMetaFolder, err)
  120. }
  121. if err := util.TestFolderWritable(*filerOptions.dir); err != nil {
  122. glog.Fatalf("Check Mapping Meta Folder (-filer.dir=\"%s\") Writable: %s", *filerOptions.dir, err)
  123. }
  124. if *serverWhiteListOption != "" {
  125. serverWhiteList = strings.Split(*serverWhiteListOption, ",")
  126. }
  127. if *isStartingFiler {
  128. go func() {
  129. r := http.NewServeMux()
  130. _, nfs_err := weed_server.NewFilerServer(r, *filerOptions.port, *filerOptions.master, *filerOptions.dir, *filerOptions.collection)
  131. if nfs_err != nil {
  132. glog.Fatalf(nfs_err.Error())
  133. }
  134. glog.V(0).Infoln("Start Weed Filer", util.VERSION, "at port", strconv.Itoa(*filerOptions.port))
  135. filerListener, e := util.NewListener(
  136. ":"+strconv.Itoa(*filerOptions.port),
  137. time.Duration(10)*time.Second,
  138. )
  139. if e != nil {
  140. glog.Fatalf(e.Error())
  141. }
  142. if e := http.Serve(filerListener, r); e != nil {
  143. glog.Fatalf("Filer Fail to serve:%s", e.Error())
  144. }
  145. }()
  146. }
  147. var raftWaitForMaster sync.WaitGroup
  148. var volumeWait sync.WaitGroup
  149. raftWaitForMaster.Add(1)
  150. volumeWait.Add(1)
  151. go func() {
  152. r := mux.NewRouter()
  153. ms := weed_server.NewMasterServer(r, *masterPort, *masterMetaFolder,
  154. *masterVolumeSizeLimitMB, *volumePulse, *masterConfFile, *masterDefaultReplicaPlacement, *serverGarbageThreshold, serverWhiteList,
  155. )
  156. glog.V(0).Infoln("Start Weed Master", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*masterPort))
  157. masterListener, e := util.NewListener(*serverIp+":"+strconv.Itoa(*masterPort), time.Duration(*serverTimeout)*time.Second)
  158. if e != nil {
  159. glog.Fatalf(e.Error())
  160. }
  161. go func() {
  162. raftWaitForMaster.Wait()
  163. time.Sleep(100 * time.Millisecond)
  164. myAddress := *serverPublicIp + ":" + strconv.Itoa(*masterPort)
  165. var peers []string
  166. if *serverPeers != "" {
  167. peers = strings.Split(*serverPeers, ",")
  168. }
  169. raftServer := weed_server.NewRaftServer(r, peers, myAddress, *masterMetaFolder, ms.Topo, *volumePulse)
  170. ms.SetRaftServer(raftServer)
  171. volumeWait.Done()
  172. }()
  173. raftWaitForMaster.Done()
  174. if e := http.Serve(masterListener, r); e != nil {
  175. glog.Fatalf("Master Fail to serve:%s", e.Error())
  176. }
  177. }()
  178. volumeWait.Wait()
  179. time.Sleep(100 * time.Millisecond)
  180. r := http.NewServeMux()
  181. volumeServer := weed_server.NewVolumeServer(r, *serverIp, *volumePort, *serverPublicIp, folders, maxCounts,
  182. *serverIp+":"+strconv.Itoa(*masterPort), *volumePulse, *serverDataCenter, *serverRack, serverWhiteList,
  183. *volumeFixJpgOrientation,
  184. )
  185. glog.V(0).Infoln("Start Weed volume server", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*volumePort))
  186. volumeListener, e := util.NewListener(
  187. *serverIp+":"+strconv.Itoa(*volumePort),
  188. time.Duration(*serverTimeout)*time.Second,
  189. )
  190. if e != nil {
  191. glog.Fatalf(e.Error())
  192. }
  193. OnInterrupt(func() {
  194. volumeServer.Shutdown()
  195. pprof.StopCPUProfile()
  196. })
  197. if e := http.Serve(volumeListener, r); e != nil {
  198. glog.Fatalf("Fail to serve:%s", e.Error())
  199. }
  200. return true
  201. }