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.

200 lines
5.9 KiB

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