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.

218 lines
6.6 KiB

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
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
4 years ago
more solid weed mount (#4089) * compare chunks by timestamp * fix slab clearing error * fix test compilation * move oldest chunk to sealed, instead of by fullness * lock on fh.entryViewCache * remove verbose logs * revert slat clearing * less logs * less logs * track write and read by timestamp * remove useless logic * add entry lock on file handle release * use mem chunk only, swap file chunk has problems * comment out code that maybe used later * add debug mode to compare data read and write * more efficient readResolvedChunks with linked list * small optimization * fix test compilation * minor fix on writer * add SeparateGarbageChunks * group chunks into sections * turn off debug mode * fix tests * fix tests * tmp enable swap file chunk * Revert "tmp enable swap file chunk" This reverts commit 985137ec472924e4815f258189f6ca9f2168a0a7. * simple refactoring * simple refactoring * do not re-use swap file chunk. Sealed chunks should not be re-used. * comment out debugging facilities * either mem chunk or swap file chunk is fine now * remove orderedMutex as *semaphore.Weighted not found impactful * optimize size calculation for changing large files * optimize performance to avoid going through the long list of chunks * still problems with swap file chunk * rename * tiny optimization * swap file chunk save only successfully read data * fix * enable both mem and swap file chunk * resolve chunks with range * rename * fix chunk interval list * also change file handle chunk group when adding chunks * pick in-active chunk with time-decayed counter * fix compilation * avoid nil with empty fh.entry * refactoring * rename * rename * refactor visible intervals to *list.List * refactor chunkViews to *list.List * add IntervalList for generic interval list * change visible interval to use IntervalList in generics * cahnge chunkViews to *IntervalList[*ChunkView] * use NewFileChunkSection to create * rename variables * refactor * fix renaming leftover * renaming * renaming * add insert interval * interval list adds lock * incrementally add chunks to readers Fixes: 1. set start and stop offset for the value object 2. clone the value object 3. use pointer instead of copy-by-value when passing to interval.Value 4. use insert interval since adding chunk could be out of order * fix tests compilation * fix tests compilation
2 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. "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. }