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.

128 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) of %d bytes", pages.f.fullpath(), offset, offset+int64(len(data)), pages.f.entry.Attributes.FileSize)
  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. fileSize := int64(pages.f.entry.Attributes.FileSize)
  55. chunkSize := min(maxList.Size(), fileSize-maxList.Offset())
  56. if chunkSize == 0 {
  57. return false
  58. }
  59. pages.saveToStorage(maxList.ToReader(), maxList.Offset(), chunkSize)
  60. return true
  61. }
  62. func (pages *ContinuousDirtyPages) saveToStorage(reader io.Reader, offset int64, size int64) {
  63. mtime := time.Now().UnixNano()
  64. pages.writeWaitGroup.Add(1)
  65. writer := func() {
  66. defer pages.writeWaitGroup.Done()
  67. reader = io.LimitReader(reader, size)
  68. chunk, collection, replication, err := pages.f.wfs.saveDataAsChunk(pages.f.fullpath())(reader, pages.f.Name, offset)
  69. if err != nil {
  70. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), offset, offset+size, err)
  71. pages.lastErr = err
  72. return
  73. }
  74. chunk.Mtime = mtime
  75. pages.collection, pages.replication = collection, replication
  76. pages.chunkAddLock.Lock()
  77. defer pages.chunkAddLock.Unlock()
  78. pages.f.addChunks([]*filer_pb.FileChunk{chunk})
  79. glog.V(3).Infof("%s saveToStorage [%d,%d)", pages.f.fullpath(), offset, offset+size)
  80. }
  81. if pages.f.wfs.concurrentWriters != nil {
  82. pages.f.wfs.concurrentWriters.Execute(writer)
  83. } else {
  84. go writer()
  85. }
  86. }
  87. func max(x, y int64) int64 {
  88. if x > y {
  89. return x
  90. }
  91. return y
  92. }
  93. func min(x, y int64) int64 {
  94. if x < y {
  95. return x
  96. }
  97. return y
  98. }
  99. func (pages *ContinuousDirtyPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
  100. return pages.intervals.ReadDataAt(data, startOffset)
  101. }