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.

161 lines
4.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package weed_server
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "hash"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "sort"
  10. "strings"
  11. "sync"
  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) ([]*filer_pb.FileChunk, hash.Hash, int64, error, []byte) {
  27. var fileChunks []*filer_pb.FileChunk
  28. md5Hash := md5.New()
  29. var partReader = ioutil.NopCloser(io.TeeReader(reader, md5Hash))
  30. chunkOffset := int64(0)
  31. var smallContent []byte
  32. var uploadErr error
  33. var wg sync.WaitGroup
  34. for {
  35. // need to throttle this for large files
  36. bytesBuffer := bufPool.Get().(*bytes.Buffer)
  37. defer bufPool.Put(bytesBuffer)
  38. limitedReader := io.LimitReader(partReader, int64(chunkSize))
  39. bytesBuffer.Reset()
  40. dataSize, err := bytesBuffer.ReadFrom(limitedReader)
  41. // data, err := ioutil.ReadAll(limitedReader)
  42. if err != nil || dataSize == 0 {
  43. return nil, md5Hash, 0, err, nil
  44. }
  45. if chunkOffset == 0 && !isAppend(r) {
  46. if dataSize < fs.option.SaveToFilerLimit || strings.HasPrefix(r.URL.Path, filer.DirectoryEtcRoot) && dataSize < 4*1024 {
  47. chunkOffset += dataSize
  48. smallContent = make([]byte, dataSize)
  49. bytesBuffer.Read(smallContent)
  50. break
  51. }
  52. }
  53. wg.Add(1)
  54. go func(offset int64) {
  55. defer wg.Done()
  56. chunk, toChunkErr := fs.dataToChunk(fileName, contentType, bytesBuffer.Bytes(), offset, so, md5Hash)
  57. if toChunkErr != nil {
  58. uploadErr = toChunkErr
  59. }
  60. if chunk != nil {
  61. fileChunks = append(fileChunks, chunk)
  62. glog.V(4).Infof("uploaded %s chunk %d to %s [%d,%d)", fileName, len(fileChunks), chunk.FileId, offset, offset+int64(chunk.Size))
  63. }
  64. }(chunkOffset)
  65. // reset variables for the next chunk
  66. chunkOffset = chunkOffset + dataSize
  67. // if last chunk was not at full chunk size, but already exhausted the reader
  68. if dataSize < int64(chunkSize) {
  69. break
  70. }
  71. }
  72. wg.Wait()
  73. if uploadErr != nil {
  74. return nil, md5Hash, 0, uploadErr, nil
  75. }
  76. sort.Slice(fileChunks, func(i, j int) bool {
  77. return fileChunks[i].Offset < fileChunks[j].Offset
  78. })
  79. return fileChunks, md5Hash, chunkOffset, nil, smallContent
  80. }
  81. func (fs *FilerServer) doUpload(urlLocation string, limitedReader io.Reader, fileName string, contentType string, pairMap map[string]string, auth security.EncodedJwt) (*operation.UploadResult, error, []byte) {
  82. stats.FilerRequestCounter.WithLabelValues("chunkUpload").Inc()
  83. start := time.Now()
  84. defer func() {
  85. stats.FilerRequestHistogram.WithLabelValues("chunkUpload").Observe(time.Since(start).Seconds())
  86. }()
  87. uploadResult, err, data := operation.Upload(urlLocation, fileName, fs.option.Cipher, limitedReader, false, contentType, pairMap, auth)
  88. if uploadResult != nil && uploadResult.RetryCount > 0 {
  89. stats.FilerRequestCounter.WithLabelValues("chunkUploadRetry").Add(float64(uploadResult.RetryCount))
  90. }
  91. return uploadResult, err, data
  92. }
  93. func (fs *FilerServer) dataToChunk(fileName, contentType string, data []byte, chunkOffset int64, so *operation.StorageOption, md5Hash hash.Hash) (*filer_pb.FileChunk, error) {
  94. dataReader := util.NewBytesReader(data)
  95. // retry to assign a different file id
  96. var fileId, urlLocation string
  97. var auth security.EncodedJwt
  98. var uploadErr error
  99. var uploadResult *operation.UploadResult
  100. for i := 0; i < 3; i++ {
  101. // assign one file id for one chunk
  102. fileId, urlLocation, auth, uploadErr = fs.assignNewFileInfo(so)
  103. if uploadErr != nil {
  104. glog.V(4).Infof("retry later due to assign error: %v", uploadErr)
  105. time.Sleep(time.Duration(i+1) * 251 * time.Millisecond)
  106. continue
  107. }
  108. // upload the chunk to the volume server
  109. uploadResult, uploadErr, _ = fs.doUpload(urlLocation, dataReader, fileName, contentType, nil, auth)
  110. if uploadErr != nil {
  111. glog.V(4).Infof("retry later due to upload error: %v", uploadErr)
  112. time.Sleep(time.Duration(i+1) * 251 * time.Millisecond)
  113. continue
  114. }
  115. break
  116. }
  117. if uploadErr != nil {
  118. glog.Errorf("upload error: %v", uploadErr)
  119. return nil, uploadErr
  120. }
  121. // if last chunk exhausted the reader exactly at the border
  122. if uploadResult.Size == 0 {
  123. return nil, nil
  124. }
  125. if chunkOffset == 0 {
  126. uploadedMd5 := util.Base64Md5ToBytes(uploadResult.ContentMd5)
  127. readedMd5 := md5Hash.Sum(nil)
  128. if !bytes.Equal(uploadedMd5, readedMd5) {
  129. glog.Errorf("md5 %x does not match %x uploaded chunk %s to the volume server", readedMd5, uploadedMd5, uploadResult.Name)
  130. }
  131. }
  132. return uploadResult.ToPbFileChunk(fileId, chunkOffset), nil
  133. }