dirty_pages_continuous.go 3.7 KB

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