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.

127 lines
3.1 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. "time"
  9. )
  10. type ContinuousDirtyPages struct {
  11. intervals *ContinuousIntervals
  12. f *File
  13. writeWaitGroup sync.WaitGroup
  14. chunkSaveErrChan chan error
  15. chunkSaveErrChanClosed bool
  16. lock sync.Mutex
  17. collection string
  18. replication string
  19. }
  20. func newDirtyPages(file *File) *ContinuousDirtyPages {
  21. return &ContinuousDirtyPages{
  22. intervals: &ContinuousIntervals{},
  23. f: file,
  24. chunkSaveErrChan: make(chan error, 8),
  25. }
  26. }
  27. func (pages *ContinuousDirtyPages) AddPage(offset int64, data []byte) {
  28. glog.V(4).Infof("%s AddPage [%d,%d) of %d bytes", pages.f.fullpath(), offset, offset+int64(len(data)), pages.f.entry.Attributes.FileSize)
  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. fileSize := int64(pages.f.entry.Attributes.FileSize)
  56. chunkSize := min(maxList.Size(), fileSize-maxList.Offset())
  57. if chunkSize == 0 {
  58. return false
  59. }
  60. pages.saveToStorage(maxList.ToReader(), maxList.Offset(), chunkSize)
  61. return true
  62. }
  63. func (pages *ContinuousDirtyPages) saveToStorage(reader io.Reader, offset int64, size int64) {
  64. if pages.chunkSaveErrChanClosed {
  65. pages.chunkSaveErrChan = make(chan error, 8)
  66. pages.chunkSaveErrChanClosed = false
  67. }
  68. mtime := time.Now().UnixNano()
  69. pages.writeWaitGroup.Add(1)
  70. go func() {
  71. defer pages.writeWaitGroup.Done()
  72. dir, _ := pages.f.fullpath().DirAndName()
  73. reader = io.LimitReader(reader, size)
  74. chunk, collection, replication, err := pages.f.wfs.saveDataAsChunk(dir)(reader, pages.f.Name, offset)
  75. if err != nil {
  76. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), offset, offset+size, err)
  77. pages.chunkSaveErrChan <- err
  78. return
  79. }
  80. chunk.Mtime = mtime
  81. pages.collection, pages.replication = collection, replication
  82. pages.f.addChunks([]*filer_pb.FileChunk{chunk})
  83. pages.chunkSaveErrChan <- nil
  84. }()
  85. }
  86. func max(x, y int64) int64 {
  87. if x > y {
  88. return x
  89. }
  90. return y
  91. }
  92. func min(x, y int64) int64 {
  93. if x < y {
  94. return x
  95. }
  96. return y
  97. }
  98. func (pages *ContinuousDirtyPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
  99. return pages.intervals.ReadDataAt(data, startOffset)
  100. }