filer_server_handlers_write.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package weed_server
  2. import (
  3. "context"
  4. "net/http"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/operation"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/security"
  12. "github.com/chrislusf/seaweedfs/weed/stats"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. var (
  17. OS_UID = uint32(os.Getuid())
  18. OS_GID = uint32(os.Getgid())
  19. )
  20. type FilerPostResult struct {
  21. Name string `json:"name,omitempty"`
  22. Size int64 `json:"size,omitempty"`
  23. Error string `json:"error,omitempty"`
  24. Fid string `json:"fid,omitempty"`
  25. Url string `json:"url,omitempty"`
  26. }
  27. func (fs *FilerServer) assignNewFileInfo(so *operation.StorageOption) (fileId, urlLocation string, auth security.EncodedJwt, err error) {
  28. stats.FilerRequestCounter.WithLabelValues("assign").Inc()
  29. start := time.Now()
  30. defer func() { stats.FilerRequestHistogram.WithLabelValues("assign").Observe(time.Since(start).Seconds()) }()
  31. ar, altRequest := so.ToAssignRequests(1)
  32. assignResult, ae := operation.Assign(fs.filer.GetMaster, fs.grpcDialOption, ar, altRequest)
  33. if ae != nil {
  34. glog.Errorf("failing to assign a file id: %v", ae)
  35. err = ae
  36. return
  37. }
  38. fileId = assignResult.Fid
  39. urlLocation = "http://" + assignResult.Url + "/" + assignResult.Fid
  40. if so.Fsync {
  41. urlLocation += "?fsync=true"
  42. }
  43. auth = assignResult.Auth
  44. return
  45. }
  46. func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  47. ctx := context.Background()
  48. query := r.URL.Query()
  49. so := fs.detectStorageOption0(r.RequestURI,
  50. query.Get("collection"),
  51. query.Get("replication"),
  52. query.Get("ttl"),
  53. query.Get("disk"),
  54. query.Get("dataCenter"),
  55. query.Get("rack"),
  56. )
  57. fs.autoChunk(ctx, w, r, so)
  58. util.CloseRequest(r)
  59. }
  60. // curl -X DELETE http://localhost:8888/path/to
  61. // curl -X DELETE http://localhost:8888/path/to?recursive=true
  62. // curl -X DELETE http://localhost:8888/path/to?recursive=true&ignoreRecursiveError=true
  63. // curl -X DELETE http://localhost:8888/path/to?recursive=true&skipChunkDeletion=true
  64. func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  65. isRecursive := r.FormValue("recursive") == "true"
  66. if !isRecursive && fs.option.recursiveDelete {
  67. if r.FormValue("recursive") != "false" {
  68. isRecursive = true
  69. }
  70. }
  71. ignoreRecursiveError := r.FormValue("ignoreRecursiveError") == "true"
  72. skipChunkDeletion := r.FormValue("skipChunkDeletion") == "true"
  73. objectPath := r.URL.Path
  74. if len(r.URL.Path) > 1 && strings.HasSuffix(objectPath, "/") {
  75. objectPath = objectPath[0 : len(objectPath)-1]
  76. }
  77. err := fs.filer.DeleteEntryMetaAndData(context.Background(), util.FullPath(objectPath), isRecursive, ignoreRecursiveError, !skipChunkDeletion, false, nil)
  78. if err != nil {
  79. glog.V(1).Infoln("deleting", objectPath, ":", err.Error())
  80. httpStatus := http.StatusInternalServerError
  81. if err == filer_pb.ErrNotFound {
  82. httpStatus = http.StatusNoContent
  83. }
  84. writeJsonError(w, r, httpStatus, err)
  85. return
  86. }
  87. w.WriteHeader(http.StatusNoContent)
  88. }
  89. func (fs *FilerServer) detectStorageOption(requestURI, qCollection, qReplication string, ttlSeconds int32, diskType string, dataCenter, rack string) *operation.StorageOption {
  90. collection := util.Nvl(qCollection, fs.option.Collection)
  91. replication := util.Nvl(qReplication, fs.option.DefaultReplication)
  92. // required by buckets folder
  93. bucketDefaultReplication, fsync := "", false
  94. if strings.HasPrefix(requestURI, fs.filer.DirBucketsPath+"/") {
  95. collection = fs.filer.DetectBucket(util.FullPath(requestURI))
  96. bucketDefaultReplication, fsync = fs.filer.ReadBucketOption(collection)
  97. }
  98. if replication == "" {
  99. replication = bucketDefaultReplication
  100. }
  101. rule := fs.filer.FilerConf.MatchStorageRule(requestURI)
  102. if ttlSeconds == 0 {
  103. ttl, err := needle.ReadTTL(rule.GetTtl())
  104. if err != nil {
  105. glog.Errorf("fail to parse %s ttl setting %s: %v", rule.LocationPrefix, rule.Ttl, err)
  106. }
  107. ttlSeconds = int32(ttl.Minutes()) * 60
  108. }
  109. return &operation.StorageOption{
  110. Replication: util.Nvl(replication, rule.Replication),
  111. Collection: util.Nvl(collection, rule.Collection),
  112. DataCenter: util.Nvl(dataCenter, fs.option.DataCenter),
  113. Rack: util.Nvl(rack, fs.option.Rack),
  114. TtlSeconds: ttlSeconds,
  115. DiskType: util.Nvl(diskType, rule.DiskType),
  116. Fsync: fsync || rule.Fsync,
  117. VolumeGrowthCount: rule.VolumeGrowthCount,
  118. }
  119. }
  120. func (fs *FilerServer) detectStorageOption0(requestURI, qCollection, qReplication string, qTtl string, diskType string, dataCenter, rack string) *operation.StorageOption {
  121. ttl, err := needle.ReadTTL(qTtl)
  122. if err != nil {
  123. glog.Errorf("fail to parse ttl %s: %v", qTtl, err)
  124. }
  125. return fs.detectStorageOption(requestURI, qCollection, qReplication, int32(ttl.Minutes())*60, diskType, dataCenter, rack)
  126. }