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.

172 lines
4.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
5 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. 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(3).Infof("%s AddPage [%d,%d)", pages.f.fullpath(), offset, offset+int64(len(data)))
  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. for {
  94. chunk, err = pages.saveToStorage(maxList.ToReader(), maxList.Offset(), maxList.Size())
  95. if err == nil {
  96. hasSavedData = true
  97. glog.V(3).Infof("%s saveToStorage [%d,%d) %s", pages.f.fullpath(), maxList.Offset(), maxList.Offset()+maxList.Size(), chunk.FileId)
  98. return
  99. } else {
  100. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), maxList.Offset(), maxList.Offset()+maxList.Size(), 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. chunk, collection, replication, err := pages.f.wfs.saveDataAsChunk(dir)(reader, pages.f.Name, offset)
  108. if err != nil {
  109. return nil, err
  110. }
  111. pages.collection, pages.replication = collection, replication
  112. return chunk, nil
  113. }
  114. func max(x, y int64) int64 {
  115. if x > y {
  116. return x
  117. }
  118. return y
  119. }
  120. func min(x, y int64) int64 {
  121. if x < y {
  122. return x
  123. }
  124. return y
  125. }
  126. func (pages *ContinuousDirtyPages) ReadDirtyData(data []byte, startOffset int64) (offset int64, size int) {
  127. pages.lock.Lock()
  128. defer pages.lock.Unlock()
  129. return pages.intervals.ReadData(data, startOffset)
  130. }