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.

134 lines
3.1 KiB

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