filer_server_handlers_write.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package weed_server
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. "os"
  7. "strings"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/security"
  13. "github.com/chrislusf/seaweedfs/weed/stats"
  14. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. var (
  18. OS_UID = uint32(os.Getuid())
  19. OS_GID = uint32(os.Getgid())
  20. ErrReadOnly = errors.New("read only")
  21. )
  22. type FilerPostResult struct {
  23. Name string `json:"name,omitempty"`
  24. Size int64 `json:"size,omitempty"`
  25. Error string `json:"error,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, contentLength int64) {
  47. ctx := context.Background()
  48. query := r.URL.Query()
  49. so, err := fs.detectStorageOption0(r.RequestURI,
  50. query.Get("collection"),
  51. query.Get("replication"),
  52. query.Get("ttl"),
  53. query.Get("disk"),
  54. query.Get("fsync"),
  55. query.Get("dataCenter"),
  56. query.Get("rack"),
  57. query.Get("dataNode"),
  58. )
  59. if err != nil {
  60. if err == ErrReadOnly {
  61. w.WriteHeader(http.StatusInsufficientStorage)
  62. } else {
  63. glog.V(1).Infoln("post", r.RequestURI, ":", err.Error())
  64. w.WriteHeader(http.StatusInternalServerError)
  65. }
  66. return
  67. }
  68. fs.autoChunk(ctx, w, r, contentLength, so)
  69. util.CloseRequest(r)
  70. }
  71. // curl -X DELETE http://localhost:8888/path/to
  72. // curl -X DELETE http://localhost:8888/path/to?recursive=true
  73. // curl -X DELETE http://localhost:8888/path/to?recursive=true&ignoreRecursiveError=true
  74. // curl -X DELETE http://localhost:8888/path/to?recursive=true&skipChunkDeletion=true
  75. func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  76. isRecursive := r.FormValue("recursive") == "true"
  77. if !isRecursive && fs.option.recursiveDelete {
  78. if r.FormValue("recursive") != "false" {
  79. isRecursive = true
  80. }
  81. }
  82. ignoreRecursiveError := r.FormValue("ignoreRecursiveError") == "true"
  83. skipChunkDeletion := r.FormValue("skipChunkDeletion") == "true"
  84. objectPath := r.URL.Path
  85. if len(r.URL.Path) > 1 && strings.HasSuffix(objectPath, "/") {
  86. objectPath = objectPath[0 : len(objectPath)-1]
  87. }
  88. err := fs.filer.DeleteEntryMetaAndData(context.Background(), util.FullPath(objectPath), isRecursive, ignoreRecursiveError, !skipChunkDeletion, false, nil)
  89. if err != nil {
  90. glog.V(1).Infoln("deleting", objectPath, ":", err.Error())
  91. httpStatus := http.StatusInternalServerError
  92. if err == filer_pb.ErrNotFound {
  93. httpStatus = http.StatusNoContent
  94. }
  95. writeJsonError(w, r, httpStatus, err)
  96. return
  97. }
  98. w.WriteHeader(http.StatusNoContent)
  99. }
  100. func (fs *FilerServer) detectStorageOption(requestURI, qCollection, qReplication string, ttlSeconds int32, diskType, dataCenter, rack, dataNode string) (*operation.StorageOption, error) {
  101. rule := fs.filer.FilerConf.MatchStorageRule(requestURI)
  102. if rule.ReadOnly {
  103. return nil, ErrReadOnly
  104. }
  105. // required by buckets folder
  106. bucketDefaultCollection, bucketDefaultReplication, fsync := "", "", false
  107. if strings.HasPrefix(requestURI, fs.filer.DirBucketsPath+"/") {
  108. bucketDefaultCollection = fs.filer.DetectBucket(util.FullPath(requestURI))
  109. bucketDefaultReplication, fsync = fs.filer.ReadBucketOption(bucketDefaultCollection)
  110. }
  111. if ttlSeconds == 0 {
  112. ttl, err := needle.ReadTTL(rule.GetTtl())
  113. if err != nil {
  114. glog.Errorf("fail to parse %s ttl setting %s: %v", rule.LocationPrefix, rule.Ttl, err)
  115. }
  116. ttlSeconds = int32(ttl.Minutes()) * 60
  117. }
  118. return &operation.StorageOption{
  119. Replication: util.Nvl(qReplication, rule.Replication, bucketDefaultReplication, fs.option.DefaultReplication),
  120. Collection: util.Nvl(qCollection, rule.Collection, bucketDefaultCollection, fs.option.Collection),
  121. DataCenter: util.Nvl(dataCenter, rule.DataCenter, fs.option.DataCenter),
  122. Rack: util.Nvl(rack, rule.Rack, fs.option.Rack),
  123. DataNode: util.Nvl(dataNode, rule.DataNode, fs.option.DataNode),
  124. TtlSeconds: ttlSeconds,
  125. DiskType: util.Nvl(diskType, rule.DiskType),
  126. Fsync: fsync || rule.Fsync,
  127. VolumeGrowthCount: rule.VolumeGrowthCount,
  128. }, nil
  129. }
  130. func (fs *FilerServer) detectStorageOption0(requestURI, qCollection, qReplication string, qTtl string, diskType string, fsync string, dataCenter, rack, dataNode string) (*operation.StorageOption, error) {
  131. ttl, err := needle.ReadTTL(qTtl)
  132. if err != nil {
  133. glog.Errorf("fail to parse ttl %s: %v", qTtl, err)
  134. }
  135. so, err := fs.detectStorageOption(requestURI, qCollection, qReplication, int32(ttl.Minutes())*60, diskType, dataCenter, rack, dataNode)
  136. if so != nil {
  137. so.Fsync = fsync == "true"
  138. }
  139. return so, err
  140. }