filer.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package command
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "google.golang.org/grpc/reflection"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/chrislusf/seaweedfs/weed/server"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. var (
  15. f FilerOptions
  16. )
  17. type FilerOptions struct {
  18. masters *string
  19. ip *string
  20. port *int
  21. publicPort *int
  22. collection *string
  23. defaultReplicaPlacement *string
  24. redirectOnRead *bool
  25. disableDirListing *bool
  26. maxMB *int
  27. dirListingLimit *int
  28. dataCenter *string
  29. enableNotification *bool
  30. disableHttp *bool
  31. // default leveldb directory, used in "weed server" mode
  32. defaultLevelDbDirectory *string
  33. }
  34. func init() {
  35. cmdFiler.Run = runFiler // break init cycle
  36. f.masters = cmdFiler.Flag.String("master", "localhost:9333", "comma-separated master servers")
  37. f.collection = cmdFiler.Flag.String("collection", "", "all data will be stored in this collection")
  38. f.ip = cmdFiler.Flag.String("ip", "", "filer server http listen ip address")
  39. f.port = cmdFiler.Flag.Int("port", 8888, "filer server http listen port")
  40. f.publicPort = cmdFiler.Flag.Int("port.readonly", 0, "readonly port opened to public")
  41. f.defaultReplicaPlacement = cmdFiler.Flag.String("defaultReplicaPlacement", "000", "default replication type if not specified")
  42. f.redirectOnRead = cmdFiler.Flag.Bool("redirectOnRead", false, "whether proxy or redirect to volume server during file GET request")
  43. f.disableDirListing = cmdFiler.Flag.Bool("disableDirListing", false, "turn off directory listing")
  44. f.maxMB = cmdFiler.Flag.Int("maxMB", 32, "split files larger than the limit")
  45. f.dirListingLimit = cmdFiler.Flag.Int("dirListLimit", 100000, "limit sub dir listing size")
  46. f.dataCenter = cmdFiler.Flag.String("dataCenter", "", "prefer to write to volumes in this data center")
  47. f.disableHttp = cmdFiler.Flag.Bool("disableHttp", false, "disable http request, only gRpc operations are allowed")
  48. }
  49. var cmdFiler = &Command{
  50. UsageLine: "filer -port=8888 -master=<ip:port>[,<ip:port>]*",
  51. Short: "start a file server that points to a master server, or a list of master servers",
  52. Long: `start a file server which accepts REST operation for any files.
  53. //create or overwrite the file, the directories /path/to will be automatically created
  54. POST /path/to/file
  55. //get the file content
  56. GET /path/to/file
  57. //create or overwrite the file, the filename in the multipart request will be used
  58. POST /path/to/
  59. //return a json format subdirectory and files listing
  60. GET /path/to/
  61. The configuration file "filer.toml" is read from ".", "$HOME/.seaweedfs/", or "/etc/seaweedfs/", in that order.
  62. The example filer.toml configuration file can be generated by "weed scaffold -config=filer"
  63. `,
  64. }
  65. func runFiler(cmd *Command, args []string) bool {
  66. util.LoadConfiguration("security", false)
  67. f.startFiler()
  68. return true
  69. }
  70. func (fo *FilerOptions) startFiler() {
  71. defaultMux := http.NewServeMux()
  72. publicVolumeMux := defaultMux
  73. if *fo.publicPort != 0 {
  74. publicVolumeMux = http.NewServeMux()
  75. }
  76. defaultLevelDbDirectory := "./filerldb2"
  77. if fo.defaultLevelDbDirectory != nil {
  78. defaultLevelDbDirectory = *fo.defaultLevelDbDirectory + "/filerldb2"
  79. }
  80. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  81. Masters: strings.Split(*fo.masters, ","),
  82. Collection: *fo.collection,
  83. DefaultReplication: *fo.defaultReplicaPlacement,
  84. RedirectOnRead: *fo.redirectOnRead,
  85. DisableDirListing: *fo.disableDirListing,
  86. MaxMB: *fo.maxMB,
  87. DirListingLimit: *fo.dirListingLimit,
  88. DataCenter: *fo.dataCenter,
  89. DefaultLevelDbDir: defaultLevelDbDirectory,
  90. DisableHttp: *fo.disableHttp,
  91. Port: *fo.port,
  92. })
  93. if nfs_err != nil {
  94. glog.Fatalf("Filer startup error: %v", nfs_err)
  95. }
  96. if *fo.publicPort != 0 {
  97. publicListeningAddress := *fo.ip + ":" + strconv.Itoa(*fo.publicPort)
  98. glog.V(0).Infoln("Start Seaweed filer server", util.VERSION, "public at", publicListeningAddress)
  99. publicListener, e := util.NewListener(publicListeningAddress, 0)
  100. if e != nil {
  101. glog.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  102. }
  103. go func() {
  104. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  105. glog.Fatalf("Volume server fail to serve public: %v", e)
  106. }
  107. }()
  108. }
  109. glog.V(0).Infof("Start Seaweed Filer %s at %s:%d", util.VERSION, *fo.ip, *fo.port)
  110. filerListener, e := util.NewListener(
  111. *fo.ip+":"+strconv.Itoa(*fo.port),
  112. time.Duration(10)*time.Second,
  113. )
  114. if e != nil {
  115. glog.Fatalf("Filer listener error: %v", e)
  116. }
  117. // starting grpc server
  118. grpcPort := *fo.port + 10000
  119. grpcL, err := util.NewListener(":"+strconv.Itoa(grpcPort), 0)
  120. if err != nil {
  121. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  122. }
  123. grpcS := util.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.filer"))
  124. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  125. reflection.Register(grpcS)
  126. go grpcS.Serve(grpcL)
  127. httpS := &http.Server{Handler: defaultMux}
  128. if err := httpS.Serve(filerListener); err != nil {
  129. glog.Fatalf("Filer Fail to serve: %v", e)
  130. }
  131. }