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.

144 lines
3.4 KiB

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