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.

94 lines
3.3 KiB

12 years ago
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", "localhost", "ip or server name")
  27. publicUrl = cmdVolume.Flag.String("publicUrl", "", "Publicly accessible <ip|server_name>:<port>")
  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. volumeWhiteList []string
  36. )
  37. func runVolume(cmd *Command, args []string) bool {
  38. if *vMaxCpu < 1 {
  39. *vMaxCpu = runtime.NumCPU()
  40. }
  41. runtime.GOMAXPROCS(*vMaxCpu)
  42. folders := strings.Split(*volumeFolders, ",")
  43. maxCountStrings := strings.Split(*maxVolumeCounts, ",")
  44. maxCounts := make([]int, 0)
  45. for _, maxString := range maxCountStrings {
  46. if max, e := strconv.Atoi(maxString); e == nil {
  47. maxCounts = append(maxCounts, max)
  48. } else {
  49. glog.Fatalf("The max specified in -max not a valid number %s", maxString)
  50. }
  51. }
  52. if len(folders) != len(maxCounts) {
  53. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
  54. }
  55. for _, folder := range folders {
  56. if err := util.TestFolderWritable(folder); err != nil {
  57. glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
  58. }
  59. }
  60. if *publicUrl == "" {
  61. *publicUrl = *ip + ":" + strconv.Itoa(*vport)
  62. }
  63. if *volumeWhiteListOption != "" {
  64. volumeWhiteList = strings.Split(*volumeWhiteListOption, ",")
  65. }
  66. r := http.NewServeMux()
  67. weed_server.NewVolumeServer(r, *ip, *vport, *publicUrl, folders, maxCounts,
  68. *masterNode, *vpulse, *dataCenter, *rack, volumeWhiteList,
  69. )
  70. glog.V(0).Infoln("Start Weed volume server", util.VERSION, "at http://"+*ip+":"+strconv.Itoa(*vport))
  71. listener, e := util.NewListener(
  72. *ip+":"+strconv.Itoa(*vport),
  73. time.Duration(*vTimeout)*time.Second,
  74. )
  75. if e != nil {
  76. glog.Fatalf(e.Error())
  77. }
  78. if e := http.Serve(listener, r); e != nil {
  79. glog.Fatalf("Fail to serve:%s", e.Error())
  80. }
  81. return true
  82. }