dirty_pages_chunked.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package filesys
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/filesys/page_writer"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  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. chunkAddLock sync.Mutex
  15. lastErr error
  16. collection string
  17. replication string
  18. uploadPipeline *page_writer.UploadPipeline
  19. hasWrites bool
  20. }
  21. var (
  22. _ = page_writer.DirtyPages(&ChunkedDirtyPages{})
  23. )
  24. func newMemoryChunkPages(fh *FileHandle, chunkSize int64) *ChunkedDirtyPages {
  25. dirtyPages := &ChunkedDirtyPages{
  26. fh: fh,
  27. }
  28. dirtyPages.uploadPipeline = page_writer.NewUploadPipeline(fh.f.wfs.concurrentWriters, chunkSize, dirtyPages.saveChunkedFileIntevalToStorage, fh.f.wfs.option.ConcurrentWriters)
  29. return dirtyPages
  30. }
  31. func (pages *ChunkedDirtyPages) AddPage(offset int64, data []byte) {
  32. pages.hasWrites = true
  33. glog.V(4).Infof("%v memory AddPage [%d, %d)", pages.fh.f.fullpath(), offset, offset+int64(len(data)))
  34. pages.uploadPipeline.SaveDataAt(data, offset)
  35. return
  36. }
  37. func (pages *ChunkedDirtyPages) FlushData() error {
  38. if !pages.hasWrites {
  39. return nil
  40. }
  41. pages.uploadPipeline.FlushAll()
  42. if pages.lastErr != nil {
  43. return fmt.Errorf("flush data: %v", pages.lastErr)
  44. }
  45. return nil
  46. }
  47. func (pages *ChunkedDirtyPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
  48. if !pages.hasWrites {
  49. return
  50. }
  51. return pages.uploadPipeline.MaybeReadDataAt(data, startOffset)
  52. }
  53. func (pages *ChunkedDirtyPages) GetStorageOptions() (collection, replication string) {
  54. return pages.collection, pages.replication
  55. }
  56. func (pages *ChunkedDirtyPages) saveChunkedFileIntevalToStorage(reader io.Reader, offset int64, size int64, cleanupFn func()) {
  57. mtime := time.Now().UnixNano()
  58. defer cleanupFn()
  59. chunk, collection, replication, err := pages.fh.f.wfs.saveDataAsChunk(pages.fh.f.fullpath())(reader, pages.fh.f.Name, offset)
  60. if err != nil {
  61. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.fh.f.fullpath(), offset, offset+size, err)
  62. pages.lastErr = err
  63. return
  64. }
  65. chunk.Mtime = mtime
  66. pages.collection, pages.replication = collection, replication
  67. pages.chunkAddLock.Lock()
  68. pages.fh.f.addChunks([]*filer_pb.FileChunk{chunk})
  69. pages.fh.entryViewCache = nil
  70. glog.V(3).Infof("%s saveToStorage %s [%d,%d)", pages.fh.f.fullpath(), chunk.FileId, offset, offset+size)
  71. pages.chunkAddLock.Unlock()
  72. }
  73. func (pages ChunkedDirtyPages) Destroy() {
  74. pages.uploadPipeline.Shutdown()
  75. }
  76. func (pages *ChunkedDirtyPages) LockForRead(startOffset, stopOffset int64) {
  77. pages.uploadPipeline.LockForRead(startOffset, stopOffset)
  78. }
  79. func (pages *ChunkedDirtyPages) UnlockForRead(startOffset, stopOffset int64) {
  80. pages.uploadPipeline.UnlockForRead(startOffset, stopOffset)
  81. }