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.

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