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.

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