needle_read.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package needle
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/stats"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  8. . "github.com/seaweedfs/seaweedfs/weed/storage/types"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. "io"
  11. )
  12. const (
  13. FlagIsCompressed = 0x01
  14. FlagHasName = 0x02
  15. FlagHasMime = 0x04
  16. FlagHasLastModifiedDate = 0x08
  17. FlagHasTtl = 0x10
  18. FlagHasPairs = 0x20
  19. FlagIsChunkManifest = 0x80
  20. LastModifiedBytesLength = 5
  21. TtlBytesLength = 2
  22. )
  23. var ErrorSizeMismatch = errors.New("size mismatch")
  24. var ErrorSizeInvalid = errors.New("size invalid")
  25. func (n *Needle) DiskSize(version Version) int64 {
  26. return GetActualSize(n.Size, version)
  27. }
  28. func ReadNeedleBlob(r backend.BackendStorageFile, offset int64, size Size, version Version) (dataSlice []byte, err error) {
  29. dataSize := GetActualSize(size, version)
  30. dataSlice = make([]byte, int(dataSize))
  31. var n int
  32. n, err = r.ReadAt(dataSlice, offset)
  33. if err != nil && int64(n) == dataSize {
  34. err = nil
  35. }
  36. if err != nil {
  37. fileSize, _, _ := r.GetStat()
  38. glog.Errorf("%s read %d dataSize %d offset %d fileSize %d: %v", r.Name(), n, dataSize, offset, fileSize, err)
  39. }
  40. return dataSlice, err
  41. }
  42. // ReadBytes hydrates the needle from the bytes buffer, with only n.Id is set.
  43. func (n *Needle) ReadBytes(bytes []byte, offset int64, size Size, version Version) (err error) {
  44. n.ParseNeedleHeader(bytes)
  45. if n.Size != size {
  46. // cookie is not always passed in for this API. Use size to do preliminary checking.
  47. if OffsetSize == 4 && offset < int64(MaxPossibleVolumeSize) {
  48. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorSizeMismatchOffsetSize).Inc()
  49. glog.Errorf("entry not found1: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size)
  50. return ErrorSizeMismatch
  51. }
  52. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorSizeMismatch).Inc()
  53. return fmt.Errorf("entry not found: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size)
  54. }
  55. switch version {
  56. case Version1:
  57. n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
  58. case Version2, Version3:
  59. err = n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
  60. }
  61. if err != nil && err != io.EOF {
  62. return err
  63. }
  64. if size > 0 {
  65. checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
  66. newChecksum := NewCRC(n.Data)
  67. if checksum != newChecksum.Value() && checksum != uint32(newChecksum) {
  68. // the crc.Value() function is to be deprecated. this double checking is for backward compatible.
  69. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorCRC).Inc()
  70. return errors.New("CRC error! Data On Disk Corrupted")
  71. }
  72. n.Checksum = newChecksum
  73. }
  74. if version == Version3 {
  75. tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
  76. n.AppendAtNs = util.BytesToUint64(bytes[tsOffset : tsOffset+TimestampSize])
  77. }
  78. return nil
  79. }
  80. // ReadData hydrates the needle from the file, with only n.Id is set.
  81. func (n *Needle) ReadData(r backend.BackendStorageFile, offset int64, size Size, version Version) (err error) {
  82. bytes, err := ReadNeedleBlob(r, offset, size, version)
  83. if err != nil {
  84. return err
  85. }
  86. return n.ReadBytes(bytes, offset, size, version)
  87. }
  88. func (n *Needle) ParseNeedleHeader(bytes []byte) {
  89. n.Cookie = BytesToCookie(bytes[0:CookieSize])
  90. n.Id = BytesToNeedleId(bytes[CookieSize : CookieSize+NeedleIdSize])
  91. n.Size = BytesToSize(bytes[CookieSize+NeedleIdSize : NeedleHeaderSize])
  92. }
  93. func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
  94. index, lenBytes := 0, len(bytes)
  95. if index < lenBytes {
  96. n.DataSize = util.BytesToUint32(bytes[index : index+4])
  97. index = index + 4
  98. if int(n.DataSize)+index > lenBytes {
  99. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  100. return fmt.Errorf("index out of range %d", 1)
  101. }
  102. n.Data = bytes[index : index+int(n.DataSize)]
  103. index = index + int(n.DataSize)
  104. }
  105. _, err = n.readNeedleDataVersion2NonData(bytes[index:])
  106. return
  107. }
  108. func (n *Needle) readNeedleDataVersion2NonData(bytes []byte) (index int, err error) {
  109. lenBytes := len(bytes)
  110. if index < lenBytes {
  111. n.Flags = bytes[index]
  112. index = index + 1
  113. }
  114. if index < lenBytes && n.HasName() {
  115. n.NameSize = uint8(bytes[index])
  116. index = index + 1
  117. if int(n.NameSize)+index > lenBytes {
  118. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  119. return index, fmt.Errorf("index out of range %d", 2)
  120. }
  121. n.Name = bytes[index : index+int(n.NameSize)]
  122. index = index + int(n.NameSize)
  123. }
  124. if index < lenBytes && n.HasMime() {
  125. n.MimeSize = uint8(bytes[index])
  126. index = index + 1
  127. if int(n.MimeSize)+index > lenBytes {
  128. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  129. return index, fmt.Errorf("index out of range %d", 3)
  130. }
  131. n.Mime = bytes[index : index+int(n.MimeSize)]
  132. index = index + int(n.MimeSize)
  133. }
  134. if index < lenBytes && n.HasLastModifiedDate() {
  135. if LastModifiedBytesLength+index > lenBytes {
  136. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  137. return index, fmt.Errorf("index out of range %d", 4)
  138. }
  139. n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
  140. index = index + LastModifiedBytesLength
  141. }
  142. if index < lenBytes && n.HasTtl() {
  143. if TtlBytesLength+index > lenBytes {
  144. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  145. return index, fmt.Errorf("index out of range %d", 5)
  146. }
  147. n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
  148. index = index + TtlBytesLength
  149. }
  150. if index < lenBytes && n.HasPairs() {
  151. if 2+index > lenBytes {
  152. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  153. return index, fmt.Errorf("index out of range %d", 6)
  154. }
  155. n.PairsSize = util.BytesToUint16(bytes[index : index+2])
  156. index += 2
  157. if int(n.PairsSize)+index > lenBytes {
  158. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  159. return index, fmt.Errorf("index out of range %d", 7)
  160. }
  161. end := index + int(n.PairsSize)
  162. n.Pairs = bytes[index:end]
  163. index = end
  164. }
  165. return index, nil
  166. }
  167. func ReadNeedleHeader(r backend.BackendStorageFile, version Version, offset int64) (n *Needle, bytes []byte, bodyLength int64, err error) {
  168. n = new(Needle)
  169. if version == Version1 || version == Version2 || version == Version3 {
  170. bytes = make([]byte, NeedleHeaderSize)
  171. var count int
  172. count, err = r.ReadAt(bytes, offset)
  173. if count <= 0 || err != nil {
  174. return nil, bytes, 0, err
  175. }
  176. n.ParseNeedleHeader(bytes)
  177. bodyLength = NeedleBodyLength(n.Size, version)
  178. }
  179. return
  180. }
  181. func PaddingLength(needleSize Size, version Version) Size {
  182. if version == Version3 {
  183. // this is same value as version2, but just listed here for clarity
  184. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize + TimestampSize) % NeedlePaddingSize)
  185. }
  186. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize) % NeedlePaddingSize)
  187. }
  188. func NeedleBodyLength(needleSize Size, version Version) int64 {
  189. if version == Version3 {
  190. return int64(needleSize) + NeedleChecksumSize + TimestampSize + int64(PaddingLength(needleSize, version))
  191. }
  192. return int64(needleSize) + NeedleChecksumSize + int64(PaddingLength(needleSize, version))
  193. }
  194. // n should be a needle already read the header
  195. // the input stream will read until next file entry
  196. func (n *Needle) ReadNeedleBody(r backend.BackendStorageFile, version Version, offset int64, bodyLength int64) (bytes []byte, err error) {
  197. if bodyLength <= 0 {
  198. return nil, nil
  199. }
  200. bytes = make([]byte, bodyLength)
  201. if _, err = r.ReadAt(bytes, offset); err != nil {
  202. return
  203. }
  204. err = n.ReadNeedleBodyBytes(bytes, version)
  205. return
  206. }
  207. func (n *Needle) ReadNeedleBodyBytes(needleBody []byte, version Version) (err error) {
  208. if len(needleBody) <= 0 {
  209. return nil
  210. }
  211. switch version {
  212. case Version1:
  213. n.Data = needleBody[:n.Size]
  214. n.Checksum = NewCRC(n.Data)
  215. case Version2, Version3:
  216. err = n.readNeedleDataVersion2(needleBody[0:n.Size])
  217. n.Checksum = NewCRC(n.Data)
  218. if version == Version3 {
  219. tsOffset := n.Size + NeedleChecksumSize
  220. n.AppendAtNs = util.BytesToUint64(needleBody[tsOffset : tsOffset+TimestampSize])
  221. }
  222. default:
  223. err = fmt.Errorf("unsupported version %d!", version)
  224. }
  225. return
  226. }
  227. func (n *Needle) IsCompressed() bool {
  228. return n.Flags&FlagIsCompressed > 0
  229. }
  230. func (n *Needle) SetIsCompressed() {
  231. n.Flags = n.Flags | FlagIsCompressed
  232. }
  233. func (n *Needle) HasName() bool {
  234. return n.Flags&FlagHasName > 0
  235. }
  236. func (n *Needle) SetHasName() {
  237. n.Flags = n.Flags | FlagHasName
  238. }
  239. func (n *Needle) HasMime() bool {
  240. return n.Flags&FlagHasMime > 0
  241. }
  242. func (n *Needle) SetHasMime() {
  243. n.Flags = n.Flags | FlagHasMime
  244. }
  245. func (n *Needle) HasLastModifiedDate() bool {
  246. return n.Flags&FlagHasLastModifiedDate > 0
  247. }
  248. func (n *Needle) SetHasLastModifiedDate() {
  249. n.Flags = n.Flags | FlagHasLastModifiedDate
  250. }
  251. func (n *Needle) HasTtl() bool {
  252. return n.Flags&FlagHasTtl > 0
  253. }
  254. func (n *Needle) SetHasTtl() {
  255. n.Flags = n.Flags | FlagHasTtl
  256. }
  257. func (n *Needle) IsChunkedManifest() bool {
  258. return n.Flags&FlagIsChunkManifest > 0
  259. }
  260. func (n *Needle) SetIsChunkManifest() {
  261. n.Flags = n.Flags | FlagIsChunkManifest
  262. }
  263. func (n *Needle) HasPairs() bool {
  264. return n.Flags&FlagHasPairs != 0
  265. }
  266. func (n *Needle) SetHasPairs() {
  267. n.Flags = n.Flags | FlagHasPairs
  268. }
  269. func GetActualSize(size Size, version Version) int64 {
  270. return NeedleHeaderSize + NeedleBodyLength(size, version)
  271. }