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.

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