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.

183 lines
6.2 KiB

13 years ago
  1. package command
  2. import (
  3. "os"
  4. "runtime"
  5. "strconv"
  6. "strings"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "net/http"
  10. "github.com/chrislusf/seaweedfs/weed/storage"
  11. "github.com/chrislusf/seaweedfs/weed/server"
  12. "time"
  13. "runtime/pprof"
  14. )
  15. var (
  16. v VolumeServerOptions
  17. )
  18. type VolumeServerOptions struct {
  19. port *int
  20. publicPort *int
  21. folders []string
  22. folderMaxLimits []int
  23. ip *string
  24. publicUrl *string
  25. bindIp *string
  26. masters *string
  27. pulseSeconds *int
  28. idleConnectionTimeout *int
  29. maxCpu *int
  30. dataCenter *string
  31. rack *string
  32. whiteList []string
  33. indexType *string
  34. fixJpgOrientation *bool
  35. readRedirect *bool
  36. cpuProfile *string
  37. memProfile *string
  38. }
  39. func init() {
  40. cmdVolume.Run = runVolume // break init cycle
  41. v.port = cmdVolume.Flag.Int("port", 8080, "http listen port")
  42. v.publicPort = cmdVolume.Flag.Int("port.public", 0, "port opened to public")
  43. v.ip = cmdVolume.Flag.String("ip", "", "ip or server name")
  44. v.publicUrl = cmdVolume.Flag.String("publicUrl", "", "Publicly accessible address")
  45. v.bindIp = cmdVolume.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  46. v.masters = cmdVolume.Flag.String("mserver", "localhost:9333", "comma-separated master servers")
  47. v.pulseSeconds = cmdVolume.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than or equal to the master's setting")
  48. v.idleConnectionTimeout = cmdVolume.Flag.Int("idleTimeout", 30, "connection idle seconds")
  49. v.maxCpu = cmdVolume.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  50. v.dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
  51. v.rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
  52. v.indexType = cmdVolume.Flag.String("index", "memory", "Choose [memory|leveldb|boltdb|btree] mode for memory~performance balance.")
  53. v.fixJpgOrientation = cmdVolume.Flag.Bool("images.fix.orientation", true, "Adjust jpg orientation when uploading.")
  54. v.readRedirect = cmdVolume.Flag.Bool("read.redirect", true, "Redirect moved or non-local volumes.")
  55. v.cpuProfile = cmdVolume.Flag.String("cpuprofile", "", "cpu profile output file")
  56. v.memProfile = cmdVolume.Flag.String("memprofile", "", "memory profile output file")
  57. }
  58. var cmdVolume = &Command{
  59. UsageLine: "volume -port=8080 -dir=/tmp -max=5 -ip=server_name -mserver=localhost:9333",
  60. Short: "start a volume server",
  61. Long: `start a volume server to provide storage spaces
  62. `,
  63. }
  64. var (
  65. volumeFolders = cmdVolume.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  66. maxVolumeCounts = cmdVolume.Flag.String("max", "7", "maximum numbers of volumes, count[,count]...")
  67. volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  68. )
  69. func runVolume(cmd *Command, args []string) bool {
  70. if *v.maxCpu < 1 {
  71. *v.maxCpu = runtime.NumCPU()
  72. }
  73. runtime.GOMAXPROCS(*v.maxCpu)
  74. util.SetupProfiling(*v.cpuProfile, *v.memProfile)
  75. v.startVolumeServer(*volumeFolders, *maxVolumeCounts, *volumeWhiteListOption)
  76. return true
  77. }
  78. func (v VolumeServerOptions) startVolumeServer(volumeFolders, maxVolumeCounts, volumeWhiteListOption string) {
  79. //Set multiple folders and each folder's max volume count limit'
  80. v.folders = strings.Split(volumeFolders, ",")
  81. maxCountStrings := strings.Split(maxVolumeCounts, ",")
  82. for _, maxString := range maxCountStrings {
  83. if max, e := strconv.Atoi(maxString); e == nil {
  84. v.folderMaxLimits = append(v.folderMaxLimits, max)
  85. } else {
  86. glog.Fatalf("The max specified in -max not a valid number %s", maxString)
  87. }
  88. }
  89. if len(v.folders) != len(v.folderMaxLimits) {
  90. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(v.folders), len(v.folderMaxLimits))
  91. }
  92. for _, folder := range v.folders {
  93. if err := util.TestFolderWritable(folder); err != nil {
  94. glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
  95. }
  96. }
  97. //security related white list configuration
  98. if volumeWhiteListOption != "" {
  99. v.whiteList = strings.Split(volumeWhiteListOption, ",")
  100. }
  101. if *v.ip == "" {
  102. *v.ip = "127.0.0.1"
  103. }
  104. if *v.publicPort == 0 {
  105. *v.publicPort = *v.port
  106. }
  107. if *v.publicUrl == "" {
  108. *v.publicUrl = *v.ip + ":" + strconv.Itoa(*v.publicPort)
  109. }
  110. isSeperatedPublicPort := *v.publicPort != *v.port
  111. volumeMux := http.NewServeMux()
  112. publicVolumeMux := volumeMux
  113. if isSeperatedPublicPort {
  114. publicVolumeMux = http.NewServeMux()
  115. }
  116. volumeNeedleMapKind := storage.NeedleMapInMemory
  117. switch *v.indexType {
  118. case "leveldb":
  119. volumeNeedleMapKind = storage.NeedleMapLevelDb
  120. case "boltdb":
  121. volumeNeedleMapKind = storage.NeedleMapBoltDb
  122. case "btree":
  123. volumeNeedleMapKind = storage.NeedleMapBtree
  124. }
  125. masters := *v.masters
  126. volumeServer := weed_server.NewVolumeServer(volumeMux, publicVolumeMux,
  127. *v.ip, *v.port, *v.publicUrl,
  128. v.folders, v.folderMaxLimits,
  129. volumeNeedleMapKind,
  130. strings.Split(masters, ","), *v.pulseSeconds, *v.dataCenter, *v.rack,
  131. v.whiteList,
  132. *v.fixJpgOrientation, *v.readRedirect,
  133. )
  134. listeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.port)
  135. glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "at", listeningAddress)
  136. listener, e := util.NewListener(listeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
  137. if e != nil {
  138. glog.Fatalf("Volume server listener error:%v", e)
  139. }
  140. if isSeperatedPublicPort {
  141. publicListeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.publicPort)
  142. glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "public at", publicListeningAddress)
  143. publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
  144. if e != nil {
  145. glog.Fatalf("Volume server listener error:%v", e)
  146. }
  147. go func() {
  148. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  149. glog.Fatalf("Volume server fail to serve public: %v", e)
  150. }
  151. }()
  152. }
  153. util.OnInterrupt(func() {
  154. volumeServer.Shutdown()
  155. pprof.StopCPUProfile()
  156. })
  157. if e := http.Serve(listener, volumeMux); e != nil {
  158. glog.Fatalf("Volume server fail to serve: %v", e)
  159. }
  160. }