dirty_pages_continuous.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package filesys
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filesys/page_writer"
  6. "io"
  7. "sync"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. )
  12. type ContinuousDirtyPages struct {
  13. intervals *page_writer.ContinuousIntervals
  14. f *File
  15. writeWaitGroup sync.WaitGroup
  16. chunkAddLock sync.Mutex
  17. lastErr error
  18. collection string
  19. replication string
  20. }
  21. func newContinuousDirtyPages(file *File) *ContinuousDirtyPages {
  22. dirtyPages := &ContinuousDirtyPages{
  23. intervals: &page_writer.ContinuousIntervals{},
  24. f: file,
  25. }
  26. return dirtyPages
  27. }
  28. func (pages *ContinuousDirtyPages) AddPage(offset int64, data []byte) {
  29. glog.V(4).Infof("%s AddPage [%d,%d)", pages.f.fullpath(), offset, offset+int64(len(data)))
  30. if len(data) > int(pages.f.wfs.option.ChunkSizeLimit) {
  31. // this is more than what buffer can hold.
  32. pages.flushAndSave(offset, data)
  33. }
  34. pages.intervals.AddInterval(data, offset)
  35. if pages.intervals.TotalSize() >= pages.f.wfs.option.ChunkSizeLimit {
  36. pages.saveExistingLargestPageToStorage()
  37. }
  38. return
  39. }
  40. func (pages *ContinuousDirtyPages) flushAndSave(offset int64, data []byte) {
  41. // flush existing
  42. pages.saveExistingPagesToStorage()
  43. // flush the new page
  44. pages.saveToStorage(bytes.NewReader(data), offset, int64(len(data)))
  45. return
  46. }
  47. func (pages *ContinuousDirtyPages) FlushData() error {
  48. pages.saveExistingPagesToStorage()
  49. pages.writeWaitGroup.Wait()
  50. if pages.lastErr != nil {
  51. return fmt.Errorf("flush data: %v", pages.lastErr)
  52. }
  53. return nil
  54. }
  55. func (pages *ContinuousDirtyPages) saveExistingPagesToStorage() {
  56. for pages.saveExistingLargestPageToStorage() {
  57. }
  58. }
  59. func (pages *ContinuousDirtyPages) saveExistingLargestPageToStorage() (hasSavedData bool) {
  60. maxList := pages.intervals.RemoveLargestIntervalLinkedList()
  61. if maxList == nil {
  62. return false
  63. }
  64. entry := pages.f.getEntry()
  65. if entry == nil {
  66. return false
  67. }
  68. fileSize := int64(entry.Attributes.FileSize)
  69. chunkSize := min(maxList.Size(), fileSize-maxList.Offset())
  70. if chunkSize == 0 {
  71. return false
  72. }
  73. pages.saveToStorage(maxList.ToReader(), maxList.Offset(), chunkSize)
  74. return true
  75. }
  76. func (pages *ContinuousDirtyPages) saveToStorage(reader io.Reader, offset int64, size int64) {
  77. mtime := time.Now().UnixNano()
  78. pages.writeWaitGroup.Add(1)
  79. writer := func() {
  80. defer pages.writeWaitGroup.Done()
  81. reader = io.LimitReader(reader, size)
  82. chunk, collection, replication, err := pages.f.wfs.saveDataAsChunk(pages.f.fullpath())(reader, pages.f.Name, offset)
  83. if err != nil {
  84. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), offset, offset+size, err)
  85. pages.lastErr = err
  86. return
  87. }
  88. chunk.Mtime = mtime
  89. pages.collection, pages.replication = collection, replication
  90. pages.chunkAddLock.Lock()
  91. defer pages.chunkAddLock.Unlock()
  92. pages.f.addChunks([]*filer_pb.FileChunk{chunk})
  93. glog.V(3).Infof("%s saveToStorage [%d,%d)", pages.f.fullpath(), offset, offset+size)
  94. }
  95. if pages.f.wfs.concurrentWriters != nil {
  96. pages.f.wfs.concurrentWriters.Execute(writer)
  97. } else {
  98. go writer()
  99. }
  100. }
  101. func (pages *ContinuousDirtyPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
  102. return pages.intervals.ReadDataAt(data, startOffset)
  103. }
  104. func (pages *ContinuousDirtyPages) GetStorageOptions() (collection, replication string) {
  105. return pages.collection, pages.replication
  106. }
  107. func (pages ContinuousDirtyPages) Destroy() {
  108. }