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.

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