filer.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package command
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "google.golang.org/grpc/reflection"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/security"
  13. "github.com/chrislusf/seaweedfs/weed/server"
  14. stats_collect "github.com/chrislusf/seaweedfs/weed/stats"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. var (
  18. f FilerOptions
  19. filerStartS3 *bool
  20. filerS3Options S3Options
  21. )
  22. type FilerOptions struct {
  23. masters *string
  24. ip *string
  25. bindIp *string
  26. port *int
  27. publicPort *int
  28. collection *string
  29. defaultReplicaPlacement *string
  30. disableDirListing *bool
  31. maxMB *int
  32. dirListingLimit *int
  33. dataCenter *string
  34. rack *string
  35. enableNotification *bool
  36. disableHttp *bool
  37. cipher *bool
  38. peers *string
  39. metricsHttpPort *int
  40. saveToFilerLimit *int
  41. defaultLevelDbDirectory *string
  42. }
  43. func init() {
  44. cmdFiler.Run = runFiler // break init cycle
  45. f.masters = cmdFiler.Flag.String("master", "localhost:9333", "comma-separated master servers")
  46. f.collection = cmdFiler.Flag.String("collection", "", "all data will be stored in this collection")
  47. f.ip = cmdFiler.Flag.String("ip", util.DetectedHostAddress(), "filer server http listen ip address")
  48. f.bindIp = cmdFiler.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  49. f.port = cmdFiler.Flag.Int("port", 8888, "filer server http listen port")
  50. f.publicPort = cmdFiler.Flag.Int("port.readonly", 0, "readonly port opened to public")
  51. f.defaultReplicaPlacement = cmdFiler.Flag.String("defaultReplicaPlacement", "", "default replication type. If not specified, use master setting.")
  52. f.disableDirListing = cmdFiler.Flag.Bool("disableDirListing", false, "turn off directory listing")
  53. f.maxMB = cmdFiler.Flag.Int("maxMB", 32, "split files larger than the limit")
  54. f.dirListingLimit = cmdFiler.Flag.Int("dirListLimit", 100000, "limit sub dir listing size")
  55. f.dataCenter = cmdFiler.Flag.String("dataCenter", "", "prefer to read and write to volumes in this data center")
  56. f.rack = cmdFiler.Flag.String("rack", "", "prefer to write to volumes in this rack")
  57. f.disableHttp = cmdFiler.Flag.Bool("disableHttp", false, "disable http request, only gRpc operations are allowed")
  58. f.cipher = cmdFiler.Flag.Bool("encryptVolumeData", false, "encrypt data on volume servers")
  59. f.peers = cmdFiler.Flag.String("peers", "", "all filers sharing the same filer store in comma separated ip:port list")
  60. f.metricsHttpPort = cmdFiler.Flag.Int("metricsPort", 0, "Prometheus metrics listen port")
  61. f.saveToFilerLimit = cmdFiler.Flag.Int("saveToFilerLimit", 0, "files smaller than this limit will be saved in filer store")
  62. f.defaultLevelDbDirectory = cmdFiler.Flag.String("defaultStoreDir", ".", "if filer.toml is empty, use an embedded filer store in the directory")
  63. // start s3 on filer
  64. filerStartS3 = cmdFiler.Flag.Bool("s3", false, "whether to start S3 gateway")
  65. filerS3Options.port = cmdFiler.Flag.Int("s3.port", 8333, "s3 server http listen port")
  66. filerS3Options.domainName = cmdFiler.Flag.String("s3.domainName", "", "suffix of the host name in comma separated list, {bucket}.{domainName}")
  67. filerS3Options.tlsPrivateKey = cmdFiler.Flag.String("s3.key.file", "", "path to the TLS private key file")
  68. filerS3Options.tlsCertificate = cmdFiler.Flag.String("s3.cert.file", "", "path to the TLS certificate file")
  69. filerS3Options.config = cmdFiler.Flag.String("s3.config", "", "path to the config file")
  70. filerS3Options.allowEmptyFolder = cmdFiler.Flag.Bool("s3.allowEmptyFolder", false, "allow empty folders")
  71. }
  72. var cmdFiler = &Command{
  73. UsageLine: "filer -port=8888 -master=<ip:port>[,<ip:port>]*",
  74. Short: "start a file server that points to a master server, or a list of master servers",
  75. Long: `start a file server which accepts REST operation for any files.
  76. //create or overwrite the file, the directories /path/to will be automatically created
  77. POST /path/to/file
  78. //get the file content
  79. GET /path/to/file
  80. //create or overwrite the file, the filename in the multipart request will be used
  81. POST /path/to/
  82. //return a json format subdirectory and files listing
  83. GET /path/to/
  84. The configuration file "filer.toml" is read from ".", "$HOME/.seaweedfs/", "/usr/local/etc/seaweedfs/", or "/etc/seaweedfs/", in that order.
  85. If the "filer.toml" is not found, an embedded filer store will be craeted under "-defaultStoreDir".
  86. The example filer.toml configuration file can be generated by "weed scaffold -config=filer"
  87. `,
  88. }
  89. func runFiler(cmd *Command, args []string) bool {
  90. util.LoadConfiguration("security", false)
  91. go stats_collect.StartMetricsServer(*f.metricsHttpPort)
  92. if *filerStartS3 {
  93. filerAddress := fmt.Sprintf("%s:%d", *f.ip, *f.port)
  94. filerS3Options.filer = &filerAddress
  95. go func() {
  96. time.Sleep(2 * time.Second)
  97. filerS3Options.startS3Server()
  98. }()
  99. }
  100. f.startFiler()
  101. return true
  102. }
  103. func (fo *FilerOptions) startFiler() {
  104. defaultMux := http.NewServeMux()
  105. publicVolumeMux := defaultMux
  106. if *fo.publicPort != 0 {
  107. publicVolumeMux = http.NewServeMux()
  108. }
  109. defaultLevelDbDirectory := util.ResolvePath(*fo.defaultLevelDbDirectory + "/filerldb2")
  110. var peers []string
  111. if *fo.peers != "" {
  112. peers = strings.Split(*fo.peers, ",")
  113. }
  114. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  115. Masters: strings.Split(*fo.masters, ","),
  116. Collection: *fo.collection,
  117. DefaultReplication: *fo.defaultReplicaPlacement,
  118. DisableDirListing: *fo.disableDirListing,
  119. MaxMB: *fo.maxMB,
  120. DirListingLimit: *fo.dirListingLimit,
  121. DataCenter: *fo.dataCenter,
  122. Rack: *fo.rack,
  123. DefaultLevelDbDir: defaultLevelDbDirectory,
  124. DisableHttp: *fo.disableHttp,
  125. Host: *fo.ip,
  126. Port: uint32(*fo.port),
  127. Cipher: *fo.cipher,
  128. SaveToFilerLimit: *fo.saveToFilerLimit,
  129. Filers: peers,
  130. })
  131. if nfs_err != nil {
  132. glog.Fatalf("Filer startup error: %v", nfs_err)
  133. }
  134. if *fo.publicPort != 0 {
  135. publicListeningAddress := *fo.bindIp + ":" + strconv.Itoa(*fo.publicPort)
  136. glog.V(0).Infoln("Start Seaweed filer server", util.Version(), "public at", publicListeningAddress)
  137. publicListener, e := util.NewListener(publicListeningAddress, 0)
  138. if e != nil {
  139. glog.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  140. }
  141. go func() {
  142. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  143. glog.Fatalf("Volume server fail to serve public: %v", e)
  144. }
  145. }()
  146. }
  147. glog.V(0).Infof("Start Seaweed Filer %s at %s:%d", util.Version(), *fo.ip, *fo.port)
  148. filerListener, e := util.NewListener(
  149. *fo.bindIp+":"+strconv.Itoa(*fo.port),
  150. time.Duration(10)*time.Second,
  151. )
  152. if e != nil {
  153. glog.Fatalf("Filer listener error: %v", e)
  154. }
  155. // starting grpc server
  156. grpcPort := *fo.port + 10000
  157. grpcL, err := util.NewListener(*fo.bindIp+":"+strconv.Itoa(grpcPort), 0)
  158. if err != nil {
  159. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  160. }
  161. grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.filer"))
  162. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  163. reflection.Register(grpcS)
  164. go grpcS.Serve(grpcL)
  165. httpS := &http.Server{Handler: defaultMux}
  166. if err := httpS.Serve(filerListener); err != nil {
  167. glog.Fatalf("Filer Fail to serve: %v", e)
  168. }
  169. }