filer_server_handlers_write_upload.go 5.4 KB

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