You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.2 KiB

more solid weed mount (#4089) * compare chunks by timestamp * fix slab clearing error * fix test compilation * move oldest chunk to sealed, instead of by fullness * lock on fh.entryViewCache * remove verbose logs * revert slat clearing * less logs * less logs * track write and read by timestamp * remove useless logic * add entry lock on file handle release * use mem chunk only, swap file chunk has problems * comment out code that maybe used later * add debug mode to compare data read and write * more efficient readResolvedChunks with linked list * small optimization * fix test compilation * minor fix on writer * add SeparateGarbageChunks * group chunks into sections * turn off debug mode * fix tests * fix tests * tmp enable swap file chunk * Revert "tmp enable swap file chunk" This reverts commit 985137ec472924e4815f258189f6ca9f2168a0a7. * simple refactoring * simple refactoring * do not re-use swap file chunk. Sealed chunks should not be re-used. * comment out debugging facilities * either mem chunk or swap file chunk is fine now * remove orderedMutex as *semaphore.Weighted not found impactful * optimize size calculation for changing large files * optimize performance to avoid going through the long list of chunks * still problems with swap file chunk * rename * tiny optimization * swap file chunk save only successfully read data * fix * enable both mem and swap file chunk * resolve chunks with range * rename * fix chunk interval list * also change file handle chunk group when adding chunks * pick in-active chunk with time-decayed counter * fix compilation * avoid nil with empty fh.entry * refactoring * rename * rename * refactor visible intervals to *list.List * refactor chunkViews to *list.List * add IntervalList for generic interval list * change visible interval to use IntervalList in generics * cahnge chunkViews to *IntervalList[*ChunkView] * use NewFileChunkSection to create * rename variables * refactor * fix renaming leftover * renaming * renaming * add insert interval * interval list adds lock * incrementally add chunks to readers Fixes: 1. set start and stop offset for the value object 2. clone the value object 3. use pointer instead of copy-by-value when passing to interval.Value 4. use insert interval since adding chunk could be out of order * fix tests compilation * fix tests compilation
2 years ago
  1. package filer
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  4. "github.com/seaweedfs/seaweedfs/weed/util/chunk_cache"
  5. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  6. "sync"
  7. )
  8. type ChunkGroup struct {
  9. lookupFn wdclient.LookupFileIdFunctionType
  10. chunkCache chunk_cache.ChunkCache
  11. manifestChunks []*filer_pb.FileChunk
  12. sections map[SectionIndex]*FileChunkSection
  13. sectionsLock sync.RWMutex
  14. }
  15. func NewChunkGroup(lookupFn wdclient.LookupFileIdFunctionType, chunkCache chunk_cache.ChunkCache, chunks []*filer_pb.FileChunk) (*ChunkGroup, error) {
  16. group := &ChunkGroup{
  17. lookupFn: lookupFn,
  18. chunkCache: chunkCache,
  19. sections: make(map[SectionIndex]*FileChunkSection),
  20. }
  21. err := group.SetChunks(chunks)
  22. return group, err
  23. }
  24. func (group *ChunkGroup) AddChunk(chunk *filer_pb.FileChunk) error {
  25. group.sectionsLock.Lock()
  26. defer group.sectionsLock.Unlock()
  27. sectionIndexStart, sectionIndexStop := SectionIndex(chunk.Offset/SectionSize), SectionIndex((chunk.Offset+int64(chunk.Size))/SectionSize)
  28. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  29. section, found := group.sections[si]
  30. if !found {
  31. section = NewFileChunkSection(si)
  32. group.sections[si] = section
  33. }
  34. section.addChunk(chunk)
  35. }
  36. return nil
  37. }
  38. func (group *ChunkGroup) ReadDataAt(fileSize int64, buff []byte, offset int64) (n int, tsNs int64, err error) {
  39. group.sectionsLock.RLock()
  40. defer group.sectionsLock.RUnlock()
  41. sectionIndexStart, sectionIndexStop := SectionIndex(offset/SectionSize), SectionIndex((offset+int64(len(buff)))/SectionSize)
  42. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  43. section, found := group.sections[si]
  44. rangeStart, rangeStop := max(offset, int64(si*SectionSize)), min(offset+int64(len(buff)), int64((si+1)*SectionSize))
  45. if !found {
  46. for i := rangeStart; i < rangeStop; i++ {
  47. buff[i-offset] = 0
  48. }
  49. continue
  50. }
  51. xn, xTsNs, xErr := section.readDataAt(group, fileSize, buff[rangeStart-offset:rangeStop-offset], rangeStart)
  52. if xErr != nil {
  53. err = xErr
  54. }
  55. n += xn
  56. tsNs = max(tsNs, xTsNs)
  57. }
  58. return
  59. }
  60. func (group *ChunkGroup) SetChunks(chunks []*filer_pb.FileChunk) error {
  61. var dataChunks []*filer_pb.FileChunk
  62. for _, chunk := range chunks {
  63. if !chunk.IsChunkManifest {
  64. dataChunks = append(dataChunks, chunk)
  65. continue
  66. }
  67. resolvedChunks, err := ResolveOneChunkManifest(group.lookupFn, chunk)
  68. if err != nil {
  69. return err
  70. }
  71. group.manifestChunks = append(group.manifestChunks, chunk)
  72. dataChunks = append(dataChunks, resolvedChunks...)
  73. }
  74. for _, chunk := range dataChunks {
  75. sectionIndexStart, sectionIndexStop := SectionIndex(chunk.Offset/SectionSize), SectionIndex((chunk.Offset+int64(chunk.Size))/SectionSize)
  76. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  77. section, found := group.sections[si]
  78. if !found {
  79. section = NewFileChunkSection(si)
  80. group.sections[si] = section
  81. }
  82. section.chunks = append(section.chunks, chunk)
  83. }
  84. }
  85. return nil
  86. }
  87. const (
  88. // see weedfs_file_lseek.go
  89. SEEK_DATA uint32 = 3 // seek to next data after the offset
  90. // SEEK_HOLE uint32 = 4 // seek to next hole after the offset
  91. )
  92. // FIXME: needa tests
  93. func (group *ChunkGroup) SearchChunks(offset, fileSize int64, whence uint32) (found bool, out int64) {
  94. group.sectionsLock.RLock()
  95. defer group.sectionsLock.RUnlock()
  96. return group.doSearchChunks(offset, fileSize, whence)
  97. }
  98. func (group *ChunkGroup) doSearchChunks(offset, fileSize int64, whence uint32) (found bool, out int64) {
  99. sectionIndex, maxSectionIndex := SectionIndex(offset/SectionSize), SectionIndex(fileSize/SectionSize)
  100. if whence == SEEK_DATA {
  101. for si := sectionIndex; si < maxSectionIndex+1; si++ {
  102. section, foundSection := group.sections[si]
  103. if !foundSection {
  104. continue
  105. }
  106. sectionStart := section.DataStartOffset(group, offset, fileSize)
  107. if sectionStart == -1 {
  108. continue
  109. }
  110. return true, sectionStart
  111. }
  112. return false, 0
  113. } else {
  114. // whence == SEEK_HOLE
  115. for si := sectionIndex; si < maxSectionIndex; si++ {
  116. section, foundSection := group.sections[si]
  117. if !foundSection {
  118. return true, offset
  119. }
  120. holeStart := section.NextStopOffset(group, offset, fileSize)
  121. if holeStart%SectionSize == 0 {
  122. continue
  123. }
  124. return true, holeStart
  125. }
  126. return true, fileSize
  127. }
  128. }