filechunk_section.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package filer
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  4. "sync"
  5. )
  6. const SectionSize = 2 * 1024 * 1024 * 32 // 64MiB
  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. section.visibleIntervals = readResolvedChunks(section.chunks, int64(section.sectionIndex)*SectionSize, (int64(section.sectionIndex)+1)*SectionSize)
  28. } else {
  29. MergeIntoVisibles(section.visibleIntervals, start, stop, chunk)
  30. garbageFileIds := FindGarbageChunks(section.visibleIntervals, start, stop)
  31. removeGarbageChunks(section, garbageFileIds)
  32. }
  33. if section.chunkViews != nil {
  34. MergeIntoChunkViews(section.chunkViews, start, stop, chunk)
  35. }
  36. return nil
  37. }
  38. func removeGarbageChunks(section *FileChunkSection, garbageFileIds map[string]struct{}) {
  39. for i := 0; i < len(section.chunks); {
  40. t := section.chunks[i]
  41. length := len(section.chunks)
  42. if _, found := garbageFileIds[t.FileId]; found {
  43. if i < length-1 {
  44. section.chunks[i] = section.chunks[length-1]
  45. }
  46. section.chunks = section.chunks[:length-1]
  47. } else {
  48. i++
  49. }
  50. }
  51. }
  52. func (section *FileChunkSection) setupForRead(group *ChunkGroup, fileSize int64) {
  53. if section.visibleIntervals == nil {
  54. section.visibleIntervals = readResolvedChunks(section.chunks, int64(section.sectionIndex)*SectionSize, (int64(section.sectionIndex)+1)*SectionSize)
  55. section.chunks, _ = SeparateGarbageChunks(section.visibleIntervals, section.chunks)
  56. if section.reader != nil {
  57. _ = section.reader.Close()
  58. section.reader = nil
  59. }
  60. }
  61. if section.chunkViews == nil {
  62. section.chunkViews = ViewFromVisibleIntervals(section.visibleIntervals, int64(section.sectionIndex)*SectionSize, (int64(section.sectionIndex)+1)*SectionSize)
  63. }
  64. if section.reader == nil {
  65. section.reader = NewChunkReaderAtFromClient(group.readerCache, section.chunkViews, min(int64(section.sectionIndex+1)*SectionSize, fileSize))
  66. }
  67. section.reader.fileSize = fileSize
  68. }
  69. func (section *FileChunkSection) readDataAt(group *ChunkGroup, fileSize int64, buff []byte, offset int64) (n int, tsNs int64, err error) {
  70. section.lock.Lock()
  71. defer section.lock.Unlock()
  72. section.setupForRead(group, fileSize)
  73. return section.reader.ReadAtWithTime(buff, offset)
  74. }
  75. func (section *FileChunkSection) DataStartOffset(group *ChunkGroup, offset int64, fileSize int64) int64 {
  76. section.lock.Lock()
  77. defer section.lock.Unlock()
  78. section.setupForRead(group, fileSize)
  79. for x := section.visibleIntervals.Front(); x != nil; x = x.Next {
  80. visible := x.Value
  81. if visible.stop <= offset {
  82. continue
  83. }
  84. if offset < visible.start {
  85. return offset
  86. }
  87. return offset
  88. }
  89. return -1
  90. }
  91. func (section *FileChunkSection) NextStopOffset(group *ChunkGroup, offset int64, fileSize int64) int64 {
  92. section.lock.Lock()
  93. defer section.lock.Unlock()
  94. section.setupForRead(group, fileSize)
  95. isAfterOffset := false
  96. for x := section.visibleIntervals.Front(); x != nil; x = x.Next {
  97. visible := x.Value
  98. if !isAfterOffset {
  99. if visible.stop <= offset {
  100. continue
  101. }
  102. isAfterOffset = true
  103. }
  104. if offset < visible.start {
  105. return offset
  106. }
  107. // now visible.start <= offset
  108. if offset < visible.stop {
  109. offset = visible.stop
  110. }
  111. }
  112. return offset
  113. }