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.

265 lines
7.8 KiB

2 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/util/mem"
  5. "io"
  6. "time"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  11. . "github.com/seaweedfs/seaweedfs/weed/storage/types"
  12. )
  13. const PagedReadLimit = 1024 * 1024
  14. // read fills in Needle content by looking up n.Id from NeedleMapper
  15. func (v *Volume) readNeedle(n *needle.Needle, readOption *ReadOption, onReadSizeFn func(size Size)) (count int, err error) {
  16. v.dataFileAccessLock.RLock()
  17. defer v.dataFileAccessLock.RUnlock()
  18. nv, ok := v.nm.Get(n.Id)
  19. if !ok || nv.Offset.IsZero() {
  20. return -1, ErrorNotFound
  21. }
  22. readSize := nv.Size
  23. if readSize.IsDeleted() {
  24. if readOption != nil && readOption.ReadDeleted && readSize != TombstoneFileSize {
  25. glog.V(3).Infof("reading deleted %s", n.String())
  26. readSize = -readSize
  27. } else {
  28. return -1, ErrorDeleted
  29. }
  30. }
  31. if readSize == 0 {
  32. return 0, nil
  33. }
  34. if onReadSizeFn != nil {
  35. onReadSizeFn(readSize)
  36. }
  37. if readOption != nil && readOption.AttemptMetaOnly && readSize > PagedReadLimit {
  38. readOption.VolumeRevision = v.SuperBlock.CompactionRevision
  39. err = n.ReadNeedleMeta(v.DataBackend, nv.Offset.ToActualOffset(), readSize, v.Version())
  40. if err == needle.ErrorSizeMismatch && OffsetSize == 4 {
  41. readOption.IsOutOfRange = true
  42. err = n.ReadNeedleMeta(v.DataBackend, nv.Offset.ToActualOffset()+int64(MaxPossibleVolumeSize), readSize, v.Version())
  43. }
  44. if err != nil {
  45. return 0, err
  46. }
  47. if !n.IsCompressed() && !n.IsChunkedManifest() {
  48. readOption.IsMetaOnly = true
  49. }
  50. }
  51. if readOption == nil || !readOption.IsMetaOnly {
  52. err = n.ReadData(v.DataBackend, nv.Offset.ToActualOffset(), readSize, v.Version())
  53. if err == needle.ErrorSizeMismatch && OffsetSize == 4 {
  54. err = n.ReadData(v.DataBackend, nv.Offset.ToActualOffset()+int64(MaxPossibleVolumeSize), readSize, v.Version())
  55. }
  56. v.checkReadWriteError(err)
  57. if err != nil {
  58. return 0, err
  59. }
  60. }
  61. count = int(n.DataSize)
  62. if !n.HasTtl() {
  63. return
  64. }
  65. ttlMinutes := n.Ttl.Minutes()
  66. if ttlMinutes == 0 {
  67. return
  68. }
  69. if !n.HasLastModifiedDate() {
  70. return
  71. }
  72. if time.Now().Before(time.Unix(0, int64(n.AppendAtNs)).Add(time.Duration(ttlMinutes) * time.Minute)) {
  73. return
  74. }
  75. return -1, ErrorNotFound
  76. }
  77. // read needle at a specific offset
  78. func (v *Volume) readNeedleMetaAt(n *needle.Needle, offset int64, size int32) (err error) {
  79. v.dataFileAccessLock.RLock()
  80. defer v.dataFileAccessLock.RUnlock()
  81. err = n.ReadNeedleMeta(v.DataBackend, offset, Size(size), v.Version())
  82. if err == needle.ErrorSizeMismatch && OffsetSize == 4 {
  83. err = n.ReadNeedleMeta(v.DataBackend, offset+int64(MaxPossibleVolumeSize), Size(size), v.Version())
  84. }
  85. if err != nil {
  86. return err
  87. }
  88. return nil
  89. }
  90. // read fills in Needle content by looking up n.Id from NeedleMapper
  91. func (v *Volume) readNeedleDataInto(n *needle.Needle, readOption *ReadOption, writer io.Writer, offset int64, size int64) (err error) {
  92. if !readOption.HasSlowRead {
  93. v.dataFileAccessLock.RLock()
  94. defer v.dataFileAccessLock.RUnlock()
  95. }
  96. if readOption.HasSlowRead {
  97. v.dataFileAccessLock.RLock()
  98. }
  99. nv, ok := v.nm.Get(n.Id)
  100. if readOption.HasSlowRead {
  101. v.dataFileAccessLock.RUnlock()
  102. }
  103. if !ok || nv.Offset.IsZero() {
  104. return ErrorNotFound
  105. }
  106. readSize := nv.Size
  107. if readSize.IsDeleted() {
  108. if readOption != nil && readOption.ReadDeleted && readSize != TombstoneFileSize {
  109. glog.V(3).Infof("reading deleted %s", n.String())
  110. readSize = -readSize
  111. } else {
  112. return ErrorDeleted
  113. }
  114. }
  115. if readSize == 0 {
  116. return nil
  117. }
  118. actualOffset := nv.Offset.ToActualOffset()
  119. if readOption.IsOutOfRange {
  120. actualOffset += int64(MaxPossibleVolumeSize)
  121. }
  122. buf := mem.Allocate(min(readOption.ReadBufferSize, int(size)))
  123. defer mem.Free(buf)
  124. // read needle data
  125. crc := needle.CRC(0)
  126. for x := offset; x < offset+size; x += int64(len(buf)) {
  127. if readOption.HasSlowRead {
  128. v.dataFileAccessLock.RLock()
  129. }
  130. // possibly re-read needle offset if volume is compacted
  131. if readOption.VolumeRevision != v.SuperBlock.CompactionRevision {
  132. // the volume is compacted
  133. nv, ok = v.nm.Get(n.Id)
  134. if !ok || nv.Offset.IsZero() {
  135. if readOption.HasSlowRead {
  136. v.dataFileAccessLock.RUnlock()
  137. }
  138. return ErrorNotFound
  139. }
  140. actualOffset = nv.Offset.ToActualOffset()
  141. readOption.VolumeRevision = v.SuperBlock.CompactionRevision
  142. }
  143. count, err := n.ReadNeedleData(v.DataBackend, actualOffset, buf, x)
  144. if readOption.HasSlowRead {
  145. v.dataFileAccessLock.RUnlock()
  146. }
  147. toWrite := min(count, int(offset+size-x))
  148. if toWrite > 0 {
  149. crc = crc.Update(buf[0:toWrite])
  150. if _, err = writer.Write(buf[0:toWrite]); err != nil {
  151. return fmt.Errorf("ReadNeedleData write: %v", err)
  152. }
  153. }
  154. if err != nil {
  155. if err == io.EOF {
  156. err = nil
  157. break
  158. }
  159. return fmt.Errorf("ReadNeedleData: %v", err)
  160. }
  161. if count <= 0 {
  162. break
  163. }
  164. }
  165. if offset == 0 && size == int64(n.DataSize) && (n.Checksum != crc && uint32(n.Checksum) != crc.Value()) {
  166. // the crc.Value() function is to be deprecated. this double checking is for backward compatible.
  167. return fmt.Errorf("ReadNeedleData checksum %v expected %v", crc, n.Checksum)
  168. }
  169. return nil
  170. }
  171. func min(x, y int) int {
  172. if x < y {
  173. return x
  174. }
  175. return y
  176. }
  177. // read fills in Needle content by looking up n.Id from NeedleMapper
  178. func (v *Volume) ReadNeedleBlob(offset int64, size Size) ([]byte, error) {
  179. v.dataFileAccessLock.RLock()
  180. defer v.dataFileAccessLock.RUnlock()
  181. return needle.ReadNeedleBlob(v.DataBackend, offset, size, v.Version())
  182. }
  183. type VolumeFileScanner interface {
  184. VisitSuperBlock(super_block.SuperBlock) error
  185. ReadNeedleBody() bool
  186. VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error
  187. }
  188. func ScanVolumeFile(dirname string, collection string, id needle.VolumeId,
  189. needleMapKind NeedleMapKind,
  190. volumeFileScanner VolumeFileScanner) (err error) {
  191. var v *Volume
  192. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  193. return fmt.Errorf("failed to load volume %d: %v", id, err)
  194. }
  195. if err = volumeFileScanner.VisitSuperBlock(v.SuperBlock); err != nil {
  196. return fmt.Errorf("failed to process volume %d super block: %v", id, err)
  197. }
  198. defer v.Close()
  199. version := v.Version()
  200. offset := int64(v.SuperBlock.BlockSize())
  201. return ScanVolumeFileFrom(version, v.DataBackend, offset, volumeFileScanner)
  202. }
  203. func ScanVolumeFileFrom(version needle.Version, datBackend backend.BackendStorageFile, offset int64, volumeFileScanner VolumeFileScanner) (err error) {
  204. n, nh, rest, e := needle.ReadNeedleHeader(datBackend, version, offset)
  205. if e != nil {
  206. if e == io.EOF {
  207. return nil
  208. }
  209. return fmt.Errorf("cannot read %s at offset %d: %v", datBackend.Name(), offset, e)
  210. }
  211. for n != nil {
  212. var needleBody []byte
  213. if volumeFileScanner.ReadNeedleBody() {
  214. // println("needle", n.Id.String(), "offset", offset, "size", n.Size, "rest", rest)
  215. if needleBody, err = n.ReadNeedleBody(datBackend, version, offset+NeedleHeaderSize, rest); err != nil {
  216. glog.V(0).Infof("cannot read needle head [%d, %d) body [%d, %d) body length %d: %v", offset, offset+NeedleHeaderSize, offset+NeedleHeaderSize, offset+NeedleHeaderSize+rest, rest, err)
  217. // err = fmt.Errorf("cannot read needle body: %v", err)
  218. // return
  219. }
  220. }
  221. err := volumeFileScanner.VisitNeedle(n, offset, nh, needleBody)
  222. if err == io.EOF {
  223. return nil
  224. }
  225. if err != nil {
  226. glog.V(0).Infof("visit needle error: %v", err)
  227. return fmt.Errorf("visit needle error: %v", err)
  228. }
  229. offset += NeedleHeaderSize + rest
  230. glog.V(4).Infof("==> new entry offset %d", offset)
  231. if n, nh, rest, err = needle.ReadNeedleHeader(datBackend, version, offset); err != nil {
  232. if err == io.EOF {
  233. return nil
  234. }
  235. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  236. }
  237. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  238. }
  239. return nil
  240. }