filechunk_group.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package filer
  2. import (
  3. "context"
  4. "io"
  5. "sync"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/util/chunk_cache"
  8. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  9. )
  10. type ChunkGroup struct {
  11. lookupFn wdclient.LookupFileIdFunctionType
  12. sections map[SectionIndex]*FileChunkSection
  13. sectionsLock sync.RWMutex
  14. readerCache *ReaderCache
  15. }
  16. func NewChunkGroup(lookupFn wdclient.LookupFileIdFunctionType, chunkCache chunk_cache.ChunkCache, chunks []*filer_pb.FileChunk) (*ChunkGroup, error) {
  17. group := &ChunkGroup{
  18. lookupFn: lookupFn,
  19. sections: make(map[SectionIndex]*FileChunkSection),
  20. readerCache: NewReaderCache(32, chunkCache, lookupFn),
  21. }
  22. err := group.SetChunks(chunks)
  23. return group, err
  24. }
  25. func (group *ChunkGroup) AddChunk(chunk *filer_pb.FileChunk) error {
  26. group.sectionsLock.Lock()
  27. defer group.sectionsLock.Unlock()
  28. sectionIndexStart, sectionIndexStop := SectionIndex(chunk.Offset/SectionSize), SectionIndex((chunk.Offset+int64(chunk.Size))/SectionSize)
  29. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  30. section, found := group.sections[si]
  31. if !found {
  32. section = NewFileChunkSection(si)
  33. group.sections[si] = section
  34. }
  35. section.addChunk(chunk)
  36. }
  37. return nil
  38. }
  39. func (group *ChunkGroup) ReadDataAt(ctx context.Context, fileSize int64, buff []byte, offset int64) (n int, tsNs int64, err error) {
  40. if offset >= fileSize {
  41. return 0, 0, io.EOF
  42. }
  43. group.sectionsLock.RLock()
  44. defer group.sectionsLock.RUnlock()
  45. sectionIndexStart, sectionIndexStop := SectionIndex(offset/SectionSize), SectionIndex((offset+int64(len(buff)))/SectionSize)
  46. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  47. section, found := group.sections[si]
  48. rangeStart, rangeStop := max(offset, int64(si*SectionSize)), min(offset+int64(len(buff)), int64((si+1)*SectionSize))
  49. if rangeStart >= rangeStop {
  50. continue
  51. }
  52. if !found {
  53. rangeStop = min(rangeStop, fileSize)
  54. for i := rangeStart; i < rangeStop; i++ {
  55. buff[i-offset] = 0
  56. }
  57. n = int(int64(n) + rangeStop - rangeStart)
  58. continue
  59. }
  60. xn, xTsNs, xErr := section.readDataAt(ctx, group, fileSize, buff[rangeStart-offset:rangeStop-offset], rangeStart)
  61. if xErr != nil {
  62. return n + xn, max(tsNs, xTsNs), xErr
  63. }
  64. n += xn
  65. tsNs = max(tsNs, xTsNs)
  66. }
  67. return
  68. }
  69. func (group *ChunkGroup) SetChunks(chunks []*filer_pb.FileChunk) error {
  70. group.sectionsLock.RLock()
  71. defer group.sectionsLock.RUnlock()
  72. var dataChunks []*filer_pb.FileChunk
  73. for _, chunk := range chunks {
  74. if !chunk.IsChunkManifest {
  75. dataChunks = append(dataChunks, chunk)
  76. continue
  77. }
  78. resolvedChunks, err := ResolveOneChunkManifest(context.Background(), group.lookupFn, chunk)
  79. if err != nil {
  80. return err
  81. }
  82. dataChunks = append(dataChunks, resolvedChunks...)
  83. }
  84. sections := make(map[SectionIndex]*FileChunkSection)
  85. for _, chunk := range dataChunks {
  86. sectionIndexStart, sectionIndexStop := SectionIndex(chunk.Offset/SectionSize), SectionIndex((chunk.Offset+int64(chunk.Size))/SectionSize)
  87. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  88. section, found := sections[si]
  89. if !found {
  90. section = NewFileChunkSection(si)
  91. sections[si] = section
  92. }
  93. section.chunks = append(section.chunks, chunk)
  94. }
  95. }
  96. group.sections = sections
  97. return nil
  98. }
  99. const (
  100. // see weedfs_file_lseek.go
  101. SEEK_DATA uint32 = 3 // seek to next data after the offset
  102. // SEEK_HOLE uint32 = 4 // seek to next hole after the offset
  103. )
  104. // FIXME: needa tests
  105. func (group *ChunkGroup) SearchChunks(ctx context.Context, offset, fileSize int64, whence uint32) (found bool, out int64) {
  106. group.sectionsLock.RLock()
  107. defer group.sectionsLock.RUnlock()
  108. return group.doSearchChunks(ctx, offset, fileSize, whence)
  109. }
  110. func (group *ChunkGroup) doSearchChunks(ctx context.Context, offset, fileSize int64, whence uint32) (found bool, out int64) {
  111. sectionIndex, maxSectionIndex := SectionIndex(offset/SectionSize), SectionIndex(fileSize/SectionSize)
  112. if whence == SEEK_DATA {
  113. for si := sectionIndex; si < maxSectionIndex+1; si++ {
  114. section, foundSection := group.sections[si]
  115. if !foundSection {
  116. continue
  117. }
  118. sectionStart := section.DataStartOffset(ctx, group, offset, fileSize)
  119. if sectionStart == -1 {
  120. continue
  121. }
  122. return true, sectionStart
  123. }
  124. return false, 0
  125. } else {
  126. // whence == SEEK_HOLE
  127. for si := sectionIndex; si < maxSectionIndex; si++ {
  128. section, foundSection := group.sections[si]
  129. if !foundSection {
  130. return true, offset
  131. }
  132. holeStart := section.NextStopOffset(ctx, group, offset, fileSize)
  133. if holeStart%SectionSize == 0 {
  134. continue
  135. }
  136. return true, holeStart
  137. }
  138. return true, fileSize
  139. }
  140. }