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.

312 lines
9.7 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. 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.VolumeServerHandlerCounter.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.VolumeServerHandlerCounter.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.VolumeServerHandlerCounter.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. err = n.ReadBytes(bytes, offset, size, version)
  87. if err == ErrorSizeMismatch && OffsetSize == 4 {
  88. offset = offset + int64(MaxPossibleVolumeSize)
  89. bytes, err = ReadNeedleBlob(r, offset, size, version)
  90. if err != nil {
  91. return err
  92. }
  93. err = n.ReadBytes(bytes, offset, size, version)
  94. }
  95. return err
  96. }
  97. func (n *Needle) ParseNeedleHeader(bytes []byte) {
  98. n.Cookie = BytesToCookie(bytes[0:CookieSize])
  99. n.Id = BytesToNeedleId(bytes[CookieSize : CookieSize+NeedleIdSize])
  100. n.Size = BytesToSize(bytes[CookieSize+NeedleIdSize : NeedleHeaderSize])
  101. }
  102. func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
  103. index, lenBytes := 0, len(bytes)
  104. if index < lenBytes {
  105. n.DataSize = util.BytesToUint32(bytes[index : index+4])
  106. index = index + 4
  107. if int(n.DataSize)+index > lenBytes {
  108. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  109. return fmt.Errorf("index out of range %d", 1)
  110. }
  111. n.Data = bytes[index : index+int(n.DataSize)]
  112. index = index + int(n.DataSize)
  113. }
  114. _, err = n.readNeedleDataVersion2NonData(bytes[index:])
  115. return
  116. }
  117. func (n *Needle) readNeedleDataVersion2NonData(bytes []byte) (index int, err error) {
  118. lenBytes := len(bytes)
  119. if index < lenBytes {
  120. n.Flags = bytes[index]
  121. index = index + 1
  122. }
  123. if index < lenBytes && n.HasName() {
  124. n.NameSize = uint8(bytes[index])
  125. index = index + 1
  126. if int(n.NameSize)+index > lenBytes {
  127. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  128. return index, fmt.Errorf("index out of range %d", 2)
  129. }
  130. n.Name = bytes[index : index+int(n.NameSize)]
  131. index = index + int(n.NameSize)
  132. }
  133. if index < lenBytes && n.HasMime() {
  134. n.MimeSize = uint8(bytes[index])
  135. index = index + 1
  136. if int(n.MimeSize)+index > lenBytes {
  137. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  138. return index, fmt.Errorf("index out of range %d", 3)
  139. }
  140. n.Mime = bytes[index : index+int(n.MimeSize)]
  141. index = index + int(n.MimeSize)
  142. }
  143. if index < lenBytes && n.HasLastModifiedDate() {
  144. if LastModifiedBytesLength+index > lenBytes {
  145. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  146. return index, fmt.Errorf("index out of range %d", 4)
  147. }
  148. n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
  149. index = index + LastModifiedBytesLength
  150. }
  151. if index < lenBytes && n.HasTtl() {
  152. if TtlBytesLength+index > lenBytes {
  153. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  154. return index, fmt.Errorf("index out of range %d", 5)
  155. }
  156. n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
  157. index = index + TtlBytesLength
  158. }
  159. if index < lenBytes && n.HasPairs() {
  160. if 2+index > lenBytes {
  161. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  162. return index, fmt.Errorf("index out of range %d", 6)
  163. }
  164. n.PairsSize = util.BytesToUint16(bytes[index : index+2])
  165. index += 2
  166. if int(n.PairsSize)+index > lenBytes {
  167. stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  168. return index, fmt.Errorf("index out of range %d", 7)
  169. }
  170. end := index + int(n.PairsSize)
  171. n.Pairs = bytes[index:end]
  172. index = end
  173. }
  174. return index, nil
  175. }
  176. func ReadNeedleHeader(r backend.BackendStorageFile, version Version, offset int64) (n *Needle, bytes []byte, bodyLength int64, err error) {
  177. n = new(Needle)
  178. if version == Version1 || version == Version2 || version == Version3 {
  179. bytes = make([]byte, NeedleHeaderSize)
  180. var count int
  181. count, err = r.ReadAt(bytes, offset)
  182. if count <= 0 || err != nil {
  183. return nil, bytes, 0, err
  184. }
  185. n.ParseNeedleHeader(bytes)
  186. bodyLength = NeedleBodyLength(n.Size, version)
  187. }
  188. return
  189. }
  190. func PaddingLength(needleSize Size, version Version) Size {
  191. if version == Version3 {
  192. // this is same value as version2, but just listed here for clarity
  193. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize + TimestampSize) % NeedlePaddingSize)
  194. }
  195. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize) % NeedlePaddingSize)
  196. }
  197. func NeedleBodyLength(needleSize Size, version Version) int64 {
  198. if version == Version3 {
  199. return int64(needleSize) + NeedleChecksumSize + TimestampSize + int64(PaddingLength(needleSize, version))
  200. }
  201. return int64(needleSize) + NeedleChecksumSize + int64(PaddingLength(needleSize, version))
  202. }
  203. // n should be a needle already read the header
  204. // the input stream will read until next file entry
  205. func (n *Needle) ReadNeedleBody(r backend.BackendStorageFile, version Version, offset int64, bodyLength int64) (bytes []byte, err error) {
  206. if bodyLength <= 0 {
  207. return nil, nil
  208. }
  209. bytes = make([]byte, bodyLength)
  210. if _, err = r.ReadAt(bytes, offset); err != nil {
  211. return
  212. }
  213. err = n.ReadNeedleBodyBytes(bytes, version)
  214. return
  215. }
  216. func (n *Needle) ReadNeedleBodyBytes(needleBody []byte, version Version) (err error) {
  217. if len(needleBody) <= 0 {
  218. return nil
  219. }
  220. switch version {
  221. case Version1:
  222. n.Data = needleBody[:n.Size]
  223. n.Checksum = NewCRC(n.Data)
  224. case Version2, Version3:
  225. err = n.readNeedleDataVersion2(needleBody[0:n.Size])
  226. n.Checksum = NewCRC(n.Data)
  227. if version == Version3 {
  228. tsOffset := n.Size + NeedleChecksumSize
  229. n.AppendAtNs = util.BytesToUint64(needleBody[tsOffset : tsOffset+TimestampSize])
  230. }
  231. default:
  232. err = fmt.Errorf("unsupported version %d!", version)
  233. }
  234. return
  235. }
  236. func (n *Needle) IsCompressed() bool {
  237. return n.Flags&FlagIsCompressed > 0
  238. }
  239. func (n *Needle) SetIsCompressed() {
  240. n.Flags = n.Flags | FlagIsCompressed
  241. }
  242. func (n *Needle) HasName() bool {
  243. return n.Flags&FlagHasName > 0
  244. }
  245. func (n *Needle) SetHasName() {
  246. n.Flags = n.Flags | FlagHasName
  247. }
  248. func (n *Needle) HasMime() bool {
  249. return n.Flags&FlagHasMime > 0
  250. }
  251. func (n *Needle) SetHasMime() {
  252. n.Flags = n.Flags | FlagHasMime
  253. }
  254. func (n *Needle) HasLastModifiedDate() bool {
  255. return n.Flags&FlagHasLastModifiedDate > 0
  256. }
  257. func (n *Needle) SetHasLastModifiedDate() {
  258. n.Flags = n.Flags | FlagHasLastModifiedDate
  259. }
  260. func (n *Needle) HasTtl() bool {
  261. return n.Flags&FlagHasTtl > 0
  262. }
  263. func (n *Needle) SetHasTtl() {
  264. n.Flags = n.Flags | FlagHasTtl
  265. }
  266. func (n *Needle) IsChunkedManifest() bool {
  267. return n.Flags&FlagIsChunkManifest > 0
  268. }
  269. func (n *Needle) SetIsChunkManifest() {
  270. n.Flags = n.Flags | FlagIsChunkManifest
  271. }
  272. func (n *Needle) HasPairs() bool {
  273. return n.Flags&FlagHasPairs != 0
  274. }
  275. func (n *Needle) SetHasPairs() {
  276. n.Flags = n.Flags | FlagHasPairs
  277. }
  278. func GetActualSize(size Size, version Version) int64 {
  279. return NeedleHeaderSize + NeedleBodyLength(size, version)
  280. }