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.

121 lines
2.9 KiB

7 years ago
7 years ago
5 years ago
  1. package filesys
  2. import (
  3. "bytes"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "io"
  7. "sync"
  8. "time"
  9. )
  10. type ContinuousDirtyPages struct {
  11. intervals *ContinuousIntervals
  12. f *File
  13. writeWaitGroup sync.WaitGroup
  14. chunkSaveErrChan chan error
  15. lock sync.Mutex
  16. collection string
  17. replication string
  18. }
  19. func newDirtyPages(file *File) *ContinuousDirtyPages {
  20. return &ContinuousDirtyPages{
  21. intervals: &ContinuousIntervals{},
  22. f: file,
  23. chunkSaveErrChan: make(chan error, 8),
  24. }
  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. go func() {
  66. defer pages.writeWaitGroup.Done()
  67. dir, _ := pages.f.fullpath().DirAndName()
  68. reader = io.LimitReader(reader, size)
  69. chunk, collection, replication, err := pages.f.wfs.saveDataAsChunk(dir)(reader, pages.f.Name, offset)
  70. if err != nil {
  71. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), offset, offset+size, err)
  72. pages.chunkSaveErrChan <- err
  73. return
  74. }
  75. chunk.Mtime = mtime
  76. pages.collection, pages.replication = collection, replication
  77. pages.f.addChunks([]*filer_pb.FileChunk{chunk})
  78. pages.chunkSaveErrChan <- nil
  79. }()
  80. }
  81. func max(x, y int64) int64 {
  82. if x > y {
  83. return x
  84. }
  85. return y
  86. }
  87. func min(x, y int64) int64 {
  88. if x < y {
  89. return x
  90. }
  91. return y
  92. }
  93. func (pages *ContinuousDirtyPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
  94. return pages.intervals.ReadDataAt(data, startOffset)
  95. }