dirty_page.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. lock sync.Mutex
  14. collection string
  15. replication string
  16. }
  17. func newDirtyPages(file *File) *ContinuousDirtyPages {
  18. return &ContinuousDirtyPages{
  19. intervals: &ContinuousIntervals{},
  20. f: file,
  21. }
  22. }
  23. func (pages *ContinuousDirtyPages) releaseResource() {
  24. }
  25. var counter = int32(0)
  26. func (pages *ContinuousDirtyPages) AddPage(offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
  27. pages.lock.Lock()
  28. defer pages.lock.Unlock()
  29. glog.V(4).Infof("%s AddPage [%d,%d) of %d bytes", pages.f.fullpath(), offset, offset+int64(len(data)), pages.f.entry.Attributes.FileSize)
  30. if len(data) > int(pages.f.wfs.option.ChunkSizeLimit) {
  31. // this is more than what buffer can hold.
  32. return pages.flushAndSave(offset, data)
  33. }
  34. pages.intervals.AddInterval(data, offset)
  35. var chunk *filer_pb.FileChunk
  36. var hasSavedData bool
  37. if pages.intervals.TotalSize() > pages.f.wfs.option.ChunkSizeLimit {
  38. chunk, hasSavedData, err = pages.saveExistingLargestPageToStorage()
  39. if hasSavedData {
  40. chunks = append(chunks, chunk)
  41. }
  42. }
  43. return
  44. }
  45. func (pages *ContinuousDirtyPages) flushAndSave(offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
  46. var chunk *filer_pb.FileChunk
  47. var newChunks []*filer_pb.FileChunk
  48. // flush existing
  49. if newChunks, err = pages.saveExistingPagesToStorage(); err == nil {
  50. if newChunks != nil {
  51. chunks = append(chunks, newChunks...)
  52. }
  53. } else {
  54. return
  55. }
  56. // flush the new page
  57. if chunk, err = pages.saveToStorage(bytes.NewReader(data), offset, int64(len(data))); err == nil {
  58. if chunk != nil {
  59. glog.V(4).Infof("%s/%s flush big request [%d,%d) to %s", pages.f.dir.FullPath(), pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size), chunk.FileId)
  60. chunks = append(chunks, chunk)
  61. }
  62. } else {
  63. glog.V(0).Infof("%s/%s failed to flush2 [%d,%d): %v", pages.f.dir.FullPath(), pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size), err)
  64. return
  65. }
  66. return
  67. }
  68. func (pages *ContinuousDirtyPages) FlushToStorage() (chunks []*filer_pb.FileChunk, err error) {
  69. pages.lock.Lock()
  70. defer pages.lock.Unlock()
  71. return pages.saveExistingPagesToStorage()
  72. }
  73. func (pages *ContinuousDirtyPages) saveExistingPagesToStorage() (chunks []*filer_pb.FileChunk, err error) {
  74. var hasSavedData bool
  75. var chunk *filer_pb.FileChunk
  76. for {
  77. chunk, hasSavedData, err = pages.saveExistingLargestPageToStorage()
  78. if !hasSavedData {
  79. return chunks, err
  80. }
  81. if err == nil {
  82. chunks = append(chunks, chunk)
  83. } else {
  84. return
  85. }
  86. }
  87. }
  88. func (pages *ContinuousDirtyPages) saveExistingLargestPageToStorage() (chunk *filer_pb.FileChunk, hasSavedData bool, err error) {
  89. maxList := pages.intervals.RemoveLargestIntervalLinkedList()
  90. if maxList == nil {
  91. return nil, false, nil
  92. }
  93. fileSize := int64(pages.f.entry.Attributes.FileSize)
  94. for {
  95. chunkSize := min(maxList.Size(), fileSize-maxList.Offset())
  96. chunk, err = pages.saveToStorage(maxList.ToReader(), maxList.Offset(), chunkSize)
  97. if err == nil {
  98. hasSavedData = true
  99. glog.V(4).Infof("%s saveToStorage %s [%d,%d) of %d bytes", pages.f.fullpath(), chunk.FileId, maxList.Offset(), maxList.Offset()+chunkSize, fileSize)
  100. return
  101. } else {
  102. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), maxList.Offset(), maxList.Offset()+chunkSize, err)
  103. time.Sleep(5 * time.Second)
  104. }
  105. }
  106. }
  107. func (pages *ContinuousDirtyPages) saveToStorage(reader io.Reader, offset int64, size int64) (*filer_pb.FileChunk, error) {
  108. dir, _ := pages.f.fullpath().DirAndName()
  109. reader = io.LimitReader(reader, size)
  110. chunk, collection, replication, err := pages.f.wfs.saveDataAsChunk(dir)(reader, pages.f.Name, offset)
  111. if err != nil {
  112. return nil, err
  113. }
  114. pages.collection, pages.replication = collection, replication
  115. return chunk, nil
  116. }
  117. func maxUint64(x, y uint64) uint64 {
  118. if x > y {
  119. return x
  120. }
  121. return y
  122. }
  123. func max(x, y int64) int64 {
  124. if x > y {
  125. return x
  126. }
  127. return y
  128. }
  129. func min(x, y int64) int64 {
  130. if x < y {
  131. return x
  132. }
  133. return y
  134. }
  135. func (pages *ContinuousDirtyPages) ReadDirtyData(data []byte, startOffset int64) (offset int64, size int) {
  136. pages.lock.Lock()
  137. defer pages.lock.Unlock()
  138. return pages.intervals.ReadData(data, startOffset)
  139. }