filer_server_handlers_write_upload.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package weed_server
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "fmt"
  6. "golang.org/x/exp/slices"
  7. "hash"
  8. "io"
  9. "net/http"
  10. "strconv"
  11. "sync"
  12. "sync/atomic"
  13. "time"
  14. "github.com/seaweedfs/seaweedfs/weed/glog"
  15. "github.com/seaweedfs/seaweedfs/weed/operation"
  16. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  17. "github.com/seaweedfs/seaweedfs/weed/security"
  18. "github.com/seaweedfs/seaweedfs/weed/stats"
  19. "github.com/seaweedfs/seaweedfs/weed/util"
  20. )
  21. var bufPool = sync.Pool{
  22. New: func() interface{} {
  23. return new(bytes.Buffer)
  24. },
  25. }
  26. func (fs *FilerServer) uploadReaderToChunks(w http.ResponseWriter, r *http.Request, reader io.Reader, chunkSize int32, fileName, contentType string, contentLength int64, so *operation.StorageOption) (fileChunks []*filer_pb.FileChunk, md5Hash hash.Hash, chunkOffset int64, uploadErr error, smallContent []byte) {
  27. query := r.URL.Query()
  28. isAppend := isAppend(r)
  29. if query.Has("offset") {
  30. offset := query.Get("offset")
  31. offsetInt, err := strconv.ParseInt(offset, 10, 64)
  32. if err != nil || offsetInt < 0 {
  33. err = fmt.Errorf("invalid 'offset': '%s'", offset)
  34. return nil, nil, 0, err, nil
  35. }
  36. if isAppend && offsetInt > 0 {
  37. err = fmt.Errorf("cannot set offset when op=append")
  38. return nil, nil, 0, err, nil
  39. }
  40. chunkOffset = offsetInt
  41. }
  42. md5Hash = md5.New()
  43. var partReader = io.NopCloser(io.TeeReader(reader, md5Hash))
  44. var wg sync.WaitGroup
  45. var bytesBufferCounter int64
  46. bytesBufferLimitCond := sync.NewCond(new(sync.Mutex))
  47. var fileChunksLock sync.Mutex
  48. var uploadErrLock sync.Mutex
  49. for {
  50. // need to throttle used byte buffer
  51. bytesBufferLimitCond.L.Lock()
  52. for atomic.LoadInt64(&bytesBufferCounter) >= 4 {
  53. glog.V(4).Infof("waiting for byte buffer %d", atomic.LoadInt64(&bytesBufferCounter))
  54. bytesBufferLimitCond.Wait()
  55. }
  56. atomic.AddInt64(&bytesBufferCounter, 1)
  57. bytesBufferLimitCond.L.Unlock()
  58. bytesBuffer := bufPool.Get().(*bytes.Buffer)
  59. glog.V(4).Infof("received byte buffer %d", atomic.LoadInt64(&bytesBufferCounter))
  60. limitedReader := io.LimitReader(partReader, int64(chunkSize))
  61. bytesBuffer.Reset()
  62. dataSize, err := bytesBuffer.ReadFrom(limitedReader)
  63. // data, err := io.ReadAll(limitedReader)
  64. if err != nil || dataSize == 0 {
  65. bufPool.Put(bytesBuffer)
  66. atomic.AddInt64(&bytesBufferCounter, -1)
  67. bytesBufferLimitCond.Signal()
  68. uploadErrLock.Lock()
  69. uploadErr = err
  70. uploadErrLock.Unlock()
  71. break
  72. }
  73. if chunkOffset == 0 && !isAppend {
  74. if dataSize < fs.option.SaveToFilerLimit {
  75. chunkOffset += dataSize
  76. smallContent = make([]byte, dataSize)
  77. bytesBuffer.Read(smallContent)
  78. bufPool.Put(bytesBuffer)
  79. atomic.AddInt64(&bytesBufferCounter, -1)
  80. bytesBufferLimitCond.Signal()
  81. stats.FilerRequestCounter.WithLabelValues(stats.ContentSaveToFiler).Inc()
  82. break
  83. }
  84. } else {
  85. stats.FilerRequestCounter.WithLabelValues(stats.AutoChunk).Inc()
  86. }
  87. wg.Add(1)
  88. go func(offset int64) {
  89. defer func() {
  90. bufPool.Put(bytesBuffer)
  91. atomic.AddInt64(&bytesBufferCounter, -1)
  92. bytesBufferLimitCond.Signal()
  93. wg.Done()
  94. }()
  95. chunks, toChunkErr := fs.dataToChunk(fileName, contentType, bytesBuffer.Bytes(), offset, so)
  96. if toChunkErr != nil {
  97. uploadErrLock.Lock()
  98. if uploadErr == nil {
  99. uploadErr = toChunkErr
  100. }
  101. uploadErrLock.Unlock()
  102. }
  103. if chunks != nil {
  104. fileChunksLock.Lock()
  105. fileChunksSize := len(fileChunks) + len(chunks)
  106. for _, chunk := range chunks {
  107. fileChunks = append(fileChunks, chunk)
  108. glog.V(4).Infof("uploaded %s chunk %d to %s [%d,%d)", fileName, fileChunksSize, chunk.FileId, offset, offset+int64(chunk.Size))
  109. }
  110. fileChunksLock.Unlock()
  111. }
  112. }(chunkOffset)
  113. // reset variables for the next chunk
  114. chunkOffset = chunkOffset + dataSize
  115. // if last chunk was not at full chunk size, but already exhausted the reader
  116. if dataSize < int64(chunkSize) {
  117. break
  118. }
  119. }
  120. wg.Wait()
  121. if uploadErr != nil {
  122. fs.filer.DeleteChunks(fileChunks)
  123. return nil, md5Hash, 0, uploadErr, nil
  124. }
  125. slices.SortFunc(fileChunks, func(a, b *filer_pb.FileChunk) bool {
  126. return a.Offset < b.Offset
  127. })
  128. return fileChunks, md5Hash, chunkOffset, nil, smallContent
  129. }
  130. func (fs *FilerServer) doUpload(urlLocation string, limitedReader io.Reader, fileName string, contentType string, pairMap map[string]string, auth security.EncodedJwt) (*operation.UploadResult, error, []byte) {
  131. stats.FilerRequestCounter.WithLabelValues(stats.ChunkUpload).Inc()
  132. start := time.Now()
  133. defer func() {
  134. stats.FilerRequestHistogram.WithLabelValues(stats.ChunkUpload).Observe(time.Since(start).Seconds())
  135. }()
  136. uploadOption := &operation.UploadOption{
  137. UploadUrl: urlLocation,
  138. Filename: fileName,
  139. Cipher: fs.option.Cipher,
  140. IsInputCompressed: false,
  141. MimeType: contentType,
  142. PairMap: pairMap,
  143. Jwt: auth,
  144. }
  145. uploadResult, err, data := operation.Upload(limitedReader, uploadOption)
  146. if uploadResult != nil && uploadResult.RetryCount > 0 {
  147. stats.FilerRequestCounter.WithLabelValues(stats.ChunkUploadRetry).Add(float64(uploadResult.RetryCount))
  148. }
  149. return uploadResult, err, data
  150. }
  151. func (fs *FilerServer) dataToChunk(fileName, contentType string, data []byte, chunkOffset int64, so *operation.StorageOption) ([]*filer_pb.FileChunk, error) {
  152. dataReader := util.NewBytesReader(data)
  153. // retry to assign a different file id
  154. var fileId, urlLocation string
  155. var auth security.EncodedJwt
  156. var uploadErr error
  157. var uploadResult *operation.UploadResult
  158. var failedFileChunks []*filer_pb.FileChunk
  159. err := util.Retry("filerDataToChunk", func() error {
  160. // assign one file id for one chunk
  161. fileId, urlLocation, auth, uploadErr = fs.assignNewFileInfo(so)
  162. if uploadErr != nil {
  163. glog.V(4).Infof("retry later due to assign error: %v", uploadErr)
  164. stats.FilerRequestCounter.WithLabelValues(stats.ChunkAssignRetry).Inc()
  165. return uploadErr
  166. }
  167. // upload the chunk to the volume server
  168. uploadResult, uploadErr, _ = fs.doUpload(urlLocation, dataReader, fileName, contentType, nil, auth)
  169. if uploadErr != nil {
  170. glog.V(4).Infof("retry later due to upload error: %v", uploadErr)
  171. stats.FilerRequestCounter.WithLabelValues(stats.ChunkDoUploadRetry).Inc()
  172. fid, _ := filer_pb.ToFileIdObject(fileId)
  173. fileChunk := filer_pb.FileChunk{
  174. FileId: fileId,
  175. Offset: chunkOffset,
  176. Fid: fid,
  177. }
  178. failedFileChunks = append(failedFileChunks, &fileChunk)
  179. return uploadErr
  180. }
  181. return nil
  182. })
  183. if err != nil {
  184. glog.Errorf("upload error: %v", err)
  185. return failedFileChunks, err
  186. }
  187. // if last chunk exhausted the reader exactly at the border
  188. if uploadResult.Size == 0 {
  189. return nil, nil
  190. }
  191. return []*filer_pb.FileChunk{uploadResult.ToPbFileChunk(fileId, chunkOffset, time.Now().UnixNano())}, nil
  192. }