volume.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. dataCenter *string
  34. rack *string
  35. whiteList []string
  36. indexType *string
  37. fixJpgOrientation *bool
  38. readRedirect *bool
  39. cpuProfile *string
  40. memProfile *string
  41. compactionMBPerSecond *int
  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.dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
  54. v.rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
  55. v.indexType = cmdVolume.Flag.String("index", "memory", "Choose [memory|leveldb|leveldbMedium|leveldbLarge] mode for memory~performance balance.")
  56. v.fixJpgOrientation = cmdVolume.Flag.Bool("images.fix.orientation", false, "Adjust jpg orientation when uploading.")
  57. v.readRedirect = cmdVolume.Flag.Bool("read.redirect", true, "Redirect moved or non-local volumes.")
  58. v.cpuProfile = cmdVolume.Flag.String("cpuprofile", "", "cpu profile output file")
  59. v.memProfile = cmdVolume.Flag.String("memprofile", "", "memory profile output file")
  60. v.compactionMBPerSecond = cmdVolume.Flag.Int("compactionMBps", 0, "limit background compaction or copying speed in mega bytes per second")
  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. util.LoadConfiguration("security", false)
  75. runtime.GOMAXPROCS(runtime.NumCPU())
  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 "leveldbMedium":
  123. volumeNeedleMapKind = storage.NeedleMapLevelDbMedium
  124. case "leveldbLarge":
  125. volumeNeedleMapKind = storage.NeedleMapLevelDbLarge
  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. *v.compactionMBPerSecond,
  136. )
  137. listeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.port)
  138. glog.V(0).Infof("Start Seaweed volume server %s at %s", util.VERSION, listeningAddress)
  139. listener, e := util.NewListener(listeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
  140. if e != nil {
  141. glog.Fatalf("Volume server listener error:%v", e)
  142. }
  143. if isSeperatedPublicPort {
  144. publicListeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.publicPort)
  145. glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "public at", publicListeningAddress)
  146. publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
  147. if e != nil {
  148. glog.Fatalf("Volume server listener error:%v", e)
  149. }
  150. go func() {
  151. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  152. glog.Fatalf("Volume server fail to serve public: %v", e)
  153. }
  154. }()
  155. }
  156. util.OnInterrupt(func() {
  157. volumeServer.Shutdown()
  158. pprof.StopCPUProfile()
  159. })
  160. // starting grpc server
  161. grpcPort := *v.port + 10000
  162. grpcL, err := util.NewListener(*v.bindIp+":"+strconv.Itoa(grpcPort), 0)
  163. if err != nil {
  164. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  165. }
  166. grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "volume"))
  167. volume_server_pb.RegisterVolumeServerServer(grpcS, volumeServer)
  168. reflection.Register(grpcS)
  169. go grpcS.Serve(grpcL)
  170. if viper.GetString("https.volume.key") != "" {
  171. if e := http.ServeTLS(listener, volumeMux,
  172. viper.GetString("https.volume.cert"), viper.GetString("https.volume.key")); e != nil {
  173. glog.Fatalf("Volume server fail to serve: %v", e)
  174. }
  175. } else {
  176. if e := http.Serve(listener, volumeMux); e != nil {
  177. glog.Fatalf("Volume server fail to serve: %v", e)
  178. }
  179. }
  180. }