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.

96 lines
2.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/mount/page_writer"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "io"
  8. "sync"
  9. "time"
  10. )
  11. type ChunkedDirtyPages struct {
  12. fh *FileHandle
  13. writeWaitGroup sync.WaitGroup
  14. lastErr error
  15. collection string
  16. replication string
  17. uploadPipeline *page_writer.UploadPipeline
  18. hasWrites bool
  19. }
  20. var (
  21. _ = page_writer.DirtyPages(&ChunkedDirtyPages{})
  22. )
  23. func newMemoryChunkPages(fh *FileHandle, chunkSize int64) *ChunkedDirtyPages {
  24. dirtyPages := &ChunkedDirtyPages{
  25. fh: fh,
  26. }
  27. swapFileDir := fh.wfs.option.getTempFilePageDir()
  28. dirtyPages.uploadPipeline = page_writer.NewUploadPipeline(fh.wfs.concurrentWriters, chunkSize,
  29. dirtyPages.saveChunkedFileIntervalToStorage, fh.wfs.option.ConcurrentWriters, swapFileDir)
  30. return dirtyPages
  31. }
  32. func (pages *ChunkedDirtyPages) AddPage(offset int64, data []byte, isSequential bool) {
  33. pages.hasWrites = true
  34. glog.V(4).Infof("%v memory AddPage [%d, %d)", pages.fh.fh, offset, offset+int64(len(data)))
  35. pages.uploadPipeline.SaveDataAt(data, offset, isSequential)
  36. return
  37. }
  38. func (pages *ChunkedDirtyPages) FlushData() error {
  39. if !pages.hasWrites {
  40. return nil
  41. }
  42. pages.uploadPipeline.FlushAll()
  43. if pages.lastErr != nil {
  44. return fmt.Errorf("flush data: %v", pages.lastErr)
  45. }
  46. return nil
  47. }
  48. func (pages *ChunkedDirtyPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
  49. if !pages.hasWrites {
  50. return
  51. }
  52. return pages.uploadPipeline.MaybeReadDataAt(data, startOffset)
  53. }
  54. func (pages *ChunkedDirtyPages) saveChunkedFileIntervalToStorage(reader io.Reader, offset int64, size int64, cleanupFn func()) {
  55. mtime := time.Now().UnixNano()
  56. defer cleanupFn()
  57. fileFullPath := pages.fh.FullPath()
  58. fileName := fileFullPath.Name()
  59. chunk, err := pages.fh.wfs.saveDataAsChunk(fileFullPath)(reader, fileName, offset)
  60. if err != nil {
  61. glog.V(0).Infof("%v saveToStorage [%d,%d): %v", fileFullPath, offset, offset+size, err)
  62. pages.lastErr = err
  63. return
  64. }
  65. chunk.ModifiedTsNs = mtime
  66. pages.fh.AddChunks([]*filer_pb.FileChunk{chunk})
  67. glog.V(3).Infof("%v saveToStorage %s [%d,%d)", fileFullPath, chunk.FileId, offset, offset+size)
  68. }
  69. func (pages ChunkedDirtyPages) Destroy() {
  70. pages.uploadPipeline.Shutdown()
  71. }
  72. func (pages *ChunkedDirtyPages) LockForRead(startOffset, stopOffset int64) {
  73. pages.uploadPipeline.LockForRead(startOffset, stopOffset)
  74. }
  75. func (pages *ChunkedDirtyPages) UnlockForRead(startOffset, stopOffset int64) {
  76. pages.uploadPipeline.UnlockForRead(startOffset, stopOffset)
  77. }