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.

104 lines
3.5 KiB

12 years ago
12 years ago
  1. package main
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/util"
  5. "code.google.com/p/weed-fs/go/weed/weed_server"
  6. "net/http"
  7. "os"
  8. "runtime"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func init() {
  14. cmdVolume.Run = runVolume // break init cycle
  15. }
  16. var cmdVolume = &Command{
  17. UsageLine: "volume -port=8080 -dir=/tmp -max=5 -ip=server_name -mserver=localhost:9333",
  18. Short: "start a volume server",
  19. Long: `start a volume server to provide storage spaces
  20. `,
  21. }
  22. var (
  23. vport = cmdVolume.Flag.Int("port", 8080, "http listen port")
  24. volumeFolders = cmdVolume.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  25. maxVolumeCounts = cmdVolume.Flag.String("max", "7", "maximum numbers of volumes, count[,count]...")
  26. ip = cmdVolume.Flag.String("ip", "", "ip or server name")
  27. publicIp = cmdVolume.Flag.String("publicIp", "", "Publicly accessible <ip|server_name>")
  28. masterNode = cmdVolume.Flag.String("mserver", "localhost:9333", "master server location")
  29. vpulse = cmdVolume.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than or equal to the master's setting")
  30. vTimeout = cmdVolume.Flag.Int("idleTimeout", 10, "connection idle seconds")
  31. vMaxCpu = cmdVolume.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  32. dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
  33. rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
  34. volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  35. fixJpgOrientation = cmdVolume.Flag.Bool("images.fix.orientation", true, "Adjust jpg orientation when uploading.")
  36. volumeWhiteList []string
  37. )
  38. func runVolume(cmd *Command, args []string) bool {
  39. if *vMaxCpu < 1 {
  40. *vMaxCpu = runtime.NumCPU()
  41. }
  42. runtime.GOMAXPROCS(*vMaxCpu)
  43. folders := strings.Split(*volumeFolders, ",")
  44. maxCountStrings := strings.Split(*maxVolumeCounts, ",")
  45. maxCounts := make([]int, 0)
  46. for _, maxString := range maxCountStrings {
  47. if max, e := strconv.Atoi(maxString); e == nil {
  48. maxCounts = append(maxCounts, max)
  49. } else {
  50. glog.Fatalf("The max specified in -max not a valid number %s", maxString)
  51. }
  52. }
  53. if len(folders) != len(maxCounts) {
  54. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
  55. }
  56. for _, folder := range folders {
  57. if err := util.TestFolderWritable(folder); err != nil {
  58. glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
  59. }
  60. }
  61. if *publicIp == "" {
  62. if *ip == "" {
  63. *publicIp = "localhost"
  64. } else {
  65. *publicIp = *ip
  66. }
  67. }
  68. if *volumeWhiteListOption != "" {
  69. volumeWhiteList = strings.Split(*volumeWhiteListOption, ",")
  70. }
  71. r := http.NewServeMux()
  72. volumeServer := weed_server.NewVolumeServer(r, *ip, *vport, *publicIp, folders, maxCounts,
  73. *masterNode, *vpulse, *dataCenter, *rack, volumeWhiteList,
  74. *fixJpgOrientation,
  75. )
  76. listeningAddress := *ip + ":" + strconv.Itoa(*vport)
  77. glog.V(0).Infoln("Start Weed volume server", util.VERSION, "at", listeningAddress)
  78. listener, e := util.NewListener(listeningAddress, time.Duration(*vTimeout)*time.Second)
  79. if e != nil {
  80. glog.Fatalf(e.Error())
  81. }
  82. OnInterrupt(func() {
  83. volumeServer.Shutdown()
  84. })
  85. if e := http.Serve(listener, r); e != nil {
  86. glog.Fatalf("Fail to serve:%s", e.Error())
  87. }
  88. return true
  89. }