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.

197 lines
6.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
12 years ago
  1. package command
  2. import (
  3. "net/http"
  4. "os"
  5. "runtime"
  6. "runtime/pprof"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  12. "github.com/chrislusf/seaweedfs/weed/server"
  13. "github.com/chrislusf/seaweedfs/weed/storage"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. "google.golang.org/grpc/reflection"
  16. )
  17. var (
  18. v VolumeServerOptions
  19. )
  20. type VolumeServerOptions struct {
  21. port *int
  22. publicPort *int
  23. folders []string
  24. folderMaxLimits []int
  25. ip *string
  26. publicUrl *string
  27. bindIp *string
  28. masters *string
  29. pulseSeconds *int
  30. idleConnectionTimeout *int
  31. maxCpu *int
  32. dataCenter *string
  33. rack *string
  34. whiteList []string
  35. indexType *string
  36. fixJpgOrientation *bool
  37. readRedirect *bool
  38. cpuProfile *string
  39. memProfile *string
  40. }
  41. func init() {
  42. cmdVolume.Run = runVolume // break init cycle
  43. v.port = cmdVolume.Flag.Int("port", 8080, "http listen port")
  44. v.publicPort = cmdVolume.Flag.Int("port.public", 0, "port opened to public")
  45. v.ip = cmdVolume.Flag.String("ip", "", "ip or server name")
  46. v.publicUrl = cmdVolume.Flag.String("publicUrl", "", "Publicly accessible address")
  47. v.bindIp = cmdVolume.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  48. v.masters = cmdVolume.Flag.String("mserver", "localhost:9333", "comma-separated master servers")
  49. v.pulseSeconds = cmdVolume.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than or equal to the master's setting")
  50. v.idleConnectionTimeout = cmdVolume.Flag.Int("idleTimeout", 30, "connection idle seconds")
  51. v.maxCpu = cmdVolume.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
  52. v.dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
  53. v.rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
  54. v.indexType = cmdVolume.Flag.String("index", "memory", "Choose [memory|leveldb|boltdb|btree] mode for memory~performance balance.")
  55. v.fixJpgOrientation = cmdVolume.Flag.Bool("images.fix.orientation", true, "Adjust jpg orientation when uploading.")
  56. v.readRedirect = cmdVolume.Flag.Bool("read.redirect", true, "Redirect moved or non-local volumes.")
  57. v.cpuProfile = cmdVolume.Flag.String("cpuprofile", "", "cpu profile output file")
  58. v.memProfile = cmdVolume.Flag.String("memprofile", "", "memory profile output file")
  59. }
  60. var cmdVolume = &Command{
  61. UsageLine: "volume -port=8080 -dir=/tmp -max=5 -ip=server_name -mserver=localhost:9333",
  62. Short: "start a volume server",
  63. Long: `start a volume server to provide storage spaces
  64. `,
  65. }
  66. var (
  67. volumeFolders = cmdVolume.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
  68. maxVolumeCounts = cmdVolume.Flag.String("max", "7", "maximum numbers of volumes, count[,count]...")
  69. volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
  70. )
  71. func runVolume(cmd *Command, args []string) bool {
  72. if *v.maxCpu < 1 {
  73. *v.maxCpu = runtime.NumCPU()
  74. }
  75. runtime.GOMAXPROCS(*v.maxCpu)
  76. util.SetupProfiling(*v.cpuProfile, *v.memProfile)
  77. v.startVolumeServer(*volumeFolders, *maxVolumeCounts, *volumeWhiteListOption)
  78. return true
  79. }
  80. func (v VolumeServerOptions) startVolumeServer(volumeFolders, maxVolumeCounts, volumeWhiteListOption string) {
  81. //Set multiple folders and each folder's max volume count limit'
  82. v.folders = strings.Split(volumeFolders, ",")
  83. maxCountStrings := strings.Split(maxVolumeCounts, ",")
  84. for _, maxString := range maxCountStrings {
  85. if max, e := strconv.Atoi(maxString); e == nil {
  86. v.folderMaxLimits = append(v.folderMaxLimits, max)
  87. } else {
  88. glog.Fatalf("The max specified in -max not a valid number %s", maxString)
  89. }
  90. }
  91. if len(v.folders) != len(v.folderMaxLimits) {
  92. glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(v.folders), len(v.folderMaxLimits))
  93. }
  94. for _, folder := range v.folders {
  95. if err := util.TestFolderWritable(folder); err != nil {
  96. glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
  97. }
  98. }
  99. //security related white list configuration
  100. if volumeWhiteListOption != "" {
  101. v.whiteList = strings.Split(volumeWhiteListOption, ",")
  102. }
  103. if *v.ip == "" {
  104. *v.ip = "127.0.0.1"
  105. }
  106. if *v.publicPort == 0 {
  107. *v.publicPort = *v.port
  108. }
  109. if *v.publicUrl == "" {
  110. *v.publicUrl = *v.ip + ":" + strconv.Itoa(*v.publicPort)
  111. }
  112. isSeperatedPublicPort := *v.publicPort != *v.port
  113. volumeMux := http.NewServeMux()
  114. publicVolumeMux := volumeMux
  115. if isSeperatedPublicPort {
  116. publicVolumeMux = http.NewServeMux()
  117. }
  118. volumeNeedleMapKind := storage.NeedleMapInMemory
  119. switch *v.indexType {
  120. case "leveldb":
  121. volumeNeedleMapKind = storage.NeedleMapLevelDb
  122. case "boltdb":
  123. volumeNeedleMapKind = storage.NeedleMapBoltDb
  124. case "btree":
  125. volumeNeedleMapKind = storage.NeedleMapBtree
  126. }
  127. masters := *v.masters
  128. volumeServer := weed_server.NewVolumeServer(volumeMux, publicVolumeMux,
  129. *v.ip, *v.port, *v.publicUrl,
  130. v.folders, v.folderMaxLimits,
  131. volumeNeedleMapKind,
  132. strings.Split(masters, ","), *v.pulseSeconds, *v.dataCenter, *v.rack,
  133. v.whiteList,
  134. *v.fixJpgOrientation, *v.readRedirect,
  135. )
  136. listeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.port)
  137. glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "at", listeningAddress)
  138. listener, e := util.NewListener(listeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
  139. if e != nil {
  140. glog.Fatalf("Volume server listener error:%v", e)
  141. }
  142. if isSeperatedPublicPort {
  143. publicListeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.publicPort)
  144. glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "public at", publicListeningAddress)
  145. publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
  146. if e != nil {
  147. glog.Fatalf("Volume server listener error:%v", e)
  148. }
  149. go func() {
  150. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  151. glog.Fatalf("Volume server fail to serve public: %v", e)
  152. }
  153. }()
  154. }
  155. util.OnInterrupt(func() {
  156. volumeServer.Shutdown()
  157. pprof.StopCPUProfile()
  158. })
  159. // starting grpc server
  160. grpcPort := *v.port + 10000
  161. grpcL, err := util.NewListener(*v.bindIp+":"+strconv.Itoa(grpcPort), 0)
  162. if err != nil {
  163. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  164. }
  165. grpcS := util.NewGrpcServer()
  166. volume_server_pb.RegisterVolumeServerServer(grpcS, volumeServer)
  167. reflection.Register(grpcS)
  168. go grpcS.Serve(grpcL)
  169. if e := http.Serve(listener, volumeMux); e != nil {
  170. glog.Fatalf("Volume server fail to serve: %v", e)
  171. }
  172. }