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.

146 lines
4.6 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
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. "strings"
  10. "sync"
  11. "time"
  12. "github.com/chrislusf/seaweedfs/weed/filer"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/operation"
  15. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  16. "github.com/chrislusf/seaweedfs/weed/security"
  17. "github.com/chrislusf/seaweedfs/weed/stats"
  18. "github.com/chrislusf/seaweedfs/weed/util"
  19. )
  20. var bufPool = sync.Pool{
  21. New: func() interface{} {
  22. return new(bytes.Buffer)
  23. },
  24. }
  25. 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) {
  26. var fileChunks []*filer_pb.FileChunk
  27. md5Hash := md5.New()
  28. var partReader = ioutil.NopCloser(io.TeeReader(reader, md5Hash))
  29. chunkOffset := int64(0)
  30. var smallContent []byte
  31. bytesBuffer := bufPool.Get().(*bytes.Buffer)
  32. defer bufPool.Put(bytesBuffer)
  33. for {
  34. limitedReader := io.LimitReader(partReader, int64(chunkSize))
  35. bytesBuffer.Reset()
  36. dataSize, err := bytesBuffer.ReadFrom(limitedReader)
  37. // data, err := ioutil.ReadAll(limitedReader)
  38. if err != nil {
  39. return nil, nil, 0, err, nil
  40. }
  41. if chunkOffset == 0 && !isAppend(r) {
  42. if dataSize < fs.option.SaveToFilerLimit || strings.HasPrefix(r.URL.Path, filer.DirectoryEtcRoot) && dataSize < 4*1024 {
  43. chunkOffset += dataSize
  44. smallContent = make([]byte, dataSize)
  45. bytesBuffer.Write(smallContent)
  46. break
  47. }
  48. }
  49. chunk, uploadErr := fs.dataToChunk(fileName, contentType, bytesBuffer.Bytes(), chunkOffset, so, md5Hash)
  50. if uploadErr != nil {
  51. return nil, nil, 0, uploadErr, nil
  52. }
  53. // if last chunk exhausted the reader exactly at the border
  54. if chunk == nil {
  55. break
  56. }
  57. // Save to chunk manifest structure
  58. fileChunks = append(fileChunks, chunk)
  59. glog.V(4).Infof("uploaded %s chunk %d to %s [%d,%d)", fileName, len(fileChunks), chunk.FileId, chunkOffset, chunkOffset+int64(chunk.Size))
  60. // reset variables for the next chunk
  61. chunkOffset = chunkOffset + int64(chunk.Size)
  62. // if last chunk was not at full chunk size, but already exhausted the reader
  63. if int64(chunk.Size) < int64(chunkSize) {
  64. break
  65. }
  66. }
  67. return fileChunks, md5Hash, chunkOffset, nil, smallContent
  68. }
  69. func (fs *FilerServer) doUpload(urlLocation string, limitedReader io.Reader, fileName string, contentType string, pairMap map[string]string, auth security.EncodedJwt) (*operation.UploadResult, error, []byte) {
  70. stats.FilerRequestCounter.WithLabelValues("chunkUpload").Inc()
  71. start := time.Now()
  72. defer func() {
  73. stats.FilerRequestHistogram.WithLabelValues("chunkUpload").Observe(time.Since(start).Seconds())
  74. }()
  75. uploadResult, err, data := operation.Upload(urlLocation, fileName, fs.option.Cipher, limitedReader, false, contentType, pairMap, auth)
  76. if uploadResult != nil && uploadResult.RetryCount > 0 {
  77. stats.FilerRequestCounter.WithLabelValues("chunkUploadRetry").Add(float64(uploadResult.RetryCount))
  78. }
  79. return uploadResult, err, data
  80. }
  81. func (fs *FilerServer) dataToChunk(fileName, contentType string, data []byte, chunkOffset int64, so *operation.StorageOption, md5Hash hash.Hash) (*filer_pb.FileChunk, error) {
  82. dataReader := util.NewBytesReader(data)
  83. // retry to assign a different file id
  84. var fileId, urlLocation string
  85. var auth security.EncodedJwt
  86. var uploadErr error
  87. var uploadResult *operation.UploadResult
  88. for i := 0; i < 3; i++ {
  89. // assign one file id for one chunk
  90. fileId, urlLocation, auth, uploadErr = fs.assignNewFileInfo(so)
  91. if uploadErr != nil {
  92. glog.V(4).Infof("retry later due to assign error: %v", uploadErr)
  93. time.Sleep(time.Duration(i+1) * 251 * time.Millisecond)
  94. continue
  95. }
  96. // upload the chunk to the volume server
  97. uploadResult, uploadErr, _ = fs.doUpload(urlLocation, dataReader, fileName, contentType, nil, auth)
  98. if uploadErr != nil {
  99. glog.V(4).Infof("retry later due to upload error: %v", uploadErr)
  100. time.Sleep(time.Duration(i+1) * 251 * time.Millisecond)
  101. continue
  102. }
  103. break
  104. }
  105. if uploadErr != nil {
  106. glog.Errorf("upload error: %v", uploadErr)
  107. return nil, uploadErr
  108. }
  109. // if last chunk exhausted the reader exactly at the border
  110. if uploadResult.Size == 0 {
  111. return nil, nil
  112. }
  113. if chunkOffset == 0 {
  114. uploadedMd5 := util.Base64Md5ToBytes(uploadResult.ContentMd5)
  115. readedMd5 := md5Hash.Sum(nil)
  116. if !bytes.Equal(uploadedMd5, readedMd5) {
  117. glog.Errorf("md5 %x does not match %x uploaded chunk %s to the volume server", readedMd5, uploadedMd5, uploadResult.Name)
  118. }
  119. }
  120. return uploadResult.ToPbFileChunk(fileId, chunkOffset), nil
  121. }