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.

301 lines
9.4 KiB

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