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.

125 lines
2.9 KiB

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