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.

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