volume_read.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // read deleted needle meta data
  82. if size < 0 {
  83. size = 0
  84. }
  85. err = n.ReadNeedleMeta(v.DataBackend, offset, Size(size), v.Version())
  86. if err == needle.ErrorSizeMismatch && OffsetSize == 4 {
  87. err = n.ReadNeedleMeta(v.DataBackend, offset+int64(MaxPossibleVolumeSize), Size(size), v.Version())
  88. }
  89. if err != nil {
  90. return err
  91. }
  92. return nil
  93. }
  94. // read fills in Needle content by looking up n.Id from NeedleMapper
  95. func (v *Volume) readNeedleDataInto(n *needle.Needle, readOption *ReadOption, writer io.Writer, offset int64, size int64) (err error) {
  96. if !readOption.HasSlowRead {
  97. v.dataFileAccessLock.RLock()
  98. defer v.dataFileAccessLock.RUnlock()
  99. }
  100. if readOption.HasSlowRead {
  101. v.dataFileAccessLock.RLock()
  102. }
  103. nv, ok := v.nm.Get(n.Id)
  104. if readOption.HasSlowRead {
  105. v.dataFileAccessLock.RUnlock()
  106. }
  107. if !ok || nv.Offset.IsZero() {
  108. return ErrorNotFound
  109. }
  110. readSize := nv.Size
  111. if readSize.IsDeleted() {
  112. if readOption != nil && readOption.ReadDeleted && readSize != TombstoneFileSize {
  113. glog.V(3).Infof("reading deleted %s", n.String())
  114. readSize = -readSize
  115. } else {
  116. return ErrorDeleted
  117. }
  118. }
  119. if readSize == 0 {
  120. return nil
  121. }
  122. actualOffset := nv.Offset.ToActualOffset()
  123. if readOption.IsOutOfRange {
  124. actualOffset += int64(MaxPossibleVolumeSize)
  125. }
  126. buf := mem.Allocate(min(readOption.ReadBufferSize, int(size)))
  127. defer mem.Free(buf)
  128. // read needle data
  129. crc := needle.CRC(0)
  130. for x := offset; x < offset+size; x += int64(len(buf)) {
  131. if readOption.HasSlowRead {
  132. v.dataFileAccessLock.RLock()
  133. }
  134. // possibly re-read needle offset if volume is compacted
  135. if readOption.VolumeRevision != v.SuperBlock.CompactionRevision {
  136. // the volume is compacted
  137. nv, ok = v.nm.Get(n.Id)
  138. if !ok || nv.Offset.IsZero() {
  139. if readOption.HasSlowRead {
  140. v.dataFileAccessLock.RUnlock()
  141. }
  142. return ErrorNotFound
  143. }
  144. actualOffset = nv.Offset.ToActualOffset()
  145. readOption.VolumeRevision = v.SuperBlock.CompactionRevision
  146. }
  147. count, err := n.ReadNeedleData(v.DataBackend, actualOffset, buf, x)
  148. if readOption.HasSlowRead {
  149. v.dataFileAccessLock.RUnlock()
  150. }
  151. toWrite := min(count, int(offset+size-x))
  152. if toWrite > 0 {
  153. crc = crc.Update(buf[0:toWrite])
  154. if _, err = writer.Write(buf[0:toWrite]); err != nil {
  155. return fmt.Errorf("ReadNeedleData write: %v", err)
  156. }
  157. }
  158. if err != nil {
  159. if err == io.EOF {
  160. err = nil
  161. break
  162. }
  163. return fmt.Errorf("ReadNeedleData: %v", err)
  164. }
  165. if count <= 0 {
  166. break
  167. }
  168. }
  169. if offset == 0 && size == int64(n.DataSize) && (n.Checksum != crc && uint32(n.Checksum) != crc.Value()) {
  170. // the crc.Value() function is to be deprecated. this double checking is for backward compatible.
  171. return fmt.Errorf("ReadNeedleData checksum %v expected %v", crc, n.Checksum)
  172. }
  173. return nil
  174. }
  175. func min(x, y int) int {
  176. if x < y {
  177. return x
  178. }
  179. return y
  180. }
  181. // read fills in Needle content by looking up n.Id from NeedleMapper
  182. func (v *Volume) ReadNeedleBlob(offset int64, size Size) ([]byte, error) {
  183. v.dataFileAccessLock.RLock()
  184. defer v.dataFileAccessLock.RUnlock()
  185. return needle.ReadNeedleBlob(v.DataBackend, offset, size, v.Version())
  186. }
  187. type VolumeFileScanner interface {
  188. VisitSuperBlock(super_block.SuperBlock) error
  189. ReadNeedleBody() bool
  190. VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error
  191. }
  192. func ScanVolumeFile(dirname string, collection string, id needle.VolumeId,
  193. needleMapKind NeedleMapKind,
  194. volumeFileScanner VolumeFileScanner) (err error) {
  195. var v *Volume
  196. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  197. return fmt.Errorf("failed to load volume %d: %v", id, err)
  198. }
  199. if err = volumeFileScanner.VisitSuperBlock(v.SuperBlock); err != nil {
  200. return fmt.Errorf("failed to process volume %d super block: %v", id, err)
  201. }
  202. defer v.Close()
  203. version := v.Version()
  204. offset := int64(v.SuperBlock.BlockSize())
  205. return ScanVolumeFileFrom(version, v.DataBackend, offset, volumeFileScanner)
  206. }
  207. func ScanVolumeFileFrom(version needle.Version, datBackend backend.BackendStorageFile, offset int64, volumeFileScanner VolumeFileScanner) (err error) {
  208. n, nh, rest, e := needle.ReadNeedleHeader(datBackend, version, offset)
  209. if e != nil {
  210. if e == io.EOF {
  211. return nil
  212. }
  213. return fmt.Errorf("cannot read %s at offset %d: %v", datBackend.Name(), offset, e)
  214. }
  215. for n != nil {
  216. var needleBody []byte
  217. if volumeFileScanner.ReadNeedleBody() {
  218. // println("needle", n.Id.String(), "offset", offset, "size", n.Size, "rest", rest)
  219. if needleBody, err = n.ReadNeedleBody(datBackend, version, offset+NeedleHeaderSize, rest); err != nil {
  220. 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)
  221. // err = fmt.Errorf("cannot read needle body: %v", err)
  222. // return
  223. }
  224. }
  225. err := volumeFileScanner.VisitNeedle(n, offset, nh, needleBody)
  226. if err == io.EOF {
  227. return nil
  228. }
  229. if err != nil {
  230. glog.V(0).Infof("visit needle error: %v", err)
  231. return fmt.Errorf("visit needle error: %v", err)
  232. }
  233. offset += NeedleHeaderSize + rest
  234. glog.V(4).Infof("==> new entry offset %d", offset)
  235. if n, nh, rest, err = needle.ReadNeedleHeader(datBackend, version, offset); err != nil {
  236. if err == io.EOF {
  237. return nil
  238. }
  239. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  240. }
  241. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  242. }
  243. return nil
  244. }