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.

219 lines
7.7 KiB

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