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.

126 lines
3.5 KiB

more solid weed mount (#4089) * compare chunks by timestamp * fix slab clearing error * fix test compilation * move oldest chunk to sealed, instead of by fullness * lock on fh.entryViewCache * remove verbose logs * revert slat clearing * less logs * less logs * track write and read by timestamp * remove useless logic * add entry lock on file handle release * use mem chunk only, swap file chunk has problems * comment out code that maybe used later * add debug mode to compare data read and write * more efficient readResolvedChunks with linked list * small optimization * fix test compilation * minor fix on writer * add SeparateGarbageChunks * group chunks into sections * turn off debug mode * fix tests * fix tests * tmp enable swap file chunk * Revert "tmp enable swap file chunk" This reverts commit 985137ec472924e4815f258189f6ca9f2168a0a7. * simple refactoring * simple refactoring * do not re-use swap file chunk. Sealed chunks should not be re-used. * comment out debugging facilities * either mem chunk or swap file chunk is fine now * remove orderedMutex as *semaphore.Weighted not found impactful * optimize size calculation for changing large files * optimize performance to avoid going through the long list of chunks * still problems with swap file chunk * rename * tiny optimization * swap file chunk save only successfully read data * fix * enable both mem and swap file chunk * resolve chunks with range * rename * fix chunk interval list * also change file handle chunk group when adding chunks * pick in-active chunk with time-decayed counter * fix compilation * avoid nil with empty fh.entry * refactoring * rename * rename * refactor visible intervals to *list.List * refactor chunkViews to *list.List * add IntervalList for generic interval list * change visible interval to use IntervalList in generics * cahnge chunkViews to *IntervalList[*ChunkView] * use NewFileChunkSection to create * rename variables * refactor * fix renaming leftover * renaming * renaming * add insert interval * interval list adds lock * incrementally add chunks to readers Fixes: 1. set start and stop offset for the value object 2. clone the value object 3. use pointer instead of copy-by-value when passing to interval.Value 4. use insert interval since adding chunk could be out of order * fix tests compilation * fix tests compilation
2 years ago
more solid weed mount (#4089) * compare chunks by timestamp * fix slab clearing error * fix test compilation * move oldest chunk to sealed, instead of by fullness * lock on fh.entryViewCache * remove verbose logs * revert slat clearing * less logs * less logs * track write and read by timestamp * remove useless logic * add entry lock on file handle release * use mem chunk only, swap file chunk has problems * comment out code that maybe used later * add debug mode to compare data read and write * more efficient readResolvedChunks with linked list * small optimization * fix test compilation * minor fix on writer * add SeparateGarbageChunks * group chunks into sections * turn off debug mode * fix tests * fix tests * tmp enable swap file chunk * Revert "tmp enable swap file chunk" This reverts commit 985137ec472924e4815f258189f6ca9f2168a0a7. * simple refactoring * simple refactoring * do not re-use swap file chunk. Sealed chunks should not be re-used. * comment out debugging facilities * either mem chunk or swap file chunk is fine now * remove orderedMutex as *semaphore.Weighted not found impactful * optimize size calculation for changing large files * optimize performance to avoid going through the long list of chunks * still problems with swap file chunk * rename * tiny optimization * swap file chunk save only successfully read data * fix * enable both mem and swap file chunk * resolve chunks with range * rename * fix chunk interval list * also change file handle chunk group when adding chunks * pick in-active chunk with time-decayed counter * fix compilation * avoid nil with empty fh.entry * refactoring * rename * rename * refactor visible intervals to *list.List * refactor chunkViews to *list.List * add IntervalList for generic interval list * change visible interval to use IntervalList in generics * cahnge chunkViews to *IntervalList[*ChunkView] * use NewFileChunkSection to create * rename variables * refactor * fix renaming leftover * renaming * renaming * add insert interval * interval list adds lock * incrementally add chunks to readers Fixes: 1. set start and stop offset for the value object 2. clone the value object 3. use pointer instead of copy-by-value when passing to interval.Value 4. use insert interval since adding chunk could be out of order * fix tests compilation * fix tests compilation
2 years ago
  1. package filer
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  4. "sync"
  5. )
  6. const SectionSize = 2 * 1024 * 1024 * 128 // 256MiB
  7. type SectionIndex int64
  8. type FileChunkSection struct {
  9. sectionIndex SectionIndex
  10. chunks []*filer_pb.FileChunk
  11. visibleIntervals *IntervalList[*VisibleInterval]
  12. chunkViews *IntervalList[*ChunkView]
  13. reader *ChunkReadAt
  14. lock sync.Mutex
  15. }
  16. func NewFileChunkSection(si SectionIndex) *FileChunkSection {
  17. return &FileChunkSection{
  18. sectionIndex: si,
  19. }
  20. }
  21. func (section *FileChunkSection) addChunk(chunk *filer_pb.FileChunk) error {
  22. section.lock.Lock()
  23. defer section.lock.Unlock()
  24. start, stop := max(int64(section.sectionIndex)*SectionSize, chunk.Offset), min(((int64(section.sectionIndex)+1)*SectionSize), chunk.Offset+int64(chunk.Size))
  25. section.chunks = append(section.chunks, chunk)
  26. if section.visibleIntervals != nil {
  27. MergeIntoVisibles(section.visibleIntervals, start, stop, chunk)
  28. garbageFileIds := FindGarbageChunks(section.visibleIntervals, start, stop)
  29. for _, garbageFileId := range garbageFileIds {
  30. length := len(section.chunks)
  31. for i, t := range section.chunks {
  32. if t.FileId == garbageFileId {
  33. section.chunks[i] = section.chunks[length-1]
  34. section.chunks = section.chunks[:length-1]
  35. break
  36. }
  37. }
  38. }
  39. }
  40. if section.chunkViews != nil {
  41. MergeIntoChunkViews(section.chunkViews, start, stop, chunk)
  42. }
  43. return nil
  44. }
  45. func (section *FileChunkSection) setupForRead(group *ChunkGroup, fileSize int64) {
  46. if section.visibleIntervals == nil {
  47. section.visibleIntervals = readResolvedChunks(section.chunks, int64(section.sectionIndex)*SectionSize, (int64(section.sectionIndex)+1)*SectionSize)
  48. section.chunks, _ = SeparateGarbageChunks(section.visibleIntervals, section.chunks)
  49. if section.reader != nil {
  50. _ = section.reader.Close()
  51. section.reader = nil
  52. }
  53. }
  54. if section.chunkViews == nil {
  55. section.chunkViews = ViewFromVisibleIntervals(section.visibleIntervals, int64(section.sectionIndex)*SectionSize, (int64(section.sectionIndex)+1)*SectionSize)
  56. }
  57. if section.reader == nil {
  58. section.reader = NewChunkReaderAtFromClient(group.lookupFn, section.chunkViews, group.chunkCache, min(int64(section.sectionIndex+1)*SectionSize, fileSize))
  59. }
  60. section.reader.fileSize = fileSize
  61. }
  62. func (section *FileChunkSection) readDataAt(group *ChunkGroup, fileSize int64, buff []byte, offset int64) (n int, tsNs int64, err error) {
  63. section.lock.Lock()
  64. defer section.lock.Unlock()
  65. section.setupForRead(group, fileSize)
  66. return section.reader.ReadAtWithTime(buff, offset)
  67. }
  68. func (section *FileChunkSection) DataStartOffset(group *ChunkGroup, offset int64, fileSize int64) int64 {
  69. section.lock.Lock()
  70. defer section.lock.Unlock()
  71. section.setupForRead(group, fileSize)
  72. for x := section.visibleIntervals.Front(); x != nil; x = x.Next {
  73. visible := x.Value
  74. if visible.stop <= offset {
  75. continue
  76. }
  77. if offset < visible.start {
  78. return offset
  79. }
  80. return offset
  81. }
  82. return -1
  83. }
  84. func (section *FileChunkSection) NextStopOffset(group *ChunkGroup, offset int64, fileSize int64) int64 {
  85. section.lock.Lock()
  86. defer section.lock.Unlock()
  87. section.setupForRead(group, fileSize)
  88. isAfterOffset := false
  89. for x := section.visibleIntervals.Front(); x != nil; x = x.Next {
  90. visible := x.Value
  91. if !isAfterOffset {
  92. if visible.stop <= offset {
  93. continue
  94. }
  95. isAfterOffset = true
  96. }
  97. if offset < visible.start {
  98. return offset
  99. }
  100. // now visible.start <= offset
  101. if offset < visible.stop {
  102. offset = visible.stop
  103. }
  104. }
  105. return offset
  106. }