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.

278 lines
8.1 KiB

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