filechunk_section.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.RWMutex
  15. isPrepared bool
  16. }
  17. func NewFileChunkSection(si SectionIndex) *FileChunkSection {
  18. return &FileChunkSection{
  19. sectionIndex: si,
  20. }
  21. }
  22. func (section *FileChunkSection) addChunk(chunk *filer_pb.FileChunk) error {
  23. section.lock.Lock()
  24. defer section.lock.Unlock()
  25. start, stop := max(int64(section.sectionIndex)*SectionSize, chunk.Offset), min(((int64(section.sectionIndex)+1)*SectionSize), chunk.Offset+int64(chunk.Size))
  26. section.chunks = append(section.chunks, chunk)
  27. if section.visibleIntervals == nil {
  28. section.visibleIntervals = readResolvedChunks(section.chunks, int64(section.sectionIndex)*SectionSize, (int64(section.sectionIndex)+1)*SectionSize)
  29. } else {
  30. MergeIntoVisibles(section.visibleIntervals, start, stop, chunk)
  31. garbageFileIds := FindGarbageChunks(section.visibleIntervals, start, stop)
  32. removeGarbageChunks(section, garbageFileIds)
  33. }
  34. if section.chunkViews != nil {
  35. MergeIntoChunkViews(section.chunkViews, start, stop, chunk)
  36. }
  37. return nil
  38. }
  39. func removeGarbageChunks(section *FileChunkSection, garbageFileIds map[string]struct{}) {
  40. for i := 0; i < len(section.chunks); {
  41. t := section.chunks[i]
  42. length := len(section.chunks)
  43. if _, found := garbageFileIds[t.FileId]; found {
  44. if i < length-1 {
  45. section.chunks[i] = section.chunks[length-1]
  46. }
  47. section.chunks = section.chunks[:length-1]
  48. } else {
  49. i++
  50. }
  51. }
  52. }
  53. func (section *FileChunkSection) setupForRead(group *ChunkGroup, fileSize int64) {
  54. if section.isPrepared {
  55. section.reader.fileSize = fileSize
  56. return
  57. }
  58. section.lock.Lock()
  59. defer section.lock.Unlock()
  60. if section.isPrepared {
  61. section.reader.fileSize = fileSize
  62. return
  63. }
  64. if section.visibleIntervals == nil {
  65. section.visibleIntervals = readResolvedChunks(section.chunks, int64(section.sectionIndex)*SectionSize, (int64(section.sectionIndex)+1)*SectionSize)
  66. section.chunks, _ = SeparateGarbageChunks(section.visibleIntervals, section.chunks)
  67. if section.reader != nil {
  68. _ = section.reader.Close()
  69. section.reader = nil
  70. }
  71. }
  72. if section.chunkViews == nil {
  73. section.chunkViews = ViewFromVisibleIntervals(section.visibleIntervals, int64(section.sectionIndex)*SectionSize, (int64(section.sectionIndex)+1)*SectionSize)
  74. }
  75. if section.reader == nil {
  76. section.reader = NewChunkReaderAtFromClient(group.readerCache, section.chunkViews, min(int64(section.sectionIndex+1)*SectionSize, fileSize))
  77. }
  78. section.isPrepared = true
  79. section.reader.fileSize = fileSize
  80. }
  81. func (section *FileChunkSection) readDataAt(group *ChunkGroup, fileSize int64, buff []byte, offset int64) (n int, tsNs int64, err error) {
  82. section.setupForRead(group, fileSize)
  83. section.lock.RLock()
  84. defer section.lock.RUnlock()
  85. return section.reader.ReadAtWithTime(buff, offset)
  86. }
  87. func (section *FileChunkSection) DataStartOffset(group *ChunkGroup, offset int64, fileSize int64) int64 {
  88. section.setupForRead(group, fileSize)
  89. section.lock.RLock()
  90. defer section.lock.RUnlock()
  91. for x := section.visibleIntervals.Front(); x != nil; x = x.Next {
  92. visible := x.Value
  93. if visible.stop <= offset {
  94. continue
  95. }
  96. if offset < visible.start {
  97. return offset
  98. }
  99. return offset
  100. }
  101. return -1
  102. }
  103. func (section *FileChunkSection) NextStopOffset(group *ChunkGroup, offset int64, fileSize int64) int64 {
  104. section.setupForRead(group, fileSize)
  105. section.lock.RLock()
  106. defer section.lock.RUnlock()
  107. isAfterOffset := false
  108. for x := section.visibleIntervals.Front(); x != nil; x = x.Next {
  109. visible := x.Value
  110. if !isAfterOffset {
  111. if visible.stop <= offset {
  112. continue
  113. }
  114. isAfterOffset = true
  115. }
  116. if offset < visible.start {
  117. return offset
  118. }
  119. // now visible.start <= offset
  120. if offset < visible.stop {
  121. offset = visible.stop
  122. }
  123. }
  124. return offset
  125. }