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.

170 lines
4.1 KiB

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