filechunk_section.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package filer
  2. import (
  3. "sync"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  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. section.lock.Lock()
  55. defer section.lock.Unlock()
  56. if section.isPrepared {
  57. section.reader.fileSize = fileSize
  58. return
  59. }
  60. if section.visibleIntervals == nil {
  61. section.visibleIntervals = readResolvedChunks(section.chunks, int64(section.sectionIndex)*SectionSize, (int64(section.sectionIndex)+1)*SectionSize)
  62. section.chunks, _ = SeparateGarbageChunks(section.visibleIntervals, section.chunks)
  63. if section.reader != nil {
  64. _ = section.reader.Close()
  65. section.reader = nil
  66. }
  67. }
  68. if section.chunkViews == nil {
  69. section.chunkViews = ViewFromVisibleIntervals(section.visibleIntervals, int64(section.sectionIndex)*SectionSize, (int64(section.sectionIndex)+1)*SectionSize)
  70. }
  71. if section.reader == nil {
  72. section.reader = NewChunkReaderAtFromClient(group.readerCache, section.chunkViews, min(int64(section.sectionIndex+1)*SectionSize, fileSize))
  73. }
  74. section.isPrepared = true
  75. section.reader.fileSize = fileSize
  76. }
  77. func (section *FileChunkSection) readDataAt(group *ChunkGroup, fileSize int64, buff []byte, offset int64) (n int, tsNs int64, err error) {
  78. section.setupForRead(group, fileSize)
  79. section.lock.RLock()
  80. defer section.lock.RUnlock()
  81. return section.reader.ReadAtWithTime(buff, offset)
  82. }
  83. func (section *FileChunkSection) DataStartOffset(group *ChunkGroup, offset int64, fileSize int64) int64 {
  84. section.setupForRead(group, fileSize)
  85. section.lock.RLock()
  86. defer section.lock.RUnlock()
  87. for x := section.visibleIntervals.Front(); x != nil; x = x.Next {
  88. visible := x.Value
  89. if visible.stop <= offset {
  90. continue
  91. }
  92. if offset < visible.start {
  93. return offset
  94. }
  95. return offset
  96. }
  97. return -1
  98. }
  99. func (section *FileChunkSection) NextStopOffset(group *ChunkGroup, offset int64, fileSize int64) int64 {
  100. section.setupForRead(group, fileSize)
  101. section.lock.RLock()
  102. defer section.lock.RUnlock()
  103. isAfterOffset := false
  104. for x := section.visibleIntervals.Front(); x != nil; x = x.Next {
  105. visible := x.Value
  106. if !isAfterOffset {
  107. if visible.stop <= offset {
  108. continue
  109. }
  110. isAfterOffset = true
  111. }
  112. if offset < visible.start {
  113. return offset
  114. }
  115. // now visible.start <= offset
  116. if offset < visible.stop {
  117. offset = visible.stop
  118. }
  119. }
  120. return offset
  121. }