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.

163 lines
5.5 KiB

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