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.

209 lines
7.2 KiB

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