You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.2 KiB

5 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "time"
  9. "github.com/valyala/fasthttp"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/operation"
  12. "github.com/chrislusf/seaweedfs/weed/stats"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/topology"
  15. )
  16. func (vs *VolumeServer) fastPostHandler(ctx *fasthttp.RequestCtx) {
  17. stats.VolumeServerRequestCounter.WithLabelValues("post").Inc()
  18. start := time.Now()
  19. defer func() {
  20. stats.VolumeServerRequestHistogram.WithLabelValues("post").Observe(time.Since(start).Seconds())
  21. }()
  22. requestPath := string(ctx.Path())
  23. vid, fid, _, _, _ := parseURLPath(requestPath)
  24. volumeId, ve := needle.NewVolumeId(vid)
  25. if ve != nil {
  26. glog.V(0).Infoln("NewVolumeId error:", ve)
  27. writeJsonError(ctx, http.StatusBadRequest, ve)
  28. return
  29. }
  30. if !vs.maybeCheckJwtAuthorization(ctx, vid, fid, true) {
  31. writeJsonError(ctx, http.StatusUnauthorized, errors.New("wrong jwt"))
  32. return
  33. }
  34. needle, originalSize, ne := needle.CreateNeedleFromRequest(ctx, vs.FixJpgOrientation, vs.fileSizeLimitBytes)
  35. if ne != nil {
  36. writeJsonError(ctx, http.StatusBadRequest, ne)
  37. return
  38. }
  39. ret := operation.UploadResult{}
  40. _, isUnchanged, writeError := topology.OldReplicatedWrite(vs.GetMaster(), vs.store, volumeId, needle, r)
  41. // http 304 status code does not allow body
  42. if writeError == nil && isUnchanged {
  43. ctx.SetStatusCode(http.StatusNotModified)
  44. return
  45. }
  46. httpStatus := http.StatusCreated
  47. if writeError != nil {
  48. httpStatus = http.StatusInternalServerError
  49. ret.Error = writeError.Error()
  50. }
  51. if needle.HasName() {
  52. ret.Name = string(needle.Name)
  53. }
  54. ret.Size = uint32(originalSize)
  55. ret.ETag = needle.Etag()
  56. fastSetEtag(ctx, ret.ETag)
  57. writeJsonQuiet(ctx, httpStatus, ret)
  58. }
  59. func (vs *VolumeServer) DeleteHandler(ctx *fasthttp.RequestCtx) {
  60. stats.VolumeServerRequestCounter.WithLabelValues("delete").Inc()
  61. start := time.Now()
  62. defer func() {
  63. stats.VolumeServerRequestHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds())
  64. }()
  65. requestPath := string(ctx.Path())
  66. n := new(needle.Needle)
  67. vid, fid, _, _, _ := parseURLPath(requestPath)
  68. volumeId, _ := needle.NewVolumeId(vid)
  69. n.ParsePath(fid)
  70. if !vs.maybeCheckJwtAuthorization(ctx, vid, fid, true) {
  71. writeJsonError(ctx, http.StatusUnauthorized, errors.New("wrong jwt"))
  72. return
  73. }
  74. // glog.V(2).Infof("volume %s deleting %s", vid, n)
  75. cookie := n.Cookie
  76. ecVolume, hasEcVolume := vs.store.FindEcVolume(volumeId)
  77. if hasEcVolume {
  78. count, err := vs.store.DeleteEcShardNeedle(context.Background(), ecVolume, n, cookie)
  79. writeDeleteResult(err, count, ctx)
  80. return
  81. }
  82. _, ok := vs.store.ReadVolumeNeedle(volumeId, n)
  83. if ok != nil {
  84. m := make(map[string]uint32)
  85. m["size"] = 0
  86. writeJsonQuiet(ctx, http.StatusNotFound, m)
  87. return
  88. }
  89. if n.Cookie != cookie {
  90. glog.V(0).Infof("delete %s with unmaching cookie from %s agent %s", requestPath, ctx.RemoteAddr(), ctx.UserAgent())
  91. writeJsonError(ctx, http.StatusBadRequest, errors.New("File Random Cookie does not match."))
  92. return
  93. }
  94. count := int64(n.Size)
  95. if n.IsChunkedManifest() {
  96. chunkManifest, e := operation.LoadChunkManifest(n.Data, n.IsGzipped())
  97. if e != nil {
  98. writeJsonError(ctx, http.StatusInternalServerError, fmt.Errorf("Load chunks manifest error: %v", e))
  99. return
  100. }
  101. // make sure all chunks had deleted before delete manifest
  102. if e := chunkManifest.DeleteChunks(vs.GetMaster(), vs.grpcDialOption); e != nil {
  103. writeJsonError(ctx, http.StatusInternalServerError, fmt.Errorf("Delete chunks error: %v", e))
  104. return
  105. }
  106. count = chunkManifest.Size
  107. }
  108. n.LastModified = uint64(time.Now().Unix())
  109. tsValue := ctx.FormValue("ts")
  110. if tsValue != nil {
  111. modifiedTime, err := strconv.ParseInt(string(tsValue), 10, 64)
  112. if err == nil {
  113. n.LastModified = uint64(modifiedTime)
  114. }
  115. }
  116. _, err := topology.ReplicatedDelete(vs.GetMaster(), vs.store, volumeId, n, ctx)
  117. writeDeleteResult(err, count, ctx)
  118. }
  119. func writeDeleteResult(err error, count int64, ctx *fasthttp.RequestCtx) {
  120. if err == nil {
  121. m := make(map[string]int64)
  122. m["size"] = count
  123. writeJsonQuiet(ctx, http.StatusAccepted, m)
  124. } else {
  125. writeJsonQuiet(ctx, http.StatusInternalServerError, fmt.Errorf("Deletion Failed: %v", err))
  126. }
  127. }