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.

155 lines
5.2 KiB

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