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.

133 lines
3.0 KiB

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