needle_read.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package needle
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/stats"
  8. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  9. . "github.com/seaweedfs/seaweedfs/weed/storage/types"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  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. if OffsetSize == 4 && offset < int64(MaxPossibleVolumeSize) {
  47. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorSizeMismatchOffsetSize).Inc()
  48. glog.Errorf("entry not found1: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size)
  49. return ErrorSizeMismatch
  50. }
  51. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorSizeMismatch).Inc()
  52. return fmt.Errorf("entry not found: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size)
  53. }
  54. if version == Version1 {
  55. n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
  56. } else {
  57. err := n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(size)])
  58. if err != nil && err != io.EOF {
  59. return err
  60. }
  61. }
  62. err = n.readNeedleTail(bytes[NeedleHeaderSize+size:], version)
  63. if err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. // ReadData hydrates the needle from the file, with only n.Id is set.
  69. func (n *Needle) ReadData(r backend.BackendStorageFile, offset int64, size Size, version Version) (err error) {
  70. bytes, err := ReadNeedleBlob(r, offset, size, version)
  71. if err != nil {
  72. return err
  73. }
  74. err = n.ReadBytes(bytes, offset, size, version)
  75. if err == ErrorSizeMismatch && OffsetSize == 4 {
  76. offset = offset + int64(MaxPossibleVolumeSize)
  77. bytes, err = ReadNeedleBlob(r, offset, size, version)
  78. if err != nil {
  79. return err
  80. }
  81. err = n.ReadBytes(bytes, offset, size, version)
  82. }
  83. return err
  84. }
  85. func (n *Needle) ParseNeedleHeader(bytes []byte) {
  86. n.Cookie = BytesToCookie(bytes[0:CookieSize])
  87. n.Id = BytesToNeedleId(bytes[CookieSize : CookieSize+NeedleIdSize])
  88. n.Size = BytesToSize(bytes[CookieSize+NeedleIdSize : NeedleHeaderSize])
  89. }
  90. func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
  91. index, lenBytes := 0, len(bytes)
  92. if index < lenBytes {
  93. n.DataSize = util.BytesToUint32(bytes[index : index+4])
  94. index = index + 4
  95. if int(n.DataSize)+index > lenBytes {
  96. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  97. return fmt.Errorf("index out of range %d", 1)
  98. }
  99. n.Data = bytes[index : index+int(n.DataSize)]
  100. index = index + int(n.DataSize)
  101. }
  102. _, err = n.readNeedleDataVersion2NonData(bytes[index:])
  103. return
  104. }
  105. func (n *Needle) readNeedleDataVersion2NonData(bytes []byte) (index int, err error) {
  106. lenBytes := len(bytes)
  107. if index < lenBytes {
  108. n.Flags = bytes[index]
  109. index = index + 1
  110. }
  111. if index < lenBytes && n.HasName() {
  112. n.NameSize = uint8(bytes[index])
  113. index = index + 1
  114. if int(n.NameSize)+index > lenBytes {
  115. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  116. return index, fmt.Errorf("index out of range %d", 2)
  117. }
  118. n.Name = bytes[index : index+int(n.NameSize)]
  119. index = index + int(n.NameSize)
  120. }
  121. if index < lenBytes && n.HasMime() {
  122. n.MimeSize = uint8(bytes[index])
  123. index = index + 1
  124. if int(n.MimeSize)+index > lenBytes {
  125. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  126. return index, fmt.Errorf("index out of range %d", 3)
  127. }
  128. n.Mime = bytes[index : index+int(n.MimeSize)]
  129. index = index + int(n.MimeSize)
  130. }
  131. if index < lenBytes && n.HasLastModifiedDate() {
  132. if LastModifiedBytesLength+index > lenBytes {
  133. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  134. return index, fmt.Errorf("index out of range %d", 4)
  135. }
  136. n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
  137. index = index + LastModifiedBytesLength
  138. }
  139. if index < lenBytes && n.HasTtl() {
  140. if TtlBytesLength+index > lenBytes {
  141. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  142. return index, fmt.Errorf("index out of range %d", 5)
  143. }
  144. n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
  145. index = index + TtlBytesLength
  146. }
  147. if index < lenBytes && n.HasPairs() {
  148. if 2+index > lenBytes {
  149. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  150. return index, fmt.Errorf("index out of range %d", 6)
  151. }
  152. n.PairsSize = util.BytesToUint16(bytes[index : index+2])
  153. index += 2
  154. if int(n.PairsSize)+index > lenBytes {
  155. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  156. return index, fmt.Errorf("index out of range %d", 7)
  157. }
  158. end := index + int(n.PairsSize)
  159. n.Pairs = bytes[index:end]
  160. index = end
  161. }
  162. return index, nil
  163. }
  164. func ReadNeedleHeader(r backend.BackendStorageFile, version Version, offset int64) (n *Needle, bytes []byte, bodyLength int64, err error) {
  165. n = new(Needle)
  166. bytes = make([]byte, NeedleHeaderSize)
  167. var count int
  168. count, err = r.ReadAt(bytes, offset)
  169. if err == io.EOF && count == NeedleHeaderSize {
  170. err = nil
  171. }
  172. if count <= 0 || err != nil {
  173. return nil, bytes, 0, err
  174. }
  175. n.ParseNeedleHeader(bytes)
  176. bodyLength = NeedleBodyLength(n.Size, version)
  177. return
  178. }
  179. // n should be a needle already read the header
  180. // the input stream will read until next file entry
  181. func (n *Needle) ReadNeedleBody(r backend.BackendStorageFile, version Version, offset int64, bodyLength int64) (bytes []byte, err error) {
  182. if bodyLength <= 0 {
  183. return nil, nil
  184. }
  185. bytes = make([]byte, bodyLength)
  186. readCount, err := r.ReadAt(bytes, offset)
  187. if err == io.EOF && int64(readCount) == bodyLength {
  188. err = nil
  189. }
  190. if err != nil {
  191. glog.Errorf("%s read %d bodyLength %d offset %d: %v", r.Name(), readCount, bodyLength, offset, err)
  192. return
  193. }
  194. err = n.ReadNeedleBodyBytes(bytes, version)
  195. return
  196. }
  197. func (n *Needle) ReadNeedleBodyBytes(needleBody []byte, version Version) (err error) {
  198. if len(needleBody) <= 0 {
  199. return nil
  200. }
  201. switch version {
  202. case Version1:
  203. n.Data = needleBody[:n.Size]
  204. err = n.readNeedleTail(needleBody[n.Size:], version)
  205. case Version2, Version3:
  206. err = n.readNeedleDataVersion2(needleBody[0:n.Size])
  207. if err == nil {
  208. err = n.readNeedleTail(needleBody[n.Size:], version)
  209. }
  210. default:
  211. err = fmt.Errorf("unsupported version %d!", version)
  212. }
  213. return
  214. }
  215. func (n *Needle) IsCompressed() bool {
  216. return n.Flags&FlagIsCompressed > 0
  217. }
  218. func (n *Needle) SetIsCompressed() {
  219. n.Flags = n.Flags | FlagIsCompressed
  220. }
  221. func (n *Needle) HasName() bool {
  222. return n.Flags&FlagHasName > 0
  223. }
  224. func (n *Needle) SetHasName() {
  225. n.Flags = n.Flags | FlagHasName
  226. }
  227. func (n *Needle) HasMime() bool {
  228. return n.Flags&FlagHasMime > 0
  229. }
  230. func (n *Needle) SetHasMime() {
  231. n.Flags = n.Flags | FlagHasMime
  232. }
  233. func (n *Needle) HasLastModifiedDate() bool {
  234. return n.Flags&FlagHasLastModifiedDate > 0
  235. }
  236. func (n *Needle) SetHasLastModifiedDate() {
  237. n.Flags = n.Flags | FlagHasLastModifiedDate
  238. }
  239. func (n *Needle) HasTtl() bool {
  240. return n.Flags&FlagHasTtl > 0
  241. }
  242. func (n *Needle) SetHasTtl() {
  243. n.Flags = n.Flags | FlagHasTtl
  244. }
  245. func (n *Needle) IsChunkedManifest() bool {
  246. return n.Flags&FlagIsChunkManifest > 0
  247. }
  248. func (n *Needle) SetIsChunkManifest() {
  249. n.Flags = n.Flags | FlagIsChunkManifest
  250. }
  251. func (n *Needle) HasPairs() bool {
  252. return n.Flags&FlagHasPairs != 0
  253. }
  254. func (n *Needle) SetHasPairs() {
  255. n.Flags = n.Flags | FlagHasPairs
  256. }
  257. func GetActualSize(size Size, version Version) int64 {
  258. return NeedleHeaderSize + NeedleBodyLength(size, version)
  259. }