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.

172 lines
6.0 KiB

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