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.

102 lines
2.8 KiB

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/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/mount/page_writer"
  6. "github.com/chrislusf/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.saveChunkedFileIntevalToStorage, 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) GetStorageOptions() (collection, replication string) {
  55. return pages.collection, pages.replication
  56. }
  57. func (pages *ChunkedDirtyPages) saveChunkedFileIntevalToStorage(reader io.Reader, offset int64, size int64, cleanupFn func()) {
  58. mtime := time.Now().UnixNano()
  59. defer cleanupFn()
  60. fileFullPath := pages.fh.FullPath()
  61. fileName := fileFullPath.Name()
  62. chunk, collection, replication, err := pages.fh.wfs.saveDataAsChunk(fileFullPath)(reader, fileName, offset)
  63. if err != nil {
  64. glog.V(0).Infof("%v saveToStorage [%d,%d): %v", fileFullPath, offset, offset+size, err)
  65. pages.lastErr = err
  66. return
  67. }
  68. chunk.Mtime = mtime
  69. pages.collection, pages.replication = collection, replication
  70. pages.fh.AddChunks([]*filer_pb.FileChunk{chunk})
  71. pages.fh.entryViewCache = nil
  72. glog.V(3).Infof("%v saveToStorage %s [%d,%d)", fileFullPath, chunk.FileId, offset, offset+size)
  73. }
  74. func (pages ChunkedDirtyPages) Destroy() {
  75. pages.uploadPipeline.Shutdown()
  76. }
  77. func (pages *ChunkedDirtyPages) LockForRead(startOffset, stopOffset int64) {
  78. pages.uploadPipeline.LockForRead(startOffset, stopOffset)
  79. }
  80. func (pages *ChunkedDirtyPages) UnlockForRead(startOffset, stopOffset int64) {
  81. pages.uploadPipeline.UnlockForRead(startOffset, stopOffset)
  82. }