filechunk_section.go 4.1 KB

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